2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 1991-1992, RSA Data Security, Inc. Created 1991.
8 * Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
9 * Ported to fulfill hasher_t interface.
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>.
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
24 #include "md5_hasher.h"
27 /* Constants for MD5Transform routine. */
45 static u_int8_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
54 /* F, G, H and I are basic MD5 functions.
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)))
61 /* ROTATE_LEFT rotates x left n bits.
63 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
65 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
66 Rotation is separate from addition to prevent recomputation.
68 #define FF(a, b, c, d, x, s, ac) { \
69 (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
70 (a) = ROTATE_LEFT ((a), (s)); \
73 #define GG(a, b, c, d, x, s, ac) { \
74 (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
75 (a) = ROTATE_LEFT ((a), (s)); \
78 #define HH(a, b, c, d, x, s, ac) { \
79 (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
80 (a) = ROTATE_LEFT ((a), (s)); \
83 #define II(a, b, c, d, x, s, ac) { \
84 (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
85 (a) = ROTATE_LEFT ((a), (s)); \
91 typedef struct private_md5_hasher_t private_md5_hasher_t
;
94 * Private data structure with hasing context.
96 struct private_md5_hasher_t
{
98 * Public interface for this hasher.
103 * State of the hasher.
111 #if BYTE_ORDER != LITTLE_ENDIAN
113 /* Encodes input (u_int32_t) into output (u_int8_t). Assumes len is
116 static void Encode (u_int8_t
*output
, u_int32_t
*input
, size_t len
)
120 for (i
= 0, j
= 0; j
< len
; i
++, j
+= 4)
122 output
[j
] = (u_int8_t
)(input
[i
] & 0xff);
123 output
[j
+1] = (u_int8_t
)((input
[i
] >> 8) & 0xff);
124 output
[j
+2] = (u_int8_t
)((input
[i
] >> 16) & 0xff);
125 output
[j
+3] = (u_int8_t
)((input
[i
] >> 24) & 0xff);
129 /* Decodes input (u_int8_t) into output (u_int32_t). Assumes len is
132 static void Decode(u_int32_t
*output
, u_int8_t
*input
, size_t len
)
136 for (i
= 0, j
= 0; j
< len
; i
++, j
+= 4)
138 output
[i
] = ((u_int32_t
)input
[j
]) | (((u_int32_t
)input
[j
+1]) << 8) |
139 (((u_int32_t
)input
[j
+2]) << 16) | (((u_int32_t
)input
[j
+3]) << 24);
143 #elif BYTE_ORDER == LITTLE_ENDIAN
144 #define Encode memcpy
145 #define Decode memcpy
148 /* MD5 basic transformation. Transforms state based on block.
150 static void MD5Transform(u_int32_t state
[4], u_int8_t block
[64])
152 u_int32_t a
= state
[0], b
= state
[1], c
= state
[2], d
= state
[3], x
[16];
154 Decode(x
, block
, 64);
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 */
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 */
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 */
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 */
234 /* MD5 block update operation. Continues an MD5 message-digest
235 * operation, processing another message block, and updating the
238 static void MD5Update(private_md5_hasher_t
*this, u_int8_t
*input
, size_t inputLen
)
241 size_t index
, partLen
;
243 /* Compute number of bytes mod 64 */
244 index
= (u_int8_t
)((this->count
[0] >> 3) & 0x3F);
246 /* Update number of bits */
247 if ((this->count
[0] += (inputLen
<< 3)) < (inputLen
<< 3))
251 this->count
[1] += (inputLen
>> 29);
253 partLen
= 64 - index
;
255 /* Transform as many times as possible. */
256 if (inputLen
>= partLen
)
258 memcpy(&this->buffer
[index
], input
, partLen
);
259 MD5Transform (this->state
, this->buffer
);
261 for (i
= partLen
; i
+ 63 < inputLen
; i
+= 64)
263 MD5Transform (this->state
, &input
[i
]);
272 /* Buffer remaining input */
273 memcpy(&this->buffer
[index
], &input
[i
], inputLen
-i
);
276 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
277 * the message digest and zeroizing the context.
279 static void MD5Final (private_md5_hasher_t
*this, u_int8_t digest
[16])
282 size_t index
, padLen
;
284 /* Save number of bits */
285 Encode (bits
, this->count
, 8);
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
);
292 /* Append length (before padding) */
293 MD5Update (this, bits
, 8);
295 if (digest
!= NULL
) /* Bill Simpson's padding */
297 /* store state in digest */
298 Encode (digest
, this->state
, 16);
302 METHOD(hasher_t
, get_hash
, void,
303 private_md5_hasher_t
*this, chunk_t chunk
, u_int8_t
*buffer
)
305 MD5Update(this, chunk
.ptr
, chunk
.len
);
308 MD5Final(this, buffer
);
309 this->public.hasher_interface
.reset(&(this->public.hasher_interface
));
313 METHOD(hasher_t
, allocate_hash
, void,
314 private_md5_hasher_t
*this, chunk_t chunk
, chunk_t
*hash
)
316 chunk_t allocated_hash
;
318 MD5Update(this, chunk
.ptr
, chunk
.len
);
321 allocated_hash
.ptr
= malloc(HASH_SIZE_MD5
);
322 allocated_hash
.len
= HASH_SIZE_MD5
;
324 MD5Final(this, allocated_hash
.ptr
);
325 this->public.hasher_interface
.reset(&(this->public.hasher_interface
));
327 *hash
= allocated_hash
;
331 METHOD(hasher_t
, get_hash_size
, size_t,
332 private_md5_hasher_t
*this)
334 return HASH_SIZE_MD5
;
337 METHOD(hasher_t
, reset
, void,
338 private_md5_hasher_t
*this)
340 this->state
[0] = 0x67452301;
341 this->state
[1] = 0xefcdab89;
342 this->state
[2] = 0x98badcfe;
343 this->state
[3] = 0x10325476;
348 METHOD(hasher_t
, destroy
, void,
349 private_md5_hasher_t
*this)
355 * Described in header.
357 md5_hasher_t
*md5_hasher_create(hash_algorithm_t algo
)
359 private_md5_hasher_t
*this;
361 if (algo
!= HASH_MD5
)
368 .hasher_interface
= {
369 .get_hash
= _get_hash
,
370 .allocate_hash
= _allocate_hash
,
371 .get_hash_size
= _get_hash_size
,
381 return &(this->public);