1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
7 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
9 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <asm/byteorder.h>
35 #define F1(x, y, z) (x ^ y ^ z) /* XOR */
36 #define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
37 #define F3(x, y, z) ((x | ~y) ^ z)
38 #define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
40 #define ROUND(a, b, c, d, f, k, x, s) { \
41 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
42 (a) = rol32((a), (s)); \
45 static void rmd256_transform(u32
*state
, const __le32
*in
)
47 u32 aa
, bb
, cc
, dd
, aaa
, bbb
, ccc
, ddd
;
49 /* Initialize left lane */
55 /* Initialize right lane */
61 /* round 1: left lane */
62 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[0], 11);
63 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[1], 14);
64 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[2], 15);
65 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[3], 12);
66 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[4], 5);
67 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[5], 8);
68 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[6], 7);
69 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[7], 9);
70 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[8], 11);
71 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[9], 13);
72 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[10], 14);
73 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[11], 15);
74 ROUND(aa
, bb
, cc
, dd
, F1
, K1
, in
[12], 6);
75 ROUND(dd
, aa
, bb
, cc
, F1
, K1
, in
[13], 7);
76 ROUND(cc
, dd
, aa
, bb
, F1
, K1
, in
[14], 9);
77 ROUND(bb
, cc
, dd
, aa
, F1
, K1
, in
[15], 8);
79 /* round 1: right lane */
80 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[5], 8);
81 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[14], 9);
82 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[7], 9);
83 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[0], 11);
84 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[9], 13);
85 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[2], 15);
86 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[11], 15);
87 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[4], 5);
88 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[13], 7);
89 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[6], 7);
90 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[15], 8);
91 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[8], 11);
92 ROUND(aaa
, bbb
, ccc
, ddd
, F4
, KK1
, in
[1], 14);
93 ROUND(ddd
, aaa
, bbb
, ccc
, F4
, KK1
, in
[10], 14);
94 ROUND(ccc
, ddd
, aaa
, bbb
, F4
, KK1
, in
[3], 12);
95 ROUND(bbb
, ccc
, ddd
, aaa
, F4
, KK1
, in
[12], 6);
97 /* Swap contents of "a" registers */
100 /* round 2: left lane */
101 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[7], 7);
102 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[4], 6);
103 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[13], 8);
104 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[1], 13);
105 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[10], 11);
106 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[6], 9);
107 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[15], 7);
108 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[3], 15);
109 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[12], 7);
110 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[0], 12);
111 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[9], 15);
112 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[5], 9);
113 ROUND(aa
, bb
, cc
, dd
, F2
, K2
, in
[2], 11);
114 ROUND(dd
, aa
, bb
, cc
, F2
, K2
, in
[14], 7);
115 ROUND(cc
, dd
, aa
, bb
, F2
, K2
, in
[11], 13);
116 ROUND(bb
, cc
, dd
, aa
, F2
, K2
, in
[8], 12);
118 /* round 2: right lane */
119 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[6], 9);
120 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[11], 13);
121 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[3], 15);
122 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[7], 7);
123 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[0], 12);
124 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[13], 8);
125 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[5], 9);
126 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[10], 11);
127 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[14], 7);
128 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[15], 7);
129 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[8], 12);
130 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[12], 7);
131 ROUND(aaa
, bbb
, ccc
, ddd
, F3
, KK2
, in
[4], 6);
132 ROUND(ddd
, aaa
, bbb
, ccc
, F3
, KK2
, in
[9], 15);
133 ROUND(ccc
, ddd
, aaa
, bbb
, F3
, KK2
, in
[1], 13);
134 ROUND(bbb
, ccc
, ddd
, aaa
, F3
, KK2
, in
[2], 11);
136 /* Swap contents of "b" registers */
139 /* round 3: left lane */
140 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[3], 11);
141 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[10], 13);
142 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[14], 6);
143 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[4], 7);
144 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[9], 14);
145 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[15], 9);
146 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[8], 13);
147 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[1], 15);
148 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[2], 14);
149 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[7], 8);
150 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[0], 13);
151 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[6], 6);
152 ROUND(aa
, bb
, cc
, dd
, F3
, K3
, in
[13], 5);
153 ROUND(dd
, aa
, bb
, cc
, F3
, K3
, in
[11], 12);
154 ROUND(cc
, dd
, aa
, bb
, F3
, K3
, in
[5], 7);
155 ROUND(bb
, cc
, dd
, aa
, F3
, K3
, in
[12], 5);
157 /* round 3: right lane */
158 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[15], 9);
159 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[5], 7);
160 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[1], 15);
161 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[3], 11);
162 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[7], 8);
163 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[14], 6);
164 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[6], 6);
165 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[9], 14);
166 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[11], 12);
167 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[8], 13);
168 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[12], 5);
169 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[2], 14);
170 ROUND(aaa
, bbb
, ccc
, ddd
, F2
, KK3
, in
[10], 13);
171 ROUND(ddd
, aaa
, bbb
, ccc
, F2
, KK3
, in
[0], 13);
172 ROUND(ccc
, ddd
, aaa
, bbb
, F2
, KK3
, in
[4], 7);
173 ROUND(bbb
, ccc
, ddd
, aaa
, F2
, KK3
, in
[13], 5);
175 /* Swap contents of "c" registers */
178 /* round 4: left lane */
179 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[1], 11);
180 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[9], 12);
181 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[11], 14);
182 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[10], 15);
183 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[0], 14);
184 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[8], 15);
185 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[12], 9);
186 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[4], 8);
187 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[13], 9);
188 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[3], 14);
189 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[7], 5);
190 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[15], 6);
191 ROUND(aa
, bb
, cc
, dd
, F4
, K4
, in
[14], 8);
192 ROUND(dd
, aa
, bb
, cc
, F4
, K4
, in
[5], 6);
193 ROUND(cc
, dd
, aa
, bb
, F4
, K4
, in
[6], 5);
194 ROUND(bb
, cc
, dd
, aa
, F4
, K4
, in
[2], 12);
196 /* round 4: right lane */
197 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[8], 15);
198 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[6], 5);
199 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[4], 8);
200 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[1], 11);
201 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[3], 14);
202 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[11], 14);
203 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[15], 6);
204 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[0], 14);
205 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[5], 6);
206 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[12], 9);
207 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[2], 12);
208 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[13], 9);
209 ROUND(aaa
, bbb
, ccc
, ddd
, F1
, KK4
, in
[9], 12);
210 ROUND(ddd
, aaa
, bbb
, ccc
, F1
, KK4
, in
[7], 5);
211 ROUND(ccc
, ddd
, aaa
, bbb
, F1
, KK4
, in
[10], 15);
212 ROUND(bbb
, ccc
, ddd
, aaa
, F1
, KK4
, in
[14], 8);
214 /* Swap contents of "d" registers */
217 /* combine results */
228 static int rmd256_init(struct shash_desc
*desc
)
230 struct rmd256_ctx
*rctx
= shash_desc_ctx(desc
);
232 rctx
->byte_count
= 0;
234 rctx
->state
[0] = RMD_H0
;
235 rctx
->state
[1] = RMD_H1
;
236 rctx
->state
[2] = RMD_H2
;
237 rctx
->state
[3] = RMD_H3
;
238 rctx
->state
[4] = RMD_H5
;
239 rctx
->state
[5] = RMD_H6
;
240 rctx
->state
[6] = RMD_H7
;
241 rctx
->state
[7] = RMD_H8
;
243 memset(rctx
->buffer
, 0, sizeof(rctx
->buffer
));
248 static int rmd256_update(struct shash_desc
*desc
, const u8
*data
,
251 struct rmd256_ctx
*rctx
= shash_desc_ctx(desc
);
252 const u32 avail
= sizeof(rctx
->buffer
) - (rctx
->byte_count
& 0x3f);
254 rctx
->byte_count
+= len
;
256 /* Enough space in buffer? If so copy and we're done */
258 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
263 memcpy((char *)rctx
->buffer
+ (sizeof(rctx
->buffer
) - avail
),
266 rmd256_transform(rctx
->state
, rctx
->buffer
);
270 while (len
>= sizeof(rctx
->buffer
)) {
271 memcpy(rctx
->buffer
, data
, sizeof(rctx
->buffer
));
272 rmd256_transform(rctx
->state
, rctx
->buffer
);
273 data
+= sizeof(rctx
->buffer
);
274 len
-= sizeof(rctx
->buffer
);
277 memcpy(rctx
->buffer
, data
, len
);
283 /* Add padding and return the message digest. */
284 static int rmd256_final(struct shash_desc
*desc
, u8
*out
)
286 struct rmd256_ctx
*rctx
= shash_desc_ctx(desc
);
287 u32 i
, index
, padlen
;
289 __le32
*dst
= (__le32
*)out
;
290 static const u8 padding
[64] = { 0x80, };
292 bits
= cpu_to_le64(rctx
->byte_count
<< 3);
294 /* Pad out to 56 mod 64 */
295 index
= rctx
->byte_count
& 0x3f;
296 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
297 rmd256_update(desc
, padding
, padlen
);
300 rmd256_update(desc
, (const u8
*)&bits
, sizeof(bits
));
302 /* Store state in digest */
303 for (i
= 0; i
< 8; i
++)
304 dst
[i
] = cpu_to_le32p(&rctx
->state
[i
]);
307 memset(rctx
, 0, sizeof(*rctx
));
312 static struct shash_alg alg
= {
313 .digestsize
= RMD256_DIGEST_SIZE
,
315 .update
= rmd256_update
,
316 .final
= rmd256_final
,
317 .descsize
= sizeof(struct rmd256_ctx
),
319 .cra_name
= "rmd256",
320 .cra_blocksize
= RMD256_BLOCK_SIZE
,
321 .cra_module
= THIS_MODULE
,
325 static int __init
rmd256_mod_init(void)
327 return crypto_register_shash(&alg
);
330 static void __exit
rmd256_mod_fini(void)
332 crypto_unregister_shash(&alg
);
335 subsys_initcall(rmd256_mod_init
);
336 module_exit(rmd256_mod_fini
);
338 MODULE_LICENSE("GPL");
339 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
340 MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
341 MODULE_ALIAS_CRYPTO("rmd256");