blob: 40e053b97b697cfa4eeda4e7935b1932fde13156 [file] [log] [blame]
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +08001/*
2 * Cryptographic API.
3 *
4 * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
5 *
6 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
7 *
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +11008 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +08009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 */
Herbert Xu7c4468b2008-11-08 09:10:40 +080016#include <crypto/internal/hash.h>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080017#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080020#include <linux/types.h>
21#include <asm/byteorder.h>
22
23#include "ripemd.h"
24
25struct rmd128_ctx {
26 u64 byte_count;
27 u32 state[4];
Harvey Harrisonb2b6a172008-07-04 19:45:57 +080028 __le32 buffer[16];
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080029};
30
Adrian-Ken Rueegseggerba6b0392008-05-09 21:25:42 +080031#define K1 RMD_K1
32#define K2 RMD_K2
33#define K3 RMD_K3
34#define K4 RMD_K4
35#define KK1 RMD_K6
36#define KK2 RMD_K7
37#define KK3 RMD_K8
38#define KK4 RMD_K1
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080039
40#define F1(x, y, z) (x ^ y ^ z) /* XOR */
41#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
42#define F3(x, y, z) ((x | ~y) ^ z)
43#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
44
45#define ROUND(a, b, c, d, f, k, x, s) { \
Herbert Xu5cdcc222008-06-02 21:30:38 +100046 (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080047 (a) = rol32((a), (s)); \
48}
49
Harvey Harrisonb2b6a172008-07-04 19:45:57 +080050static void rmd128_transform(u32 *state, const __le32 *in)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +080051{
52 u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
53
54 /* Initialize left lane */
55 aa = state[0];
56 bb = state[1];
57 cc = state[2];
58 dd = state[3];
59
60 /* Initialize right lane */
61 aaa = state[0];
62 bbb = state[1];
63 ccc = state[2];
64 ddd = state[3];
65
66 /* round 1: left lane */
67 ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
68 ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
69 ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
70 ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
71 ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
72 ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
73 ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
74 ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
75 ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
76 ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
77 ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
78 ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
79 ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
80 ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
81 ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
82 ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
83
84 /* round 2: left lane */
85 ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
86 ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
87 ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
88 ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
89 ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
90 ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
91 ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
92 ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
93 ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
94 ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
95 ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
96 ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
97 ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
98 ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
99 ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
100 ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
101
102 /* round 3: left lane */
103 ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
104 ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
105 ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
106 ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
107 ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
108 ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
109 ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
110 ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
111 ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
112 ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
113 ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
114 ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
115 ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
116 ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
117 ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
118 ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
119
120 /* round 4: left lane */
121 ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
122 ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
123 ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
124 ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
125 ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
126 ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
127 ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
128 ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
129 ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
130 ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
131 ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
132 ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
133 ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
134 ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
135 ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
136 ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
137
138 /* round 1: right lane */
139 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
140 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
141 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
142 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
143 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
144 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
145 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
146 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
147 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
148 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
149 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
150 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
151 ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
152 ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
153 ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
154 ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
155
156 /* round 2: right lane */
157 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
158 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
159 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
160 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
161 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
162 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
163 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
164 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
165 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
166 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
167 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
168 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
169 ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
170 ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
171 ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
172 ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
173
174 /* round 3: right lane */
175 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
176 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
177 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
178 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
179 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
180 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
181 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
182 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
183 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
184 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
185 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
186 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
187 ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
188 ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
189 ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
190 ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
191
192 /* round 4: right lane */
193 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
194 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
195 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
196 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
197 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
198 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
199 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
200 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
201 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
202 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
203 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
204 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
205 ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
206 ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
207 ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
208 ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
209
210 /* combine results */
211 ddd += cc + state[1]; /* final result for state[0] */
212 state[1] = state[2] + dd + aaa;
213 state[2] = state[3] + aa + bbb;
214 state[3] = state[0] + bb + ccc;
215 state[0] = ddd;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800216}
217
Herbert Xu7c4468b2008-11-08 09:10:40 +0800218static int rmd128_init(struct shash_desc *desc)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800219{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800220 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800221
222 rctx->byte_count = 0;
223
224 rctx->state[0] = RMD_H0;
225 rctx->state[1] = RMD_H1;
226 rctx->state[2] = RMD_H2;
227 rctx->state[3] = RMD_H3;
228
229 memset(rctx->buffer, 0, sizeof(rctx->buffer));
Herbert Xu7c4468b2008-11-08 09:10:40 +0800230
231 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800232}
233
Herbert Xu7c4468b2008-11-08 09:10:40 +0800234static int rmd128_update(struct shash_desc *desc, const u8 *data,
235 unsigned int len)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800236{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800237 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800238 const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
239
240 rctx->byte_count += len;
241
242 /* Enough space in buffer? If so copy and we're done */
243 if (avail > len) {
244 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
245 data, len);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800246 goto out;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800247 }
248
249 memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
250 data, avail);
251
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800252 rmd128_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800253 data += avail;
254 len -= avail;
255
256 while (len >= sizeof(rctx->buffer)) {
257 memcpy(rctx->buffer, data, sizeof(rctx->buffer));
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800258 rmd128_transform(rctx->state, rctx->buffer);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800259 data += sizeof(rctx->buffer);
260 len -= sizeof(rctx->buffer);
261 }
262
263 memcpy(rctx->buffer, data, len);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800264
265out:
266 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800267}
268
269/* Add padding and return the message digest. */
Herbert Xu7c4468b2008-11-08 09:10:40 +0800270static int rmd128_final(struct shash_desc *desc, u8 *out)
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800271{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800272 struct rmd128_ctx *rctx = shash_desc_ctx(desc);
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800273 u32 i, index, padlen;
Harvey Harrisonb2b6a172008-07-04 19:45:57 +0800274 __le64 bits;
275 __le32 *dst = (__le32 *)out;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800276 static const u8 padding[64] = { 0x80, };
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800277
278 bits = cpu_to_le64(rctx->byte_count << 3);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800279
280 /* Pad out to 56 mod 64 */
281 index = rctx->byte_count & 0x3f;
282 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
Herbert Xu7c4468b2008-11-08 09:10:40 +0800283 rmd128_update(desc, padding, padlen);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800284
285 /* Append length */
Herbert Xu7c4468b2008-11-08 09:10:40 +0800286 rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800287
288 /* Store state in digest */
Adrian-Ken Rueegsegger0f923a22008-05-20 11:41:48 +0800289 for (i = 0; i < 4; i++)
Herbert Xu5cdcc222008-06-02 21:30:38 +1000290 dst[i] = cpu_to_le32p(&rctx->state[i]);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800291
292 /* Wipe context */
293 memset(rctx, 0, sizeof(*rctx));
Herbert Xu7c4468b2008-11-08 09:10:40 +0800294
295 return 0;
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800296}
297
Herbert Xu7c4468b2008-11-08 09:10:40 +0800298static struct shash_alg alg = {
299 .digestsize = RMD128_DIGEST_SIZE,
300 .init = rmd128_init,
301 .update = rmd128_update,
302 .final = rmd128_final,
303 .descsize = sizeof(struct rmd128_ctx),
304 .base = {
305 .cra_name = "rmd128",
306 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
307 .cra_blocksize = RMD128_BLOCK_SIZE,
308 .cra_module = THIS_MODULE,
309 }
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800310};
311
312static int __init rmd128_mod_init(void)
313{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800314 return crypto_register_shash(&alg);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800315}
316
317static void __exit rmd128_mod_fini(void)
318{
Herbert Xu7c4468b2008-11-08 09:10:40 +0800319 crypto_unregister_shash(&alg);
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800320}
321
322module_init(rmd128_mod_init);
323module_exit(rmd128_mod_fini);
324
325MODULE_LICENSE("GPL");
Adrian-Ken Rueegsegger3181c222011-01-04 15:35:51 +1100326MODULE_AUTHOR("Adrian-Ken Rueegsegger <ken@codelabs.ch>");
Adrian-Ken Rueegseggerc6580eb2008-05-07 22:14:10 +0800327MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
Kees Cook5d26a102014-11-20 17:05:53 -0800328MODULE_ALIAS_CRYPTO("rmd128");