Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rmd128.c
Go to the documentation of this file.
1 /*
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  *
8  * Copyright (c) 2008 Adrian-Ken Rueegsegger <[email protected]>
9  *
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  */
16 #include <crypto/internal/hash.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/types.h>
21 #include <asm/byteorder.h>
22 
23 #include "ripemd.h"
24 
25 struct rmd128_ctx {
27  u32 state[4];
29 };
30 
31 #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
39 
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) { \
46  (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
47  (a) = rol32((a), (s)); \
48 }
49 
50 static void rmd128_transform(u32 *state, const __le32 *in)
51 {
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;
216 
217  return;
218 }
219 
220 static int rmd128_init(struct shash_desc *desc)
221 {
222  struct rmd128_ctx *rctx = shash_desc_ctx(desc);
223 
224  rctx->byte_count = 0;
225 
226  rctx->state[0] = RMD_H0;
227  rctx->state[1] = RMD_H1;
228  rctx->state[2] = RMD_H2;
229  rctx->state[3] = RMD_H3;
230 
231  memset(rctx->buffer, 0, sizeof(rctx->buffer));
232 
233  return 0;
234 }
235 
236 static int rmd128_update(struct shash_desc *desc, const u8 *data,
237  unsigned int len)
238 {
239  struct rmd128_ctx *rctx = shash_desc_ctx(desc);
240  const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
241 
242  rctx->byte_count += len;
243 
244  /* Enough space in buffer? If so copy and we're done */
245  if (avail > len) {
246  memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
247  data, len);
248  goto out;
249  }
250 
251  memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
252  data, avail);
253 
254  rmd128_transform(rctx->state, rctx->buffer);
255  data += avail;
256  len -= avail;
257 
258  while (len >= sizeof(rctx->buffer)) {
259  memcpy(rctx->buffer, data, sizeof(rctx->buffer));
260  rmd128_transform(rctx->state, rctx->buffer);
261  data += sizeof(rctx->buffer);
262  len -= sizeof(rctx->buffer);
263  }
264 
265  memcpy(rctx->buffer, data, len);
266 
267 out:
268  return 0;
269 }
270 
271 /* Add padding and return the message digest. */
272 static int rmd128_final(struct shash_desc *desc, u8 *out)
273 {
274  struct rmd128_ctx *rctx = shash_desc_ctx(desc);
275  u32 i, index, padlen;
276  __le64 bits;
277  __le32 *dst = (__le32 *)out;
278  static const u8 padding[64] = { 0x80, };
279 
280  bits = cpu_to_le64(rctx->byte_count << 3);
281 
282  /* Pad out to 56 mod 64 */
283  index = rctx->byte_count & 0x3f;
284  padlen = (index < 56) ? (56 - index) : ((64+56) - index);
285  rmd128_update(desc, padding, padlen);
286 
287  /* Append length */
288  rmd128_update(desc, (const u8 *)&bits, sizeof(bits));
289 
290  /* Store state in digest */
291  for (i = 0; i < 4; i++)
292  dst[i] = cpu_to_le32p(&rctx->state[i]);
293 
294  /* Wipe context */
295  memset(rctx, 0, sizeof(*rctx));
296 
297  return 0;
298 }
299 
300 static struct shash_alg alg = {
301  .digestsize = RMD128_DIGEST_SIZE,
302  .init = rmd128_init,
303  .update = rmd128_update,
304  .final = rmd128_final,
305  .descsize = sizeof(struct rmd128_ctx),
306  .base = {
307  .cra_name = "rmd128",
308  .cra_flags = CRYPTO_ALG_TYPE_SHASH,
309  .cra_blocksize = RMD128_BLOCK_SIZE,
310  .cra_module = THIS_MODULE,
311  }
312 };
313 
314 static int __init rmd128_mod_init(void)
315 {
316  return crypto_register_shash(&alg);
317 }
318 
319 static void __exit rmd128_mod_fini(void)
320 {
322 }
323 
324 module_init(rmd128_mod_init);
325 module_exit(rmd128_mod_fini);
326 
327 MODULE_LICENSE("GPL");
328 MODULE_AUTHOR("Adrian-Ken Rueegsegger <[email protected]>");
329 MODULE_DESCRIPTION("RIPEMD-128 Message Digest");