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