blob: 29308fb97e7e695c9e332b6ff2da1dd694f0413a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +08002/*
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 *
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +11009 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080010 */
Herbert Xu7c4468b2008-11-08 09:10:40 +080011#include <crypto/internal/hash.h>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080012#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/mm.h>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080015#include <linux/types.h>
16#include <asm/byteorder.h>
17
18#include "ripemd.h"
19
20struct rmd128_ctx {
21 u64 byte_count;
22 u32 state[4];
Harvey Harrisonb2b6a172008-07-04 19:45:57 +080023 __le32 buffer[16];
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080024};
25
Adrian-Ken Rueegseggerba6b0392008-05-09 21:25:42 +080026#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
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080034
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) { \
Herbert Xu5cdcc222008-06-02 21:30:38 +100041 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080042 (a) = rol32((a), (s)); \
43}
44
Harvey Harrisonb2b6a172008-07-04 19:45:57 +080045static void rmd128_transform(u32 *state, const __le32 *in)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080046{
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;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800211}
212
Herbert Xu7c4468b2008-11-08 09:10:40 +0800213static int rmd128_init(struct shash_desc *desc)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800214{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800215 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800216
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));
Herbert Xu7c4468b2008-11-08 09:10:40 +0800225
226 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800227}
228
Herbert Xu7c4468b2008-11-08 09:10:40 +0800229static int rmd128_update(struct shash_desc *desc, const u8 *data,
230 unsigned int len)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800231{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800232 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800233 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);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800241 goto out;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800242 }
243
244 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
245 data, avail);
246
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800247 rmd128_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800248 data += avail;
249 len -= avail;
250
251 while (len >= sizeof(rctx->buffer)) {
252 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800253 rmd128_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800254 data += sizeof(rctx->buffer);
255 len -= sizeof(rctx->buffer);
256 }
257
258 memcpy(rctx->buffer, data, len);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800259
260out:
261 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800262}
263
264/* Add padding and return the message digest. */
Herbert Xu7c4468b2008-11-08 09:10:40 +0800265static int rmd128_final(struct shash_desc *desc, u8 *out)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800266{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800267 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800268 u32 i, index, padlen;
Harvey Harrisonb2b6a172008-07-04 19:45:57 +0800269 __le64 bits;
270 __le32 *dst = (__le32 *)out;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800271 static const u8 padding[64] = { 0x80, };
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800272
273 bits = cpu_to_le64(rctx->byte_count << 3);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800274
275 /* Pad out to 56 mod 64 */
276 index = rctx->byte_count & 0x3f;
277 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800278 rmd128_update(desc, padding, padlen);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800279
280 /* Append length */
Herbert Xu7c4468b2008-11-08 09:10:40 +0800281 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800282
283 /* Store state in digest */
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800284 for (i = 0; i < 4; i++)
Herbert Xu5cdcc222008-06-02 21:30:38 +1000285 dst[i] = cpu_to_le32p(&rctx->state[i]);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800286
287 /* Wipe context */
288 memset(rctx, 0, sizeof(*rctx));
Herbert Xu7c4468b2008-11-08 09:10:40 +0800289
290 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800291}
292
Herbert Xu7c4468b2008-11-08 09:10:40 +0800293static 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",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700301 .cra_driver_name = "rmd128-generic",
Herbert Xu7c4468b2008-11-08 09:10:40 +0800302 .cra_blocksize = RMD128_BLOCK_SIZE,
303 .cra_module = THIS_MODULE,
304 }
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800305};
306
307static int __init rmd128_mod_init(void)
308{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800309 return crypto_register_shash(&alg);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800310}
311
312static void __exit rmd128_mod_fini(void)
313{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800314 crypto_unregister_shash(&alg);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800315}
316
Eric Biggersc4741b22019-04-11 21:57:42 -0700317subsys_initcall(rmd128_mod_init);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800318module_exit(rmd128_mod_fini);
319
320MODULE_LICENSE("GPL");
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +1100321MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800322MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
Kees Cook5d26a102014-11-20 17:05:53 -0800323MODULE_ALIAS_CRYPTO("rmd128");