comparison libtomcrypt/src/hashes/md4.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @param md4.c 12 @param md4.c
15 Submitted by Dobes Vandermeer ([email protected]) 13 Submitted by Dobes Vandermeer ([email protected])
16 */ 14 */
17 15
18 #ifdef LTC_MD4 16 #ifdef LTC_MD4
19 17
20 const struct ltc_hash_descriptor md4_desc = 18 const struct ltc_hash_descriptor md4_desc =
21 { 19 {
22 "md4", 20 "md4",
23 6, 21 6,
24 16, 22 16,
25 64, 23 64,
26 24
27 /* OID */ 25 /* OID */
28 { 1, 2, 840, 113549, 2, 4, }, 26 { 1, 2, 840, 113549, 2, 4, },
29 6, 27 6,
30 28
31 &md4_init, 29 &md4_init,
54 #define H(x, y, z) ((x) ^ (y) ^ (z)) 52 #define H(x, y, z) ((x) ^ (y) ^ (z))
55 53
56 /* ROTATE_LEFT rotates x left n bits. */ 54 /* ROTATE_LEFT rotates x left n bits. */
57 #define ROTATE_LEFT(x, n) ROLc(x, n) 55 #define ROTATE_LEFT(x, n) ROLc(x, n)
58 56
59 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */ 57 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
60 /* Rotation is separate from addition to prevent recomputation */ 58 /* Rotation is separate from addition to prevent recomputation */
61 59
62 #define FF(a, b, c, d, x, s) { \ 60 #define FF(a, b, c, d, x, s) { \
63 (a) += F ((b), (c), (d)) + (x); \ 61 (a) += F ((b), (c), (d)) + (x); \
64 (a) = ROTATE_LEFT ((a), (s)); \ 62 (a) = ROTATE_LEFT ((a), (s)); \
65 } 63 }
89 87
90 /* copy the state into 512-bits into W[0..15] */ 88 /* copy the state into 512-bits into W[0..15] */
91 for (i = 0; i < 16; i++) { 89 for (i = 0; i < 16; i++) {
92 LOAD32L(x[i], buf + (4*i)); 90 LOAD32L(x[i], buf + (4*i));
93 } 91 }
94 92
95 /* Round 1 */ 93 /* Round 1 */
96 FF (a, b, c, d, x[ 0], S11); /* 1 */ 94 FF (a, b, c, d, x[ 0], S11); /* 1 */
97 FF (d, a, b, c, x[ 1], S12); /* 2 */ 95 FF (d, a, b, c, x[ 1], S12); /* 2 */
98 FF (c, d, a, b, x[ 2], S13); /* 3 */ 96 FF (c, d, a, b, x[ 2], S13); /* 3 */
99 FF (b, c, d, a, x[ 3], S14); /* 4 */ 97 FF (b, c, d, a, x[ 3], S14); /* 4 */
100 FF (a, b, c, d, x[ 4], S11); /* 5 */ 98 FF (a, b, c, d, x[ 4], S11); /* 5 */
101 FF (d, a, b, c, x[ 5], S12); /* 6 */ 99 FF (d, a, b, c, x[ 5], S12); /* 6 */
102 FF (c, d, a, b, x[ 6], S13); /* 7 */ 100 FF (c, d, a, b, x[ 6], S13); /* 7 */
103 FF (b, c, d, a, x[ 7], S14); /* 8 */ 101 FF (b, c, d, a, x[ 7], S14); /* 8 */
104 FF (a, b, c, d, x[ 8], S11); /* 9 */ 102 FF (a, b, c, d, x[ 8], S11); /* 9 */
105 FF (d, a, b, c, x[ 9], S12); /* 10 */ 103 FF (d, a, b, c, x[ 9], S12); /* 10 */
106 FF (c, d, a, b, x[10], S13); /* 11 */ 104 FF (c, d, a, b, x[10], S13); /* 11 */
107 FF (b, c, d, a, x[11], S14); /* 12 */ 105 FF (b, c, d, a, x[11], S14); /* 12 */
108 FF (a, b, c, d, x[12], S11); /* 13 */ 106 FF (a, b, c, d, x[12], S11); /* 13 */
109 FF (d, a, b, c, x[13], S12); /* 14 */ 107 FF (d, a, b, c, x[13], S12); /* 14 */
110 FF (c, d, a, b, x[14], S13); /* 15 */ 108 FF (c, d, a, b, x[14], S13); /* 15 */
111 FF (b, c, d, a, x[15], S14); /* 16 */ 109 FF (b, c, d, a, x[15], S14); /* 16 */
112 110
113 /* Round 2 */ 111 /* Round 2 */
114 GG (a, b, c, d, x[ 0], S21); /* 17 */ 112 GG (a, b, c, d, x[ 0], S21); /* 17 */
115 GG (d, a, b, c, x[ 4], S22); /* 18 */ 113 GG (d, a, b, c, x[ 4], S22); /* 18 */
116 GG (c, d, a, b, x[ 8], S23); /* 19 */ 114 GG (c, d, a, b, x[ 8], S23); /* 19 */
117 GG (b, c, d, a, x[12], S24); /* 20 */ 115 GG (b, c, d, a, x[12], S24); /* 20 */
118 GG (a, b, c, d, x[ 1], S21); /* 21 */ 116 GG (a, b, c, d, x[ 1], S21); /* 21 */
119 GG (d, a, b, c, x[ 5], S22); /* 22 */ 117 GG (d, a, b, c, x[ 5], S22); /* 22 */
120 GG (c, d, a, b, x[ 9], S23); /* 23 */ 118 GG (c, d, a, b, x[ 9], S23); /* 23 */
121 GG (b, c, d, a, x[13], S24); /* 24 */ 119 GG (b, c, d, a, x[13], S24); /* 24 */
122 GG (a, b, c, d, x[ 2], S21); /* 25 */ 120 GG (a, b, c, d, x[ 2], S21); /* 25 */
123 GG (d, a, b, c, x[ 6], S22); /* 26 */ 121 GG (d, a, b, c, x[ 6], S22); /* 26 */
124 GG (c, d, a, b, x[10], S23); /* 27 */ 122 GG (c, d, a, b, x[10], S23); /* 27 */
125 GG (b, c, d, a, x[14], S24); /* 28 */ 123 GG (b, c, d, a, x[14], S24); /* 28 */
126 GG (a, b, c, d, x[ 3], S21); /* 29 */ 124 GG (a, b, c, d, x[ 3], S21); /* 29 */
127 GG (d, a, b, c, x[ 7], S22); /* 30 */ 125 GG (d, a, b, c, x[ 7], S22); /* 30 */
128 GG (c, d, a, b, x[11], S23); /* 31 */ 126 GG (c, d, a, b, x[11], S23); /* 31 */
129 GG (b, c, d, a, x[15], S24); /* 32 */ 127 GG (b, c, d, a, x[15], S24); /* 32 */
130 128
131 /* Round 3 */ 129 /* Round 3 */
132 HH (a, b, c, d, x[ 0], S31); /* 33 */ 130 HH (a, b, c, d, x[ 0], S31); /* 33 */
133 HH (d, a, b, c, x[ 8], S32); /* 34 */ 131 HH (d, a, b, c, x[ 8], S32); /* 34 */
134 HH (c, d, a, b, x[ 4], S33); /* 35 */ 132 HH (c, d, a, b, x[ 4], S33); /* 35 */
135 HH (b, c, d, a, x[12], S34); /* 36 */ 133 HH (b, c, d, a, x[12], S34); /* 36 */
136 HH (a, b, c, d, x[ 2], S31); /* 37 */ 134 HH (a, b, c, d, x[ 2], S31); /* 37 */
137 HH (d, a, b, c, x[10], S32); /* 38 */ 135 HH (d, a, b, c, x[10], S32); /* 38 */
138 HH (c, d, a, b, x[ 6], S33); /* 39 */ 136 HH (c, d, a, b, x[ 6], S33); /* 39 */
139 HH (b, c, d, a, x[14], S34); /* 40 */ 137 HH (b, c, d, a, x[14], S34); /* 40 */
140 HH (a, b, c, d, x[ 1], S31); /* 41 */ 138 HH (a, b, c, d, x[ 1], S31); /* 41 */
141 HH (d, a, b, c, x[ 9], S32); /* 42 */ 139 HH (d, a, b, c, x[ 9], S32); /* 42 */
142 HH (c, d, a, b, x[ 5], S33); /* 43 */ 140 HH (c, d, a, b, x[ 5], S33); /* 43 */
143 HH (b, c, d, a, x[13], S34); /* 44 */ 141 HH (b, c, d, a, x[13], S34); /* 44 */
144 HH (a, b, c, d, x[ 3], S31); /* 45 */ 142 HH (a, b, c, d, x[ 3], S31); /* 45 */
145 HH (d, a, b, c, x[11], S32); /* 46 */ 143 HH (d, a, b, c, x[11], S32); /* 46 */
146 HH (c, d, a, b, x[ 7], S33); /* 47 */ 144 HH (c, d, a, b, x[ 7], S33); /* 47 */
147 HH (b, c, d, a, x[15], S34); /* 48 */ 145 HH (b, c, d, a, x[15], S34); /* 48 */
148 146
149 147
150 /* Update our state */ 148 /* Update our state */
151 md->md4.state[0] = md->md4.state[0] + a; 149 md->md4.state[0] = md->md4.state[0] + a;
152 md->md4.state[1] = md->md4.state[1] + b; 150 md->md4.state[1] = md->md4.state[1] + b;
153 md->md4.state[2] = md->md4.state[2] + c; 151 md->md4.state[2] = md->md4.state[2] + c;
240 for (i = 0; i < 4; i++) { 238 for (i = 0; i < 4; i++) {
241 STORE32L(md->md4.state[i], out+(4*i)); 239 STORE32L(md->md4.state[i], out+(4*i));
242 } 240 }
243 #ifdef LTC_CLEAN_STACK 241 #ifdef LTC_CLEAN_STACK
244 zeromem(md, sizeof(hash_state)); 242 zeromem(md, sizeof(hash_state));
245 #endif 243 #endif
246 return CRYPT_OK; 244 return CRYPT_OK;
247 } 245 }
248 246
249 /** 247 /**
250 Self-test the hash 248 Self-test the hash
251 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 249 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
252 */ 250 */
253 int md4_test(void) 251 int md4_test(void)
254 { 252 {
255 #ifndef LTC_TEST 253 #ifndef LTC_TEST
256 return CRYPT_NOP; 254 return CRYPT_NOP;
257 #else 255 #else
258 static const struct md4_test_case { 256 static const struct md4_test_case {
259 char *input; 257 const char *input;
260 unsigned char digest[16]; 258 unsigned char hash[16];
261 } cases[] = { 259 } tests[] = {
262 { "", 260 { "",
263 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 261 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
264 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} }, 262 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
265 { "a", 263 { "a",
266 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 264 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
267 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} }, 265 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
268 { "abc", 266 { "abc",
269 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 267 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
270 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} }, 268 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
271 { "message digest", 269 { "message digest",
272 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 270 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
273 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} }, 271 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
274 { "abcdefghijklmnopqrstuvwxyz", 272 { "abcdefghijklmnopqrstuvwxyz",
275 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 273 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
276 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} }, 274 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
277 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 275 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
278 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 276 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
279 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} }, 277 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
280 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 278 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
281 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 279 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
282 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} }, 280 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} },
283 }; 281 };
282
284 int i; 283 int i;
284 unsigned char tmp[16];
285 hash_state md; 285 hash_state md;
286 unsigned char digest[16]; 286
287 287 for(i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
288 for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) {
289 md4_init(&md); 288 md4_init(&md);
290 md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input)); 289 md4_process(&md, (unsigned char *)tests[i].input, (unsigned long)strlen(tests[i].input));
291 md4_done(&md, digest); 290 md4_done(&md, tmp);
292 if (XMEMCMP(digest, cases[i].digest, 16) != 0) { 291 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD4", i)) {
293 return CRYPT_FAIL_TESTVECTOR; 292 return CRYPT_FAIL_TESTVECTOR;
294 } 293 }
295 294
296 } 295 }
297 return CRYPT_OK; 296 return CRYPT_OK;
300 299
301 #endif 300 #endif
302 301
303 302
304 303
305 /* $Source$ */ 304 /* ref: $Format:%D$ */
306 /* $Revision$ */ 305 /* git commit: $Format:%H$ */
307 /* $Date$ */ 306 /* commit time: $Format:%ai$ */