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