18 uint32_t
inline f1(uint32_t x, uint32_t y, uint32_t z) {
return x ^ y ^ z; }
19 uint32_t
inline f2(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) | (~x & z); }
20 uint32_t
inline f3(uint32_t x, uint32_t y, uint32_t z) {
return (x | ~y) ^ z; }
21 uint32_t
inline f4(uint32_t x, uint32_t y, uint32_t z) {
return (x & z) | (y & ~z); }
22 uint32_t
inline f5(uint32_t x, uint32_t y, uint32_t z) {
return x ^ (y | ~z); }
25 void inline Initialize(uint32_t* s)
34 uint32_t
inline rol(uint32_t x,
int i) {
return (x << i) | (x >> (32 - i)); }
36 void inline Round(uint32_t& a, uint32_t b, uint32_t& c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k,
int r)
38 a = rol(a + f + x + k, r) + e;
42 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); }
43 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); }
44 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); }
45 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); }
46 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); }
48 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); }
49 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); }
50 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); }
51 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); }
52 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); }
55 void Transform(uint32_t* s,
const unsigned char* chunk)
57 uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4];
58 uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1;
59 uint32_t w0 = ReadLE32(chunk + 0), w1 = ReadLE32(chunk + 4), w2 = ReadLE32(chunk + 8), w3 = ReadLE32(chunk + 12);
60 uint32_t w4 = ReadLE32(chunk + 16), w5 = ReadLE32(chunk + 20), w6 = ReadLE32(chunk + 24), w7 = ReadLE32(chunk + 28);
61 uint32_t w8 = ReadLE32(chunk + 32), w9 = ReadLE32(chunk + 36), w10 = ReadLE32(chunk + 40), w11 = ReadLE32(chunk + 44);
62 uint32_t w12 = ReadLE32(chunk + 48), w13 = ReadLE32(chunk + 52), w14 = ReadLE32(chunk + 56), w15 = ReadLE32(chunk + 60);
64 R11(a1, b1, c1, d1, e1, w0, 11);
65 R12(a2, b2, c2, d2, e2, w5, 8);
66 R11(e1, a1, b1, c1, d1, w1, 14);
67 R12(e2, a2, b2, c2, d2, w14, 9);
68 R11(d1, e1, a1, b1, c1, w2, 15);
69 R12(d2, e2, a2, b2, c2, w7, 9);
70 R11(c1, d1, e1, a1, b1, w3, 12);
71 R12(c2, d2, e2, a2, b2, w0, 11);
72 R11(b1, c1, d1, e1, a1, w4, 5);
73 R12(b2, c2, d2, e2, a2, w9, 13);
74 R11(a1, b1, c1, d1, e1, w5, 8);
75 R12(a2, b2, c2, d2, e2, w2, 15);
76 R11(e1, a1, b1, c1, d1, w6, 7);
77 R12(e2, a2, b2, c2, d2, w11, 15);
78 R11(d1, e1, a1, b1, c1, w7, 9);
79 R12(d2, e2, a2, b2, c2, w4, 5);
80 R11(c1, d1, e1, a1, b1, w8, 11);
81 R12(c2, d2, e2, a2, b2, w13, 7);
82 R11(b1, c1, d1, e1, a1, w9, 13);
83 R12(b2, c2, d2, e2, a2, w6, 7);
84 R11(a1, b1, c1, d1, e1, w10, 14);
85 R12(a2, b2, c2, d2, e2, w15, 8);
86 R11(e1, a1, b1, c1, d1, w11, 15);
87 R12(e2, a2, b2, c2, d2, w8, 11);
88 R11(d1, e1, a1, b1, c1, w12, 6);
89 R12(d2, e2, a2, b2, c2, w1, 14);
90 R11(c1, d1, e1, a1, b1, w13, 7);
91 R12(c2, d2, e2, a2, b2, w10, 14);
92 R11(b1, c1, d1, e1, a1, w14, 9);
93 R12(b2, c2, d2, e2, a2, w3, 12);
94 R11(a1, b1, c1, d1, e1, w15, 8);
95 R12(a2, b2, c2, d2, e2, w12, 6);
97 R21(e1, a1, b1, c1, d1, w7, 7);
98 R22(e2, a2, b2, c2, d2, w6, 9);
99 R21(d1, e1, a1, b1, c1, w4, 6);
100 R22(d2, e2, a2, b2, c2, w11, 13);
101 R21(c1, d1, e1, a1, b1, w13, 8);
102 R22(c2, d2, e2, a2, b2, w3, 15);
103 R21(b1, c1, d1, e1, a1, w1, 13);
104 R22(b2, c2, d2, e2, a2, w7, 7);
105 R21(a1, b1, c1, d1, e1, w10, 11);
106 R22(a2, b2, c2, d2, e2, w0, 12);
107 R21(e1, a1, b1, c1, d1, w6, 9);
108 R22(e2, a2, b2, c2, d2, w13, 8);
109 R21(d1, e1, a1, b1, c1, w15, 7);
110 R22(d2, e2, a2, b2, c2, w5, 9);
111 R21(c1, d1, e1, a1, b1, w3, 15);
112 R22(c2, d2, e2, a2, b2, w10, 11);
113 R21(b1, c1, d1, e1, a1, w12, 7);
114 R22(b2, c2, d2, e2, a2, w14, 7);
115 R21(a1, b1, c1, d1, e1, w0, 12);
116 R22(a2, b2, c2, d2, e2, w15, 7);
117 R21(e1, a1, b1, c1, d1, w9, 15);
118 R22(e2, a2, b2, c2, d2, w8, 12);
119 R21(d1, e1, a1, b1, c1, w5, 9);
120 R22(d2, e2, a2, b2, c2, w12, 7);
121 R21(c1, d1, e1, a1, b1, w2, 11);
122 R22(c2, d2, e2, a2, b2, w4, 6);
123 R21(b1, c1, d1, e1, a1, w14, 7);
124 R22(b2, c2, d2, e2, a2, w9, 15);
125 R21(a1, b1, c1, d1, e1, w11, 13);
126 R22(a2, b2, c2, d2, e2, w1, 13);
127 R21(e1, a1, b1, c1, d1, w8, 12);
128 R22(e2, a2, b2, c2, d2, w2, 11);
130 R31(d1, e1, a1, b1, c1, w3, 11);
131 R32(d2, e2, a2, b2, c2, w15, 9);
132 R31(c1, d1, e1, a1, b1, w10, 13);
133 R32(c2, d2, e2, a2, b2, w5, 7);
134 R31(b1, c1, d1, e1, a1, w14, 6);
135 R32(b2, c2, d2, e2, a2, w1, 15);
136 R31(a1, b1, c1, d1, e1, w4, 7);
137 R32(a2, b2, c2, d2, e2, w3, 11);
138 R31(e1, a1, b1, c1, d1, w9, 14);
139 R32(e2, a2, b2, c2, d2, w7, 8);
140 R31(d1, e1, a1, b1, c1, w15, 9);
141 R32(d2, e2, a2, b2, c2, w14, 6);
142 R31(c1, d1, e1, a1, b1, w8, 13);
143 R32(c2, d2, e2, a2, b2, w6, 6);
144 R31(b1, c1, d1, e1, a1, w1, 15);
145 R32(b2, c2, d2, e2, a2, w9, 14);
146 R31(a1, b1, c1, d1, e1, w2, 14);
147 R32(a2, b2, c2, d2, e2, w11, 12);
148 R31(e1, a1, b1, c1, d1, w7, 8);
149 R32(e2, a2, b2, c2, d2, w8, 13);
150 R31(d1, e1, a1, b1, c1, w0, 13);
151 R32(d2, e2, a2, b2, c2, w12, 5);
152 R31(c1, d1, e1, a1, b1, w6, 6);
153 R32(c2, d2, e2, a2, b2, w2, 14);
154 R31(b1, c1, d1, e1, a1, w13, 5);
155 R32(b2, c2, d2, e2, a2, w10, 13);
156 R31(a1, b1, c1, d1, e1, w11, 12);
157 R32(a2, b2, c2, d2, e2, w0, 13);
158 R31(e1, a1, b1, c1, d1, w5, 7);
159 R32(e2, a2, b2, c2, d2, w4, 7);
160 R31(d1, e1, a1, b1, c1, w12, 5);
161 R32(d2, e2, a2, b2, c2, w13, 5);
163 R41(c1, d1, e1, a1, b1, w1, 11);
164 R42(c2, d2, e2, a2, b2, w8, 15);
165 R41(b1, c1, d1, e1, a1, w9, 12);
166 R42(b2, c2, d2, e2, a2, w6, 5);
167 R41(a1, b1, c1, d1, e1, w11, 14);
168 R42(a2, b2, c2, d2, e2, w4, 8);
169 R41(e1, a1, b1, c1, d1, w10, 15);
170 R42(e2, a2, b2, c2, d2, w1, 11);
171 R41(d1, e1, a1, b1, c1, w0, 14);
172 R42(d2, e2, a2, b2, c2, w3, 14);
173 R41(c1, d1, e1, a1, b1, w8, 15);
174 R42(c2, d2, e2, a2, b2, w11, 14);
175 R41(b1, c1, d1, e1, a1, w12, 9);
176 R42(b2, c2, d2, e2, a2, w15, 6);
177 R41(a1, b1, c1, d1, e1, w4, 8);
178 R42(a2, b2, c2, d2, e2, w0, 14);
179 R41(e1, a1, b1, c1, d1, w13, 9);
180 R42(e2, a2, b2, c2, d2, w5, 6);
181 R41(d1, e1, a1, b1, c1, w3, 14);
182 R42(d2, e2, a2, b2, c2, w12, 9);
183 R41(c1, d1, e1, a1, b1, w7, 5);
184 R42(c2, d2, e2, a2, b2, w2, 12);
185 R41(b1, c1, d1, e1, a1, w15, 6);
186 R42(b2, c2, d2, e2, a2, w13, 9);
187 R41(a1, b1, c1, d1, e1, w14, 8);
188 R42(a2, b2, c2, d2, e2, w9, 12);
189 R41(e1, a1, b1, c1, d1, w5, 6);
190 R42(e2, a2, b2, c2, d2, w7, 5);
191 R41(d1, e1, a1, b1, c1, w6, 5);
192 R42(d2, e2, a2, b2, c2, w10, 15);
193 R41(c1, d1, e1, a1, b1, w2, 12);
194 R42(c2, d2, e2, a2, b2, w14, 8);
196 R51(b1, c1, d1, e1, a1, w4, 9);
197 R52(b2, c2, d2, e2, a2, w12, 8);
198 R51(a1, b1, c1, d1, e1, w0, 15);
199 R52(a2, b2, c2, d2, e2, w15, 5);
200 R51(e1, a1, b1, c1, d1, w5, 5);
201 R52(e2, a2, b2, c2, d2, w10, 12);
202 R51(d1, e1, a1, b1, c1, w9, 11);
203 R52(d2, e2, a2, b2, c2, w4, 9);
204 R51(c1, d1, e1, a1, b1, w7, 6);
205 R52(c2, d2, e2, a2, b2, w1, 12);
206 R51(b1, c1, d1, e1, a1, w12, 8);
207 R52(b2, c2, d2, e2, a2, w5, 5);
208 R51(a1, b1, c1, d1, e1, w2, 13);
209 R52(a2, b2, c2, d2, e2, w8, 14);
210 R51(e1, a1, b1, c1, d1, w10, 12);
211 R52(e2, a2, b2, c2, d2, w7, 6);
212 R51(d1, e1, a1, b1, c1, w14, 5);
213 R52(d2, e2, a2, b2, c2, w6, 8);
214 R51(c1, d1, e1, a1, b1, w1, 12);
215 R52(c2, d2, e2, a2, b2, w2, 13);
216 R51(b1, c1, d1, e1, a1, w3, 13);
217 R52(b2, c2, d2, e2, a2, w13, 6);
218 R51(a1, b1, c1, d1, e1, w8, 14);
219 R52(a2, b2, c2, d2, e2, w14, 5);
220 R51(e1, a1, b1, c1, d1, w11, 11);
221 R52(e2, a2, b2, c2, d2, w0, 15);
222 R51(d1, e1, a1, b1, c1, w6, 8);
223 R52(d2, e2, a2, b2, c2, w3, 13);
224 R51(c1, d1, e1, a1, b1, w15, 5);
225 R52(c2, d2, e2, a2, b2, w9, 11);
226 R51(b1, c1, d1, e1, a1, w13, 6);
227 R52(b2, c2, d2, e2, a2, w11, 11);
230 s[0] = s[1] + c1 + d2;
231 s[1] = s[2] + d1 + e2;
232 s[2] = s[3] + e1 + a2;
233 s[3] = s[4] + a1 + b2;
245 ripemd160::Initialize(
s);
250 const unsigned char* end = data + len;
251 size_t bufsize =
bytes % 64;
252 if (bufsize && bufsize + len >= 64) {
254 memcpy(
buf + bufsize, data, 64 - bufsize);
255 bytes += 64 - bufsize;
256 data += 64 - bufsize;
257 ripemd160::Transform(
s,
buf);
260 while (end >= data + 64) {
262 ripemd160::Transform(
s, data);
276 static const unsigned char pad[64] = {0x80};
277 unsigned char sizedesc[8];
278 WriteLE64(sizedesc,
bytes << 3);
281 WriteLE32(hash,
s[0]);
282 WriteLE32(hash + 4,
s[1]);
283 WriteLE32(hash + 8,
s[2]);
284 WriteLE32(hash + 12,
s[3]);
285 WriteLE32(hash + 16,
s[4]);
291 ripemd160::Initialize(
s);
#define Round(a, b, c, d, e, f, g, h, k, w)
static const size_t OUTPUT_SIZE
CRIPEMD160 & Write(const unsigned char *data, size_t len)
void * memcpy(void *a, const void *b, size_t c)
void Finalize(unsigned char hash[OUTPUT_SIZE])
A hasher class for RIPEMD-160.
Internal RIPEMD-160 implementation.