Annotation of embedaddon/strongswan/src/libstrongswan/plugins/md5/md5_hasher.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2005-2006 Martin Willi
3: * Copyright (C) 2005 Jan Hutter
4: * HSR Hochschule fuer Technik Rapperswil
5: * Copyright (C) 1991-1992, RSA Data Security, Inc. Created 1991.
6: * All rights reserved.
7: *
8: * Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
9: * Ported to fulfill hasher_t interface.
10: *
11: * This program is free software; you can redistribute it and/or modify it
12: * under the terms of the GNU General Public License as published by the
13: * Free Software Foundation; either version 2 of the License, or (at your
14: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15: *
16: * This program is distributed in the hope that it will be useful, but
17: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19: * for more details.
20: */
21:
22: #include <string.h>
23:
24: #include "md5_hasher.h"
25:
26:
27: /* Constants for MD5Transform routine. */
28: #define S11 7
29: #define S12 12
30: #define S13 17
31: #define S14 22
32: #define S21 5
33: #define S22 9
34: #define S23 14
35: #define S24 20
36: #define S31 4
37: #define S32 11
38: #define S33 16
39: #define S34 23
40: #define S41 6
41: #define S42 10
42: #define S43 15
43: #define S44 21
44:
45: static uint8_t PADDING[64] = {
46: 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
49: };
50:
51: /*
52: * ugly macro stuff
53: */
54: /* F, G, H and I are basic MD5 functions.
55: */
56: #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
57: #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
58: #define H(x, y, z) ((x) ^ (y) ^ (z))
59: #define I(x, y, z) ((y) ^ ((x) | (~z)))
60:
61: /* ROTATE_LEFT rotates x left n bits.
62: */
63: #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
64:
65: /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
66: Rotation is separate from addition to prevent recomputation.
67: */
68: #define FF(a, b, c, d, x, s, ac) { \
69: (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
70: (a) = ROTATE_LEFT ((a), (s)); \
71: (a) += (b); \
72: }
73: #define GG(a, b, c, d, x, s, ac) { \
74: (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
75: (a) = ROTATE_LEFT ((a), (s)); \
76: (a) += (b); \
77: }
78: #define HH(a, b, c, d, x, s, ac) { \
79: (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
80: (a) = ROTATE_LEFT ((a), (s)); \
81: (a) += (b); \
82: }
83: #define II(a, b, c, d, x, s, ac) { \
84: (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
85: (a) = ROTATE_LEFT ((a), (s)); \
86: (a) += (b); \
87: }
88:
89:
90:
91: typedef struct private_md5_hasher_t private_md5_hasher_t;
92:
93: /**
94: * Private data structure with hashing context.
95: */
96: struct private_md5_hasher_t {
97: /**
98: * Public interface for this hasher.
99: */
100: md5_hasher_t public;
101:
102: /*
103: * State of the hasher.
104: */
105: uint32_t state[5];
106: uint32_t count[2];
107: uint8_t buffer[64];
108: };
109:
110:
111: #if BYTE_ORDER != LITTLE_ENDIAN
112:
113: /* Encodes input (uint32_t) into output (uint8_t). Assumes len is
114: * a multiple of 4.
115: */
116: static void Encode (uint8_t *output, uint32_t *input, size_t len)
117: {
118: size_t i, j;
119:
120: for (i = 0, j = 0; j < len; i++, j += 4)
121: {
122: output[j] = (uint8_t)(input[i] & 0xff);
123: output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
124: output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
125: output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
126: }
127: }
128:
129: /* Decodes input (uint8_t) into output (uint32_t). Assumes len is
130: * a multiple of 4.
131: */
132: static void Decode(uint32_t *output, uint8_t *input, size_t len)
133: {
134: size_t i, j;
135:
136: for (i = 0, j = 0; j < len; i++, j += 4)
137: {
138: output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
139: (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
140: }
141: }
142:
143: #elif BYTE_ORDER == LITTLE_ENDIAN
144: #define Encode memcpy
145: #define Decode memcpy
146: #endif
147:
148: /* MD5 basic transformation. Transforms state based on block.
149: */
150: static void MD5Transform(uint32_t state[4], uint8_t block[64])
151: {
152: uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
153:
154: Decode(x, block, 64);
155:
156: /* Round 1 */
157: FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
158: FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
159: FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
160: FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
161: FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
162: FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
163: FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
164: FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
165: FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
166: FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
167: FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
168: FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
169: FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
170: FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
171: FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
172: FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
173:
174: /* Round 2 */
175: GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
176: GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
177: GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
178: GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
179: GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
180: GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
181: GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
182: GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
183: GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
184: GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
185: GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
186: GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
187: GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
188: GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
189: GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
190: GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
191:
192: /* Round 3 */
193: HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
194: HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
195: HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
196: HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
197: HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
198: HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
199: HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
200: HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
201: HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
202: HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
203: HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
204: HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
205: HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
206: HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
207: HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
208: HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
209:
210: /* Round 4 */
211: II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
212: II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
213: II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
214: II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
215: II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
216: II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
217: II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
218: II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
219: II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
220: II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
221: II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
222: II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
223: II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
224: II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
225: II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
226: II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
227:
228: state[0] += a;
229: state[1] += b;
230: state[2] += c;
231: state[3] += d;
232: }
233:
234: /* MD5 block update operation. Continues an MD5 message-digest
235: * operation, processing another message block, and updating the
236: * context.
237: */
238: static void MD5Update(private_md5_hasher_t *this, uint8_t *input, size_t inputLen)
239: {
240: uint32_t i;
241: size_t index, partLen;
242:
243: /* Compute number of bytes mod 64 */
244: index = (uint8_t)((this->count[0] >> 3) & 0x3F);
245:
246: /* Update number of bits */
247: if ((this->count[0] += (inputLen << 3)) < (inputLen << 3))
248: {
249: this->count[1]++;
250: }
251: this->count[1] += (inputLen >> 29);
252:
253: partLen = 64 - index;
254:
255: /* Transform as many times as possible. */
256: if (inputLen >= partLen)
257: {
258: memcpy(&this->buffer[index], input, partLen);
259: MD5Transform (this->state, this->buffer);
260:
261: for (i = partLen; i + 63 < inputLen; i += 64)
262: {
263: MD5Transform (this->state, &input[i]);
264: }
265: index = 0;
266: }
267: else
268: {
269: i = 0;
270: }
271:
272: /* Buffer remaining input */
273: memcpy(&this->buffer[index], &input[i], inputLen-i);
274: }
275:
276: /* MD5 finalization. Ends an MD5 message-digest operation, writing the
277: * the message digest and zeroizing the context.
278: */
279: static void MD5Final (private_md5_hasher_t *this, uint8_t digest[16])
280: {
281: uint8_t bits[8];
282: size_t index, padLen;
283:
284: /* Save number of bits */
285: Encode (bits, this->count, 8);
286:
287: /* Pad out to 56 mod 64. */
288: index = (size_t)((this->count[0] >> 3) & 0x3f);
289: padLen = (index < 56) ? (56 - index) : (120 - index);
290: MD5Update (this, PADDING, padLen);
291:
292: /* Append length (before padding) */
293: MD5Update (this, bits, 8);
294:
295: if (digest != NULL) /* Bill Simpson's padding */
296: {
297: /* store state in digest */
298: Encode (digest, this->state, 16);
299: }
300: }
301:
302: METHOD(hasher_t, reset, bool,
303: private_md5_hasher_t *this)
304: {
305: this->state[0] = 0x67452301;
306: this->state[1] = 0xefcdab89;
307: this->state[2] = 0x98badcfe;
308: this->state[3] = 0x10325476;
309: this->count[0] = 0;
310: this->count[1] = 0;
311:
312: return TRUE;
313: }
314:
315: METHOD(hasher_t, get_hash, bool,
316: private_md5_hasher_t *this, chunk_t chunk, uint8_t *buffer)
317: {
318: MD5Update(this, chunk.ptr, chunk.len);
319: if (buffer != NULL)
320: {
321: MD5Final(this, buffer);
322: reset(this);
323: }
324: return TRUE;
325: }
326:
327: METHOD(hasher_t, allocate_hash, bool,
328: private_md5_hasher_t *this, chunk_t chunk, chunk_t *hash)
329: {
330: MD5Update(this, chunk.ptr, chunk.len);
331: if (hash != NULL)
332: {
333: *hash = chunk_alloc(HASH_SIZE_MD5);
334: MD5Final(this, hash->ptr);
335: reset(this);
336: }
337: return TRUE;
338: }
339:
340: METHOD(hasher_t, get_hash_size, size_t,
341: private_md5_hasher_t *this)
342: {
343: return HASH_SIZE_MD5;
344: }
345:
346: METHOD(hasher_t, destroy, void,
347: private_md5_hasher_t *this)
348: {
349: free(this);
350: }
351:
352: /*
353: * Described in header.
354: */
355: md5_hasher_t *md5_hasher_create(hash_algorithm_t algo)
356: {
357: private_md5_hasher_t *this;
358:
359: if (algo != HASH_MD5)
360: {
361: return NULL;
362: }
363:
364: INIT(this,
365: .public = {
366: .hasher_interface = {
367: .get_hash = _get_hash,
368: .allocate_hash = _allocate_hash,
369: .get_hash_size = _get_hash_size,
370: .reset = _reset,
371: .destroy = _destroy,
372: },
373: },
374: );
375:
376: /* initialize */
377: reset(this);
378:
379: return &(this->public);
380: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>