1 /* MIT (BSD) license - see LICENSE file for details */
2 /* RIPEMD core code translated from the Bitcoin project's C++:
4 * src/crypto/ripemd160.cpp commit f914f1a746d7f91951c1da262a4a749dd3ebfa71
5 * Copyright (c) 2014 The Bitcoin Core developers
6 * Distributed under the MIT software license, see the accompanying
7 * file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include <ccan/crypto/ripemd160/ripemd160.h>
10 #include <ccan/endian/endian.h>
11 #include <ccan/compiler/compiler.h>
16 static void invalidate_ripemd160(struct ripemd160_ctx *ctx)
18 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
25 static void check_ripemd160(struct ripemd160_ctx *ctx UNUSED)
27 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
28 assert(ctx->c.num != -1U);
30 assert(ctx->bytes != -1ULL);
34 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
35 void ripemd160_init(struct ripemd160_ctx *ctx)
37 RIPEMD160_Init(&ctx->c);
40 void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
43 RIPEMD160_Update(&ctx->c, p, size);
46 void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
48 RIPEMD160_Final(res->u.u8, &ctx->c);
49 invalidate_ripemd160(ctx);
52 static uint32_t inline f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
53 static uint32_t inline f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); }
54 static uint32_t inline f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; }
55 static uint32_t inline f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); }
56 static uint32_t inline f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); }
58 /** Initialize RIPEMD-160 state. */
59 static void inline Initialize(uint32_t* s)
68 static uint32_t inline rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); }
70 static void inline Round(uint32_t *a, uint32_t b UNUSED, uint32_t *c, uint32_t d UNUSED, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)
72 *a = rol(*a + f + x + k, r) + e;
76 static void inline R11(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); }
77 static void inline R21(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x5A827999ul, r); }
78 static void inline R31(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6ED9EBA1ul, r); }
79 static void inline R41(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x8F1BBCDCul, r); }
80 static void inline R51(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0xA953FD4Eul, r); }
82 static void inline R12(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0x50A28BE6ul, r); }
83 static void inline R22(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x5C4DD124ul, r); }
84 static void inline R32(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6D703EF3ul, r); }
85 static void inline R42(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x7A6D76E9ul, r); }
86 static void inline R52(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); }
88 /** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */
89 static void Transform(uint32_t *s, const uint32_t *chunk)
91 uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
92 uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
93 uint32_t w0 = le32_to_cpu(chunk[0]), w1 = le32_to_cpu(chunk[1]), w2 = le32_to_cpu(chunk[2]), w3 = le32_to_cpu(chunk[3]);
94 uint32_t w4 = le32_to_cpu(chunk[4]), w5 = le32_to_cpu(chunk[5]), w6 = le32_to_cpu(chunk[6]), w7 = le32_to_cpu(chunk[7]);
95 uint32_t w8 = le32_to_cpu(chunk[8]), w9 = le32_to_cpu(chunk[9]), w10 = le32_to_cpu(chunk[10]), w11 = le32_to_cpu(chunk[11]);
96 uint32_t w12 = le32_to_cpu(chunk[12]), w13 = le32_to_cpu(chunk[13]), w14 = le32_to_cpu(chunk[14]), w15 = le32_to_cpu(chunk[15]);
98 R11(&a1, b1, &c1, d1, e1, w0, 11);
99 R12(&a2, b2, &c2, d2, e2, w5, 8);
100 R11(&e1, a1, &b1, c1, d1, w1, 14);
101 R12(&e2, a2, &b2, c2, d2, w14, 9);
102 R11(&d1, e1, &a1, b1, c1, w2, 15);
103 R12(&d2, e2, &a2, b2, c2, w7, 9);
104 R11(&c1, d1, &e1, a1, b1, w3, 12);
105 R12(&c2, d2, &e2, a2, b2, w0, 11);
106 R11(&b1, c1, &d1, e1, a1, w4, 5);
107 R12(&b2, c2, &d2, e2, a2, w9, 13);
108 R11(&a1, b1, &c1, d1, e1, w5, 8);
109 R12(&a2, b2, &c2, d2, e2, w2, 15);
110 R11(&e1, a1, &b1, c1, d1, w6, 7);
111 R12(&e2, a2, &b2, c2, d2, w11, 15);
112 R11(&d1, e1, &a1, b1, c1, w7, 9);
113 R12(&d2, e2, &a2, b2, c2, w4, 5);
114 R11(&c1, d1, &e1, a1, b1, w8, 11);
115 R12(&c2, d2, &e2, a2, b2, w13, 7);
116 R11(&b1, c1, &d1, e1, a1, w9, 13);
117 R12(&b2, c2, &d2, e2, a2, w6, 7);
118 R11(&a1, b1, &c1, d1, e1, w10, 14);
119 R12(&a2, b2, &c2, d2, e2, w15, 8);
120 R11(&e1, a1, &b1, c1, d1, w11, 15);
121 R12(&e2, a2, &b2, c2, d2, w8, 11);
122 R11(&d1, e1, &a1, b1, c1, w12, 6);
123 R12(&d2, e2, &a2, b2, c2, w1, 14);
124 R11(&c1, d1, &e1, a1, b1, w13, 7);
125 R12(&c2, d2, &e2, a2, b2, w10, 14);
126 R11(&b1, c1, &d1, e1, a1, w14, 9);
127 R12(&b2, c2, &d2, e2, a2, w3, 12);
128 R11(&a1, b1, &c1, d1, e1, w15, 8);
129 R12(&a2, b2, &c2, d2, e2, w12, 6);
131 R21(&e1, a1, &b1, c1, d1, w7, 7);
132 R22(&e2, a2, &b2, c2, d2, w6, 9);
133 R21(&d1, e1, &a1, b1, c1, w4, 6);
134 R22(&d2, e2, &a2, b2, c2, w11, 13);
135 R21(&c1, d1, &e1, a1, b1, w13, 8);
136 R22(&c2, d2, &e2, a2, b2, w3, 15);
137 R21(&b1, c1, &d1, e1, a1, w1, 13);
138 R22(&b2, c2, &d2, e2, a2, w7, 7);
139 R21(&a1, b1, &c1, d1, e1, w10, 11);
140 R22(&a2, b2, &c2, d2, e2, w0, 12);
141 R21(&e1, a1, &b1, c1, d1, w6, 9);
142 R22(&e2, a2, &b2, c2, d2, w13, 8);
143 R21(&d1, e1, &a1, b1, c1, w15, 7);
144 R22(&d2, e2, &a2, b2, c2, w5, 9);
145 R21(&c1, d1, &e1, a1, b1, w3, 15);
146 R22(&c2, d2, &e2, a2, b2, w10, 11);
147 R21(&b1, c1, &d1, e1, a1, w12, 7);
148 R22(&b2, c2, &d2, e2, a2, w14, 7);
149 R21(&a1, b1, &c1, d1, e1, w0, 12);
150 R22(&a2, b2, &c2, d2, e2, w15, 7);
151 R21(&e1, a1, &b1, c1, d1, w9, 15);
152 R22(&e2, a2, &b2, c2, d2, w8, 12);
153 R21(&d1, e1, &a1, b1, c1, w5, 9);
154 R22(&d2, e2, &a2, b2, c2, w12, 7);
155 R21(&c1, d1, &e1, a1, b1, w2, 11);
156 R22(&c2, d2, &e2, a2, b2, w4, 6);
157 R21(&b1, c1, &d1, e1, a1, w14, 7);
158 R22(&b2, c2, &d2, e2, a2, w9, 15);
159 R21(&a1, b1, &c1, d1, e1, w11, 13);
160 R22(&a2, b2, &c2, d2, e2, w1, 13);
161 R21(&e1, a1, &b1, c1, d1, w8, 12);
162 R22(&e2, a2, &b2, c2, d2, w2, 11);
164 R31(&d1, e1, &a1, b1, c1, w3, 11);
165 R32(&d2, e2, &a2, b2, c2, w15, 9);
166 R31(&c1, d1, &e1, a1, b1, w10, 13);
167 R32(&c2, d2, &e2, a2, b2, w5, 7);
168 R31(&b1, c1, &d1, e1, a1, w14, 6);
169 R32(&b2, c2, &d2, e2, a2, w1, 15);
170 R31(&a1, b1, &c1, d1, e1, w4, 7);
171 R32(&a2, b2, &c2, d2, e2, w3, 11);
172 R31(&e1, a1, &b1, c1, d1, w9, 14);
173 R32(&e2, a2, &b2, c2, d2, w7, 8);
174 R31(&d1, e1, &a1, b1, c1, w15, 9);
175 R32(&d2, e2, &a2, b2, c2, w14, 6);
176 R31(&c1, d1, &e1, a1, b1, w8, 13);
177 R32(&c2, d2, &e2, a2, b2, w6, 6);
178 R31(&b1, c1, &d1, e1, a1, w1, 15);
179 R32(&b2, c2, &d2, e2, a2, w9, 14);
180 R31(&a1, b1, &c1, d1, e1, w2, 14);
181 R32(&a2, b2, &c2, d2, e2, w11, 12);
182 R31(&e1, a1, &b1, c1, d1, w7, 8);
183 R32(&e2, a2, &b2, c2, d2, w8, 13);
184 R31(&d1, e1, &a1, b1, c1, w0, 13);
185 R32(&d2, e2, &a2, b2, c2, w12, 5);
186 R31(&c1, d1, &e1, a1, b1, w6, 6);
187 R32(&c2, d2, &e2, a2, b2, w2, 14);
188 R31(&b1, c1, &d1, e1, a1, w13, 5);
189 R32(&b2, c2, &d2, e2, a2, w10, 13);
190 R31(&a1, b1, &c1, d1, e1, w11, 12);
191 R32(&a2, b2, &c2, d2, e2, w0, 13);
192 R31(&e1, a1, &b1, c1, d1, w5, 7);
193 R32(&e2, a2, &b2, c2, d2, w4, 7);
194 R31(&d1, e1, &a1, b1, c1, w12, 5);
195 R32(&d2, e2, &a2, b2, c2, w13, 5);
197 R41(&c1, d1, &e1, a1, b1, w1, 11);
198 R42(&c2, d2, &e2, a2, b2, w8, 15);
199 R41(&b1, c1, &d1, e1, a1, w9, 12);
200 R42(&b2, c2, &d2, e2, a2, w6, 5);
201 R41(&a1, b1, &c1, d1, e1, w11, 14);
202 R42(&a2, b2, &c2, d2, e2, w4, 8);
203 R41(&e1, a1, &b1, c1, d1, w10, 15);
204 R42(&e2, a2, &b2, c2, d2, w1, 11);
205 R41(&d1, e1, &a1, b1, c1, w0, 14);
206 R42(&d2, e2, &a2, b2, c2, w3, 14);
207 R41(&c1, d1, &e1, a1, b1, w8, 15);
208 R42(&c2, d2, &e2, a2, b2, w11, 14);
209 R41(&b1, c1, &d1, e1, a1, w12, 9);
210 R42(&b2, c2, &d2, e2, a2, w15, 6);
211 R41(&a1, b1, &c1, d1, e1, w4, 8);
212 R42(&a2, b2, &c2, d2, e2, w0, 14);
213 R41(&e1, a1, &b1, c1, d1, w13, 9);
214 R42(&e2, a2, &b2, c2, d2, w5, 6);
215 R41(&d1, e1, &a1, b1, c1, w3, 14);
216 R42(&d2, e2, &a2, b2, c2, w12, 9);
217 R41(&c1, d1, &e1, a1, b1, w7, 5);
218 R42(&c2, d2, &e2, a2, b2, w2, 12);
219 R41(&b1, c1, &d1, e1, a1, w15, 6);
220 R42(&b2, c2, &d2, e2, a2, w13, 9);
221 R41(&a1, b1, &c1, d1, e1, w14, 8);
222 R42(&a2, b2, &c2, d2, e2, w9, 12);
223 R41(&e1, a1, &b1, c1, d1, w5, 6);
224 R42(&e2, a2, &b2, c2, d2, w7, 5);
225 R41(&d1, e1, &a1, b1, c1, w6, 5);
226 R42(&d2, e2, &a2, b2, c2, w10, 15);
227 R41(&c1, d1, &e1, a1, b1, w2, 12);
228 R42(&c2, d2, &e2, a2, b2, w14, 8);
230 R51(&b1, c1, &d1, e1, a1, w4, 9);
231 R52(&b2, c2, &d2, e2, a2, w12, 8);
232 R51(&a1, b1, &c1, d1, e1, w0, 15);
233 R52(&a2, b2, &c2, d2, e2, w15, 5);
234 R51(&e1, a1, &b1, c1, d1, w5, 5);
235 R52(&e2, a2, &b2, c2, d2, w10, 12);
236 R51(&d1, e1, &a1, b1, c1, w9, 11);
237 R52(&d2, e2, &a2, b2, c2, w4, 9);
238 R51(&c1, d1, &e1, a1, b1, w7, 6);
239 R52(&c2, d2, &e2, a2, b2, w1, 12);
240 R51(&b1, c1, &d1, e1, a1, w12, 8);
241 R52(&b2, c2, &d2, e2, a2, w5, 5);
242 R51(&a1, b1, &c1, d1, e1, w2, 13);
243 R52(&a2, b2, &c2, d2, e2, w8, 14);
244 R51(&e1, a1, &b1, c1, d1, w10, 12);
245 R52(&e2, a2, &b2, c2, d2, w7, 6);
246 R51(&d1, e1, &a1, b1, c1, w14, 5);
247 R52(&d2, e2, &a2, b2, c2, w6, 8);
248 R51(&c1, d1, &e1, a1, b1, w1, 12);
249 R52(&c2, d2, &e2, a2, b2, w2, 13);
250 R51(&b1, c1, &d1, e1, a1, w3, 13);
251 R52(&b2, c2, &d2, e2, a2, w13, 6);
252 R51(&a1, b1, &c1, d1, e1, w8, 14);
253 R52(&a2, b2, &c2, d2, e2, w14, 5);
254 R51(&e1, a1, &b1, c1, d1, w11, 11);
255 R52(&e2, a2, &b2, c2, d2, w0, 15);
256 R51(&d1, e1, &a1, b1, c1, w6, 8);
257 R52(&d2, e2, &a2, b2, c2, w3, 13);
258 R51(&c1, d1, &e1, a1, b1, w15, 5);
259 R52(&c2, d2, &e2, a2, b2, w9, 11);
260 R51(&b1, c1, &d1, e1, a1, w13, 6);
261 R52(&b2, c2, &d2, e2, a2, w11, 11);
264 s[0] = s[1] + c1 + d2;
265 s[1] = s[2] + d1 + e2;
266 s[2] = s[3] + e1 + a2;
267 s[3] = s[4] + a1 + b2;
271 static bool alignment_ok(const void *p UNUSED, size_t n UNUSED)
273 #if HAVE_UNALIGNED_ACCESS
276 return ((size_t)p % n == 0);
280 static void add(struct ripemd160_ctx *ctx, const void *p, size_t len)
282 const unsigned char *data = p;
283 size_t bufsize = ctx->bytes % 64;
285 if (bufsize + len >= 64) {
286 // Fill the buffer, and process it.
287 memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
288 ctx->bytes += 64 - bufsize;
289 data += 64 - bufsize;
291 Transform(ctx->s, ctx->buf.u32);
296 // Process full chunks directly from the source.
297 if (alignment_ok(data, sizeof(uint32_t)))
298 Transform(ctx->s, (const uint32_t *)data);
300 memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
301 Transform(ctx->s, ctx->buf.u32);
309 // Fill the buffer with what remains.
310 memcpy(ctx->buf.u8 + bufsize, data, len);
315 void ripemd160_init(struct ripemd160_ctx *ctx)
317 struct ripemd160_ctx init = RIPEMD160_INIT;
321 void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
323 check_ripemd160(ctx);
327 void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
329 static const unsigned char pad[64] = {0x80};
333 sizedesc = cpu_to_le64(ctx->bytes << 3);
334 /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
335 add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
336 /* Add number of bits of data (big endian) */
337 add(ctx, &sizedesc, 8);
338 for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
339 res->u.u32[i] = cpu_to_le32(ctx->s[i]);
340 invalidate_ripemd160(ctx);
344 void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size)
346 struct ripemd160_ctx ctx;
348 ripemd160_init(&ctx);
349 ripemd160_update(&ctx, p, size);
350 ripemd160_done(&ctx, ripemd);
353 void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v)
355 ripemd160_update(ctx, &v, sizeof(v));
358 void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v)
360 ripemd160_update(ctx, &v, sizeof(v));
363 void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v)
365 ripemd160_update(ctx, &v, sizeof(v));
368 void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v)
370 ripemd160_update(ctx, &v, sizeof(v));
373 /* Add as little-endian */
374 void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v)
376 leint16_t lev = cpu_to_le16(v);
377 ripemd160_update(ctx, &lev, sizeof(lev));
380 void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v)
382 leint32_t lev = cpu_to_le32(v);
383 ripemd160_update(ctx, &lev, sizeof(lev));
386 void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v)
388 leint64_t lev = cpu_to_le64(v);
389 ripemd160_update(ctx, &lev, sizeof(lev));
392 /* Add as big-endian */
393 void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v)
395 beint16_t bev = cpu_to_be16(v);
396 ripemd160_update(ctx, &bev, sizeof(bev));
399 void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v)
401 beint32_t bev = cpu_to_be32(v);
402 ripemd160_update(ctx, &bev, sizeof(bev));
405 void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v)
407 beint64_t bev = cpu_to_be64(v);
408 ripemd160_update(ctx, &bev, sizeof(bev));