4 * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
8 * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/crypto.h>
20 #include <linux/cryptohash.h>
21 #include <linux/types.h>
22 #include <asm/byteorder.h>
43 #define F1(x, y, z) (x ^ y ^ z) /* XOR */
44 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
45 #define F3(x, y, z) ((x | ~y) ^ z)
46 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
47 #define F5(x, y, z) (x ^ (y | ~z))
49 #define ROUND(a, b, c, d, e, f, k, x, s) { \
50 (a) += f((b), (c), (d)) + le32_to_cpu(x) + (k); \
51 (a) = rol32((a), (s)) + (e); \
52 (c) = rol32((c), 10); \
55 static void rmd160_transform(u32
*state
, u32
const *in
)
57 u32 aa
, bb
, cc
, dd
, ee
, aaa
, bbb
, ccc
, ddd
, eee
;
59 /* Initialize left lane */
66 /* Initialize right lane */
73 /* round 1: left lane */
74 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[0], 11);
75 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[1], 14);
76 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[2], 15);
77 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[3], 12);
78 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[4], 5);
79 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[5], 8);
80 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[6], 7);
81 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[7], 9);
82 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[8], 11);
83 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[9], 13);
84 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[10], 14);
85 ROUND(ee
, aa
, bb
, cc
, dd
, F1
, K1
, in
[11], 15);
86 ROUND(dd
, ee
, aa
, bb
, cc
, F1
, K1
, in
[12], 6);
87 ROUND(cc
, dd
, ee
, aa
, bb
, F1
, K1
, in
[13], 7);
88 ROUND(bb
, cc
, dd
, ee
, aa
, F1
, K1
, in
[14], 9);
89 ROUND(aa
, bb
, cc
, dd
, ee
, F1
, K1
, in
[15], 8);
91 /* round 2: left lane" */
92 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[7], 7);
93 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[4], 6);
94 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[13], 8);
95 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[1], 13);
96 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[10], 11);
97 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[6], 9);
98 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[15], 7);
99 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[3], 15);
100 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[12], 7);
101 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[0], 12);
102 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[9], 15);
103 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[5], 9);
104 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[2], 11);
105 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[14], 7);
106 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[11], 13);
107 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[8], 12);
109 /* round 3: left lane" */
110 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[3], 11);
111 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[10], 13);
112 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[14], 6);
113 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[4], 7);
114 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[9], 14);
115 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[15], 9);
116 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[8], 13);
117 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[1], 15);
118 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[2], 14);
119 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[7], 8);
120 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[0], 13);
121 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[6], 6);
122 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[13], 5);
123 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[11], 12);
124 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[5], 7);
125 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[12], 5);
127 /* round 4: left lane" */
128 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[1], 11);
129 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[9], 12);
130 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[11], 14);
131 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[10], 15);
132 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[0], 14);
133 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[8], 15);
134 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[12], 9);
135 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[4], 8);
136 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[13], 9);
137 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[3], 14);
138 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[7], 5);
139 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[15], 6);
140 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[14], 8);
141 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[5], 6);
142 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[6], 5);
143 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[2], 12);
145 /* round 5: left lane" */
146 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[4], 9);
147 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[0], 15);
148 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[5], 5);
149 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[9], 11);
150 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[7], 6);
151 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[12], 8);
152 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[2], 13);
153 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[10], 12);
154 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[14], 5);
155 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[1], 12);
156 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[3], 13);
157 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[8], 14);
158 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[11], 11);
159 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[6], 8);
160 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[15], 5);
161 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[13], 6);
163 /* round 1: right lane */
164 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[5], 8);
165 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[14], 9);
166 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[7], 9);
167 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[0], 11);
168 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[9], 13);
169 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[2], 15);
170 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[11], 15);
171 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[4], 5);
172 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[13], 7);
173 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[6], 7);
174 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[15], 8);
175 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[8], 11);
176 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[1], 14);
177 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[10], 14);
178 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[3], 12);
179 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[12], 6);
181 /* round 2: right lane */
182 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[6], 9);
183 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[11], 13);
184 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[3], 15);
185 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[7], 7);
186 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[0], 12);
187 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[13], 8);
188 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[5], 9);
189 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[10], 11);
190 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[14], 7);
191 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[15], 7);
192 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[8], 12);
193 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[12], 7);
194 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[4], 6);
195 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[9], 15);
196 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[1], 13);
197 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[2], 11);
199 /* round 3: right lane */
200 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[15], 9);
201 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[5], 7);
202 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[1], 15);
203 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[3], 11);
204 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[7], 8);
205 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[14], 6);
206 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[6], 6);
207 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[9], 14);
208 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[11], 12);
209 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[8], 13);
210 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[12], 5);
211 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[2], 14);
212 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[10], 13);
213 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[0], 13);
214 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[4], 7);
215 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[13], 5);
217 /* round 4: right lane */
218 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[8], 15);
219 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[6], 5);
220 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[4], 8);
221 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[1], 11);
222 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[3], 14);
223 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[11], 14);
224 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[15], 6);
225 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[0], 14);
226 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[5], 6);
227 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[12], 9);
228 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[2], 12);
229 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[13], 9);
230 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[9], 12);
231 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[7], 5);
232 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[10], 15);
233 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[14], 8);
235 /* round 5: right lane */
236 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[12], 8);
237 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[15], 5);
238 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[10], 12);
239 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[4], 9);
240 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[1], 12);
241 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[5], 5);
242 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[8], 14);
243 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[7], 6);
244 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[6], 8);
245 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[2], 13);
246 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[13], 6);
247 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[14], 5);
248 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[0], 15);
249 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[3], 13);
250 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[9], 11);
251 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[11], 11);
253 /* combine results */
254 ddd
+= cc
+ state
[1]; /* final result for state[0] */
255 state
[1] = state
[2] + dd
+ eee
;
256 state
[2] = state
[3] + ee
+ aaa
;
257 state
[3] = state
[4] + aa
+ bbb
;
258 state
[4] = state
[0] + bb
+ ccc
;
264 static void rmd160_init(struct crypto_tfm
*tfm
)
266 struct rmd160_ctx
*rctx
= crypto_tfm_ctx(tfm
);
268 rctx
->byte_count
= 0;
270 rctx
->state
[0] = RMD_H0
;
271 rctx
->state
[1] = RMD_H1
;
272 rctx
->state
[2] = RMD_H2
;
273 rctx
->state
[3] = RMD_H3
;
274 rctx
->state
[4] = RMD_H4
;
276 memset(rctx
->buffer
, 0, sizeof(rctx
->buffer
));
279 static void rmd160_update(struct crypto_tfm
*tfm
, const u8
*data
,
282 struct rmd160_ctx
*rctx
= crypto_tfm_ctx(tfm
);
283 const u32 avail
= sizeof(rctx
->buffer
) - (rctx
->byte_count
& 0x3f);
285 rctx
->byte_count
+= len
;
287 /* Enough space in buffer? If so copy and we're done */
289 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
294 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
297 rmd160_transform(rctx
->state
, rctx
->buffer
);
301 while (len
>= sizeof(rctx
->buffer
)) {
302 memcpy(rctx
->buffer
, data
, sizeof(rctx
->buffer
));
303 rmd160_transform(rctx
->state
, rctx
->buffer
);
304 data
+= sizeof(rctx
->buffer
);
305 len
-= sizeof(rctx
->buffer
);
308 memcpy(rctx
->buffer
, data
, len
);
311 /* Add padding and return the message digest. */
312 static void rmd160_final(struct crypto_tfm
*tfm
, u8
*out
)
314 struct rmd160_ctx
*rctx
= crypto_tfm_ctx(tfm
);
315 u32 i
, index
, padlen
;
317 u32
*dst
= (u32
*)out
;
318 static const u8 padding
[64] = { 0x80, };
320 bits
= cpu_to_le64(rctx
->byte_count
<< 3);
322 /* Pad out to 56 mod 64 */
323 index
= rctx
->byte_count
& 0x3f;
324 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
325 rmd160_update(tfm
, padding
, padlen
);
328 rmd160_update(tfm
, (const u8
*)&bits
, sizeof(bits
));
330 /* Store state in digest */
331 for (i
= 0; i
< 5; i
++)
332 dst
[i
] = cpu_to_le32(rctx
->state
[i
]);
335 memset(rctx
, 0, sizeof(*rctx
));
338 static struct crypto_alg alg
= {
339 .cra_name
= "rmd160",
340 .cra_driver_name
= "rmd160",
341 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
342 .cra_blocksize
= RMD160_BLOCK_SIZE
,
343 .cra_ctxsize
= sizeof(struct rmd160_ctx
),
344 .cra_module
= THIS_MODULE
,
345 .cra_list
= LIST_HEAD_INIT(alg
.cra_list
),
346 .cra_u
= { .digest
= {
347 .dia_digestsize
= RMD160_DIGEST_SIZE
,
348 .dia_init
= rmd160_init
,
349 .dia_update
= rmd160_update
,
350 .dia_final
= rmd160_final
} }
353 static int __init
rmd160_mod_init(void)
355 return crypto_register_alg(&alg
);
358 static void __exit
rmd160_mod_fini(void)
360 crypto_unregister_alg(&alg
);
363 module_init(rmd160_mod_init
);
364 module_exit(rmd160_mod_fini
);
366 MODULE_LICENSE("GPL");
367 MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
369 MODULE_ALIAS("rmd160");