1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API.
4 *
5 * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
6 *
7 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
8 *
9 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
10 */
11 #include <crypto/internal/hash.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/types.h>
16 #include <asm/byteorder.h>
17
18 #include "ripemd.h"
19
20 struct rmd128_ctx {
21 u64 byte_count;
22 u32 state[4];
23 __le32 buffer[16];
24 };
25
26 #define K1 RMD_K1
27 #define K2 RMD_K2
28 #define K3 RMD_K3
29 #define K4 RMD_K4
30 #define KK1 RMD_K6
31 #define KK2 RMD_K7
32 #define KK3 RMD_K8
33 #define KK4 RMD_K1
34
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 */
39
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)); \
43 }
44
rmd128_transform(u32 * state,const __le32 * in)45 static void rmd128_transform(u32 *state, const __le32 *in)
46 {
47 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
48
49 /* Initialize left lane */
50 aa = state[0];
51 bb = state[1];
52 cc = state[2];
53 dd = state[3];
54
55 /* Initialize right lane */
56 aaa = state[0];
57 bbb = state[1];
58 ccc = state[2];
59 ddd = state[3];
60
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);
78
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);
96
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);
114
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);
132
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);
150
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);
168
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);
186
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);
204
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;
210 state[0] = ddd;
211 }
212
rmd128_init(struct shash_desc * desc)213 static int rmd128_init(struct shash_desc *desc)
214 {
215 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
216
217 rctx->byte_count = 0;
218
219 rctx->state[0] = RMD_H0;
220 rctx->state[1] = RMD_H1;
221 rctx->state[2] = RMD_H2;
222 rctx->state[3] = RMD_H3;
223
224 memset(rctx->buffer, 0, sizeof(rctx->buffer));
225
226 return 0;
227 }
228
rmd128_update(struct shash_desc * desc,const u8 * data,unsigned int len)229 static int rmd128_update(struct shash_desc *desc, const u8 *data,
230 unsigned int len)
231 {
232 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
233 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
234
235 rctx->byte_count += len;
236
237 /* Enough space in buffer? If so copy and we're done */
238 if (avail > len) {
239 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
240 data, len);
241 goto out;
242 }
243
244 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
245 data, avail);
246
247 rmd128_transform(rctx->state, rctx->buffer);
248 data += avail;
249 len -= avail;
250
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);
256 }
257
258 memcpy(rctx->buffer, data, len);
259
260 out:
261 return 0;
262 }
263
264 /* Add padding and return the message digest. */
rmd128_final(struct shash_desc * desc,u8 * out)265 static int rmd128_final(struct shash_desc *desc, u8 *out)
266 {
267 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
268 u32 i, index, padlen;
269 __le64 bits;
270 __le32 *dst = (__le32 *)out;
271 static const u8 padding[64] = { 0x80, };
272
273 bits = cpu_to_le64(rctx->byte_count << 3);
274
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);
279
280 /* Append length */
281 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
282
283 /* Store state in digest */
284 for (i = 0; i < 4; i++)
285 dst[i] = cpu_to_le32p(&rctx->state[i]);
286
287 /* Wipe context */
288 memset(rctx, 0, sizeof(*rctx));
289
290 return 0;
291 }
292
293 static struct shash_alg alg = {
294 .digestsize = RMD128_DIGEST_SIZE,
295 .init = rmd128_init,
296 .update = rmd128_update,
297 .final = rmd128_final,
298 .descsize = sizeof(struct rmd128_ctx),
299 .base = {
300 .cra_name = "rmd128",
301 .cra_driver_name = "rmd128-generic",
302 .cra_blocksize = RMD128_BLOCK_SIZE,
303 .cra_module = THIS_MODULE,
304 }
305 };
306
rmd128_mod_init(void)307 static int __init rmd128_mod_init(void)
308 {
309 return crypto_register_shash(&alg);
310 }
311
rmd128_mod_fini(void)312 static void __exit rmd128_mod_fini(void)
313 {
314 crypto_unregister_shash(&alg);
315 }
316
317 subsys_initcall(rmd128_mod_init);
318 module_exit(rmd128_mod_fini);
319
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");
324