Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : a implementation of MD4 designed for use in the SMB authentication protocol
4 : Copyright (C) Andrew Tridgell 1997-1998.
5 :
6 : This program is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "replace.h"
21 : #include "../lib/crypto/md4.h"
22 :
23 : /* NOTE: This code makes no attempt to be fast!
24 :
25 : It assumes that a int is at least 32 bits long
26 : */
27 :
28 : struct mdfour_state {
29 : uint32_t A, B, C, D;
30 : };
31 :
32 2104217 : static uint32_t F(uint32_t X, uint32_t Y, uint32_t Z)
33 : {
34 2104217 : return (X&Y) | ((~X)&Z);
35 : }
36 :
37 2104217 : static uint32_t G(uint32_t X, uint32_t Y, uint32_t Z)
38 : {
39 2104217 : return (X&Y) | (X&Z) | (Y&Z);
40 : }
41 :
42 2104217 : static uint32_t H(uint32_t X, uint32_t Y, uint32_t Z)
43 : {
44 2104217 : return X^Y^Z;
45 : }
46 :
47 6309465 : static uint32_t lshift(uint32_t x, int s)
48 : {
49 6309465 : x &= 0xFFFFFFFF;
50 6309465 : return ((x<<s)&0xFFFFFFFF) | (x>>(32-s));
51 : }
52 :
53 : #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
54 : #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32_t)0x5A827999,s)
55 : #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32_t)0x6ED9EBA1,s)
56 :
57 : /* this applies md4 to 64 byte chunks */
58 133007 : static void mdfour64(struct mdfour_state *s, uint32_t *M)
59 : {
60 1593 : int j;
61 1593 : uint32_t AA, BB, CC, DD;
62 1593 : uint32_t X[16];
63 :
64 2261119 : for (j=0;j<16;j++)
65 2128112 : X[j] = M[j];
66 :
67 133007 : AA = s->A; BB = s->B; CC = s->C; DD = s->D;
68 :
69 133007 : ROUND1(s->A,s->B,s->C,s->D, 0, 3); ROUND1(s->D,s->A,s->B,s->C, 1, 7);
70 133007 : ROUND1(s->C,s->D,s->A,s->B, 2, 11); ROUND1(s->B,s->C,s->D,s->A, 3, 19);
71 133007 : ROUND1(s->A,s->B,s->C,s->D, 4, 3); ROUND1(s->D,s->A,s->B,s->C, 5, 7);
72 133007 : ROUND1(s->C,s->D,s->A,s->B, 6, 11); ROUND1(s->B,s->C,s->D,s->A, 7, 19);
73 133007 : ROUND1(s->A,s->B,s->C,s->D, 8, 3); ROUND1(s->D,s->A,s->B,s->C, 9, 7);
74 133007 : ROUND1(s->C,s->D,s->A,s->B, 10, 11); ROUND1(s->B,s->C,s->D,s->A, 11, 19);
75 133007 : ROUND1(s->A,s->B,s->C,s->D, 12, 3); ROUND1(s->D,s->A,s->B,s->C, 13, 7);
76 133007 : ROUND1(s->C,s->D,s->A,s->B, 14, 11); ROUND1(s->B,s->C,s->D,s->A, 15, 19);
77 :
78 133007 : ROUND2(s->A,s->B,s->C,s->D, 0, 3); ROUND2(s->D,s->A,s->B,s->C, 4, 5);
79 133007 : ROUND2(s->C,s->D,s->A,s->B, 8, 9); ROUND2(s->B,s->C,s->D,s->A, 12, 13);
80 133007 : ROUND2(s->A,s->B,s->C,s->D, 1, 3); ROUND2(s->D,s->A,s->B,s->C, 5, 5);
81 133007 : ROUND2(s->C,s->D,s->A,s->B, 9, 9); ROUND2(s->B,s->C,s->D,s->A, 13, 13);
82 133007 : ROUND2(s->A,s->B,s->C,s->D, 2, 3); ROUND2(s->D,s->A,s->B,s->C, 6, 5);
83 133007 : ROUND2(s->C,s->D,s->A,s->B, 10, 9); ROUND2(s->B,s->C,s->D,s->A, 14, 13);
84 133007 : ROUND2(s->A,s->B,s->C,s->D, 3, 3); ROUND2(s->D,s->A,s->B,s->C, 7, 5);
85 133007 : ROUND2(s->C,s->D,s->A,s->B, 11, 9); ROUND2(s->B,s->C,s->D,s->A, 15, 13);
86 :
87 133007 : ROUND3(s->A,s->B,s->C,s->D, 0, 3); ROUND3(s->D,s->A,s->B,s->C, 8, 9);
88 133007 : ROUND3(s->C,s->D,s->A,s->B, 4, 11); ROUND3(s->B,s->C,s->D,s->A, 12, 15);
89 133007 : ROUND3(s->A,s->B,s->C,s->D, 2, 3); ROUND3(s->D,s->A,s->B,s->C, 10, 9);
90 133007 : ROUND3(s->C,s->D,s->A,s->B, 6, 11); ROUND3(s->B,s->C,s->D,s->A, 14, 15);
91 133007 : ROUND3(s->A,s->B,s->C,s->D, 1, 3); ROUND3(s->D,s->A,s->B,s->C, 9, 9);
92 133007 : ROUND3(s->C,s->D,s->A,s->B, 5, 11); ROUND3(s->B,s->C,s->D,s->A, 13, 15);
93 133007 : ROUND3(s->A,s->B,s->C,s->D, 3, 3); ROUND3(s->D,s->A,s->B,s->C, 11, 9);
94 133007 : ROUND3(s->C,s->D,s->A,s->B, 7, 11); ROUND3(s->B,s->C,s->D,s->A, 15, 15);
95 :
96 133007 : s->A += AA;
97 133007 : s->B += BB;
98 133007 : s->C += CC;
99 133007 : s->D += DD;
100 :
101 133007 : s->A &= 0xFFFFFFFF;
102 133007 : s->B &= 0xFFFFFFFF;
103 133007 : s->C &= 0xFFFFFFFF;
104 133007 : s->D &= 0xFFFFFFFF;
105 :
106 2235631 : for (j=0;j<16;j++)
107 2102624 : X[j] = 0;
108 133007 : }
109 :
110 209245 : static void copy64(uint32_t *M, const uint8_t *in)
111 : {
112 2003 : int i;
113 :
114 3557165 : for (i=0;i<16;i++)
115 3347920 : M[i] = ((uint32_t)in[i*4+3] << 24) |
116 3347920 : ((uint32_t)in[i*4+2] << 16) |
117 3347920 : ((uint32_t)in[i*4+1] << 8) |
118 3347920 : ((uint32_t)in[i*4+0] << 0);
119 209245 : }
120 :
121 379960 : static void copy4(uint8_t *out, uint32_t x)
122 : {
123 379960 : out[0] = x&0xFF;
124 379960 : out[1] = (x>>8)&0xFF;
125 379960 : out[2] = (x>>16)&0xFF;
126 379960 : out[3] = (x>>24)&0xFF;
127 379140 : }
128 :
129 : /**
130 : * produce a md4 message digest from data of length n bytes
131 : */
132 76238 : _PUBLIC_ void mdfour(uint8_t *out, const uint8_t *in, int n)
133 : {
134 410 : uint8_t buf[128];
135 410 : uint32_t M[16];
136 76238 : uint32_t b = n * 8;
137 410 : int i;
138 410 : struct mdfour_state state;
139 :
140 76238 : state.A = 0x67452301;
141 76238 : state.B = 0xefcdab89;
142 76238 : state.C = 0x98badcfe;
143 76238 : state.D = 0x10325476;
144 :
145 127237 : while (n > 64) {
146 50999 : copy64(M, in);
147 50999 : mdfour64(&state, M);
148 50999 : in += 64;
149 50999 : n -= 64;
150 : }
151 :
152 9834702 : for (i=0;i<128;i++)
153 9758464 : buf[i] = 0;
154 76238 : memcpy(buf, in, n);
155 76238 : buf[n] = 0x80;
156 :
157 76238 : if (n <= 55) {
158 70468 : copy4(buf+56, b);
159 70468 : copy64(M, buf);
160 70468 : mdfour64(&state, M);
161 : } else {
162 5770 : copy4(buf+120, b);
163 5770 : copy64(M, buf);
164 5770 : mdfour64(&state, M);
165 5770 : copy64(M, buf+64);
166 5770 : mdfour64(&state, M);
167 : }
168 :
169 9834702 : for (i=0;i<128;i++)
170 9758464 : buf[i] = 0;
171 76238 : copy64(M, buf);
172 :
173 76238 : copy4(out, state.A);
174 76238 : copy4(out+4, state.B);
175 76238 : copy4(out+8, state.C);
176 76238 : copy4(out+12, state.D);
177 76238 : }
178 :
179 :
|