1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13    @param rmd256.c
14    RLTC_MD256 Hash function
15 */
16 
17 #ifdef LTC_RIPEMD256
18 
19 const struct ltc_hash_descriptor rmd256_desc =
20 {
21     "rmd256",
22     13,
23     32,
24     64,
25 
26     /* OID */
27    { 1, 3, 36, 3, 2, 3 },
28    6,
29 
30     &rmd256_init,
31     &rmd256_process,
32     &rmd256_done,
33     &rmd256_test,
34     NULL
35 };
36 
37 /* the four basic functions F(), G() and H() */
38 #define F(x, y, z)        ((x) ^ (y) ^ (z))
39 #define G(x, y, z)        (((x) & (y)) | (~(x) & (z)))
40 #define H(x, y, z)        (((x) | ~(y)) ^ (z))
41 #define I(x, y, z)        (((x) & (z)) | ((y) & ~(z)))
42 
43 /* the eight basic operations FF() through III() */
44 #define FF(a, b, c, d, x, s)        \
45       (a) += F((b), (c), (d)) + (x);\
46       (a) = ROLc((a), (s));
47 
48 #define GG(a, b, c, d, x, s)        \
49       (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
50       (a) = ROLc((a), (s));
51 
52 #define HH(a, b, c, d, x, s)        \
53       (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
54       (a) = ROLc((a), (s));
55 
56 #define II(a, b, c, d, x, s)        \
57       (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
58       (a) = ROLc((a), (s));
59 
60 #define FFF(a, b, c, d, x, s)        \
61       (a) += F((b), (c), (d)) + (x);\
62       (a) = ROLc((a), (s));
63 
64 #define GGG(a, b, c, d, x, s)        \
65       (a) += G((b), (c), (d)) + (x) + 0x6d703ef3UL;\
66       (a) = ROLc((a), (s));
67 
68 #define HHH(a, b, c, d, x, s)        \
69       (a) += H((b), (c), (d)) + (x) + 0x5c4dd124UL;\
70       (a) = ROLc((a), (s));
71 
72 #define III(a, b, c, d, x, s)        \
73       (a) += I((b), (c), (d)) + (x) + 0x50a28be6UL;\
74       (a) = ROLc((a), (s));
75 
76 #ifdef LTC_CLEAN_STACK
_rmd256_compress(hash_state * md,const unsigned char * buf)77 static int _rmd256_compress(hash_state *md, const unsigned char *buf)
78 #else
79 static int  rmd256_compress(hash_state *md, const unsigned char *buf)
80 #endif
81 {
82    ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,tmp,X[16];
83    int i;
84 
85    /* load words X */
86    for (i = 0; i < 16; i++){
87       LOAD32L(X[i], buf + (4 * i));
88    }
89 
90    /* load state */
91    aa = md->rmd256.state[0];
92    bb = md->rmd256.state[1];
93    cc = md->rmd256.state[2];
94    dd = md->rmd256.state[3];
95    aaa = md->rmd256.state[4];
96    bbb = md->rmd256.state[5];
97    ccc = md->rmd256.state[6];
98    ddd = md->rmd256.state[7];
99 
100    /* round 1 */
101    FF(aa, bb, cc, dd, X[ 0], 11);
102    FF(dd, aa, bb, cc, X[ 1], 14);
103    FF(cc, dd, aa, bb, X[ 2], 15);
104    FF(bb, cc, dd, aa, X[ 3], 12);
105    FF(aa, bb, cc, dd, X[ 4],  5);
106    FF(dd, aa, bb, cc, X[ 5],  8);
107    FF(cc, dd, aa, bb, X[ 6],  7);
108    FF(bb, cc, dd, aa, X[ 7],  9);
109    FF(aa, bb, cc, dd, X[ 8], 11);
110    FF(dd, aa, bb, cc, X[ 9], 13);
111    FF(cc, dd, aa, bb, X[10], 14);
112    FF(bb, cc, dd, aa, X[11], 15);
113    FF(aa, bb, cc, dd, X[12],  6);
114    FF(dd, aa, bb, cc, X[13],  7);
115    FF(cc, dd, aa, bb, X[14],  9);
116    FF(bb, cc, dd, aa, X[15],  8);
117 
118    /* parallel round 1 */
119    III(aaa, bbb, ccc, ddd, X[ 5],  8);
120    III(ddd, aaa, bbb, ccc, X[14],  9);
121    III(ccc, ddd, aaa, bbb, X[ 7],  9);
122    III(bbb, ccc, ddd, aaa, X[ 0], 11);
123    III(aaa, bbb, ccc, ddd, X[ 9], 13);
124    III(ddd, aaa, bbb, ccc, X[ 2], 15);
125    III(ccc, ddd, aaa, bbb, X[11], 15);
126    III(bbb, ccc, ddd, aaa, X[ 4],  5);
127    III(aaa, bbb, ccc, ddd, X[13],  7);
128    III(ddd, aaa, bbb, ccc, X[ 6],  7);
129    III(ccc, ddd, aaa, bbb, X[15],  8);
130    III(bbb, ccc, ddd, aaa, X[ 8], 11);
131    III(aaa, bbb, ccc, ddd, X[ 1], 14);
132    III(ddd, aaa, bbb, ccc, X[10], 14);
133    III(ccc, ddd, aaa, bbb, X[ 3], 12);
134    III(bbb, ccc, ddd, aaa, X[12],  6);
135 
136    tmp = aa; aa = aaa; aaa = tmp;
137 
138    /* round 2 */
139    GG(aa, bb, cc, dd, X[ 7],  7);
140    GG(dd, aa, bb, cc, X[ 4],  6);
141    GG(cc, dd, aa, bb, X[13],  8);
142    GG(bb, cc, dd, aa, X[ 1], 13);
143    GG(aa, bb, cc, dd, X[10], 11);
144    GG(dd, aa, bb, cc, X[ 6],  9);
145    GG(cc, dd, aa, bb, X[15],  7);
146    GG(bb, cc, dd, aa, X[ 3], 15);
147    GG(aa, bb, cc, dd, X[12],  7);
148    GG(dd, aa, bb, cc, X[ 0], 12);
149    GG(cc, dd, aa, bb, X[ 9], 15);
150    GG(bb, cc, dd, aa, X[ 5],  9);
151    GG(aa, bb, cc, dd, X[ 2], 11);
152    GG(dd, aa, bb, cc, X[14],  7);
153    GG(cc, dd, aa, bb, X[11], 13);
154    GG(bb, cc, dd, aa, X[ 8], 12);
155 
156    /* parallel round 2 */
157    HHH(aaa, bbb, ccc, ddd, X[ 6],  9);
158    HHH(ddd, aaa, bbb, ccc, X[11], 13);
159    HHH(ccc, ddd, aaa, bbb, X[ 3], 15);
160    HHH(bbb, ccc, ddd, aaa, X[ 7],  7);
161    HHH(aaa, bbb, ccc, ddd, X[ 0], 12);
162    HHH(ddd, aaa, bbb, ccc, X[13],  8);
163    HHH(ccc, ddd, aaa, bbb, X[ 5],  9);
164    HHH(bbb, ccc, ddd, aaa, X[10], 11);
165    HHH(aaa, bbb, ccc, ddd, X[14],  7);
166    HHH(ddd, aaa, bbb, ccc, X[15],  7);
167    HHH(ccc, ddd, aaa, bbb, X[ 8], 12);
168    HHH(bbb, ccc, ddd, aaa, X[12],  7);
169    HHH(aaa, bbb, ccc, ddd, X[ 4],  6);
170    HHH(ddd, aaa, bbb, ccc, X[ 9], 15);
171    HHH(ccc, ddd, aaa, bbb, X[ 1], 13);
172    HHH(bbb, ccc, ddd, aaa, X[ 2], 11);
173 
174    tmp = bb; bb = bbb; bbb = tmp;
175 
176    /* round 3 */
177    HH(aa, bb, cc, dd, X[ 3], 11);
178    HH(dd, aa, bb, cc, X[10], 13);
179    HH(cc, dd, aa, bb, X[14],  6);
180    HH(bb, cc, dd, aa, X[ 4],  7);
181    HH(aa, bb, cc, dd, X[ 9], 14);
182    HH(dd, aa, bb, cc, X[15],  9);
183    HH(cc, dd, aa, bb, X[ 8], 13);
184    HH(bb, cc, dd, aa, X[ 1], 15);
185    HH(aa, bb, cc, dd, X[ 2], 14);
186    HH(dd, aa, bb, cc, X[ 7],  8);
187    HH(cc, dd, aa, bb, X[ 0], 13);
188    HH(bb, cc, dd, aa, X[ 6],  6);
189    HH(aa, bb, cc, dd, X[13],  5);
190    HH(dd, aa, bb, cc, X[11], 12);
191    HH(cc, dd, aa, bb, X[ 5],  7);
192    HH(bb, cc, dd, aa, X[12],  5);
193 
194    /* parallel round 3 */
195    GGG(aaa, bbb, ccc, ddd, X[15],  9);
196    GGG(ddd, aaa, bbb, ccc, X[ 5],  7);
197    GGG(ccc, ddd, aaa, bbb, X[ 1], 15);
198    GGG(bbb, ccc, ddd, aaa, X[ 3], 11);
199    GGG(aaa, bbb, ccc, ddd, X[ 7],  8);
200    GGG(ddd, aaa, bbb, ccc, X[14],  6);
201    GGG(ccc, ddd, aaa, bbb, X[ 6],  6);
202    GGG(bbb, ccc, ddd, aaa, X[ 9], 14);
203    GGG(aaa, bbb, ccc, ddd, X[11], 12);
204    GGG(ddd, aaa, bbb, ccc, X[ 8], 13);
205    GGG(ccc, ddd, aaa, bbb, X[12],  5);
206    GGG(bbb, ccc, ddd, aaa, X[ 2], 14);
207    GGG(aaa, bbb, ccc, ddd, X[10], 13);
208    GGG(ddd, aaa, bbb, ccc, X[ 0], 13);
209    GGG(ccc, ddd, aaa, bbb, X[ 4],  7);
210    GGG(bbb, ccc, ddd, aaa, X[13],  5);
211 
212    tmp = cc; cc = ccc; ccc = tmp;
213 
214    /* round 4 */
215    II(aa, bb, cc, dd, X[ 1], 11);
216    II(dd, aa, bb, cc, X[ 9], 12);
217    II(cc, dd, aa, bb, X[11], 14);
218    II(bb, cc, dd, aa, X[10], 15);
219    II(aa, bb, cc, dd, X[ 0], 14);
220    II(dd, aa, bb, cc, X[ 8], 15);
221    II(cc, dd, aa, bb, X[12],  9);
222    II(bb, cc, dd, aa, X[ 4],  8);
223    II(aa, bb, cc, dd, X[13],  9);
224    II(dd, aa, bb, cc, X[ 3], 14);
225    II(cc, dd, aa, bb, X[ 7],  5);
226    II(bb, cc, dd, aa, X[15],  6);
227    II(aa, bb, cc, dd, X[14],  8);
228    II(dd, aa, bb, cc, X[ 5],  6);
229    II(cc, dd, aa, bb, X[ 6],  5);
230    II(bb, cc, dd, aa, X[ 2], 12);
231 
232    /* parallel round 4 */
233    FFF(aaa, bbb, ccc, ddd, X[ 8], 15);
234    FFF(ddd, aaa, bbb, ccc, X[ 6],  5);
235    FFF(ccc, ddd, aaa, bbb, X[ 4],  8);
236    FFF(bbb, ccc, ddd, aaa, X[ 1], 11);
237    FFF(aaa, bbb, ccc, ddd, X[ 3], 14);
238    FFF(ddd, aaa, bbb, ccc, X[11], 14);
239    FFF(ccc, ddd, aaa, bbb, X[15],  6);
240    FFF(bbb, ccc, ddd, aaa, X[ 0], 14);
241    FFF(aaa, bbb, ccc, ddd, X[ 5],  6);
242    FFF(ddd, aaa, bbb, ccc, X[12],  9);
243    FFF(ccc, ddd, aaa, bbb, X[ 2], 12);
244    FFF(bbb, ccc, ddd, aaa, X[13],  9);
245    FFF(aaa, bbb, ccc, ddd, X[ 9], 12);
246    FFF(ddd, aaa, bbb, ccc, X[ 7],  5);
247    FFF(ccc, ddd, aaa, bbb, X[10], 15);
248    FFF(bbb, ccc, ddd, aaa, X[14],  8);
249 
250    tmp = dd; dd = ddd; ddd = tmp;
251 
252    /* combine results */
253    md->rmd256.state[0] += aa;
254    md->rmd256.state[1] += bb;
255    md->rmd256.state[2] += cc;
256    md->rmd256.state[3] += dd;
257    md->rmd256.state[4] += aaa;
258    md->rmd256.state[5] += bbb;
259    md->rmd256.state[6] += ccc;
260    md->rmd256.state[7] += ddd;
261 
262    return CRYPT_OK;
263 }
264 
265 #ifdef LTC_CLEAN_STACK
rmd256_compress(hash_state * md,const unsigned char * buf)266 static int rmd256_compress(hash_state *md, const unsigned char *buf)
267 {
268    int err;
269    err = _rmd256_compress(md, buf);
270    burn_stack(sizeof(ulong32) * 25 + sizeof(int));
271    return err;
272 }
273 #endif
274 
275 /**
276    Initialize the hash state
277    @param md   The hash state you wish to initialize
278    @return CRYPT_OK if successful
279 */
rmd256_init(hash_state * md)280 int rmd256_init(hash_state * md)
281 {
282    LTC_ARGCHK(md != NULL);
283    md->rmd256.state[0] = 0x67452301UL;
284    md->rmd256.state[1] = 0xefcdab89UL;
285    md->rmd256.state[2] = 0x98badcfeUL;
286    md->rmd256.state[3] = 0x10325476UL;
287    md->rmd256.state[4] = 0x76543210UL;
288    md->rmd256.state[5] = 0xfedcba98UL;
289    md->rmd256.state[6] = 0x89abcdefUL;
290    md->rmd256.state[7] = 0x01234567UL;
291    md->rmd256.curlen   = 0;
292    md->rmd256.length   = 0;
293    return CRYPT_OK;
294 }
295 
296 /**
297    Process a block of memory though the hash
298    @param md     The hash state
299    @param in     The data to hash
300    @param inlen  The length of the data (octets)
301    @return CRYPT_OK if successful
302 */
303 HASH_PROCESS(rmd256_process, rmd256_compress, rmd256, 64)
304 
305 /**
306    Terminate the hash to get the digest
307    @param md  The hash state
308    @param out [out] The destination of the hash (16 bytes)
309    @return CRYPT_OK if successful
310 */
rmd256_done(hash_state * md,unsigned char * out)311 int rmd256_done(hash_state * md, unsigned char *out)
312 {
313     int i;
314 
315     LTC_ARGCHK(md  != NULL);
316     LTC_ARGCHK(out != NULL);
317 
318     if (md->rmd256.curlen >= sizeof(md->rmd256.buf)) {
319        return CRYPT_INVALID_ARG;
320     }
321 
322 
323     /* increase the length of the message */
324     md->rmd256.length += md->rmd256.curlen * 8;
325 
326     /* append the '1' bit */
327     md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0x80;
328 
329     /* if the length is currently above 56 bytes we append zeros
330      * then compress.  Then we can fall back to padding zeros and length
331      * encoding like normal.
332      */
333     if (md->rmd256.curlen > 56) {
334         while (md->rmd256.curlen < 64) {
335             md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0;
336         }
337         rmd256_compress(md, md->rmd256.buf);
338         md->rmd256.curlen = 0;
339     }
340 
341     /* pad upto 56 bytes of zeroes */
342     while (md->rmd256.curlen < 56) {
343         md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0;
344     }
345 
346     /* store length */
347     STORE64L(md->rmd256.length, md->rmd256.buf+56);
348     rmd256_compress(md, md->rmd256.buf);
349 
350     /* copy output */
351     for (i = 0; i < 8; i++) {
352         STORE32L(md->rmd256.state[i], out+(4*i));
353     }
354 #ifdef LTC_CLEAN_STACK
355     zeromem(md, sizeof(hash_state));
356 #endif
357    return CRYPT_OK;
358 }
359 
360 /**
361   Self-test the hash
362   @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
363 */
rmd256_test(void)364 int rmd256_test(void)
365 {
366 #ifndef LTC_TEST
367    return CRYPT_NOP;
368 #else
369    static const struct {
370         const char *msg;
371         unsigned char hash[32];
372    } tests[] = {
373    { "",
374      { 0x02, 0xba, 0x4c, 0x4e, 0x5f, 0x8e, 0xcd, 0x18,
375        0x77, 0xfc, 0x52, 0xd6, 0x4d, 0x30, 0xe3, 0x7a,
376        0x2d, 0x97, 0x74, 0xfb, 0x1e, 0x5d, 0x02, 0x63,
377        0x80, 0xae, 0x01, 0x68, 0xe3, 0xc5, 0x52, 0x2d }
378    },
379    { "a",
380      { 0xf9, 0x33, 0x3e, 0x45, 0xd8, 0x57, 0xf5, 0xd9,
381        0x0a, 0x91, 0xba, 0xb7, 0x0a, 0x1e, 0xba, 0x0c,
382        0xfb, 0x1b, 0xe4, 0xb0, 0x78, 0x3c, 0x9a, 0xcf,
383        0xcd, 0x88, 0x3a, 0x91, 0x34, 0x69, 0x29, 0x25 }
384    },
385    { "abc",
386      { 0xaf, 0xbd, 0x6e, 0x22, 0x8b, 0x9d, 0x8c, 0xbb,
387        0xce, 0xf5, 0xca, 0x2d, 0x03, 0xe6, 0xdb, 0xa1,
388        0x0a, 0xc0, 0xbc, 0x7d, 0xcb, 0xe4, 0x68, 0x0e,
389        0x1e, 0x42, 0xd2, 0xe9, 0x75, 0x45, 0x9b, 0x65 }
390    },
391    { "message digest",
392      { 0x87, 0xe9, 0x71, 0x75, 0x9a, 0x1c, 0xe4, 0x7a,
393        0x51, 0x4d, 0x5c, 0x91, 0x4c, 0x39, 0x2c, 0x90,
394        0x18, 0xc7, 0xc4, 0x6b, 0xc1, 0x44, 0x65, 0x55,
395        0x4a, 0xfc, 0xdf, 0x54, 0xa5, 0x07, 0x0c, 0x0e }
396    },
397    { "abcdefghijklmnopqrstuvwxyz",
398      { 0x64, 0x9d, 0x30, 0x34, 0x75, 0x1e, 0xa2, 0x16,
399        0x77, 0x6b, 0xf9, 0xa1, 0x8a, 0xcc, 0x81, 0xbc,
400        0x78, 0x96, 0x11, 0x8a, 0x51, 0x97, 0x96, 0x87,
401        0x82, 0xdd, 0x1f, 0xd9, 0x7d, 0x8d, 0x51, 0x33 }
402    },
403    { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
404      { 0x57, 0x40, 0xa4, 0x08, 0xac, 0x16, 0xb7, 0x20,
405        0xb8, 0x44, 0x24, 0xae, 0x93, 0x1c, 0xbb, 0x1f,
406        0xe3, 0x63, 0xd1, 0xd0, 0xbf, 0x40, 0x17, 0xf1,
407        0xa8, 0x9f, 0x7e, 0xa6, 0xde, 0x77, 0xa0, 0xb8 }
408    }
409    };
410 
411    int i;
412    unsigned char tmp[32];
413    hash_state md;
414 
415    for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
416        rmd256_init(&md);
417        rmd256_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg));
418        rmd256_done(&md, tmp);
419        if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD256", i)) {
420           return CRYPT_FAIL_TESTVECTOR;
421        }
422    }
423    return CRYPT_OK;
424 #endif
425 }
426 
427 #endif
428 
429 /* ref:         $Format:%D$ */
430 /* git commit:  $Format:%H$ */
431 /* commit time: $Format:%ai$ */
432