blob: 3c730e9de5fdf114bca17ae1df4fc309b95729cc [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +08002/*
3 * Cryptographic API.
4 *
5 * RIPEMD-256 - 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 Rueegseggerc555c282008-05-09 21:27:02 +080010 */
Herbert Xud8a5e2e2008-11-08 09:58:10 +080011#include <crypto/internal/hash.h>
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080012#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/mm.h>
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080015#include <linux/types.h>
16#include <asm/byteorder.h>
17
18#include "ripemd.h"
19
20struct rmd256_ctx {
21 u64 byte_count;
22 u32 state[8];
Harvey Harrisoncaee1682008-07-04 19:48:58 +080023 __le32 buffer[16];
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080024};
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) { \
Herbert Xu5cdcc222008-06-02 21:30:38 +100041 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080042 (a) = rol32((a), (s)); \
43}
44
Harvey Harrisoncaee1682008-07-04 19:48:58 +080045static void rmd256_transform(u32 *state, const __le32 *in)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080046{
Gustavo A. R. Silvad75f4822018-07-18 12:12:00 -050047 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080048
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[4];
57 bbb = state[5];
58 ccc = state[6];
59 ddd = state[7];
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 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);
96
97 /* Swap contents of "a" registers */
Gustavo A. R. Silvad75f4822018-07-18 12:12:00 -050098 swap(aa, aaa);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +080099
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);
117
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);
135
136 /* Swap contents of "b" registers */
Gustavo A. R. Silvad75f4822018-07-18 12:12:00 -0500137 swap(bb, bbb);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800138
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);
156
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);
174
175 /* Swap contents of "c" registers */
Gustavo A. R. Silvad75f4822018-07-18 12:12:00 -0500176 swap(cc, ccc);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800177
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);
195
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);
213
214 /* Swap contents of "d" registers */
Gustavo A. R. Silvad75f4822018-07-18 12:12:00 -0500215 swap(dd, ddd);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800216
217 /* combine results */
218 state[0] += aa;
219 state[1] += bb;
220 state[2] += cc;
221 state[3] += dd;
222 state[4] += aaa;
223 state[5] += bbb;
224 state[6] += ccc;
225 state[7] += ddd;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800226}
227
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800228static int rmd256_init(struct shash_desc *desc)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800229{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800230 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800231
232 rctx->byte_count = 0;
233
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;
242
243 memset(rctx->buffer, 0, sizeof(rctx->buffer));
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800244
245 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800246}
247
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800248static int rmd256_update(struct shash_desc *desc, const u8 *data,
249 unsigned int len)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800250{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800251 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800252 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
253
254 rctx->byte_count += len;
255
256 /* Enough space in buffer? If so copy and we're done */
257 if (avail > len) {
258 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
259 data, len);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800260 goto out;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800261 }
262
263 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
264 data, avail);
265
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000266 rmd256_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800267 data += avail;
268 len -= avail;
269
270 while (len >= sizeof(rctx->buffer)) {
271 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000272 rmd256_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800273 data += sizeof(rctx->buffer);
274 len -= sizeof(rctx->buffer);
275 }
276
277 memcpy(rctx->buffer, data, len);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800278
279out:
280 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800281}
282
283/* Add padding and return the message digest. */
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800284static int rmd256_final(struct shash_desc *desc, u8 *out)
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800285{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800286 struct rmd256_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000287 u32 i, index, padlen;
Harvey Harrisoncaee1682008-07-04 19:48:58 +0800288 __le64 bits;
289 __le32 *dst = (__le32 *)out;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800290 static const u8 padding[64] = { 0x80, };
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000291
292 bits = cpu_to_le64(rctx->byte_count << 3);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800293
294 /* Pad out to 56 mod 64 */
295 index = rctx->byte_count & 0x3f;
296 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800297 rmd256_update(desc, padding, padlen);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800298
299 /* Append length */
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800300 rmd256_update(desc, (const u8 *)&bits, sizeof(bits));
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800301
302 /* Store state in digest */
Adrian-Ken Rueegsegger2952ff12008-05-26 20:33:44 +1000303 for (i = 0; i < 8; i++)
Herbert Xu5cdcc222008-06-02 21:30:38 +1000304 dst[i] = cpu_to_le32p(&rctx->state[i]);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800305
306 /* Wipe context */
307 memset(rctx, 0, sizeof(*rctx));
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800308
309 return 0;
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800310}
311
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800312static struct shash_alg alg = {
313 .digestsize = RMD256_DIGEST_SIZE,
314 .init = rmd256_init,
315 .update = rmd256_update,
316 .final = rmd256_final,
317 .descsize = sizeof(struct rmd256_ctx),
318 .base = {
319 .cra_name = "rmd256",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700320 .cra_driver_name = "rmd256-generic",
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800321 .cra_blocksize = RMD256_BLOCK_SIZE,
322 .cra_module = THIS_MODULE,
323 }
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800324};
325
326static int __init rmd256_mod_init(void)
327{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800328 return crypto_register_shash(&alg);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800329}
330
331static void __exit rmd256_mod_fini(void)
332{
Herbert Xud8a5e2e2008-11-08 09:58:10 +0800333 crypto_unregister_shash(&alg);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800334}
335
Eric Biggersc4741b22019-04-11 21:57:42 -0700336subsys_initcall(rmd256_mod_init);
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800337module_exit(rmd256_mod_fini);
338
339MODULE_LICENSE("GPL");
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +1100340MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
Adrian-Ken Rueegseggerc555c282008-05-09 21:27:02 +0800341MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
Kees Cook5d26a102014-11-20 17:05:53 -0800342MODULE_ALIAS_CRYPTO("rmd256");