4 * RIPEMD-256 - 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>
41 #define F1(x, y, z) (x ^ y ^ z) /* XOR */
42 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
43 #define F3(x, y, z) ((x | ~y) ^ z)
44 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
46 #define ROUND(a, b, c, d, f, k, x, s) { \
47 (a) += f((b), (c), (d)) + (x) + (k); \
48 (a) = rol32((a), (s)); \
51 static void rmd256_transform(u32
*state
, u32
const *in
)
53 u32 aa
, bb
, cc
, dd
, aaa
, bbb
, ccc
, ddd
, tmp
;
55 /* Initialize left lane */
61 /* Initialize right lane */
67 /* round 1: left lane */
68 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[0], 11);
69 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[1], 14);
70 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[2], 15);
71 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[3], 12);
72 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[4], 5);
73 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[5], 8);
74 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[6], 7);
75 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[7], 9);
76 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[8], 11);
77 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[9], 13);
78 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[10], 14);
79 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[11], 15);
80 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[12], 6);
81 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[13], 7);
82 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[14], 9);
83 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[15], 8);
85 /* round 1: right lane */
86 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[5], 8);
87 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[14], 9);
88 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[7], 9);
89 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[0], 11);
90 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[9], 13);
91 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[2], 15);
92 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[11], 15);
93 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[4], 5);
94 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[13], 7);
95 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[6], 7);
96 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[15], 8);
97 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[8], 11);
98 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[1], 14);
99 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[10], 14);
100 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[3], 12);
101 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[12], 6);
103 /* Swap contents of "a" registers */
104 tmp
= aa
; aa
= aaa
; aaa
= tmp
;
106 /* round 2: left lane */
107 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[7], 7);
108 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[4], 6);
109 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[13], 8);
110 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[1], 13);
111 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[10], 11);
112 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[6], 9);
113 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[15], 7);
114 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[3], 15);
115 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[12], 7);
116 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[0], 12);
117 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[9], 15);
118 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[5], 9);
119 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[2], 11);
120 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[14], 7);
121 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[11], 13);
122 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[8], 12);
124 /* round 2: right lane */
125 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[6], 9);
126 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[11], 13);
127 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[3], 15);
128 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[7], 7);
129 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[0], 12);
130 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[13], 8);
131 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[5], 9);
132 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[10], 11);
133 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[14], 7);
134 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[15], 7);
135 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[8], 12);
136 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[12], 7);
137 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[4], 6);
138 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[9], 15);
139 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[1], 13);
140 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[2], 11);
142 /* Swap contents of "b" registers */
143 tmp
= bb
; bb
= bbb
; bbb
= tmp
;
145 /* round 3: left lane */
146 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[3], 11);
147 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[10], 13);
148 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[14], 6);
149 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[4], 7);
150 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[9], 14);
151 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[15], 9);
152 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[8], 13);
153 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[1], 15);
154 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[2], 14);
155 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[7], 8);
156 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[0], 13);
157 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[6], 6);
158 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[13], 5);
159 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[11], 12);
160 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[5], 7);
161 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[12], 5);
163 /* round 3: right lane */
164 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[15], 9);
165 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[5], 7);
166 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[1], 15);
167 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[3], 11);
168 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[7], 8);
169 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[14], 6);
170 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[6], 6);
171 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[9], 14);
172 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[11], 12);
173 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[8], 13);
174 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[12], 5);
175 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[2], 14);
176 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[10], 13);
177 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[0], 13);
178 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[4], 7);
179 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[13], 5);
181 /* Swap contents of "c" registers */
182 tmp
= cc
; cc
= ccc
; ccc
= tmp
;
184 /* round 4: left lane */
185 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[1], 11);
186 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[9], 12);
187 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[11], 14);
188 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[10], 15);
189 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[0], 14);
190 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[8], 15);
191 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[12], 9);
192 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[4], 8);
193 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[13], 9);
194 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[3], 14);
195 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[7], 5);
196 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[15], 6);
197 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[14], 8);
198 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[5], 6);
199 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[6], 5);
200 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[2], 12);
202 /* round 4: right lane */
203 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[8], 15);
204 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[6], 5);
205 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[4], 8);
206 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[1], 11);
207 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[3], 14);
208 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[11], 14);
209 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[15], 6);
210 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[0], 14);
211 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[5], 6);
212 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[12], 9);
213 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[2], 12);
214 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[13], 9);
215 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[9], 12);
216 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[7], 5);
217 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[10], 15);
218 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[14], 8);
220 /* Swap contents of "d" registers */
221 tmp
= dd
; dd
= ddd
; ddd
= tmp
;
223 /* combine results */
236 static inline void le32_to_cpu_array(u32
*buf
, unsigned int words
)
244 static inline void cpu_to_le32_array(u32
*buf
, unsigned int words
)
252 static inline void rmd256_transform_helper(struct rmd256_ctx
*ctx
)
254 le32_to_cpu_array(ctx
->buffer
, sizeof(ctx
->buffer
) / sizeof(u32
));
255 rmd256_transform(ctx
->state
, ctx
->buffer
);
258 static void rmd256_init(struct crypto_tfm
*tfm
)
260 struct rmd256_ctx
*rctx
= crypto_tfm_ctx(tfm
);
262 rctx
->byte_count
= 0;
264 rctx
->state
[0] = RMD_H0
;
265 rctx
->state
[1] = RMD_H1
;
266 rctx
->state
[2] = RMD_H2
;
267 rctx
->state
[3] = RMD_H3
;
268 rctx
->state
[4] = RMD_H5
;
269 rctx
->state
[5] = RMD_H6
;
270 rctx
->state
[6] = RMD_H7
;
271 rctx
->state
[7] = RMD_H8
;
273 memset(rctx
->buffer
, 0, sizeof(rctx
->buffer
));
276 static void rmd256_update(struct crypto_tfm
*tfm
, const u8
*data
,
279 struct rmd256_ctx
*rctx
= crypto_tfm_ctx(tfm
);
280 const u32 avail
= sizeof(rctx
->buffer
) - (rctx
->byte_count
& 0x3f);
282 rctx
->byte_count
+= len
;
284 /* Enough space in buffer? If so copy and we're done */
286 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
291 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
294 rmd256_transform_helper(rctx
);
298 while (len
>= sizeof(rctx
->buffer
)) {
299 memcpy(rctx
->buffer
, data
, sizeof(rctx
->buffer
));
300 rmd256_transform_helper(rctx
);
301 data
+= sizeof(rctx
->buffer
);
302 len
-= sizeof(rctx
->buffer
);
305 memcpy(rctx
->buffer
, data
, len
);
308 /* Add padding and return the message digest. */
309 static void rmd256_final(struct crypto_tfm
*tfm
, u8
*out
)
311 struct rmd256_ctx
*rctx
= crypto_tfm_ctx(tfm
);
314 static const u8 padding
[64] = { 0x80, };
315 bits
= rctx
->byte_count
<< 3;
317 /* Pad out to 56 mod 64 */
318 index
= rctx
->byte_count
& 0x3f;
319 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
320 rmd256_update(tfm
, padding
, padlen
);
323 rmd256_update(tfm
, (const u8
*)&bits
, sizeof(bits
));
325 /* Store state in digest */
326 memcpy(out
, rctx
->state
, sizeof(rctx
->state
));
329 memset(rctx
, 0, sizeof(*rctx
));
332 static struct crypto_alg alg
= {
333 .cra_name
= "rmd256",
334 .cra_driver_name
= "rmd256",
335 .cra_flags
= CRYPTO_ALG_TYPE_DIGEST
,
336 .cra_blocksize
= RMD256_BLOCK_SIZE
,
337 .cra_ctxsize
= sizeof(struct rmd256_ctx
),
338 .cra_module
= THIS_MODULE
,
339 .cra_list
= LIST_HEAD_INIT(alg
.cra_list
),
340 .cra_u
= { .digest
= {
341 .dia_digestsize
= RMD256_DIGEST_SIZE
,
342 .dia_init
= rmd256_init
,
343 .dia_update
= rmd256_update
,
344 .dia_final
= rmd256_final
} }
347 static int __init
rmd256_mod_init(void)
349 return crypto_register_alg(&alg
);
352 static void __exit
rmd256_mod_fini(void)
354 crypto_unregister_alg(&alg
);
357 module_init(rmd256_mod_init
);
358 module_exit(rmd256_mod_fini
);
360 MODULE_LICENSE("GPL");
361 MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
363 MODULE_ALIAS("rmd256");