1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * RIPEMD-128 - 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 rmd128_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 2: left lane */
80 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
81 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
82 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
83 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
84 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
85 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
86 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
87 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
88 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
89 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
90 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
91 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
92 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
93 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
94 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
95 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
97 /* round 3: left lane */
98 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
99 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
100 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
101 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
102 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
103 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
104 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
105 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
106 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
107 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
108 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
109 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
110 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
111 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
112 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
113 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
115 /* round 4: left lane */
116 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
117 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
118 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
119 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
120 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
121 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
122 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
123 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
124 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
125 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
126 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
127 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
128 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
129 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
130 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
131 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
133 /* round 1: right lane */
134 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
135 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
136 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
137 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
138 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
139 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
140 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
141 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
142 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
143 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
144 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
145 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
146 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
147 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
148 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
149 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
151 /* round 2: right lane */
152 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
153 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
154 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
155 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
156 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
157 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
158 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
159 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
160 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
161 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
162 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
163 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
164 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
165 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
166 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
167 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
169 /* round 3: right lane */
170 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
171 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
172 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
173 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
174 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
175 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
176 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
177 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
178 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
179 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
180 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
181 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
182 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
183 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
184 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
185 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
187 /* round 4: right lane */
188 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
189 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
190 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
191 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
192 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
193 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
194 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
195 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
196 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
197 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
198 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
199 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
200 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
201 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
202 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
203 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
205 /* combine results */
206 ddd += cc + state[1]; /* final result for state[0] */
207 state[1] = state[2] + dd + aaa;
208 state[2] = state[3] + aa + bbb;
209 state[3] = state[0] + bb + ccc;
213 static int rmd128_init(struct shash_desc *desc)
215 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
217 rctx->byte_count = 0;
219 rctx->state[0] = RMD_H0;
220 rctx->state[1] = RMD_H1;
221 rctx->state[2] = RMD_H2;
222 rctx->state[3] = RMD_H3;
224 memset(rctx->buffer, 0, sizeof(rctx->buffer));
229 static int rmd128_update(struct shash_desc *desc, const u8 *data,
232 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
233 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
235 rctx->byte_count += len;
237 /* Enough space in buffer? If so copy and we're done */
239 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
244 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
247 rmd128_transform(rctx->state, rctx->buffer);
251 while (len >= sizeof(rctx->buffer)) {
252 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
253 rmd128_transform(rctx->state, rctx->buffer);
254 data += sizeof(rctx->buffer);
255 len -= sizeof(rctx->buffer);
258 memcpy(rctx->buffer, data, len);
264 /* Add padding and return the message digest. */
265 static int rmd128_final(struct shash_desc *desc, u8 *out)
267 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
268 u32 i, index, padlen;
270 __le32 *dst = (__le32 *)out;
271 static const u8 padding[64] = { 0x80, };
273 bits = cpu_to_le64(rctx->byte_count << 3);
275 /* Pad out to 56 mod 64 */
276 index = rctx->byte_count & 0x3f;
277 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
278 rmd128_update(desc, padding, padlen);
281 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
283 /* Store state in digest */
284 for (i = 0; i < 4; i++)
285 dst[i] = cpu_to_le32p(&rctx->state[i]);
288 memset(rctx, 0, sizeof(*rctx));
293 static struct shash_alg alg = {
294 .digestsize = RMD128_DIGEST_SIZE,
296 .update = rmd128_update,
297 .final = rmd128_final,
298 .descsize = sizeof(struct rmd128_ctx),
300 .cra_name = "rmd128",
301 .cra_driver_name = "rmd128-generic",
302 .cra_blocksize = RMD128_BLOCK_SIZE,
303 .cra_module = THIS_MODULE,
307 static int __init rmd128_mod_init(void)
309 return crypto_register_shash(&alg);
312 static void __exit rmd128_mod_fini(void)
314 crypto_unregister_shash(&alg);
317 subsys_initcall(rmd128_mod_init);
318 module_exit(rmd128_mod_fini);
320 MODULE_LICENSE("GPL");
321 MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
322 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
323 MODULE_ALIAS_CRYPTO("rmd128");