4 * RIPEMD-320 - 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_cpup(&(x)) + (k); \
51 (a) = rol32((a), (s)) + (e); \
52 (c) = rol32((c), 10); \
55 static void rmd320_transform(u32
*state
, const __le32
*in
)
57 u32 aa
, bb
, cc
, dd
, ee
, aaa
, bbb
, ccc
, ddd
, eee
, tmp
;
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 1: right lane */
92 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[5], 8);
93 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[14], 9);
94 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[7], 9);
95 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[0], 11);
96 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[9], 13);
97 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[2], 15);
98 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[11], 15);
99 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[4], 5);
100 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[13], 7);
101 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[6], 7);
102 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[15], 8);
103 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F5
, KK1
, in
[8], 11);
104 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F5
, KK1
, in
[1], 14);
105 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F5
, KK1
, in
[10], 14);
106 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F5
, KK1
, in
[3], 12);
107 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F5
, KK1
, in
[12], 6);
109 /* Swap contents of "a" registers */
110 tmp
= aa
; aa
= aaa
; aaa
= tmp
;
112 /* round 2: left lane" */
113 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[7], 7);
114 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[4], 6);
115 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[13], 8);
116 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[1], 13);
117 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[10], 11);
118 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[6], 9);
119 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[15], 7);
120 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[3], 15);
121 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[12], 7);
122 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[0], 12);
123 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[9], 15);
124 ROUND(dd
, ee
, aa
, bb
, cc
, F2
, K2
, in
[5], 9);
125 ROUND(cc
, dd
, ee
, aa
, bb
, F2
, K2
, in
[2], 11);
126 ROUND(bb
, cc
, dd
, ee
, aa
, F2
, K2
, in
[14], 7);
127 ROUND(aa
, bb
, cc
, dd
, ee
, F2
, K2
, in
[11], 13);
128 ROUND(ee
, aa
, bb
, cc
, dd
, F2
, K2
, in
[8], 12);
130 /* round 2: right lane */
131 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[6], 9);
132 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[11], 13);
133 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[3], 15);
134 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[7], 7);
135 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[0], 12);
136 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[13], 8);
137 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[5], 9);
138 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[10], 11);
139 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[14], 7);
140 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[15], 7);
141 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[8], 12);
142 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F4
, KK2
, in
[12], 7);
143 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F4
, KK2
, in
[4], 6);
144 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F4
, KK2
, in
[9], 15);
145 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F4
, KK2
, in
[1], 13);
146 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F4
, KK2
, in
[2], 11);
148 /* Swap contents of "b" registers */
149 tmp
= bb
; bb
= bbb
; bbb
= tmp
;
151 /* round 3: left lane" */
152 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[3], 11);
153 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[10], 13);
154 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[14], 6);
155 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[4], 7);
156 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[9], 14);
157 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[15], 9);
158 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[8], 13);
159 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[1], 15);
160 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[2], 14);
161 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[7], 8);
162 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[0], 13);
163 ROUND(cc
, dd
, ee
, aa
, bb
, F3
, K3
, in
[6], 6);
164 ROUND(bb
, cc
, dd
, ee
, aa
, F3
, K3
, in
[13], 5);
165 ROUND(aa
, bb
, cc
, dd
, ee
, F3
, K3
, in
[11], 12);
166 ROUND(ee
, aa
, bb
, cc
, dd
, F3
, K3
, in
[5], 7);
167 ROUND(dd
, ee
, aa
, bb
, cc
, F3
, K3
, in
[12], 5);
169 /* round 3: right lane */
170 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[15], 9);
171 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[5], 7);
172 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[1], 15);
173 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[3], 11);
174 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[7], 8);
175 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[14], 6);
176 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[6], 6);
177 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[9], 14);
178 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[11], 12);
179 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[8], 13);
180 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[12], 5);
181 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F3
, KK3
, in
[2], 14);
182 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F3
, KK3
, in
[10], 13);
183 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F3
, KK3
, in
[0], 13);
184 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F3
, KK3
, in
[4], 7);
185 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F3
, KK3
, in
[13], 5);
187 /* Swap contents of "c" registers */
188 tmp
= cc
; cc
= ccc
; ccc
= tmp
;
190 /* round 4: left lane" */
191 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[1], 11);
192 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[9], 12);
193 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[11], 14);
194 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[10], 15);
195 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[0], 14);
196 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[8], 15);
197 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[12], 9);
198 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[4], 8);
199 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[13], 9);
200 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[3], 14);
201 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[7], 5);
202 ROUND(bb
, cc
, dd
, ee
, aa
, F4
, K4
, in
[15], 6);
203 ROUND(aa
, bb
, cc
, dd
, ee
, F4
, K4
, in
[14], 8);
204 ROUND(ee
, aa
, bb
, cc
, dd
, F4
, K4
, in
[5], 6);
205 ROUND(dd
, ee
, aa
, bb
, cc
, F4
, K4
, in
[6], 5);
206 ROUND(cc
, dd
, ee
, aa
, bb
, F4
, K4
, in
[2], 12);
208 /* round 4: right lane */
209 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[8], 15);
210 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[6], 5);
211 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[4], 8);
212 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[1], 11);
213 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[3], 14);
214 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[11], 14);
215 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[15], 6);
216 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[0], 14);
217 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[5], 6);
218 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[12], 9);
219 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[2], 12);
220 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F2
, KK4
, in
[13], 9);
221 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F2
, KK4
, in
[9], 12);
222 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F2
, KK4
, in
[7], 5);
223 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F2
, KK4
, in
[10], 15);
224 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F2
, KK4
, in
[14], 8);
226 /* Swap contents of "d" registers */
227 tmp
= dd
; dd
= ddd
; ddd
= tmp
;
229 /* round 5: left lane" */
230 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[4], 9);
231 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[0], 15);
232 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[5], 5);
233 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[9], 11);
234 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[7], 6);
235 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[12], 8);
236 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[2], 13);
237 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[10], 12);
238 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[14], 5);
239 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[1], 12);
240 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[3], 13);
241 ROUND(aa
, bb
, cc
, dd
, ee
, F5
, K5
, in
[8], 14);
242 ROUND(ee
, aa
, bb
, cc
, dd
, F5
, K5
, in
[11], 11);
243 ROUND(dd
, ee
, aa
, bb
, cc
, F5
, K5
, in
[6], 8);
244 ROUND(cc
, dd
, ee
, aa
, bb
, F5
, K5
, in
[15], 5);
245 ROUND(bb
, cc
, dd
, ee
, aa
, F5
, K5
, in
[13], 6);
247 /* round 5: right lane */
248 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[12], 8);
249 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[15], 5);
250 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[10], 12);
251 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[4], 9);
252 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[1], 12);
253 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[5], 5);
254 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[8], 14);
255 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[7], 6);
256 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[6], 8);
257 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[2], 13);
258 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[13], 6);
259 ROUND(aaa
, bbb
, ccc
, ddd
, eee
, F1
, KK5
, in
[14], 5);
260 ROUND(eee
, aaa
, bbb
, ccc
, ddd
, F1
, KK5
, in
[0], 15);
261 ROUND(ddd
, eee
, aaa
, bbb
, ccc
, F1
, KK5
, in
[3], 13);
262 ROUND(ccc
, ddd
, eee
, aaa
, bbb
, F1
, KK5
, in
[9], 11);
263 ROUND(bbb
, ccc
, ddd
, eee
, aaa
, F1
, KK5
, in
[11], 11);
265 /* Swap contents of "e" registers */
266 tmp
= ee
; ee
= eee
; eee
= tmp
;
268 /* combine results */
283 static void rmd320_init(struct crypto_tfm
*tfm
)
285 struct rmd320_ctx
*rctx
= crypto_tfm_ctx(tfm
);
287 rctx
->byte_count
= 0;
289 rctx
->state
[0] = RMD_H0
;
290 rctx
->state
[1] = RMD_H1
;
291 rctx
->state
[2] = RMD_H2
;
292 rctx
->state
[3] = RMD_H3
;
293 rctx
->state
[4] = RMD_H4
;
294 rctx
->state
[5] = RMD_H5
;
295 rctx
->state
[6] = RMD_H6
;
296 rctx
->state
[7] = RMD_H7
;
297 rctx
->state
[8] = RMD_H8
;
298 rctx
->state
[9] = RMD_H9
;
300 memset(rctx
->buffer
, 0, sizeof(rctx
->buffer
));
303 static void rmd320_update(struct crypto_tfm
*tfm
, const u8
*data
,
306 struct rmd320_ctx
*rctx
= crypto_tfm_ctx(tfm
);
307 const u32 avail
= sizeof(rctx
->buffer
) - (rctx
->byte_count
& 0x3f);
309 rctx
->byte_count
+= len
;
311 /* Enough space in buffer? If so copy and we're done */
313 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
318 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
321 rmd320_transform(rctx
->state
, rctx
->buffer
);
325 while (len
>= sizeof(rctx
->buffer
)) {
326 memcpy(rctx
->buffer
, data
, sizeof(rctx
->buffer
));
327 rmd320_transform(rctx
->state
, rctx
->buffer
);
328 data
+= sizeof(rctx
->buffer
);
329 len
-= sizeof(rctx
->buffer
);
332 memcpy(rctx
->buffer
, data
, len
);
335 /* Add padding and return the message digest. */
336 static void rmd320_final(struct crypto_tfm
*tfm
, u8
*out
)
338 struct rmd320_ctx
*rctx
= crypto_tfm_ctx(tfm
);
339 u32 i
, index
, padlen
;
341 __le32
*dst
= (__le32
*)out
;
342 static const u8 padding
[64] = { 0x80, };
344 bits
= cpu_to_le64(rctx
->byte_count
<< 3);
346 /* Pad out to 56 mod 64 */
347 index
= rctx
->byte_count
& 0x3f;
348 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
349 rmd320_update(tfm
, padding
, padlen
);
352 rmd320_update(tfm
, (const u8
*)&bits
, sizeof(bits
));
354 /* Store state in digest */
355 for (i
= 0; i
< 10; i
++)
356 dst
[i
] = cpu_to_le32p(&rctx
->state
[i
]);
359 memset(rctx
, 0, sizeof(*rctx
));
362 static struct crypto_alg alg
= {
363 .cra_name
= "rmd320",
364 .cra_driver_name
= "rmd320",
365 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
366 .cra_blocksize
= RMD320_BLOCK_SIZE
,
367 .cra_ctxsize
= sizeof(struct rmd320_ctx
),
368 .cra_module
= THIS_MODULE
,
369 .cra_list
= LIST_HEAD_INIT(alg
.cra_list
),
370 .cra_u
= { .digest
= {
371 .dia_digestsize
= RMD320_DIGEST_SIZE
,
372 .dia_init
= rmd320_init
,
373 .dia_update
= rmd320_update
,
374 .dia_final
= rmd320_final
} }
377 static int __init
rmd320_mod_init(void)
379 return crypto_register_alg(&alg
);
382 static void __exit
rmd320_mod_fini(void)
384 crypto_unregister_alg(&alg
);
387 module_init(rmd320_mod_init
);
388 module_exit(rmd320_mod_fini
);
390 MODULE_LICENSE("GPL");
391 MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
393 MODULE_ALIAS("rmd320");