libbtc
bitcoinclibrary
ripemd160.c
Go to the documentation of this file.
1 
25 #include <string.h>
26 
27 #include "ripemd160.h"
28 
29 #define ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
30 
31 #define F(x, y, z) ((x) ^ (y) ^ (z))
32 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
33 #define H(x, y, z) (((x) | ~(y)) ^ (z))
34 #define IQ(x, y, z) (((x) & (z)) | ((y) & ~(z)))
35 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
36 
37 #define FF(a, b, c, d, e, x, s) \
38  { \
39  (a) += F((b), (c), (d)) + (x); \
40  (a) = ROL((a), (s)) + (e); \
41  (c) = ROL((c), 10); \
42  }
43 #define GG(a, b, c, d, e, x, s) \
44  { \
45  (a) += G((b), (c), (d)) + (x)+0x5a827999UL; \
46  (a) = ROL((a), (s)) + (e); \
47  (c) = ROL((c), 10); \
48  }
49 #define HH(a, b, c, d, e, x, s) \
50  { \
51  (a) += H((b), (c), (d)) + (x)+0x6ed9eba1UL; \
52  (a) = ROL((a), (s)) + (e); \
53  (c) = ROL((c), 10); \
54  }
55 #define II(a, b, c, d, e, x, s) \
56  { \
57  (a) += IQ((b), (c), (d)) + (x)+0x8f1bbcdcUL; \
58  (a) = ROL((a), (s)) + (e); \
59  (c) = ROL((c), 10); \
60  }
61 #define JJ(a, b, c, d, e, x, s) \
62  { \
63  (a) += J((b), (c), (d)) + (x)+0xa953fd4eUL; \
64  (a) = ROL((a), (s)) + (e); \
65  (c) = ROL((c), 10); \
66  }
67 #define FFF(a, b, c, d, e, x, s) \
68  { \
69  (a) += F((b), (c), (d)) + (x); \
70  (a) = ROL((a), (s)) + (e); \
71  (c) = ROL((c), 10); \
72  }
73 #define GGG(a, b, c, d, e, x, s) \
74  { \
75  (a) += G((b), (c), (d)) + (x)+0x7a6d76e9UL; \
76  (a) = ROL((a), (s)) + (e); \
77  (c) = ROL((c), 10); \
78  }
79 #define HHH(a, b, c, d, e, x, s) \
80  { \
81  (a) += H((b), (c), (d)) + (x)+0x6d703ef3UL; \
82  (a) = ROL((a), (s)) + (e); \
83  (c) = ROL((c), 10); \
84  }
85 #define III(a, b, c, d, e, x, s) \
86  { \
87  (a) += IQ((b), (c), (d)) + (x)+0x5c4dd124UL; \
88  (a) = ROL((a), (s)) + (e); \
89  (c) = ROL((c), 10); \
90  }
91 #define JJJ(a, b, c, d, e, x, s) \
92  { \
93  (a) += J((b), (c), (d)) + (x)+0x50a28be6UL; \
94  (a) = ROL((a), (s)) + (e); \
95  (c) = ROL((c), 10); \
96  }
97 
98 static void compress(uint32_t* MDbuf, uint32_t* X)
99 {
100  uint32_t aa = MDbuf[0], bb = MDbuf[1], cc = MDbuf[2], dd = MDbuf[3], ee = MDbuf[4];
101  uint32_t aaa = MDbuf[0], bbb = MDbuf[1], ccc = MDbuf[2], ddd = MDbuf[3], eee = MDbuf[4];
102 
103  /* round 1 */
104  FF(aa, bb, cc, dd, ee, X[0], 11);
105  FF(ee, aa, bb, cc, dd, X[1], 14);
106  FF(dd, ee, aa, bb, cc, X[2], 15);
107  FF(cc, dd, ee, aa, bb, X[3], 12);
108  FF(bb, cc, dd, ee, aa, X[4], 5);
109  FF(aa, bb, cc, dd, ee, X[5], 8);
110  FF(ee, aa, bb, cc, dd, X[6], 7);
111  FF(dd, ee, aa, bb, cc, X[7], 9);
112  FF(cc, dd, ee, aa, bb, X[8], 11);
113  FF(bb, cc, dd, ee, aa, X[9], 13);
114  FF(aa, bb, cc, dd, ee, X[10], 14);
115  FF(ee, aa, bb, cc, dd, X[11], 15);
116  FF(dd, ee, aa, bb, cc, X[12], 6);
117  FF(cc, dd, ee, aa, bb, X[13], 7);
118  FF(bb, cc, dd, ee, aa, X[14], 9);
119  FF(aa, bb, cc, dd, ee, X[15], 8);
120 
121  /* round 2 */
122  GG(ee, aa, bb, cc, dd, X[7], 7);
123  GG(dd, ee, aa, bb, cc, X[4], 6);
124  GG(cc, dd, ee, aa, bb, X[13], 8);
125  GG(bb, cc, dd, ee, aa, X[1], 13);
126  GG(aa, bb, cc, dd, ee, X[10], 11);
127  GG(ee, aa, bb, cc, dd, X[6], 9);
128  GG(dd, ee, aa, bb, cc, X[15], 7);
129  GG(cc, dd, ee, aa, bb, X[3], 15);
130  GG(bb, cc, dd, ee, aa, X[12], 7);
131  GG(aa, bb, cc, dd, ee, X[0], 12);
132  GG(ee, aa, bb, cc, dd, X[9], 15);
133  GG(dd, ee, aa, bb, cc, X[5], 9);
134  GG(cc, dd, ee, aa, bb, X[2], 11);
135  GG(bb, cc, dd, ee, aa, X[14], 7);
136  GG(aa, bb, cc, dd, ee, X[11], 13);
137  GG(ee, aa, bb, cc, dd, X[8], 12);
138 
139  /* round 3 */
140  HH(dd, ee, aa, bb, cc, X[3], 11);
141  HH(cc, dd, ee, aa, bb, X[10], 13);
142  HH(bb, cc, dd, ee, aa, X[14], 6);
143  HH(aa, bb, cc, dd, ee, X[4], 7);
144  HH(ee, aa, bb, cc, dd, X[9], 14);
145  HH(dd, ee, aa, bb, cc, X[15], 9);
146  HH(cc, dd, ee, aa, bb, X[8], 13);
147  HH(bb, cc, dd, ee, aa, X[1], 15);
148  HH(aa, bb, cc, dd, ee, X[2], 14);
149  HH(ee, aa, bb, cc, dd, X[7], 8);
150  HH(dd, ee, aa, bb, cc, X[0], 13);
151  HH(cc, dd, ee, aa, bb, X[6], 6);
152  HH(bb, cc, dd, ee, aa, X[13], 5);
153  HH(aa, bb, cc, dd, ee, X[11], 12);
154  HH(ee, aa, bb, cc, dd, X[5], 7);
155  HH(dd, ee, aa, bb, cc, X[12], 5);
156 
157  /* round 4 */
158  II(cc, dd, ee, aa, bb, X[1], 11);
159  II(bb, cc, dd, ee, aa, X[9], 12);
160  II(aa, bb, cc, dd, ee, X[11], 14);
161  II(ee, aa, bb, cc, dd, X[10], 15);
162  II(dd, ee, aa, bb, cc, X[0], 14);
163  II(cc, dd, ee, aa, bb, X[8], 15);
164  II(bb, cc, dd, ee, aa, X[12], 9);
165  II(aa, bb, cc, dd, ee, X[4], 8);
166  II(ee, aa, bb, cc, dd, X[13], 9);
167  II(dd, ee, aa, bb, cc, X[3], 14);
168  II(cc, dd, ee, aa, bb, X[7], 5);
169  II(bb, cc, dd, ee, aa, X[15], 6);
170  II(aa, bb, cc, dd, ee, X[14], 8);
171  II(ee, aa, bb, cc, dd, X[5], 6);
172  II(dd, ee, aa, bb, cc, X[6], 5);
173  II(cc, dd, ee, aa, bb, X[2], 12);
174 
175  /* round 5 */
176  JJ(bb, cc, dd, ee, aa, X[4], 9);
177  JJ(aa, bb, cc, dd, ee, X[0], 15);
178  JJ(ee, aa, bb, cc, dd, X[5], 5);
179  JJ(dd, ee, aa, bb, cc, X[9], 11);
180  JJ(cc, dd, ee, aa, bb, X[7], 6);
181  JJ(bb, cc, dd, ee, aa, X[12], 8);
182  JJ(aa, bb, cc, dd, ee, X[2], 13);
183  JJ(ee, aa, bb, cc, dd, X[10], 12);
184  JJ(dd, ee, aa, bb, cc, X[14], 5);
185  JJ(cc, dd, ee, aa, bb, X[1], 12);
186  JJ(bb, cc, dd, ee, aa, X[3], 13);
187  JJ(aa, bb, cc, dd, ee, X[8], 14);
188  JJ(ee, aa, bb, cc, dd, X[11], 11);
189  JJ(dd, ee, aa, bb, cc, X[6], 8);
190  JJ(cc, dd, ee, aa, bb, X[15], 5);
191  JJ(bb, cc, dd, ee, aa, X[13], 6);
192 
193  /* parallel round 1 */
194  JJJ(aaa, bbb, ccc, ddd, eee, X[5], 8);
195  JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
196  JJJ(ddd, eee, aaa, bbb, ccc, X[7], 9);
197  JJJ(ccc, ddd, eee, aaa, bbb, X[0], 11);
198  JJJ(bbb, ccc, ddd, eee, aaa, X[9], 13);
199  JJJ(aaa, bbb, ccc, ddd, eee, X[2], 15);
200  JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
201  JJJ(ddd, eee, aaa, bbb, ccc, X[4], 5);
202  JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
203  JJJ(bbb, ccc, ddd, eee, aaa, X[6], 7);
204  JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
205  JJJ(eee, aaa, bbb, ccc, ddd, X[8], 11);
206  JJJ(ddd, eee, aaa, bbb, ccc, X[1], 14);
207  JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
208  JJJ(bbb, ccc, ddd, eee, aaa, X[3], 12);
209  JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
210 
211  /* parallel round 2 */
212  III(eee, aaa, bbb, ccc, ddd, X[6], 9);
213  III(ddd, eee, aaa, bbb, ccc, X[11], 13);
214  III(ccc, ddd, eee, aaa, bbb, X[3], 15);
215  III(bbb, ccc, ddd, eee, aaa, X[7], 7);
216  III(aaa, bbb, ccc, ddd, eee, X[0], 12);
217  III(eee, aaa, bbb, ccc, ddd, X[13], 8);
218  III(ddd, eee, aaa, bbb, ccc, X[5], 9);
219  III(ccc, ddd, eee, aaa, bbb, X[10], 11);
220  III(bbb, ccc, ddd, eee, aaa, X[14], 7);
221  III(aaa, bbb, ccc, ddd, eee, X[15], 7);
222  III(eee, aaa, bbb, ccc, ddd, X[8], 12);
223  III(ddd, eee, aaa, bbb, ccc, X[12], 7);
224  III(ccc, ddd, eee, aaa, bbb, X[4], 6);
225  III(bbb, ccc, ddd, eee, aaa, X[9], 15);
226  III(aaa, bbb, ccc, ddd, eee, X[1], 13);
227  III(eee, aaa, bbb, ccc, ddd, X[2], 11);
228 
229  /* parallel round 3 */
230  HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
231  HHH(ccc, ddd, eee, aaa, bbb, X[5], 7);
232  HHH(bbb, ccc, ddd, eee, aaa, X[1], 15);
233  HHH(aaa, bbb, ccc, ddd, eee, X[3], 11);
234  HHH(eee, aaa, bbb, ccc, ddd, X[7], 8);
235  HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
236  HHH(ccc, ddd, eee, aaa, bbb, X[6], 6);
237  HHH(bbb, ccc, ddd, eee, aaa, X[9], 14);
238  HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
239  HHH(eee, aaa, bbb, ccc, ddd, X[8], 13);
240  HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
241  HHH(ccc, ddd, eee, aaa, bbb, X[2], 14);
242  HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
243  HHH(aaa, bbb, ccc, ddd, eee, X[0], 13);
244  HHH(eee, aaa, bbb, ccc, ddd, X[4], 7);
245  HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
246 
247  /* parallel round 4 */
248  GGG(ccc, ddd, eee, aaa, bbb, X[8], 15);
249  GGG(bbb, ccc, ddd, eee, aaa, X[6], 5);
250  GGG(aaa, bbb, ccc, ddd, eee, X[4], 8);
251  GGG(eee, aaa, bbb, ccc, ddd, X[1], 11);
252  GGG(ddd, eee, aaa, bbb, ccc, X[3], 14);
253  GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
254  GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
255  GGG(aaa, bbb, ccc, ddd, eee, X[0], 14);
256  GGG(eee, aaa, bbb, ccc, ddd, X[5], 6);
257  GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
258  GGG(ccc, ddd, eee, aaa, bbb, X[2], 12);
259  GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
260  GGG(aaa, bbb, ccc, ddd, eee, X[9], 12);
261  GGG(eee, aaa, bbb, ccc, ddd, X[7], 5);
262  GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
263  GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
264 
265  /* parallel round 5 */
266  FFF(bbb, ccc, ddd, eee, aaa, X[12], 8);
267  FFF(aaa, bbb, ccc, ddd, eee, X[15], 5);
268  FFF(eee, aaa, bbb, ccc, ddd, X[10], 12);
269  FFF(ddd, eee, aaa, bbb, ccc, X[4], 9);
270  FFF(ccc, ddd, eee, aaa, bbb, X[1], 12);
271  FFF(bbb, ccc, ddd, eee, aaa, X[5], 5);
272  FFF(aaa, bbb, ccc, ddd, eee, X[8], 14);
273  FFF(eee, aaa, bbb, ccc, ddd, X[7], 6);
274  FFF(ddd, eee, aaa, bbb, ccc, X[6], 8);
275  FFF(ccc, ddd, eee, aaa, bbb, X[2], 13);
276  FFF(bbb, ccc, ddd, eee, aaa, X[13], 6);
277  FFF(aaa, bbb, ccc, ddd, eee, X[14], 5);
278  FFF(eee, aaa, bbb, ccc, ddd, X[0], 15);
279  FFF(ddd, eee, aaa, bbb, ccc, X[3], 13);
280  FFF(ccc, ddd, eee, aaa, bbb, X[9], 11);
281  FFF(bbb, ccc, ddd, eee, aaa, X[11], 11);
282 
283  /* combine results */
284  ddd += cc + MDbuf[1];
285  MDbuf[1] = MDbuf[2] + dd + eee;
286  MDbuf[2] = MDbuf[3] + ee + aaa;
287  MDbuf[3] = MDbuf[4] + aa + bbb;
288  MDbuf[4] = MDbuf[0] + bb + ccc;
289  MDbuf[0] = ddd;
290 }
291 
292 void ripemd160(const uint8_t* msg, uint32_t msg_len, uint8_t* hash)
293 {
294  uint32_t i;
295  int j;
296  uint32_t digest[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0UL};
297 
298  for (i = 0; i < (msg_len >> 6); ++i) {
299  uint32_t chunk[16];
300 
301  for (j = 0; j < 16; ++j) {
302  chunk[j] = (uint32_t)(*(msg++));
303  chunk[j] |= (uint32_t)(*(msg++)) << 8;
304  chunk[j] |= (uint32_t)(*(msg++)) << 16;
305  chunk[j] |= (uint32_t)(*(msg++)) << 24;
306  }
307 
308  compress(digest, chunk);
309  }
310 
311  // Last chunk
312  {
313  uint32_t chunk[16] = {0};
314 
315  for (i = 0; i < (msg_len & 63); ++i) {
316  chunk[i >> 2] ^= (uint32_t)*msg++ << ((i & 3) << 3);
317  }
318 
319  chunk[(msg_len >> 2) & 15] ^= (uint32_t)1 << (8 * (msg_len & 3) + 7);
320 
321  if ((msg_len & 63) > 55) {
322  compress(digest, chunk);
323  memset(chunk, 0, 64);
324  }
325 
326  chunk[14] = msg_len << 3;
327  chunk[15] = (msg_len >> 29);
328  compress(digest, chunk);
329  }
330 
331  for (i = 0; i < 5; ++i) {
332  *(hash++) = digest[i];
333  *(hash++) = digest[i] >> 8;
334  *(hash++) = digest[i] >> 16;
335  *(hash++) = digest[i] >> 24;
336  }
337 }
#define GGG(a, b, c, d, e, x, s)
Definition: ripemd160.c:73
#define FFF(a, b, c, d, e, x, s)
Definition: ripemd160.c:67
#define GG(a, b, c, d, e, x, s)
Definition: ripemd160.c:43
#define FF(a, b, c, d, e, x, s)
Definition: ripemd160.c:37
#define II(a, b, c, d, e, x, s)
Definition: ripemd160.c:55
#define HHH(a, b, c, d, e, x, s)
Definition: ripemd160.c:79
#define JJJ(a, b, c, d, e, x, s)
Definition: ripemd160.c:91
#define JJ(a, b, c, d, e, x, s)
Definition: ripemd160.c:61
void ripemd160(const uint8_t *msg, uint32_t msg_len, uint8_t *hash)
Definition: ripemd160.c:292
#define III(a, b, c, d, e, x, s)
Definition: ripemd160.c:85
#define HH(a, b, c, d, e, x, s)
Definition: ripemd160.c:49
static void compress(uint32_t *MDbuf, uint32_t *X)
Definition: ripemd160.c:98