md5_finito(struct md5 * m,void * res)1 /*
2 * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Kungliga Tekniska
20 * H�gskolan and its contributors.
21 *
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41
42 RCSID("$Id: md5.c,v 1.2 2001/12/04 02:06:30 rjs3 Exp $");
43 #endif
44
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "md5.h"
49
50 #ifndef min
51 #define min(a,b) (((a)>(b))?(b):(a))
52 #endif
53
54 #define A m->counter[0]
55 #define B m->counter[1]
56 #define C m->counter[2]
57 #define D m->counter[3]
58 #define X data
59
60 void
61 md5_init (struct md5 *m)
62 {
63 m->offset = 0;
64 m->sz = 0;
65 D = 0x10325476;
66 C = 0x98badcfe;
67 B = 0xefcdab89;
68 A = 0x67452301;
69 }
70
71 static inline u_int32_t
72 cshift (u_int32_t x, unsigned int n)
73 {
74 return (x << n) | (x >> (32 - n));
75 }
76
77 #define F(x,y,z) ((x & y) | (~x & z))
78 #define G(x,y,z) ((x & z) | (y & ~z))
79 #define H(x,y,z) (x ^ y ^ z)
80 #define I(x,y,z) (y ^ (x | ~z))
81
82 #define DOIT(a,b,c,d,k,s,i,OP) \
83 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
84
85 #define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
86 #define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
87 #define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
88 #define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
89
90 static inline void
91 calc (struct md5 *m, u_int32_t *data)
92 {
93 u_int32_t AA, BB, CC, DD;
94
95 AA = A;
96 BB = B;
97 CC = C;
98 DD = D;
99
100 /* Round 1 */
101
102 DO1(A,B,C,D,0,7,0xd76aa478);
103 DO1(D,A,B,C,1,12,0xe8c7b756);
104 DO1(C,D,A,B,2,17,0x242070db);
105 DO1(B,C,D,A,3,22,0xc1bdceee);
106
107 DO1(A,B,C,D,4,7,0xf57c0faf);
108 DO1(D,A,B,C,5,12,0x4787c62a);
109 DO1(C,D,A,B,6,17,0xa8304613);
110 DO1(B,C,D,A,7,22,0xfd469501);
111
112 DO1(A,B,C,D,8,7,0x698098d8);
113 DO1(D,A,B,C,9,12,0x8b44f7af);
114 DO1(C,D,A,B,10,17,0xffff5bb1);
115 DO1(B,C,D,A,11,22,0x895cd7be);
116
117 DO1(A,B,C,D,12,7,0x6b901122);
118 DO1(D,A,B,C,13,12,0xfd987193);
119 DO1(C,D,A,B,14,17,0xa679438e);
120 DO1(B,C,D,A,15,22,0x49b40821);
121
122 /* Round 2 */
123
124 DO2(A,B,C,D,1,5,0xf61e2562);
125 DO2(D,A,B,C,6,9,0xc040b340);
126 DO2(C,D,A,B,11,14,0x265e5a51);
127 DO2(B,C,D,A,0,20,0xe9b6c7aa);
128
129 DO2(A,B,C,D,5,5,0xd62f105d);
130 DO2(D,A,B,C,10,9,0x2441453);
131 DO2(C,D,A,B,15,14,0xd8a1e681);
132 DO2(B,C,D,A,4,20,0xe7d3fbc8);
133
134 DO2(A,B,C,D,9,5,0x21e1cde6);
135 DO2(D,A,B,C,14,9,0xc33707d6);
136 DO2(C,D,A,B,3,14,0xf4d50d87);
137 DO2(B,C,D,A,8,20,0x455a14ed);
138
139 DO2(A,B,C,D,13,5,0xa9e3e905);
140 DO2(D,A,B,C,2,9,0xfcefa3f8);
141 DO2(C,D,A,B,7,14,0x676f02d9);
142 DO2(B,C,D,A,12,20,0x8d2a4c8a);
143
144 /* Round 3 */
145
146 DO3(A,B,C,D,5,4,0xfffa3942);
147 DO3(D,A,B,C,8,11,0x8771f681);
148 DO3(C,D,A,B,11,16,0x6d9d6122);
149 DO3(B,C,D,A,14,23,0xfde5380c);
150
151 DO3(A,B,C,D,1,4,0xa4beea44);
152 DO3(D,A,B,C,4,11,0x4bdecfa9);
153 DO3(C,D,A,B,7,16,0xf6bb4b60);
154 DO3(B,C,D,A,10,23,0xbebfbc70);
155
156 DO3(A,B,C,D,13,4,0x289b7ec6);
157 DO3(D,A,B,C,0,11,0xeaa127fa);
158 DO3(C,D,A,B,3,16,0xd4ef3085);
159 DO3(B,C,D,A,6,23,0x4881d05);
160
161 DO3(A,B,C,D,9,4,0xd9d4d039);
162 DO3(D,A,B,C,12,11,0xe6db99e5);
163 DO3(C,D,A,B,15,16,0x1fa27cf8);
164 DO3(B,C,D,A,2,23,0xc4ac5665);
165
166 /* Round 4 */
167
168 DO4(A,B,C,D,0,6,0xf4292244);
169 DO4(D,A,B,C,7,10,0x432aff97);
170 DO4(C,D,A,B,14,15,0xab9423a7);
171 DO4(B,C,D,A,5,21,0xfc93a039);
172
173 DO4(A,B,C,D,12,6,0x655b59c3);
174 DO4(D,A,B,C,3,10,0x8f0ccc92);
175 DO4(C,D,A,B,10,15,0xffeff47d);
176 DO4(B,C,D,A,1,21,0x85845dd1);
177
178 DO4(A,B,C,D,8,6,0x6fa87e4f);
179 DO4(D,A,B,C,15,10,0xfe2ce6e0);
180 DO4(C,D,A,B,6,15,0xa3014314);
181 DO4(B,C,D,A,13,21,0x4e0811a1);
182
183 DO4(A,B,C,D,4,6,0xf7537e82);
184 DO4(D,A,B,C,11,10,0xbd3af235);
185 DO4(C,D,A,B,2,15,0x2ad7d2bb);
186 DO4(B,C,D,A,9,21,0xeb86d391);
187
188 A += AA;
189 B += BB;
190 C += CC;
191 D += DD;
192 }
193
194 /*
195 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
196 */
197
198 static inline u_int32_t
199 swap_u_int32_t (u_int32_t t)
200 {
201 #if defined(WORDS_BIGENDIAN)
202 #define ROL(x,n) ((x)<<(n))|((x)>>(32-(n)))
203 u_int32_t temp1, temp2;
204
205 temp1 = ROL(t,16);
206 temp2 = temp1 >> 8;
207 temp1 &= 0x00ff00ff;
208 temp2 &= 0x00ff00ff;
209 temp1 <<= 8;
210 return temp1 | temp2;
211 #else
212 return t;
213 #endif
214 }
215
216 struct x32{
217 unsigned int a:32;
218 unsigned int b:32;
219 };
220
221 void
222 md5_update (struct md5 *m, const void *v, size_t len)
223 {
224 const unsigned char *p = v;
225 m->sz += len;
226 while(len > 0){
227 size_t l = min(len, 64 - m->offset);
228 memcpy(m->save + m->offset, p, l);
229 m->offset += l;
230 p += l;
231 len -= l;
232 if(m->offset == 64){
233 #if defined(WORDS_BIGENDIAN)
234 int i;
235 u_int32_t current[16];
236 struct x32 *u = (struct x32*)m->save;
237 for(i = 0; i < 8; i++){
238 current[2*i+0] = swap_u_int32_t(u[i].a);
239 current[2*i+1] = swap_u_int32_t(u[i].b);
240 }
241 calc(m, current);
242 #else
243 calc(m, (u_int32_t*)m->save);
244 #endif
245 m->offset = 0;
246 }
247 }
248 }
249
250 void
251 md5_finito (struct md5 *m, void *res)
252 {
253 static unsigned char zeros[72];
254 u_int32_t len;
255 unsigned int dstart = (120 - m->offset - 1) % 64 + 1;
256
257 *zeros = 0x80;
258 memset (zeros + 1, 0, sizeof(zeros) - 1);
259 len = 8 * m->sz;
260 zeros[dstart+0] = (len >> 0) & 0xff;
261 zeros[dstart+1] = (len >> 8) & 0xff;
262 zeros[dstart+2] = (len >> 16) & 0xff;
263 zeros[dstart+3] = (len >> 24) & 0xff;
264 md5_update (m, zeros, dstart + 8);
265 {
266 int i;
267 unsigned char *r = (unsigned char *)res;
268
269 for (i = 0; i < 4; ++i) {
270 r[4*i] = m->counter[i] & 0xFF;
271 r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
272 r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
273 r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
274 }
275 }
276 #if 0
277 {
278 int i;
279 u_int32_t *r = (u_int32_t *)res;
280
281 for (i = 0; i < 4; ++i)
282 r[i] = swap_u_int32_t (m->counter[i]);
283 }
284 #endif
285 }
286
287 /*
288 * This is only for linkage compatibility!
289 */
290 #undef MD5Init
291 #undef MD5Update
292 #undef MD5Final
293
294 void
295 MD5Init (MD5_CTX *mdContext)
296 {
297 md5_init(&mdContext->m.d5);
298 }
299
300 void
301 MD5Update (MD5_CTX *mdContext, const unsigned char *inBuf, unsigned int inLen)
302 {
303 md5_update(&mdContext->m.d5, (unsigned char *)inBuf, inLen);
304 }
305
306 void
307 MD5Final (unsigned char digest[16], MD5_CTX *mdContext)
308 {
309 md5_finito(&mdContext->m.d5, digest);
310 }
311