comparison md4.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 6362d3854bb4
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 /* Submitted by Dobes Vandermeer ([email protected]) */
12 #include "mycrypt.h"
13
14 #ifdef MD4
15
16 const struct _hash_descriptor md4_desc =
17 {
18 "md4",
19 6,
20 16,
21 64,
22 &md4_init,
23 &md4_process,
24 &md4_done,
25 &md4_test
26 };
27
28 #define S11 3
29 #define S12 7
30 #define S13 11
31 #define S14 19
32 #define S21 3
33 #define S22 5
34 #define S23 9
35 #define S24 13
36 #define S31 3
37 #define S32 9
38 #define S33 11
39 #define S34 15
40
41 /* F, G and H are basic MD4 functions. */
42 #define F(x, y, z) (z ^ (x & (y ^ z)))
43 #define G(x, y, z) ((x & y) | (z & (x | y)))
44 #define H(x, y, z) ((x) ^ (y) ^ (z))
45
46 /* ROTATE_LEFT rotates x left n bits. */
47 #define ROTATE_LEFT(x, n) ROL(x, n)
48
49 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
50 /* Rotation is separate from addition to prevent recomputation */
51
52 #define FF(a, b, c, d, x, s) { \
53 (a) += F ((b), (c), (d)) + (x); \
54 (a) = ROTATE_LEFT ((a), (s)); \
55 }
56 #define GG(a, b, c, d, x, s) { \
57 (a) += G ((b), (c), (d)) + (x) + 0x5a827999UL; \
58 (a) = ROTATE_LEFT ((a), (s)); \
59 }
60 #define HH(a, b, c, d, x, s) { \
61 (a) += H ((b), (c), (d)) + (x) + 0x6ed9eba1UL; \
62 (a) = ROTATE_LEFT ((a), (s)); \
63 }
64
65 #ifdef CLEAN_STACK
66 static void _md4_compress(hash_state *md, unsigned char *buf)
67 #else
68 static void md4_compress(hash_state *md, unsigned char *buf)
69 #endif
70 {
71 ulong32 x[16], a, b, c, d;
72 int i;
73
74 /* copy state */
75 a = md->md4.state[0];
76 b = md->md4.state[1];
77 c = md->md4.state[2];
78 d = md->md4.state[3];
79
80 /* copy the state into 512-bits into W[0..15] */
81 for (i = 0; i < 16; i++) {
82 LOAD32L(x[i], buf + (4*i));
83 }
84
85 /* Round 1 */
86 FF (a, b, c, d, x[ 0], S11); /* 1 */
87 FF (d, a, b, c, x[ 1], S12); /* 2 */
88 FF (c, d, a, b, x[ 2], S13); /* 3 */
89 FF (b, c, d, a, x[ 3], S14); /* 4 */
90 FF (a, b, c, d, x[ 4], S11); /* 5 */
91 FF (d, a, b, c, x[ 5], S12); /* 6 */
92 FF (c, d, a, b, x[ 6], S13); /* 7 */
93 FF (b, c, d, a, x[ 7], S14); /* 8 */
94 FF (a, b, c, d, x[ 8], S11); /* 9 */
95 FF (d, a, b, c, x[ 9], S12); /* 10 */
96 FF (c, d, a, b, x[10], S13); /* 11 */
97 FF (b, c, d, a, x[11], S14); /* 12 */
98 FF (a, b, c, d, x[12], S11); /* 13 */
99 FF (d, a, b, c, x[13], S12); /* 14 */
100 FF (c, d, a, b, x[14], S13); /* 15 */
101 FF (b, c, d, a, x[15], S14); /* 16 */
102
103 /* Round 2 */
104 GG (a, b, c, d, x[ 0], S21); /* 17 */
105 GG (d, a, b, c, x[ 4], S22); /* 18 */
106 GG (c, d, a, b, x[ 8], S23); /* 19 */
107 GG (b, c, d, a, x[12], S24); /* 20 */
108 GG (a, b, c, d, x[ 1], S21); /* 21 */
109 GG (d, a, b, c, x[ 5], S22); /* 22 */
110 GG (c, d, a, b, x[ 9], S23); /* 23 */
111 GG (b, c, d, a, x[13], S24); /* 24 */
112 GG (a, b, c, d, x[ 2], S21); /* 25 */
113 GG (d, a, b, c, x[ 6], S22); /* 26 */
114 GG (c, d, a, b, x[10], S23); /* 27 */
115 GG (b, c, d, a, x[14], S24); /* 28 */
116 GG (a, b, c, d, x[ 3], S21); /* 29 */
117 GG (d, a, b, c, x[ 7], S22); /* 30 */
118 GG (c, d, a, b, x[11], S23); /* 31 */
119 GG (b, c, d, a, x[15], S24); /* 32 */
120
121 /* Round 3 */
122 HH (a, b, c, d, x[ 0], S31); /* 33 */
123 HH (d, a, b, c, x[ 8], S32); /* 34 */
124 HH (c, d, a, b, x[ 4], S33); /* 35 */
125 HH (b, c, d, a, x[12], S34); /* 36 */
126 HH (a, b, c, d, x[ 2], S31); /* 37 */
127 HH (d, a, b, c, x[10], S32); /* 38 */
128 HH (c, d, a, b, x[ 6], S33); /* 39 */
129 HH (b, c, d, a, x[14], S34); /* 40 */
130 HH (a, b, c, d, x[ 1], S31); /* 41 */
131 HH (d, a, b, c, x[ 9], S32); /* 42 */
132 HH (c, d, a, b, x[ 5], S33); /* 43 */
133 HH (b, c, d, a, x[13], S34); /* 44 */
134 HH (a, b, c, d, x[ 3], S31); /* 45 */
135 HH (d, a, b, c, x[11], S32); /* 46 */
136 HH (c, d, a, b, x[ 7], S33); /* 47 */
137 HH (b, c, d, a, x[15], S34); /* 48 */
138
139
140 /* Update our state */
141 md->md4.state[0] = md->md4.state[0] + a;
142 md->md4.state[1] = md->md4.state[1] + b;
143 md->md4.state[2] = md->md4.state[2] + c;
144 md->md4.state[3] = md->md4.state[3] + d;
145 }
146
147 #ifdef CLEAN_STACK
148 static void md4_compress(hash_state *md, unsigned char *buf)
149 {
150 _md4_compress(md, buf);
151 burn_stack(sizeof(ulong32) * 20 + sizeof(int));
152 }
153 #endif
154
155 void md4_init(hash_state * md)
156 {
157 _ARGCHK(md != NULL);
158 md->md4.state[0] = 0x67452301UL;
159 md->md4.state[1] = 0xefcdab89UL;
160 md->md4.state[2] = 0x98badcfeUL;
161 md->md4.state[3] = 0x10325476UL;
162 md->md4.length = 0;
163 md->md4.curlen = 0;
164 }
165
166 HASH_PROCESS(md4_process, md4_compress, md4, 64)
167
168 int md4_done(hash_state * md, unsigned char *hash)
169 {
170 int i;
171
172 _ARGCHK(md != NULL);
173 _ARGCHK(hash != NULL);
174
175 if (md->md4.curlen >= sizeof(md->md4.buf)) {
176 return CRYPT_INVALID_ARG;
177 }
178
179 /* increase the length of the message */
180 md->md4.length += md->md4.curlen * 8;
181
182 /* append the '1' bit */
183 md->md4.buf[md->md4.curlen++] = (unsigned char)0x80;
184
185 /* if the length is currently above 56 bytes we append zeros
186 * then compress. Then we can fall back to padding zeros and length
187 * encoding like normal.
188 */
189 if (md->md4.curlen > 56) {
190 while (md->md4.curlen < 64) {
191 md->md4.buf[md->md4.curlen++] = (unsigned char)0;
192 }
193 md4_compress(md, md->md4.buf);
194 md->md4.curlen = 0;
195 }
196
197 /* pad upto 56 bytes of zeroes */
198 while (md->md4.curlen < 56) {
199 md->md4.buf[md->md4.curlen++] = (unsigned char)0;
200 }
201
202 /* store length */
203 STORE64L(md->md4.length, md->md4.buf+56);
204 md4_compress(md, md->md4.buf);
205
206 /* copy output */
207 for (i = 0; i < 4; i++) {
208 STORE32L(md->md4.state[i], hash+(4*i));
209 }
210 #ifdef CLEAN_STACK
211 zeromem(md, sizeof(hash_state));
212 #endif
213 return CRYPT_OK;
214 }
215
216 int md4_test(void)
217 {
218 #ifndef LTC_TEST
219 return CRYPT_NOP;
220 #else
221 static const struct md4_test_case {
222 char *input;
223 unsigned char digest[16];
224 } cases[] = {
225 { "",
226 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
227 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
228 { "a",
229 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
230 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
231 { "abc",
232 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
233 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
234 { "message digest",
235 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
236 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
237 { "abcdefghijklmnopqrstuvwxyz",
238 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
239 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
240 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
241 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
242 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
243 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
244 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
245 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} },
246 };
247 int i;
248 hash_state md;
249 unsigned char digest[16];
250
251 for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) {
252 md4_init(&md);
253 md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input));
254 md4_done(&md, digest);
255 if (memcmp(digest, cases[i].digest, 16) != 0) {
256 return CRYPT_FAIL_TESTVECTOR;
257 }
258
259 }
260 return CRYPT_OK;
261 #endif
262 }
263
264 #endif
265
266