added Id svn:keyword
[strongswan.git] / src / libstrongswan / plugins / md4 / md4_hasher.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 1990-1992, RSA Data Security, Inc. Created 1990.
6 * All rights reserved.
7 *
8 * Derived from the RSA Data Security, Inc. MD4 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 * $Id$
22 */
23
24 #include <string.h>
25
26 #include "md4_hasher.h"
27
28 /*
29 * Constants for MD4Transform routine.
30 */
31 #define S11 3
32 #define S12 7
33 #define S13 11
34 #define S14 19
35 #define S21 3
36 #define S22 5
37 #define S23 9
38 #define S24 13
39 #define S31 3
40 #define S32 9
41 #define S33 11
42 #define S34 15
43
44 static u_int8_t PADDING[64] = {
45 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 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
48 };
49
50 /*
51 * F, G, H and I are basic MD4 functions.
52 */
53 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
54 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
55 #define H(x, y, z) ((x) ^ (y) ^ (z))
56
57 /*
58 * ROTATE_LEFT rotates x left n bits.
59 */
60 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
61
62 /* FF, GG and HH are transformations for rounds 1, 2 and 3
63 * Rotation is separate from addition to prevent recomputation
64 */
65 #define FF(a, b, c, d, x, s) { \
66 (a) += F ((b), (c), (d)) + (x); \
67 (a) = ROTATE_LEFT ((a), (s)); \
68 }
69 #define GG(a, b, c, d, x, s) { \
70 (a) += G ((b), (c), (d)) + (x) + (u_int32_t)0x5a827999; \
71 (a) = ROTATE_LEFT ((a), (s)); \
72 }
73 #define HH(a, b, c, d, x, s) { \
74 (a) += H ((b), (c), (d)) + (x) + (u_int32_t)0x6ed9eba1; \
75 (a) = ROTATE_LEFT ((a), (s)); \
76 }
77
78 typedef struct private_md4_hasher_t private_md4_hasher_t;
79
80 /**
81 * Private data structure with hasing context.
82 */
83 struct private_md4_hasher_t {
84 /**
85 * Public interface for this hasher.
86 */
87 md4_hasher_t public;
88
89 /*
90 * State of the hasher.
91 */
92 u_int32_t state[4];
93 u_int32_t count[2];
94 u_int8_t buffer[64];
95 };
96
97 #if BYTE_ORDER != LITTLE_ENDIAN
98
99 /* Encodes input (u_int32_t) into output (u_int8_t). Assumes len is
100 * a multiple of 4.
101 */
102 static void Encode (u_int8_t *output, u_int32_t *input, size_t len)
103 {
104 size_t i, j;
105
106 for (i = 0, j = 0; j < len; i++, j += 4)
107 {
108 output[j] = (u_int8_t)(input[i] & 0xff);
109 output[j+1] = (u_int8_t)((input[i] >> 8) & 0xff);
110 output[j+2] = (u_int8_t)((input[i] >> 16) & 0xff);
111 output[j+3] = (u_int8_t)((input[i] >> 24) & 0xff);
112 }
113 }
114
115 /* Decodes input (u_int8_t) into output (u_int32_t). Assumes len is
116 * a multiple of 4.
117 */
118 static void Decode(u_int32_t *output, u_int8_t *input, size_t len)
119 {
120 size_t i, j;
121
122 for (i = 0, j = 0; j < len; i++, j += 4)
123 {
124 output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) |
125 (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24);
126 }
127 }
128
129 #elif BYTE_ORDER == LITTLE_ENDIAN
130 #define Encode memcpy
131 #define Decode memcpy
132 #endif
133
134 /*
135 * MD4 basic transformation. Transforms state based on block.
136 */
137 static void MD4Transform(u_int32_t state[4], u_int8_t block[64])
138 {
139 u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
140
141 Decode(x, block, 64);
142
143 /* Round 1 */
144 FF (a, b, c, d, x[ 0], S11); /* 1 */
145 FF (d, a, b, c, x[ 1], S12); /* 2 */
146 FF (c, d, a, b, x[ 2], S13); /* 3 */
147 FF (b, c, d, a, x[ 3], S14); /* 4 */
148 FF (a, b, c, d, x[ 4], S11); /* 5 */
149 FF (d, a, b, c, x[ 5], S12); /* 6 */
150 FF (c, d, a, b, x[ 6], S13); /* 7 */
151 FF (b, c, d, a, x[ 7], S14); /* 8 */
152 FF (a, b, c, d, x[ 8], S11); /* 9 */
153 FF (d, a, b, c, x[ 9], S12); /* 10 */
154 FF (c, d, a, b, x[10], S13); /* 11 */
155 FF (b, c, d, a, x[11], S14); /* 12 */
156 FF (a, b, c, d, x[12], S11); /* 13 */
157 FF (d, a, b, c, x[13], S12); /* 14 */
158 FF (c, d, a, b, x[14], S13); /* 15 */
159 FF (b, c, d, a, x[15], S14); /* 16 */
160
161 /* Round 2 */
162 GG (a, b, c, d, x[ 0], S21); /* 17 */
163 GG (d, a, b, c, x[ 4], S22); /* 18 */
164 GG (c, d, a, b, x[ 8], S23); /* 19 */
165 GG (b, c, d, a, x[12], S24); /* 20 */
166 GG (a, b, c, d, x[ 1], S21); /* 21 */
167 GG (d, a, b, c, x[ 5], S22); /* 22 */
168 GG (c, d, a, b, x[ 9], S23); /* 23 */
169 GG (b, c, d, a, x[13], S24); /* 24 */
170 GG (a, b, c, d, x[ 2], S21); /* 25 */
171 GG (d, a, b, c, x[ 6], S22); /* 26 */
172 GG (c, d, a, b, x[10], S23); /* 27 */
173 GG (b, c, d, a, x[14], S24); /* 28 */
174 GG (a, b, c, d, x[ 3], S21); /* 29 */
175 GG (d, a, b, c, x[ 7], S22); /* 30 */
176 GG (c, d, a, b, x[11], S23); /* 31 */
177 GG (b, c, d, a, x[15], S24); /* 32 */
178
179 /* Round 3 */
180 HH (a, b, c, d, x[ 0], S31); /* 33 */
181 HH (d, a, b, c, x[ 8], S32); /* 34 */
182 HH (c, d, a, b, x[ 4], S33); /* 35 */
183 HH (b, c, d, a, x[12], S34); /* 36 */
184 HH (a, b, c, d, x[ 2], S31); /* 37 */
185 HH (d, a, b, c, x[10], S32); /* 38 */
186 HH (c, d, a, b, x[ 6], S33); /* 39 */
187 HH (b, c, d, a, x[14], S34); /* 40 */
188 HH (a, b, c, d, x[ 1], S31); /* 41 */
189 HH (d, a, b, c, x[ 9], S32); /* 42 */
190 HH (c, d, a, b, x[ 5], S33); /* 43 */
191 HH (b, c, d, a, x[13], S34); /* 44 */
192 HH (a, b, c, d, x[ 3], S31); /* 45 */
193 HH (d, a, b, c, x[11], S32); /* 46 */
194 HH (c, d, a, b, x[ 7], S33); /* 47 */
195 HH (b, c, d, a, x[15], S34); /* 48 */
196
197 state[0] += a;
198 state[1] += b;
199 state[2] += c;
200 state[3] += d;
201 }
202
203 /* MD4 block update operation. Continues an MD4 message-digest
204 * operation, processing another message block, and updating the
205 * context.
206 */
207 static void MD4Update(private_md4_hasher_t *this, u_int8_t *input, size_t inputLen)
208 {
209 u_int32_t i;
210 size_t index, partLen;
211
212 /* Compute number of bytes mod 64 */
213 index = (u_int8_t)((this->count[0] >> 3) & 0x3F);
214
215 /* Update number of bits */
216 if ((this->count[0] += (inputLen << 3)) < (inputLen << 3))
217 {
218 this->count[1]++;
219 }
220 this->count[1] += (inputLen >> 29);
221
222 partLen = 64 - index;
223
224 /* Transform as many times as possible. */
225 if (inputLen >= partLen)
226 {
227 memcpy(&this->buffer[index], input, partLen);
228 MD4Transform (this->state, this->buffer);
229
230 for (i = partLen; i + 63 < inputLen; i += 64)
231 {
232 MD4Transform (this->state, &input[i]);
233 }
234 index = 0;
235 }
236 else
237 {
238 i = 0;
239 }
240
241 /* Buffer remaining input */
242 memcpy(&this->buffer[index], &input[i], inputLen-i);
243 }
244
245 /* MD4 finalization. Ends an MD4 message-digest operation, writing the
246 * the message digest and zeroizing the context.
247 */
248 static void MD4Final (private_md4_hasher_t *this, u_int8_t digest[16])
249 {
250 u_int8_t bits[8];
251 size_t index, padLen;
252
253 /* Save number of bits */
254 Encode (bits, this->count, 8);
255
256 /* Pad out to 56 mod 64. */
257 index = (size_t)((this->count[0] >> 3) & 0x3f);
258 padLen = (index < 56) ? (56 - index) : (120 - index);
259 MD4Update (this, PADDING, padLen);
260
261 /* Append length (before padding) */
262 MD4Update (this, bits, 8);
263
264 if (digest != NULL) /* Bill Simpson's padding */
265 {
266 /* store state in digest */
267 Encode (digest, this->state, 16);
268 }
269 }
270
271
272
273 /**
274 * Implementation of hasher_t.get_hash.
275 */
276 static void get_hash(private_md4_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
277 {
278 MD4Update(this, chunk.ptr, chunk.len);
279 if (buffer != NULL)
280 {
281 MD4Final(this, buffer);
282 this->public.hasher_interface.reset(&(this->public.hasher_interface));
283 }
284 }
285
286
287 /**
288 * Implementation of hasher_t.allocate_hash.
289 */
290 static void allocate_hash(private_md4_hasher_t *this, chunk_t chunk, chunk_t *hash)
291 {
292 chunk_t allocated_hash;
293
294 MD4Update(this, chunk.ptr, chunk.len);
295 if (hash != NULL)
296 {
297 allocated_hash.ptr = malloc(HASH_SIZE_MD4);
298 allocated_hash.len = HASH_SIZE_MD4;
299
300 MD4Final(this, allocated_hash.ptr);
301 this->public.hasher_interface.reset(&(this->public.hasher_interface));
302
303 *hash = allocated_hash;
304 }
305 }
306
307 /**
308 * Implementation of hasher_t.get_hash_size.
309 */
310 static size_t get_hash_size(private_md4_hasher_t *this)
311 {
312 return HASH_SIZE_MD4;
313 }
314
315 /**
316 * Implementation of hasher_t.reset.
317 */
318 static void reset(private_md4_hasher_t *this)
319 {
320 this->state[0] = 0x67452301;
321 this->state[1] = 0xefcdab89;
322 this->state[2] = 0x98badcfe;
323 this->state[3] = 0x10325476;
324 this->count[0] = 0;
325 this->count[1] = 0;
326 }
327
328 /**
329 * Implementation of hasher_t.destroy.
330 */
331 static void destroy(private_md4_hasher_t *this)
332 {
333 free(this);
334 }
335
336 /*
337 * Described in header.
338 */
339 md4_hasher_t *md4_hasher_create(hash_algorithm_t algo)
340 {
341 private_md4_hasher_t *this;
342
343 if (algo != HASH_MD4)
344 {
345 return NULL;
346 }
347 this = malloc_thing(private_md4_hasher_t);
348
349 this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
350 this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
351 this->public.hasher_interface.get_hash_size = (size_t (*) (hasher_t*))get_hash_size;
352 this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
353 this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
354
355 /* initialize */
356 reset(this);
357
358 return &(this->public);
359 }