0
|
1 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
|
2 */
|
|
3
|
|
4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
|
5 rights reserved.
|
|
6
|
|
7 License to copy and use this software is granted provided that it
|
|
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
|
9 Algorithm" in all material mentioning or referencing this software
|
|
10 or this function.
|
|
11
|
|
12 License is also granted to make and use derivative works provided
|
|
13 that such works are identified as "derived from the RSA Data
|
|
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
|
|
15 mentioning or referencing the derived work.
|
|
16
|
|
17 RSA Data Security, Inc. makes no representations concerning either
|
|
18 the merchantability of this software or the suitability of this
|
|
19 software for any particular purpose. It is provided "as is"
|
|
20 without express or implied warranty of any kind.
|
|
21
|
|
22 These notices must be retained in any copies of any part of this
|
|
23 documentation and/or software.
|
|
24 */
|
|
25
|
|
26 /* Synched up with: Not in FSF. */
|
|
27 /* This file has been Mule-ized. See comment at Fmd5(), though. */
|
|
28
|
|
29 #include <config.h>
|
|
30 #include "lisp.h"
|
|
31
|
|
32 #include "buffer.h"
|
|
33 #include "insdel.h"
|
|
34
|
|
35 typedef unsigned char *POINTER;/* POINTER defines a generic pointer type */
|
|
36 typedef unsigned short int UINT2;/* UINT2 defines a two byte word */
|
70
|
37 typedef unsigned long int UINT4;/* UINT4 defines a four byte word */
|
0
|
38
|
|
39 #define PROTO_LIST(list) list
|
|
40 #define MD_CTX MD5_CTX
|
|
41 #define MDInit MD5Init
|
|
42 #define MDUpdate MD5Update
|
|
43 #define MDFinal MD5Final
|
|
44
|
|
45 /* MD5 context. */
|
|
46 typedef struct {
|
|
47 UINT4 state[4]; /* state (ABCD) */
|
|
48 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
|
49 unsigned char buffer[64]; /* input buffer */
|
|
50 } MD5_CTX;
|
|
51
|
|
52 void MD5Init PROTO_LIST ((MD5_CTX *));
|
|
53 void MD5Update PROTO_LIST
|
|
54 ((MD5_CTX *, CONST unsigned char *, unsigned int));
|
|
55 void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
|
|
56
|
|
57 /* Constants for MD5Transform routine.
|
|
58 */
|
|
59 #define S11 7
|
|
60 #define S12 12
|
|
61 #define S13 17
|
|
62 #define S14 22
|
|
63 #define S21 5
|
|
64 #define S22 9
|
|
65 #define S23 14
|
|
66 #define S24 20
|
|
67 #define S31 4
|
|
68 #define S32 11
|
|
69 #define S33 16
|
|
70 #define S34 23
|
|
71 #define S41 6
|
|
72 #define S42 10
|
|
73 #define S43 15
|
|
74 #define S44 21
|
|
75
|
|
76 static void MD5Transform PROTO_LIST ((UINT4 [4], CONST unsigned char [64]));
|
|
77 static void Encode PROTO_LIST
|
|
78 ((unsigned char *, UINT4 *, unsigned int));
|
|
79 static void Decode PROTO_LIST
|
|
80 ((UINT4 *, CONST unsigned char *, unsigned int));
|
|
81 static void MD5_memcpy PROTO_LIST ((POINTER, CONST POINTER, unsigned int));
|
|
82 static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
|
|
83
|
|
84 static unsigned char PADDING[64] = {
|
|
85 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
88 };
|
|
89
|
|
90 /* F, G, H and I are basic MD5 functions.
|
|
91 */
|
|
92 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
|
93 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
|
94 #define H(x, y, z) ((x) ^ (y) ^ (z))
|
|
95 #define I(x, y, z) ((y) ^ ((x) | (~z)))
|
|
96
|
|
97 /* ROTATE_LEFT rotates x left n bits.
|
|
98 */
|
|
99 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
|
100
|
|
101 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
|
102 Rotation is separate from addition to prevent recomputation.
|
|
103 */
|
|
104 #define FF(a, b, c, d, x, s, ac) { \
|
|
105 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
106 (a) = ROTATE_LEFT ((a), (s)); \
|
|
107 (a) += (b); \
|
|
108 }
|
|
109 #define GG(a, b, c, d, x, s, ac) { \
|
|
110 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
111 (a) = ROTATE_LEFT ((a), (s)); \
|
|
112 (a) += (b); \
|
|
113 }
|
|
114 #define HH(a, b, c, d, x, s, ac) { \
|
|
115 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
116 (a) = ROTATE_LEFT ((a), (s)); \
|
|
117 (a) += (b); \
|
|
118 }
|
|
119 #define II(a, b, c, d, x, s, ac) { \
|
|
120 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
121 (a) = ROTATE_LEFT ((a), (s)); \
|
|
122 (a) += (b); \
|
|
123 }
|
|
124
|
|
125 /* MD5 initialization. Begins an MD5 operation, writing a new context.
|
|
126 */
|
|
127 void
|
|
128 MD5Init (MD5_CTX *context)
|
|
129 {
|
|
130 context->count[0] = context->count[1] = 0;
|
|
131
|
|
132 /* Load magic initialization constants. */
|
|
133 context->state[0] = 0x67452301;
|
|
134 context->state[1] = 0xefcdab89;
|
|
135 context->state[2] = 0x98badcfe;
|
|
136 context->state[3] = 0x10325476;
|
|
137 }
|
|
138
|
|
139 /* MD5 block update operation. Continues an MD5 message-digest
|
|
140 operation, processing another message block, and updating the
|
|
141 context.
|
|
142 */
|
|
143 void
|
|
144 MD5Update (MD5_CTX *context, CONST unsigned char *input, unsigned int inputLen)
|
|
145 {
|
|
146 unsigned int i, indecks, partLen;
|
|
147
|
|
148 /* Compute number of bytes mod 64 */
|
|
149 indecks = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
|
150
|
|
151 /* Update number of bits */
|
|
152 if ((context->count[0] += ((UINT4)inputLen << 3))
|
|
153 < ((UINT4)inputLen << 3))
|
|
154 context->count[1]++;
|
|
155 context->count[1] += ((UINT4)inputLen >> 29);
|
|
156
|
|
157 partLen = 64 - indecks;
|
|
158
|
|
159 /* Transform as many times as possible. */
|
|
160 if (inputLen >= partLen)
|
|
161 {
|
|
162 MD5_memcpy ((POINTER)&context->buffer[indecks], (CONST POINTER)input,
|
|
163 partLen);
|
|
164 MD5Transform (context->state, context->buffer);
|
|
165
|
|
166 for (i = partLen; i + 63 < inputLen; i += 64)
|
|
167 MD5Transform (context->state, &input[i]);
|
|
168
|
|
169 indecks = 0;
|
|
170 }
|
|
171 else
|
|
172 i = 0;
|
|
173
|
|
174 /* Buffer remaining input */
|
|
175 MD5_memcpy ((POINTER)&context->buffer[indecks], (CONST POINTER)&input[i],
|
|
176 inputLen-i);
|
|
177 }
|
|
178
|
|
179 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
2
|
180 message digest and zeroizing the context. */
|
0
|
181 void
|
|
182 MD5Final (unsigned char digest[16], MD5_CTX *context)
|
|
183 {
|
|
184 unsigned char bits[8];
|
|
185 unsigned int indecks, padLen;
|
|
186
|
|
187 /* Save number of bits */
|
|
188 Encode (bits, context->count, 8);
|
|
189
|
|
190 /* Pad out to 56 mod 64.
|
|
191 */
|
|
192 indecks = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
|
193 padLen = (indecks < 56) ? (56 - indecks) : (120 - indecks);
|
|
194 MD5Update (context, PADDING, padLen);
|
|
195
|
|
196 /* Append length (before padding) */
|
|
197 MD5Update (context, bits, 8);
|
|
198 /* Store state in digest */
|
|
199 Encode (digest, context->state, 16);
|
|
200
|
|
201 /* Zeroize sensitive information.
|
|
202 */
|
|
203 MD5_memset ((POINTER)context, 0, sizeof (*context));
|
|
204 }
|
|
205
|
|
206 /* MD5 basic transformation. Transforms state based on block.
|
|
207 */
|
|
208 static void
|
|
209 MD5Transform (UINT4 state[4], CONST unsigned char block[64])
|
|
210 {
|
|
211 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
|
212
|
|
213 Decode (x, block, 64);
|
|
214
|
|
215 /* Round 1 */
|
|
216 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
|
217 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
|
218 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
|
219 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
|
220 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
|
221 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
|
222 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
|
223 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
|
224 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
|
225 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
|
226 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
|
227 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
|
228 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
|
229 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
|
230 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
|
231 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
|
232
|
|
233 /* Round 2 */
|
|
234 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
|
235 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
|
236 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
|
237 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
|
238 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
|
239 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
|
240 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
|
241 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
|
242 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
|
243 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
|
244 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
|
245 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
|
246 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
|
247 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
|
248 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
|
249 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
|
250
|
|
251 /* Round 3 */
|
|
252 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
|
253 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
|
254 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
|
255 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
|
256 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
|
257 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
|
258 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
|
259 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
|
260 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
|
261 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
|
262 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
|
263 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
|
264 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
|
265 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
|
266 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
|
267 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
|
268
|
|
269 /* Round 4 */
|
|
270 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
|
271 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
|
272 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
|
273 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
|
274 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
|
275 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
|
276 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
|
277 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
|
278 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
|
279 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
|
280 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
|
281 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
|
282 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
|
283 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
|
284 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
|
285 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
|
286
|
|
287 state[0] += a;
|
|
288 state[1] += b;
|
|
289 state[2] += c;
|
|
290 state[3] += d;
|
|
291
|
|
292 /* Zeroize sensitive information.
|
|
293 */
|
|
294 MD5_memset ((POINTER)x, 0, sizeof (x));
|
|
295 }
|
|
296
|
|
297 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
|
|
298 a multiple of 4.
|
|
299 */
|
|
300 static void
|
|
301 Encode (unsigned char *output, UINT4 *input, unsigned int len)
|
|
302 {
|
|
303 unsigned int i, j;
|
|
304
|
|
305 for (i = 0, j = 0; j < len; i++, j += 4)
|
|
306 {
|
|
307 output[j] = (unsigned char)(input[i] & 0xff);
|
|
308 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
|
309 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
|
310 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
|
311 }
|
|
312 }
|
|
313
|
|
314 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
|
|
315 a multiple of 4.
|
|
316 */
|
|
317 static void
|
|
318 Decode (UINT4 *output, CONST unsigned char *input, unsigned int len)
|
|
319 {
|
|
320 unsigned int i, j;
|
|
321
|
|
322 for (i = 0, j = 0; j < len; i++, j += 4)
|
|
323 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
|
|
324 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
|
|
325 }
|
|
326
|
|
327 static void
|
|
328 MD5_memcpy (POINTER output, CONST POINTER input, unsigned int len)
|
|
329 {
|
|
330 memcpy (output, input, len);
|
|
331 }
|
|
332
|
|
333 static void
|
|
334 MD5_memset (POINTER output, int value, unsigned int len)
|
|
335 {
|
|
336 memset (output, value, len);
|
|
337 }
|
|
338
|
|
339 /* unused */
|
|
340 #if 0
|
|
341 static void
|
|
342 LispMDString (char *string)
|
|
343 {
|
|
344 MD_CTX context;
|
|
345 unsigned char digest[16];
|
|
346 unsigned int len = strlen(string);
|
|
347
|
|
348 MDInit (&context);
|
|
349 MDUpdate (&context, string, len);
|
|
350 MDFinal (digest, &context);
|
|
351 }
|
|
352 #endif
|
|
353
|
|
354
|
|
355 /* XEmacs interface code. */
|
|
356 Lisp_Object Qmd5;
|
|
357
|
|
358 /* #### There could be potential problems here with Mule. I don't
|
|
359 know enough about the uses of MD5 to be able to tell for sure
|
|
360 whether this is a problem. The basic potential problem is that
|
|
361 the hash value will be computed based on the internal representation
|
|
362 of the buffer; this would likely cause problems if the string
|
|
363 contains extended characters, because the extended characters
|
|
364 will get sent over the wire in an external form that is different
|
|
365 from their internal representation, and thus their MD5 hash would
|
|
366 be different. */
|
|
367
|
20
|
368 DEFUN ("md5", Fmd5, 1, 3, 0, /*
|
0
|
369 Return the MD5 (a secure message digest algorithm) of an object.
|
|
370 OBJECT is either a string or a buffer.
|
|
371 Optional arguments START and END denote buffer positions for computing the
|
|
372 hash of a portion of OBJECT.
|
20
|
373 */
|
|
374 (object, start, end))
|
0
|
375 {
|
|
376 MD_CTX context;
|
|
377 unsigned char digest[16];
|
|
378 unsigned char thehash[32];
|
|
379 int i;
|
|
380
|
|
381 MDInit (&context);
|
|
382
|
|
383 if (NILP (object))
|
|
384 {
|
|
385 MDUpdate (&context, (CONST unsigned char *) "", 0);
|
|
386 }
|
|
387 else if (BUFFERP (object))
|
|
388 {
|
|
389 struct buffer *b = decode_buffer (object, 1);
|
|
390 Bufpos begv, endv;
|
|
391 Lisp_Object string;
|
|
392
|
|
393 /* Figure out where we need to get info from */
|
|
394 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
|
|
395
|
|
396 /* Get the string data from the buffer */
|
|
397 string = make_string_from_buffer (b, begv, endv - begv);
|
|
398
|
|
399 /* Compute the digest */
|
16
|
400 MDUpdate (&context, (unsigned char *) XSTRING_DATA (string),
|
|
401 XSTRING_LENGTH (string));
|
0
|
402 }
|
|
403 else
|
|
404 {
|
|
405 Bytecount len, bstart, bend;
|
|
406 CHECK_STRING (object);
|
|
407 get_string_range_byte (object, start, end, &bstart, &bend,
|
|
408 GB_HISTORICAL_STRING_BEHAVIOR);
|
|
409 len = bend - bstart;
|
16
|
410 MDUpdate (&context, ((unsigned char *) XSTRING_DATA (object)
|
0
|
411 + bstart), len);
|
|
412 }
|
|
413
|
|
414 MDFinal (digest, &context);
|
|
415 for (i = 0; i < 16; i++)
|
|
416 sprintf ((char *) (thehash + (i * 2)), "%02x", digest[i]);
|
|
417
|
|
418 return (make_string (thehash, 32));
|
|
419 }
|
|
420
|
|
421 void
|
|
422 syms_of_md5 (void)
|
|
423 {
|
20
|
424 DEFSUBR (Fmd5);
|
0
|
425 defsymbol (&Qmd5, "md5");
|
|
426 }
|
|
427
|
|
428 void
|
|
429 vars_of_md5 (void)
|
|
430 {
|
|
431 Fprovide (Qmd5);
|
|
432 }
|