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);
19 inline uint32_t f3(uint32_t x, uint32_t y, uint32_t z) {
22 inline uint32_t f4(uint32_t x, uint32_t y, uint32_t z) {
23 return (x & z) | (y & ~z);
25 inline uint32_t f5(uint32_t x, uint32_t y, uint32_t z) {
30 inline void Initialize(uint32_t *s) {
38 inline uint32_t rol(uint32_t x,
int i) {
39 return (x << i) | (x >> (32 - i));
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;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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;
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);
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);
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);
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);
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);
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;
283 ripemd160::Initialize(
s);
287 const uint8_t *end = data + len;
288 size_t bufsize =
bytes % 64;
289 if (bufsize && bufsize + len >= 64) {
291 memcpy(
buf + bufsize, data, 64 - bufsize);
292 bytes += 64 - bufsize;
293 data += 64 - bufsize;
297 while (end - data >= 64) {
305 memcpy(
buf + bufsize, data, end - data);
312 static const uint8_t pad[64] = {0x80};
326 ripemd160::Initialize(
s);
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
static void WriteLE32(uint8_t *ptr, uint32_t x)
static uint32_t ReadLE32(const uint8_t *ptr)
static void WriteLE64(uint8_t *ptr, uint64_t x)
#define Round(a, b, c, d, e, f, g, h, k, w)
Internal RIPEMD-160 implementation.
void Transform(uint32_t *s, const uint8_t *chunk, size_t blocks)