Bitcoin ABC  0.29.2
P2P Digital Currency
ripemd160.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <crypto/ripemd160.h>
6 
7 #include <crypto/common.h>
8 
9 #include <cstring>
10 
11 // Internal implementation code.
12 namespace {
14 namespace ripemd160 {
15  inline uint32_t f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
16  inline uint32_t f2(uint32_t x, uint32_t y, uint32_t z) {
17  return (x & y) | (~x & z);
18  }
19  inline uint32_t f3(uint32_t x, uint32_t y, uint32_t z) {
20  return (x | ~y) ^ z;
21  }
22  inline uint32_t f4(uint32_t x, uint32_t y, uint32_t z) {
23  return (x & z) | (y & ~z);
24  }
25  inline uint32_t f5(uint32_t x, uint32_t y, uint32_t z) {
26  return x ^ (y | ~z);
27  }
28 
30  inline void Initialize(uint32_t *s) {
31  s[0] = 0x67452301ul;
32  s[1] = 0xEFCDAB89ul;
33  s[2] = 0x98BADCFEul;
34  s[3] = 0x10325476ul;
35  s[4] = 0xC3D2E1F0ul;
36  }
37 
38  inline uint32_t rol(uint32_t x, int i) {
39  return (x << i) | (x >> (32 - i));
40  }
41 
42  inline void Round(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
43  uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) {
44  a = rol(a + f + x + k, r) + e;
45  c = rol(c, 10);
46  }
47 
48  inline void R11(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
49  uint32_t e, uint32_t x, int r) {
50  Round(a, b, c, d, e, f1(b, c, d), x, 0, r);
51  }
52  inline void R21(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
53  uint32_t e, uint32_t x, int r) {
54  Round(a, b, c, d, e, f2(b, c, d), x, 0x5A827999ul, r);
55  }
56  inline void R31(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
57  uint32_t e, uint32_t x, int r) {
58  Round(a, b, c, d, e, f3(b, c, d), x, 0x6ED9EBA1ul, r);
59  }
60  inline void R41(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
61  uint32_t e, uint32_t x, int r) {
62  Round(a, b, c, d, e, f4(b, c, d), x, 0x8F1BBCDCul, r);
63  }
64  inline void R51(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
65  uint32_t e, uint32_t x, int r) {
66  Round(a, b, c, d, e, f5(b, c, d), x, 0xA953FD4Eul, r);
67  }
68 
69  inline void R12(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
70  uint32_t e, uint32_t x, int r) {
71  Round(a, b, c, d, e, f5(b, c, d), x, 0x50A28BE6ul, r);
72  }
73  inline void R22(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
74  uint32_t e, uint32_t x, int r) {
75  Round(a, b, c, d, e, f4(b, c, d), x, 0x5C4DD124ul, r);
76  }
77  inline void R32(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
78  uint32_t e, uint32_t x, int r) {
79  Round(a, b, c, d, e, f3(b, c, d), x, 0x6D703EF3ul, r);
80  }
81  inline void R42(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
82  uint32_t e, uint32_t x, int r) {
83  Round(a, b, c, d, e, f2(b, c, d), x, 0x7A6D76E9ul, r);
84  }
85  inline void R52(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d,
86  uint32_t e, uint32_t x, int r) {
87  Round(a, b, c, d, e, f1(b, c, d), x, 0, r);
88  }
89 
91  void Transform(uint32_t *s, const uint8_t *chunk) {
92  uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
93  uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
94  uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4),
95  w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12);
96  uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20),
97  w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28);
98  uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36),
99  w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44);
100  uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52),
101  w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60);
102 
103  R11(a1, b1, c1, d1, e1, w0, 11);
104  R12(a2, b2, c2, d2, e2, w5, 8);
105  R11(e1, a1, b1, c1, d1, w1, 14);
106  R12(e2, a2, b2, c2, d2, w14, 9);
107  R11(d1, e1, a1, b1, c1, w2, 15);
108  R12(d2, e2, a2, b2, c2, w7, 9);
109  R11(c1, d1, e1, a1, b1, w3, 12);
110  R12(c2, d2, e2, a2, b2, w0, 11);
111  R11(b1, c1, d1, e1, a1, w4, 5);
112  R12(b2, c2, d2, e2, a2, w9, 13);
113  R11(a1, b1, c1, d1, e1, w5, 8);
114  R12(a2, b2, c2, d2, e2, w2, 15);
115  R11(e1, a1, b1, c1, d1, w6, 7);
116  R12(e2, a2, b2, c2, d2, w11, 15);
117  R11(d1, e1, a1, b1, c1, w7, 9);
118  R12(d2, e2, a2, b2, c2, w4, 5);
119  R11(c1, d1, e1, a1, b1, w8, 11);
120  R12(c2, d2, e2, a2, b2, w13, 7);
121  R11(b1, c1, d1, e1, a1, w9, 13);
122  R12(b2, c2, d2, e2, a2, w6, 7);
123  R11(a1, b1, c1, d1, e1, w10, 14);
124  R12(a2, b2, c2, d2, e2, w15, 8);
125  R11(e1, a1, b1, c1, d1, w11, 15);
126  R12(e2, a2, b2, c2, d2, w8, 11);
127  R11(d1, e1, a1, b1, c1, w12, 6);
128  R12(d2, e2, a2, b2, c2, w1, 14);
129  R11(c1, d1, e1, a1, b1, w13, 7);
130  R12(c2, d2, e2, a2, b2, w10, 14);
131  R11(b1, c1, d1, e1, a1, w14, 9);
132  R12(b2, c2, d2, e2, a2, w3, 12);
133  R11(a1, b1, c1, d1, e1, w15, 8);
134  R12(a2, b2, c2, d2, e2, w12, 6);
135 
136  R21(e1, a1, b1, c1, d1, w7, 7);
137  R22(e2, a2, b2, c2, d2, w6, 9);
138  R21(d1, e1, a1, b1, c1, w4, 6);
139  R22(d2, e2, a2, b2, c2, w11, 13);
140  R21(c1, d1, e1, a1, b1, w13, 8);
141  R22(c2, d2, e2, a2, b2, w3, 15);
142  R21(b1, c1, d1, e1, a1, w1, 13);
143  R22(b2, c2, d2, e2, a2, w7, 7);
144  R21(a1, b1, c1, d1, e1, w10, 11);
145  R22(a2, b2, c2, d2, e2, w0, 12);
146  R21(e1, a1, b1, c1, d1, w6, 9);
147  R22(e2, a2, b2, c2, d2, w13, 8);
148  R21(d1, e1, a1, b1, c1, w15, 7);
149  R22(d2, e2, a2, b2, c2, w5, 9);
150  R21(c1, d1, e1, a1, b1, w3, 15);
151  R22(c2, d2, e2, a2, b2, w10, 11);
152  R21(b1, c1, d1, e1, a1, w12, 7);
153  R22(b2, c2, d2, e2, a2, w14, 7);
154  R21(a1, b1, c1, d1, e1, w0, 12);
155  R22(a2, b2, c2, d2, e2, w15, 7);
156  R21(e1, a1, b1, c1, d1, w9, 15);
157  R22(e2, a2, b2, c2, d2, w8, 12);
158  R21(d1, e1, a1, b1, c1, w5, 9);
159  R22(d2, e2, a2, b2, c2, w12, 7);
160  R21(c1, d1, e1, a1, b1, w2, 11);
161  R22(c2, d2, e2, a2, b2, w4, 6);
162  R21(b1, c1, d1, e1, a1, w14, 7);
163  R22(b2, c2, d2, e2, a2, w9, 15);
164  R21(a1, b1, c1, d1, e1, w11, 13);
165  R22(a2, b2, c2, d2, e2, w1, 13);
166  R21(e1, a1, b1, c1, d1, w8, 12);
167  R22(e2, a2, b2, c2, d2, w2, 11);
168 
169  R31(d1, e1, a1, b1, c1, w3, 11);
170  R32(d2, e2, a2, b2, c2, w15, 9);
171  R31(c1, d1, e1, a1, b1, w10, 13);
172  R32(c2, d2, e2, a2, b2, w5, 7);
173  R31(b1, c1, d1, e1, a1, w14, 6);
174  R32(b2, c2, d2, e2, a2, w1, 15);
175  R31(a1, b1, c1, d1, e1, w4, 7);
176  R32(a2, b2, c2, d2, e2, w3, 11);
177  R31(e1, a1, b1, c1, d1, w9, 14);
178  R32(e2, a2, b2, c2, d2, w7, 8);
179  R31(d1, e1, a1, b1, c1, w15, 9);
180  R32(d2, e2, a2, b2, c2, w14, 6);
181  R31(c1, d1, e1, a1, b1, w8, 13);
182  R32(c2, d2, e2, a2, b2, w6, 6);
183  R31(b1, c1, d1, e1, a1, w1, 15);
184  R32(b2, c2, d2, e2, a2, w9, 14);
185  R31(a1, b1, c1, d1, e1, w2, 14);
186  R32(a2, b2, c2, d2, e2, w11, 12);
187  R31(e1, a1, b1, c1, d1, w7, 8);
188  R32(e2, a2, b2, c2, d2, w8, 13);
189  R31(d1, e1, a1, b1, c1, w0, 13);
190  R32(d2, e2, a2, b2, c2, w12, 5);
191  R31(c1, d1, e1, a1, b1, w6, 6);
192  R32(c2, d2, e2, a2, b2, w2, 14);
193  R31(b1, c1, d1, e1, a1, w13, 5);
194  R32(b2, c2, d2, e2, a2, w10, 13);
195  R31(a1, b1, c1, d1, e1, w11, 12);
196  R32(a2, b2, c2, d2, e2, w0, 13);
197  R31(e1, a1, b1, c1, d1, w5, 7);
198  R32(e2, a2, b2, c2, d2, w4, 7);
199  R31(d1, e1, a1, b1, c1, w12, 5);
200  R32(d2, e2, a2, b2, c2, w13, 5);
201 
202  R41(c1, d1, e1, a1, b1, w1, 11);
203  R42(c2, d2, e2, a2, b2, w8, 15);
204  R41(b1, c1, d1, e1, a1, w9, 12);
205  R42(b2, c2, d2, e2, a2, w6, 5);
206  R41(a1, b1, c1, d1, e1, w11, 14);
207  R42(a2, b2, c2, d2, e2, w4, 8);
208  R41(e1, a1, b1, c1, d1, w10, 15);
209  R42(e2, a2, b2, c2, d2, w1, 11);
210  R41(d1, e1, a1, b1, c1, w0, 14);
211  R42(d2, e2, a2, b2, c2, w3, 14);
212  R41(c1, d1, e1, a1, b1, w8, 15);
213  R42(c2, d2, e2, a2, b2, w11, 14);
214  R41(b1, c1, d1, e1, a1, w12, 9);
215  R42(b2, c2, d2, e2, a2, w15, 6);
216  R41(a1, b1, c1, d1, e1, w4, 8);
217  R42(a2, b2, c2, d2, e2, w0, 14);
218  R41(e1, a1, b1, c1, d1, w13, 9);
219  R42(e2, a2, b2, c2, d2, w5, 6);
220  R41(d1, e1, a1, b1, c1, w3, 14);
221  R42(d2, e2, a2, b2, c2, w12, 9);
222  R41(c1, d1, e1, a1, b1, w7, 5);
223  R42(c2, d2, e2, a2, b2, w2, 12);
224  R41(b1, c1, d1, e1, a1, w15, 6);
225  R42(b2, c2, d2, e2, a2, w13, 9);
226  R41(a1, b1, c1, d1, e1, w14, 8);
227  R42(a2, b2, c2, d2, e2, w9, 12);
228  R41(e1, a1, b1, c1, d1, w5, 6);
229  R42(e2, a2, b2, c2, d2, w7, 5);
230  R41(d1, e1, a1, b1, c1, w6, 5);
231  R42(d2, e2, a2, b2, c2, w10, 15);
232  R41(c1, d1, e1, a1, b1, w2, 12);
233  R42(c2, d2, e2, a2, b2, w14, 8);
234 
235  R51(b1, c1, d1, e1, a1, w4, 9);
236  R52(b2, c2, d2, e2, a2, w12, 8);
237  R51(a1, b1, c1, d1, e1, w0, 15);
238  R52(a2, b2, c2, d2, e2, w15, 5);
239  R51(e1, a1, b1, c1, d1, w5, 5);
240  R52(e2, a2, b2, c2, d2, w10, 12);
241  R51(d1, e1, a1, b1, c1, w9, 11);
242  R52(d2, e2, a2, b2, c2, w4, 9);
243  R51(c1, d1, e1, a1, b1, w7, 6);
244  R52(c2, d2, e2, a2, b2, w1, 12);
245  R51(b1, c1, d1, e1, a1, w12, 8);
246  R52(b2, c2, d2, e2, a2, w5, 5);
247  R51(a1, b1, c1, d1, e1, w2, 13);
248  R52(a2, b2, c2, d2, e2, w8, 14);
249  R51(e1, a1, b1, c1, d1, w10, 12);
250  R52(e2, a2, b2, c2, d2, w7, 6);
251  R51(d1, e1, a1, b1, c1, w14, 5);
252  R52(d2, e2, a2, b2, c2, w6, 8);
253  R51(c1, d1, e1, a1, b1, w1, 12);
254  R52(c2, d2, e2, a2, b2, w2, 13);
255  R51(b1, c1, d1, e1, a1, w3, 13);
256  R52(b2, c2, d2, e2, a2, w13, 6);
257  R51(a1, b1, c1, d1, e1, w8, 14);
258  R52(a2, b2, c2, d2, e2, w14, 5);
259  R51(e1, a1, b1, c1, d1, w11, 11);
260  R52(e2, a2, b2, c2, d2, w0, 15);
261  R51(d1, e1, a1, b1, c1, w6, 8);
262  R52(d2, e2, a2, b2, c2, w3, 13);
263  R51(c1, d1, e1, a1, b1, w15, 5);
264  R52(c2, d2, e2, a2, b2, w9, 11);
265  R51(b1, c1, d1, e1, a1, w13, 6);
266  R52(b2, c2, d2, e2, a2, w11, 11);
267 
268  uint32_t t = s[0];
269  s[0] = s[1] + c1 + d2;
270  s[1] = s[2] + d1 + e2;
271  s[2] = s[3] + e1 + a2;
272  s[3] = s[4] + a1 + b2;
273  s[4] = t + b1 + c2;
274  }
275 
276 } // namespace ripemd160
277 
278 } // namespace
279 
281 
283  ripemd160::Initialize(s);
284 }
285 
286 CRIPEMD160 &CRIPEMD160::Write(const uint8_t *data, size_t len) {
287  const uint8_t *end = data + len;
288  size_t bufsize = bytes % 64;
289  if (bufsize && bufsize + len >= 64) {
290  // Fill the buffer, and process it.
291  memcpy(buf + bufsize, data, 64 - bufsize);
292  bytes += 64 - bufsize;
293  data += 64 - bufsize;
295  bufsize = 0;
296  }
297  while (end - data >= 64) {
298  // Process full chunks directly from the source.
299  ripemd160::Transform(s, data);
300  bytes += 64;
301  data += 64;
302  }
303  if (end > data) {
304  // Fill the buffer with what remains.
305  memcpy(buf + bufsize, data, end - data);
306  bytes += end - data;
307  }
308  return *this;
309 }
310 
311 void CRIPEMD160::Finalize(uint8_t hash[OUTPUT_SIZE]) {
312  static const uint8_t pad[64] = {0x80};
313  uint8_t sizedesc[8];
314  WriteLE64(sizedesc, bytes << 3);
315  Write(pad, 1 + ((119 - (bytes % 64)) % 64));
316  Write(sizedesc, 8);
317  WriteLE32(hash, s[0]);
318  WriteLE32(hash + 4, s[1]);
319  WriteLE32(hash + 8, s[2]);
320  WriteLE32(hash + 12, s[3]);
321  WriteLE32(hash + 16, s[4]);
322 }
323 
325  bytes = 0;
326  ripemd160::Initialize(s);
327  return *this;
328 }
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:311
uint64_t bytes
Definition: ripemd160.h:16
uint8_t buf[64]
Definition: ripemd160.h:15
uint32_t s[5]
Definition: ripemd160.h:14
CRIPEMD160 & Reset()
Definition: ripemd160.cpp:324
static void WriteLE32(uint8_t *ptr, uint32_t x)
Definition: common.h:40
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:23
static void WriteLE64(uint8_t *ptr, uint64_t x)
Definition: common.h:45
#define Round(a, b, c, d, e, f, g, h, k, w)
Definition: hash_impl.h:24
Internal RIPEMD-160 implementation.
Definition: ripemd160.cpp:14
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)