]>
Commit | Line | Data |
---|---|---|
dff50e09 | 1 | /* |
1b78aef5 DS |
2 | a implementation of MD4 designed for use in the SMB authentication protocol |
3 | Copyright (C) Andrew Tridgell 1997-1998. | |
dff50e09 | 4 | |
1b78aef5 DS |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
dff50e09 | 9 | |
1b78aef5 DS |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
dff50e09 | 14 | |
1b78aef5 | 15 | You should have received a copy of the GNU General Public License |
e8daaf60 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
1b78aef5 DS |
17 | */ |
18 | ||
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | #include "mdfour.h" | |
22 | ||
dff50e09 | 23 | /* NOTE: This code makes no attempt to be fast! |
1b78aef5 DS |
24 | |
25 | It assumes that a int is at least 32 bits long | |
26 | */ | |
27 | ||
28 | static struct mdfour *m; | |
29 | ||
30 | #define MASK32 (0xffffffff) | |
31 | ||
32 | #define F(X,Y,Z) ((((X)&(Y)) | ((~(X))&(Z)))) | |
33 | #define G(X,Y,Z) ((((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))) | |
34 | #define H(X,Y,Z) (((X)^(Y)^(Z))) | |
35 | #define lshift(x,s) (((((x)<<(s))&MASK32) | (((x)>>(32-(s)))&MASK32))) | |
36 | ||
37 | #define ROUND1(a,b,c,d,k,s) a = lshift((a + F(b,c,d) + M[k])&MASK32, s) | |
38 | #define ROUND2(a,b,c,d,k,s) a = lshift((a + G(b,c,d) + M[k] + 0x5A827999)&MASK32,s) | |
39 | #define ROUND3(a,b,c,d,k,s) a = lshift((a + H(b,c,d) + M[k] + 0x6ED9EBA1)&MASK32,s) | |
40 | ||
41 | /* this applies md4 to 64 byte chunks */ | |
42 | static void | |
43 | mdfour64(uint32_t *M) | |
44 | { | |
45 | uint32_t AA, BB, CC, DD; | |
46 | uint32_t A,B,C,D; | |
47 | ||
dff50e09 | 48 | A = m->A; B = m->B; C = m->C; D = m->D; |
1b78aef5 DS |
49 | AA = A; BB = B; CC = C; DD = D; |
50 | ||
dff50e09 | 51 | ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7); |
1b78aef5 | 52 | ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19); |
dff50e09 | 53 | ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7); |
1b78aef5 | 54 | ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19); |
dff50e09 | 55 | ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7); |
1b78aef5 | 56 | ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19); |
dff50e09 FCE |
57 | ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7); |
58 | ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19); | |
1b78aef5 DS |
59 | |
60 | ||
dff50e09 | 61 | ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5); |
1b78aef5 | 62 | ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13); |
dff50e09 | 63 | ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5); |
1b78aef5 | 64 | ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13); |
dff50e09 | 65 | ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5); |
1b78aef5 | 66 | ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13); |
dff50e09 | 67 | ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5); |
1b78aef5 DS |
68 | ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13); |
69 | ||
dff50e09 | 70 | ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9); |
1b78aef5 | 71 | ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15); |
dff50e09 | 72 | ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9); |
1b78aef5 | 73 | ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15); |
dff50e09 | 74 | ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9); |
1b78aef5 | 75 | ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15); |
dff50e09 | 76 | ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9); |
1b78aef5 DS |
77 | ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15); |
78 | ||
dff50e09 | 79 | A += AA; B += BB; |
1b78aef5 | 80 | C += CC; D += DD; |
dff50e09 FCE |
81 | |
82 | A &= MASK32; B &= MASK32; | |
1b78aef5 DS |
83 | C &= MASK32; D &= MASK32; |
84 | ||
85 | m->A = A; m->B = B; m->C = C; m->D = D; | |
86 | } | |
87 | ||
88 | static void | |
89 | copy64(uint32_t *M, const unsigned char *in) | |
90 | { | |
91 | int i; | |
92 | ||
93 | for (i=0;i<16;i++) | |
94 | M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) | | |
95 | (in[i*4+1]<<8) | (in[i*4+0]<<0); | |
96 | } | |
97 | ||
98 | static void | |
99 | copy4(unsigned char *out,uint32_t x) | |
100 | { | |
101 | out[0] = x&0xFF; | |
102 | out[1] = (x>>8)&0xFF; | |
103 | out[2] = (x>>16)&0xFF; | |
104 | out[3] = (x>>24)&0xFF; | |
105 | } | |
106 | ||
107 | void | |
108 | mdfour_begin(struct mdfour *md) | |
109 | { | |
110 | md->A = 0x67452301; | |
111 | md->B = 0xefcdab89; | |
112 | md->C = 0x98badcfe; | |
113 | md->D = 0x10325476; | |
114 | md->totalN = 0; | |
115 | md->tail_len = 0; | |
116 | } | |
117 | ||
118 | ||
119 | static void | |
120 | mdfour_tail(const unsigned char *in, int n) | |
121 | { | |
122 | unsigned char buf[128]; | |
123 | uint32_t M[16]; | |
124 | uint32_t b; | |
125 | ||
126 | m->totalN += n; | |
127 | ||
128 | b = m->totalN * 8; | |
129 | ||
130 | memset(buf, 0, 128); | |
131 | if (n) memcpy(buf, in, n); | |
132 | buf[n] = 0x80; | |
133 | ||
134 | if (n <= 55) | |
135 | { | |
136 | copy4(buf+56, b); | |
137 | copy64(M, buf); | |
138 | mdfour64(M); | |
139 | } | |
140 | else | |
141 | { | |
dff50e09 | 142 | copy4(buf+120, b); |
1b78aef5 DS |
143 | copy64(M, buf); |
144 | mdfour64(M); | |
145 | copy64(M, buf+64); | |
146 | mdfour64(M); | |
147 | } | |
148 | } | |
149 | ||
150 | void | |
151 | mdfour_update(struct mdfour *md, const unsigned char *in, int n) | |
152 | { | |
153 | uint32_t M[16]; | |
154 | ||
155 | m = md; | |
156 | ||
157 | if (in == NULL) | |
158 | { | |
159 | mdfour_tail(md->tail, md->tail_len); | |
160 | return; | |
161 | } | |
162 | ||
163 | if (md->tail_len) | |
164 | { | |
165 | int len = 64 - md->tail_len; | |
166 | if (len > n) len = n; | |
167 | memcpy(md->tail+md->tail_len, in, len); | |
168 | md->tail_len += len; | |
169 | n -= len; | |
170 | in += len; | |
171 | if (md->tail_len == 64) | |
172 | { | |
173 | copy64(M, md->tail); | |
174 | mdfour64(M); | |
175 | m->totalN += 64; | |
176 | md->tail_len = 0; | |
177 | } | |
178 | } | |
179 | ||
180 | while (n >= 64) | |
181 | { | |
182 | copy64(M, in); | |
183 | mdfour64(M); | |
184 | in += 64; | |
185 | n -= 64; | |
186 | m->totalN += 64; | |
187 | } | |
188 | ||
189 | if (n) | |
190 | { | |
191 | memcpy(md->tail, in, n); | |
192 | md->tail_len = n; | |
193 | } | |
194 | } | |
195 | ||
196 | ||
197 | void | |
198 | mdfour_result(struct mdfour *md, unsigned char *out) | |
199 | { | |
200 | m = md; | |
201 | ||
202 | copy4(out, m->A); | |
203 | copy4(out+4, m->B); | |
204 | copy4(out+8, m->C); | |
205 | copy4(out+12, m->D); | |
206 | } | |
207 | ||
208 | ||
209 | void | |
210 | mdfour(unsigned char *out, const unsigned char *in, int n) | |
211 | { | |
212 | struct mdfour md; | |
213 | mdfour_begin(&md); | |
214 | mdfour_update(&md, in, n); | |
215 | mdfour_update(&md, NULL, 0); | |
216 | mdfour_result(&md, out); | |
217 | } | |
0c5f5812 JS |
218 | |
219 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |