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]);
99 R11(&a1, b1, &c1, d1, e1, w0, 11);
100 R12(&a2, b2, &c2, d2, e2, w5, 8);
101 R11(&e1, a1, &b1, c1, d1, w1, 14);
102 R12(&e2, a2, &b2, c2, d2, w14, 9);
103 R11(&d1, e1, &a1, b1, c1, w2, 15);
104 R12(&d2, e2, &a2, b2, c2, w7, 9);
105 R11(&c1, d1, &e1, a1, b1, w3, 12);
106 R12(&c2, d2, &e2, a2, b2, w0, 11);
107 R11(&b1, c1, &d1, e1, a1, w4, 5);
108 R12(&b2, c2, &d2, e2, a2, w9, 13);
109 R11(&a1, b1, &c1, d1, e1, w5, 8);
110 R12(&a2, b2, &c2, d2, e2, w2, 15);
111 R11(&e1, a1, &b1, c1, d1, w6, 7);
112 R12(&e2, a2, &b2, c2, d2, w11, 15);
113 R11(&d1, e1, &a1, b1, c1, w7, 9);
114 R12(&d2, e2, &a2, b2, c2, w4, 5);
115 R11(&c1, d1, &e1, a1, b1, w8, 11);
116 R12(&c2, d2, &e2, a2, b2, w13, 7);
117 R11(&b1, c1, &d1, e1, a1, w9, 13);
118 R12(&b2, c2, &d2, e2, a2, w6, 7);
119 R11(&a1, b1, &c1, d1, e1, w10, 14);
120 R12(&a2, b2, &c2, d2, e2, w15, 8);
121 R11(&e1, a1, &b1, c1, d1, w11, 15);
122 R12(&e2, a2, &b2, c2, d2, w8, 11);
123 R11(&d1, e1, &a1, b1, c1, w12, 6);
124 R12(&d2, e2, &a2, b2, c2, w1, 14);
125 R11(&c1, d1, &e1, a1, b1, w13, 7);
126 R12(&c2, d2, &e2, a2, b2, w10, 14);
127 R11(&b1, c1, &d1, e1, a1, w14, 9);
128 R12(&b2, c2, &d2, e2, a2, w3, 12);
129 R11(&a1, b1, &c1, d1, e1, w15, 8);
130 R12(&a2, b2, &c2, d2, e2, w12, 6);
132 R21(&e1, a1, &b1, c1, d1, w7, 7);
133 R22(&e2, a2, &b2, c2, d2, w6, 9);
134 R21(&d1, e1, &a1, b1, c1, w4, 6);
135 R22(&d2, e2, &a2, b2, c2, w11, 13);
136 R21(&c1, d1, &e1, a1, b1, w13, 8);
137 R22(&c2, d2, &e2, a2, b2, w3, 15);
138 R21(&b1, c1, &d1, e1, a1, w1, 13);
139 R22(&b2, c2, &d2, e2, a2, w7, 7);
140 R21(&a1, b1, &c1, d1, e1, w10, 11);
141 R22(&a2, b2, &c2, d2, e2, w0, 12);
142 R21(&e1, a1, &b1, c1, d1, w6, 9);
143 R22(&e2, a2, &b2, c2, d2, w13, 8);
144 R21(&d1, e1, &a1, b1, c1, w15, 7);
145 R22(&d2, e2, &a2, b2, c2, w5, 9);
146 R21(&c1, d1, &e1, a1, b1, w3, 15);
147 R22(&c2, d2, &e2, a2, b2, w10, 11);
148 R21(&b1, c1, &d1, e1, a1, w12, 7);
149 R22(&b2, c2, &d2, e2, a2, w14, 7);
150 R21(&a1, b1, &c1, d1, e1, w0, 12);
151 R22(&a2, b2, &c2, d2, e2, w15, 7);
152 R21(&e1, a1, &b1, c1, d1, w9, 15);
153 R22(&e2, a2, &b2, c2, d2, w8, 12);
154 R21(&d1, e1, &a1, b1, c1, w5, 9);
155 R22(&d2, e2, &a2, b2, c2, w12, 7);
156 R21(&c1, d1, &e1, a1, b1, w2, 11);
157 R22(&c2, d2, &e2, a2, b2, w4, 6);
158 R21(&b1, c1, &d1, e1, a1, w14, 7);
159 R22(&b2, c2, &d2, e2, a2, w9, 15);
160 R21(&a1, b1, &c1, d1, e1, w11, 13);
161 R22(&a2, b2, &c2, d2, e2, w1, 13);
162 R21(&e1, a1, &b1, c1, d1, w8, 12);
163 R22(&e2, a2, &b2, c2, d2, w2, 11);
165 R31(&d1, e1, &a1, b1, c1, w3, 11);
166 R32(&d2, e2, &a2, b2, c2, w15, 9);
167 R31(&c1, d1, &e1, a1, b1, w10, 13);
168 R32(&c2, d2, &e2, a2, b2, w5, 7);
169 R31(&b1, c1, &d1, e1, a1, w14, 6);
170 R32(&b2, c2, &d2, e2, a2, w1, 15);
171 R31(&a1, b1, &c1, d1, e1, w4, 7);
172 R32(&a2, b2, &c2, d2, e2, w3, 11);
173 R31(&e1, a1, &b1, c1, d1, w9, 14);
174 R32(&e2, a2, &b2, c2, d2, w7, 8);
175 R31(&d1, e1, &a1, b1, c1, w15, 9);
176 R32(&d2, e2, &a2, b2, c2, w14, 6);
177 R31(&c1, d1, &e1, a1, b1, w8, 13);
178 R32(&c2, d2, &e2, a2, b2, w6, 6);
179 R31(&b1, c1, &d1, e1, a1, w1, 15);
180 R32(&b2, c2, &d2, e2, a2, w9, 14);
181 R31(&a1, b1, &c1, d1, e1, w2, 14);
182 R32(&a2, b2, &c2, d2, e2, w11, 12);
183 R31(&e1, a1, &b1, c1, d1, w7, 8);
184 R32(&e2, a2, &b2, c2, d2, w8, 13);
185 R31(&d1, e1, &a1, b1, c1, w0, 13);
186 R32(&d2, e2, &a2, b2, c2, w12, 5);
187 R31(&c1, d1, &e1, a1, b1, w6, 6);
188 R32(&c2, d2, &e2, a2, b2, w2, 14);
189 R31(&b1, c1, &d1, e1, a1, w13, 5);
190 R32(&b2, c2, &d2, e2, a2, w10, 13);
191 R31(&a1, b1, &c1, d1, e1, w11, 12);
192 R32(&a2, b2, &c2, d2, e2, w0, 13);
193 R31(&e1, a1, &b1, c1, d1, w5, 7);
194 R32(&e2, a2, &b2, c2, d2, w4, 7);
195 R31(&d1, e1, &a1, b1, c1, w12, 5);
196 R32(&d2, e2, &a2, b2, c2, w13, 5);
198 R41(&c1, d1, &e1, a1, b1, w1, 11);
199 R42(&c2, d2, &e2, a2, b2, w8, 15);
200 R41(&b1, c1, &d1, e1, a1, w9, 12);
201 R42(&b2, c2, &d2, e2, a2, w6, 5);
202 R41(&a1, b1, &c1, d1, e1, w11, 14);
203 R42(&a2, b2, &c2, d2, e2, w4, 8);
204 R41(&e1, a1, &b1, c1, d1, w10, 15);
205 R42(&e2, a2, &b2, c2, d2, w1, 11);
206 R41(&d1, e1, &a1, b1, c1, w0, 14);
207 R42(&d2, e2, &a2, b2, c2, w3, 14);
208 R41(&c1, d1, &e1, a1, b1, w8, 15);
209 R42(&c2, d2, &e2, a2, b2, w11, 14);
210 R41(&b1, c1, &d1, e1, a1, w12, 9);
211 R42(&b2, c2, &d2, e2, a2, w15, 6);
212 R41(&a1, b1, &c1, d1, e1, w4, 8);
213 R42(&a2, b2, &c2, d2, e2, w0, 14);
214 R41(&e1, a1, &b1, c1, d1, w13, 9);
215 R42(&e2, a2, &b2, c2, d2, w5, 6);
216 R41(&d1, e1, &a1, b1, c1, w3, 14);
217 R42(&d2, e2, &a2, b2, c2, w12, 9);
218 R41(&c1, d1, &e1, a1, b1, w7, 5);
219 R42(&c2, d2, &e2, a2, b2, w2, 12);
220 R41(&b1, c1, &d1, e1, a1, w15, 6);
221 R42(&b2, c2, &d2, e2, a2, w13, 9);
222 R41(&a1, b1, &c1, d1, e1, w14, 8);
223 R42(&a2, b2, &c2, d2, e2, w9, 12);
224 R41(&e1, a1, &b1, c1, d1, w5, 6);
225 R42(&e2, a2, &b2, c2, d2, w7, 5);
226 R41(&d1, e1, &a1, b1, c1, w6, 5);
227 R42(&d2, e2, &a2, b2, c2, w10, 15);
228 R41(&c1, d1, &e1, a1, b1, w2, 12);
229 R42(&c2, d2, &e2, a2, b2, w14, 8);
231 R51(&b1, c1, &d1, e1, a1, w4, 9);
232 R52(&b2, c2, &d2, e2, a2, w12, 8);
233 R51(&a1, b1, &c1, d1, e1, w0, 15);
234 R52(&a2, b2, &c2, d2, e2, w15, 5);
235 R51(&e1, a1, &b1, c1, d1, w5, 5);
236 R52(&e2, a2, &b2, c2, d2, w10, 12);
237 R51(&d1, e1, &a1, b1, c1, w9, 11);
238 R52(&d2, e2, &a2, b2, c2, w4, 9);
239 R51(&c1, d1, &e1, a1, b1, w7, 6);
240 R52(&c2, d2, &e2, a2, b2, w1, 12);
241 R51(&b1, c1, &d1, e1, a1, w12, 8);
242 R52(&b2, c2, &d2, e2, a2, w5, 5);
243 R51(&a1, b1, &c1, d1, e1, w2, 13);
244 R52(&a2, b2, &c2, d2, e2, w8, 14);
245 R51(&e1, a1, &b1, c1, d1, w10, 12);
246 R52(&e2, a2, &b2, c2, d2, w7, 6);
247 R51(&d1, e1, &a1, b1, c1, w14, 5);
248 R52(&d2, e2, &a2, b2, c2, w6, 8);
249 R51(&c1, d1, &e1, a1, b1, w1, 12);
250 R52(&c2, d2, &e2, a2, b2, w2, 13);
251 R51(&b1, c1, &d1, e1, a1, w3, 13);
252 R52(&b2, c2, &d2, e2, a2, w13, 6);
253 R51(&a1, b1, &c1, d1, e1, w8, 14);
254 R52(&a2, b2, &c2, d2, e2, w14, 5);
255 R51(&e1, a1, &b1, c1, d1, w11, 11);
256 R52(&e2, a2, &b2, c2, d2, w0, 15);
257 R51(&d1, e1, &a1, b1, c1, w6, 8);
258 R52(&d2, e2, &a2, b2, c2, w3, 13);
259 R51(&c1, d1, &e1, a1, b1, w15, 5);
260 R52(&c2, d2, &e2, a2, b2, w9, 11);
261 R51(&b1, c1, &d1, e1, a1, w13, 6);
262 R52(&b2, c2, &d2, e2, a2, w11, 11);
265 s[0] = s[1] + c1 + d2;
266 s[1] = s[2] + d1 + e2;
267 s[2] = s[3] + e1 + a2;
268 s[3] = s[4] + a1 + b2;
272 static bool alignment_ok(const void *p UNUSED, size_t n UNUSED)
274 #if HAVE_UNALIGNED_ACCESS
277 return ((size_t)p % n == 0);
281 static void add(struct ripemd160_ctx *ctx, const void *p, size_t len)
283 const unsigned char *data = p;
284 size_t bufsize = ctx->bytes % 64;
286 if (bufsize + len >= 64) {
287 // Fill the buffer, and process it.
288 memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
289 ctx->bytes += 64 - bufsize;
290 data += 64 - bufsize;
292 Transform(ctx->s, ctx->buf.u32);
297 // Process full chunks directly from the source.
298 if (alignment_ok(data, sizeof(uint32_t)))
299 Transform(ctx->s, (const uint32_t *)data);
301 memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
302 Transform(ctx->s, ctx->buf.u32);
310 // Fill the buffer with what remains.
311 memcpy(ctx->buf.u8 + bufsize, data, len);
316 void ripemd160_init(struct ripemd160_ctx *ctx)
318 struct ripemd160_ctx init = RIPEMD160_INIT;
322 void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size)
324 check_ripemd160(ctx);
328 void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res)
330 static const unsigned char pad[64] = {0x80};
334 sizedesc = cpu_to_le64(ctx->bytes << 3);
335 /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
336 add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
337 /* Add number of bits of data (big endian) */
338 add(ctx, &sizedesc, 8);
339 for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
340 res->u.u32[i] = cpu_to_le32(ctx->s[i]);
341 invalidate_ripemd160(ctx);
345 void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size)
347 struct ripemd160_ctx ctx;
349 ripemd160_init(&ctx);
350 ripemd160_update(&ctx, p, size);
351 ripemd160_done(&ctx, ripemd);
354 void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v)
356 ripemd160_update(ctx, &v, sizeof(v));
359 void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v)
361 ripemd160_update(ctx, &v, sizeof(v));
364 void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v)
366 ripemd160_update(ctx, &v, sizeof(v));
369 void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v)
371 ripemd160_update(ctx, &v, sizeof(v));
374 /* Add as little-endian */
375 void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v)
377 leint16_t lev = cpu_to_le16(v);
378 ripemd160_update(ctx, &lev, sizeof(lev));
381 void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v)
383 leint32_t lev = cpu_to_le32(v);
384 ripemd160_update(ctx, &lev, sizeof(lev));
387 void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v)
389 leint64_t lev = cpu_to_le64(v);
390 ripemd160_update(ctx, &lev, sizeof(lev));
393 /* Add as big-endian */
394 void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v)
396 beint16_t bev = cpu_to_be16(v);
397 ripemd160_update(ctx, &bev, sizeof(bev));
400 void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v)
402 beint32_t bev = cpu_to_be32(v);
403 ripemd160_update(ctx, &bev, sizeof(bev));
406 void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v)
408 beint64_t bev = cpu_to_be64(v);
409 ripemd160_update(ctx, &bev, sizeof(bev));