Loading...
1/*
2 * Modified to interface to the Linux kernel
3 * Copyright (c) 2009, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 */
18
19/* --------------------------------------------------------------------------
20 * VMAC and VHASH Implementation by Ted Krovetz (tdk@acm.org) and Wei Dai.
21 * This implementation is herby placed in the public domain.
22 * The authors offers no warranty. Use at your own risk.
23 * Please send bug reports to the authors.
24 * Last modified: 17 APR 08, 1700 PDT
25 * ----------------------------------------------------------------------- */
26
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/crypto.h>
30#include <linux/module.h>
31#include <linux/scatterlist.h>
32#include <asm/byteorder.h>
33#include <crypto/scatterwalk.h>
34#include <crypto/vmac.h>
35#include <crypto/internal/hash.h>
36
37/*
38 * Constants and masks
39 */
40#define UINT64_C(x) x##ULL
41static const u64 p64 = UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
42static const u64 m62 = UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
43static const u64 m63 = UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
44static const u64 m64 = UINT64_C(0xffffffffffffffff); /* 64-bit mask */
45static const u64 mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */
46
47#define pe64_to_cpup le64_to_cpup /* Prefer little endian */
48
49#ifdef __LITTLE_ENDIAN
50#define INDEX_HIGH 1
51#define INDEX_LOW 0
52#else
53#define INDEX_HIGH 0
54#define INDEX_LOW 1
55#endif
56
57/*
58 * The following routines are used in this implementation. They are
59 * written via macros to simulate zero-overhead call-by-reference.
60 *
61 * MUL64: 64x64->128-bit multiplication
62 * PMUL64: assumes top bits cleared on inputs
63 * ADD128: 128x128->128-bit addition
64 */
65
66#define ADD128(rh, rl, ih, il) \
67 do { \
68 u64 _il = (il); \
69 (rl) += (_il); \
70 if ((rl) < (_il)) \
71 (rh)++; \
72 (rh) += (ih); \
73 } while (0)
74
75#define MUL32(i1, i2) ((u64)(u32)(i1)*(u32)(i2))
76
77#define PMUL64(rh, rl, i1, i2) /* Assumes m doesn't overflow */ \
78 do { \
79 u64 _i1 = (i1), _i2 = (i2); \
80 u64 m = MUL32(_i1, _i2>>32) + MUL32(_i1>>32, _i2); \
81 rh = MUL32(_i1>>32, _i2>>32); \
82 rl = MUL32(_i1, _i2); \
83 ADD128(rh, rl, (m >> 32), (m << 32)); \
84 } while (0)
85
86#define MUL64(rh, rl, i1, i2) \
87 do { \
88 u64 _i1 = (i1), _i2 = (i2); \
89 u64 m1 = MUL32(_i1, _i2>>32); \
90 u64 m2 = MUL32(_i1>>32, _i2); \
91 rh = MUL32(_i1>>32, _i2>>32); \
92 rl = MUL32(_i1, _i2); \
93 ADD128(rh, rl, (m1 >> 32), (m1 << 32)); \
94 ADD128(rh, rl, (m2 >> 32), (m2 << 32)); \
95 } while (0)
96
97/*
98 * For highest performance the L1 NH and L2 polynomial hashes should be
99 * carefully implemented to take advantage of one's target architecture.
100 * Here these two hash functions are defined multiple time; once for
101 * 64-bit architectures, once for 32-bit SSE2 architectures, and once
102 * for the rest (32-bit) architectures.
103 * For each, nh_16 *must* be defined (works on multiples of 16 bytes).
104 * Optionally, nh_vmac_nhbytes can be defined (for multiples of
105 * VMAC_NHBYTES), and nh_16_2 and nh_vmac_nhbytes_2 (versions that do two
106 * NH computations at once).
107 */
108
109#ifdef CONFIG_64BIT
110
111#define nh_16(mp, kp, nw, rh, rl) \
112 do { \
113 int i; u64 th, tl; \
114 rh = rl = 0; \
115 for (i = 0; i < nw; i += 2) { \
116 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
117 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
118 ADD128(rh, rl, th, tl); \
119 } \
120 } while (0)
121
122#define nh_16_2(mp, kp, nw, rh, rl, rh1, rl1) \
123 do { \
124 int i; u64 th, tl; \
125 rh1 = rl1 = rh = rl = 0; \
126 for (i = 0; i < nw; i += 2) { \
127 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
128 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
129 ADD128(rh, rl, th, tl); \
130 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
131 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
132 ADD128(rh1, rl1, th, tl); \
133 } \
134 } while (0)
135
136#if (VMAC_NHBYTES >= 64) /* These versions do 64-bytes of message at a time */
137#define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
138 do { \
139 int i; u64 th, tl; \
140 rh = rl = 0; \
141 for (i = 0; i < nw; i += 8) { \
142 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
143 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
144 ADD128(rh, rl, th, tl); \
145 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
146 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
147 ADD128(rh, rl, th, tl); \
148 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
149 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
150 ADD128(rh, rl, th, tl); \
151 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
152 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
153 ADD128(rh, rl, th, tl); \
154 } \
155 } while (0)
156
157#define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh1, rl1) \
158 do { \
159 int i; u64 th, tl; \
160 rh1 = rl1 = rh = rl = 0; \
161 for (i = 0; i < nw; i += 8) { \
162 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
163 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
164 ADD128(rh, rl, th, tl); \
165 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
166 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
167 ADD128(rh1, rl1, th, tl); \
168 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
169 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
170 ADD128(rh, rl, th, tl); \
171 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+4], \
172 pe64_to_cpup((mp)+i+3)+(kp)[i+5]); \
173 ADD128(rh1, rl1, th, tl); \
174 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
175 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
176 ADD128(rh, rl, th, tl); \
177 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+6], \
178 pe64_to_cpup((mp)+i+5)+(kp)[i+7]); \
179 ADD128(rh1, rl1, th, tl); \
180 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
181 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
182 ADD128(rh, rl, th, tl); \
183 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+8], \
184 pe64_to_cpup((mp)+i+7)+(kp)[i+9]); \
185 ADD128(rh1, rl1, th, tl); \
186 } \
187 } while (0)
188#endif
189
190#define poly_step(ah, al, kh, kl, mh, ml) \
191 do { \
192 u64 t1h, t1l, t2h, t2l, t3h, t3l, z = 0; \
193 /* compute ab*cd, put bd into result registers */ \
194 PMUL64(t3h, t3l, al, kh); \
195 PMUL64(t2h, t2l, ah, kl); \
196 PMUL64(t1h, t1l, ah, 2*kh); \
197 PMUL64(ah, al, al, kl); \
198 /* add 2 * ac to result */ \
199 ADD128(ah, al, t1h, t1l); \
200 /* add together ad + bc */ \
201 ADD128(t2h, t2l, t3h, t3l); \
202 /* now (ah,al), (t2l,2*t2h) need summing */ \
203 /* first add the high registers, carrying into t2h */ \
204 ADD128(t2h, ah, z, t2l); \
205 /* double t2h and add top bit of ah */ \
206 t2h = 2 * t2h + (ah >> 63); \
207 ah &= m63; \
208 /* now add the low registers */ \
209 ADD128(ah, al, mh, ml); \
210 ADD128(ah, al, z, t2h); \
211 } while (0)
212
213#else /* ! CONFIG_64BIT */
214
215#ifndef nh_16
216#define nh_16(mp, kp, nw, rh, rl) \
217 do { \
218 u64 t1, t2, m1, m2, t; \
219 int i; \
220 rh = rl = t = 0; \
221 for (i = 0; i < nw; i += 2) { \
222 t1 = pe64_to_cpup(mp+i) + kp[i]; \
223 t2 = pe64_to_cpup(mp+i+1) + kp[i+1]; \
224 m2 = MUL32(t1 >> 32, t2); \
225 m1 = MUL32(t1, t2 >> 32); \
226 ADD128(rh, rl, MUL32(t1 >> 32, t2 >> 32), \
227 MUL32(t1, t2)); \
228 rh += (u64)(u32)(m1 >> 32) \
229 + (u32)(m2 >> 32); \
230 t += (u64)(u32)m1 + (u32)m2; \
231 } \
232 ADD128(rh, rl, (t >> 32), (t << 32)); \
233 } while (0)
234#endif
235
236static void poly_step_func(u64 *ahi, u64 *alo,
237 const u64 *kh, const u64 *kl,
238 const u64 *mh, const u64 *ml)
239{
240#define a0 (*(((u32 *)alo)+INDEX_LOW))
241#define a1 (*(((u32 *)alo)+INDEX_HIGH))
242#define a2 (*(((u32 *)ahi)+INDEX_LOW))
243#define a3 (*(((u32 *)ahi)+INDEX_HIGH))
244#define k0 (*(((u32 *)kl)+INDEX_LOW))
245#define k1 (*(((u32 *)kl)+INDEX_HIGH))
246#define k2 (*(((u32 *)kh)+INDEX_LOW))
247#define k3 (*(((u32 *)kh)+INDEX_HIGH))
248
249 u64 p, q, t;
250 u32 t2;
251
252 p = MUL32(a3, k3);
253 p += p;
254 p += *(u64 *)mh;
255 p += MUL32(a0, k2);
256 p += MUL32(a1, k1);
257 p += MUL32(a2, k0);
258 t = (u32)(p);
259 p >>= 32;
260 p += MUL32(a0, k3);
261 p += MUL32(a1, k2);
262 p += MUL32(a2, k1);
263 p += MUL32(a3, k0);
264 t |= ((u64)((u32)p & 0x7fffffff)) << 32;
265 p >>= 31;
266 p += (u64)(((u32 *)ml)[INDEX_LOW]);
267 p += MUL32(a0, k0);
268 q = MUL32(a1, k3);
269 q += MUL32(a2, k2);
270 q += MUL32(a3, k1);
271 q += q;
272 p += q;
273 t2 = (u32)(p);
274 p >>= 32;
275 p += (u64)(((u32 *)ml)[INDEX_HIGH]);
276 p += MUL32(a0, k1);
277 p += MUL32(a1, k0);
278 q = MUL32(a2, k3);
279 q += MUL32(a3, k2);
280 q += q;
281 p += q;
282 *(u64 *)(alo) = (p << 32) | t2;
283 p >>= 32;
284 *(u64 *)(ahi) = p + t;
285
286#undef a0
287#undef a1
288#undef a2
289#undef a3
290#undef k0
291#undef k1
292#undef k2
293#undef k3
294}
295
296#define poly_step(ah, al, kh, kl, mh, ml) \
297 poly_step_func(&(ah), &(al), &(kh), &(kl), &(mh), &(ml))
298
299#endif /* end of specialized NH and poly definitions */
300
301/* At least nh_16 is defined. Defined others as needed here */
302#ifndef nh_16_2
303#define nh_16_2(mp, kp, nw, rh, rl, rh2, rl2) \
304 do { \
305 nh_16(mp, kp, nw, rh, rl); \
306 nh_16(mp, ((kp)+2), nw, rh2, rl2); \
307 } while (0)
308#endif
309#ifndef nh_vmac_nhbytes
310#define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
311 nh_16(mp, kp, nw, rh, rl)
312#endif
313#ifndef nh_vmac_nhbytes_2
314#define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh2, rl2) \
315 do { \
316 nh_vmac_nhbytes(mp, kp, nw, rh, rl); \
317 nh_vmac_nhbytes(mp, ((kp)+2), nw, rh2, rl2); \
318 } while (0)
319#endif
320
321static void vhash_abort(struct vmac_ctx *ctx)
322{
323 ctx->polytmp[0] = ctx->polykey[0] ;
324 ctx->polytmp[1] = ctx->polykey[1] ;
325 ctx->first_block_processed = 0;
326}
327
328static u64 l3hash(u64 p1, u64 p2, u64 k1, u64 k2, u64 len)
329{
330 u64 rh, rl, t, z = 0;
331
332 /* fully reduce (p1,p2)+(len,0) mod p127 */
333 t = p1 >> 63;
334 p1 &= m63;
335 ADD128(p1, p2, len, t);
336 /* At this point, (p1,p2) is at most 2^127+(len<<64) */
337 t = (p1 > m63) + ((p1 == m63) && (p2 == m64));
338 ADD128(p1, p2, z, t);
339 p1 &= m63;
340
341 /* compute (p1,p2)/(2^64-2^32) and (p1,p2)%(2^64-2^32) */
342 t = p1 + (p2 >> 32);
343 t += (t >> 32);
344 t += (u32)t > 0xfffffffeu;
345 p1 += (t >> 32);
346 p2 += (p1 << 32);
347
348 /* compute (p1+k1)%p64 and (p2+k2)%p64 */
349 p1 += k1;
350 p1 += (0 - (p1 < k1)) & 257;
351 p2 += k2;
352 p2 += (0 - (p2 < k2)) & 257;
353
354 /* compute (p1+k1)*(p2+k2)%p64 */
355 MUL64(rh, rl, p1, p2);
356 t = rh >> 56;
357 ADD128(t, rl, z, rh);
358 rh <<= 8;
359 ADD128(t, rl, z, rh);
360 t += t << 8;
361 rl += t;
362 rl += (0 - (rl < t)) & 257;
363 rl += (0 - (rl > p64-1)) & 257;
364 return rl;
365}
366
367static void vhash_update(const unsigned char *m,
368 unsigned int mbytes, /* Pos multiple of VMAC_NHBYTES */
369 struct vmac_ctx *ctx)
370{
371 u64 rh, rl, *mptr;
372 const u64 *kptr = (u64 *)ctx->nhkey;
373 int i;
374 u64 ch, cl;
375 u64 pkh = ctx->polykey[0];
376 u64 pkl = ctx->polykey[1];
377
378 if (!mbytes)
379 return;
380
381 BUG_ON(mbytes % VMAC_NHBYTES);
382
383 mptr = (u64 *)m;
384 i = mbytes / VMAC_NHBYTES; /* Must be non-zero */
385
386 ch = ctx->polytmp[0];
387 cl = ctx->polytmp[1];
388
389 if (!ctx->first_block_processed) {
390 ctx->first_block_processed = 1;
391 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
392 rh &= m62;
393 ADD128(ch, cl, rh, rl);
394 mptr += (VMAC_NHBYTES/sizeof(u64));
395 i--;
396 }
397
398 while (i--) {
399 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
400 rh &= m62;
401 poly_step(ch, cl, pkh, pkl, rh, rl);
402 mptr += (VMAC_NHBYTES/sizeof(u64));
403 }
404
405 ctx->polytmp[0] = ch;
406 ctx->polytmp[1] = cl;
407}
408
409static u64 vhash(unsigned char m[], unsigned int mbytes,
410 u64 *tagl, struct vmac_ctx *ctx)
411{
412 u64 rh, rl, *mptr;
413 const u64 *kptr = (u64 *)ctx->nhkey;
414 int i, remaining;
415 u64 ch, cl;
416 u64 pkh = ctx->polykey[0];
417 u64 pkl = ctx->polykey[1];
418
419 mptr = (u64 *)m;
420 i = mbytes / VMAC_NHBYTES;
421 remaining = mbytes % VMAC_NHBYTES;
422
423 if (ctx->first_block_processed) {
424 ch = ctx->polytmp[0];
425 cl = ctx->polytmp[1];
426 } else if (i) {
427 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, ch, cl);
428 ch &= m62;
429 ADD128(ch, cl, pkh, pkl);
430 mptr += (VMAC_NHBYTES/sizeof(u64));
431 i--;
432 } else if (remaining) {
433 nh_16(mptr, kptr, 2*((remaining+15)/16), ch, cl);
434 ch &= m62;
435 ADD128(ch, cl, pkh, pkl);
436 mptr += (VMAC_NHBYTES/sizeof(u64));
437 goto do_l3;
438 } else {/* Empty String */
439 ch = pkh; cl = pkl;
440 goto do_l3;
441 }
442
443 while (i--) {
444 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
445 rh &= m62;
446 poly_step(ch, cl, pkh, pkl, rh, rl);
447 mptr += (VMAC_NHBYTES/sizeof(u64));
448 }
449 if (remaining) {
450 nh_16(mptr, kptr, 2*((remaining+15)/16), rh, rl);
451 rh &= m62;
452 poly_step(ch, cl, pkh, pkl, rh, rl);
453 }
454
455do_l3:
456 vhash_abort(ctx);
457 remaining *= 8;
458 return l3hash(ch, cl, ctx->l3key[0], ctx->l3key[1], remaining);
459}
460
461static u64 vmac(unsigned char m[], unsigned int mbytes,
462 const unsigned char n[16], u64 *tagl,
463 struct vmac_ctx_t *ctx)
464{
465 u64 *in_n, *out_p;
466 u64 p, h;
467 int i;
468
469 in_n = ctx->__vmac_ctx.cached_nonce;
470 out_p = ctx->__vmac_ctx.cached_aes;
471
472 i = n[15] & 1;
473 if ((*(u64 *)(n+8) != in_n[1]) || (*(u64 *)(n) != in_n[0])) {
474 in_n[0] = *(u64 *)(n);
475 in_n[1] = *(u64 *)(n+8);
476 ((unsigned char *)in_n)[15] &= 0xFE;
477 crypto_cipher_encrypt_one(ctx->child,
478 (unsigned char *)out_p, (unsigned char *)in_n);
479
480 ((unsigned char *)in_n)[15] |= (unsigned char)(1-i);
481 }
482 p = be64_to_cpup(out_p + i);
483 h = vhash(m, mbytes, (u64 *)0, &ctx->__vmac_ctx);
484 return le64_to_cpu(p + h);
485}
486
487static int vmac_set_key(unsigned char user_key[], struct vmac_ctx_t *ctx)
488{
489 u64 in[2] = {0}, out[2];
490 unsigned i;
491 int err = 0;
492
493 err = crypto_cipher_setkey(ctx->child, user_key, VMAC_KEY_LEN);
494 if (err)
495 return err;
496
497 /* Fill nh key */
498 ((unsigned char *)in)[0] = 0x80;
499 for (i = 0; i < sizeof(ctx->__vmac_ctx.nhkey)/8; i += 2) {
500 crypto_cipher_encrypt_one(ctx->child,
501 (unsigned char *)out, (unsigned char *)in);
502 ctx->__vmac_ctx.nhkey[i] = be64_to_cpup(out);
503 ctx->__vmac_ctx.nhkey[i+1] = be64_to_cpup(out+1);
504 ((unsigned char *)in)[15] += 1;
505 }
506
507 /* Fill poly key */
508 ((unsigned char *)in)[0] = 0xC0;
509 in[1] = 0;
510 for (i = 0; i < sizeof(ctx->__vmac_ctx.polykey)/8; i += 2) {
511 crypto_cipher_encrypt_one(ctx->child,
512 (unsigned char *)out, (unsigned char *)in);
513 ctx->__vmac_ctx.polytmp[i] =
514 ctx->__vmac_ctx.polykey[i] =
515 be64_to_cpup(out) & mpoly;
516 ctx->__vmac_ctx.polytmp[i+1] =
517 ctx->__vmac_ctx.polykey[i+1] =
518 be64_to_cpup(out+1) & mpoly;
519 ((unsigned char *)in)[15] += 1;
520 }
521
522 /* Fill ip key */
523 ((unsigned char *)in)[0] = 0xE0;
524 in[1] = 0;
525 for (i = 0; i < sizeof(ctx->__vmac_ctx.l3key)/8; i += 2) {
526 do {
527 crypto_cipher_encrypt_one(ctx->child,
528 (unsigned char *)out, (unsigned char *)in);
529 ctx->__vmac_ctx.l3key[i] = be64_to_cpup(out);
530 ctx->__vmac_ctx.l3key[i+1] = be64_to_cpup(out+1);
531 ((unsigned char *)in)[15] += 1;
532 } while (ctx->__vmac_ctx.l3key[i] >= p64
533 || ctx->__vmac_ctx.l3key[i+1] >= p64);
534 }
535
536 /* Invalidate nonce/aes cache and reset other elements */
537 ctx->__vmac_ctx.cached_nonce[0] = (u64)-1; /* Ensure illegal nonce */
538 ctx->__vmac_ctx.cached_nonce[1] = (u64)0; /* Ensure illegal nonce */
539 ctx->__vmac_ctx.first_block_processed = 0;
540
541 return err;
542}
543
544static int vmac_setkey(struct crypto_shash *parent,
545 const u8 *key, unsigned int keylen)
546{
547 struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
548
549 if (keylen != VMAC_KEY_LEN) {
550 crypto_shash_set_flags(parent, CRYPTO_TFM_RES_BAD_KEY_LEN);
551 return -EINVAL;
552 }
553
554 return vmac_set_key((u8 *)key, ctx);
555}
556
557static int vmac_init(struct shash_desc *pdesc)
558{
559 return 0;
560}
561
562static int vmac_update(struct shash_desc *pdesc, const u8 *p,
563 unsigned int len)
564{
565 struct crypto_shash *parent = pdesc->tfm;
566 struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
567 int expand;
568 int min;
569
570 expand = VMAC_NHBYTES - ctx->partial_size > 0 ?
571 VMAC_NHBYTES - ctx->partial_size : 0;
572
573 min = len < expand ? len : expand;
574
575 memcpy(ctx->partial + ctx->partial_size, p, min);
576 ctx->partial_size += min;
577
578 if (len < expand)
579 return 0;
580
581 vhash_update(ctx->partial, VMAC_NHBYTES, &ctx->__vmac_ctx);
582 ctx->partial_size = 0;
583
584 len -= expand;
585 p += expand;
586
587 if (len % VMAC_NHBYTES) {
588 memcpy(ctx->partial, p + len - (len % VMAC_NHBYTES),
589 len % VMAC_NHBYTES);
590 ctx->partial_size = len % VMAC_NHBYTES;
591 }
592
593 vhash_update(p, len - len % VMAC_NHBYTES, &ctx->__vmac_ctx);
594
595 return 0;
596}
597
598static int vmac_final(struct shash_desc *pdesc, u8 *out)
599{
600 struct crypto_shash *parent = pdesc->tfm;
601 struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
602 vmac_t mac;
603 u8 nonce[16] = {};
604
605 /* vmac() ends up accessing outside the array bounds that
606 * we specify. In appears to access up to the next 2-word
607 * boundary. We'll just be uber cautious and zero the
608 * unwritten bytes in the buffer.
609 */
610 if (ctx->partial_size) {
611 memset(ctx->partial + ctx->partial_size, 0,
612 VMAC_NHBYTES - ctx->partial_size);
613 }
614 mac = vmac(ctx->partial, ctx->partial_size, nonce, NULL, ctx);
615 memcpy(out, &mac, sizeof(vmac_t));
616 memset(&mac, 0, sizeof(vmac_t));
617 memset(&ctx->__vmac_ctx, 0, sizeof(struct vmac_ctx));
618 ctx->partial_size = 0;
619 return 0;
620}
621
622static int vmac_init_tfm(struct crypto_tfm *tfm)
623{
624 struct crypto_cipher *cipher;
625 struct crypto_instance *inst = (void *)tfm->__crt_alg;
626 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
627 struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
628
629 cipher = crypto_spawn_cipher(spawn);
630 if (IS_ERR(cipher))
631 return PTR_ERR(cipher);
632
633 ctx->child = cipher;
634 return 0;
635}
636
637static void vmac_exit_tfm(struct crypto_tfm *tfm)
638{
639 struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
640 crypto_free_cipher(ctx->child);
641}
642
643static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
644{
645 struct shash_instance *inst;
646 struct crypto_alg *alg;
647 int err;
648
649 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
650 if (err)
651 return err;
652
653 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
654 CRYPTO_ALG_TYPE_MASK);
655 if (IS_ERR(alg))
656 return PTR_ERR(alg);
657
658 inst = shash_alloc_instance("vmac", alg);
659 err = PTR_ERR(inst);
660 if (IS_ERR(inst))
661 goto out_put_alg;
662
663 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
664 shash_crypto_instance(inst),
665 CRYPTO_ALG_TYPE_MASK);
666 if (err)
667 goto out_free_inst;
668
669 inst->alg.base.cra_priority = alg->cra_priority;
670 inst->alg.base.cra_blocksize = alg->cra_blocksize;
671 inst->alg.base.cra_alignmask = alg->cra_alignmask;
672
673 inst->alg.digestsize = sizeof(vmac_t);
674 inst->alg.base.cra_ctxsize = sizeof(struct vmac_ctx_t);
675 inst->alg.base.cra_init = vmac_init_tfm;
676 inst->alg.base.cra_exit = vmac_exit_tfm;
677
678 inst->alg.init = vmac_init;
679 inst->alg.update = vmac_update;
680 inst->alg.final = vmac_final;
681 inst->alg.setkey = vmac_setkey;
682
683 err = shash_register_instance(tmpl, inst);
684 if (err) {
685out_free_inst:
686 shash_free_instance(shash_crypto_instance(inst));
687 }
688
689out_put_alg:
690 crypto_mod_put(alg);
691 return err;
692}
693
694static struct crypto_template vmac_tmpl = {
695 .name = "vmac",
696 .create = vmac_create,
697 .free = shash_free_instance,
698 .module = THIS_MODULE,
699};
700
701static int __init vmac_module_init(void)
702{
703 return crypto_register_template(&vmac_tmpl);
704}
705
706static void __exit vmac_module_exit(void)
707{
708 crypto_unregister_template(&vmac_tmpl);
709}
710
711module_init(vmac_module_init);
712module_exit(vmac_module_exit);
713
714MODULE_LICENSE("GPL");
715MODULE_DESCRIPTION("VMAC hash algorithm");
1/*
2 * VMAC: Message Authentication Code using Universal Hashing
3 *
4 * Reference: https://tools.ietf.org/html/draft-krovetz-vmac-01
5 *
6 * Copyright (c) 2009, Intel Corporation.
7 * Copyright (c) 2018, Google Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 * Place - Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23/*
24 * Derived from:
25 * VMAC and VHASH Implementation by Ted Krovetz (tdk@acm.org) and Wei Dai.
26 * This implementation is herby placed in the public domain.
27 * The authors offers no warranty. Use at your own risk.
28 * Last modified: 17 APR 08, 1700 PDT
29 */
30
31#include <asm/unaligned.h>
32#include <linux/init.h>
33#include <linux/types.h>
34#include <linux/crypto.h>
35#include <linux/module.h>
36#include <linux/scatterlist.h>
37#include <asm/byteorder.h>
38#include <crypto/scatterwalk.h>
39#include <crypto/internal/hash.h>
40
41/*
42 * User definable settings.
43 */
44#define VMAC_TAG_LEN 64
45#define VMAC_KEY_SIZE 128/* Must be 128, 192 or 256 */
46#define VMAC_KEY_LEN (VMAC_KEY_SIZE/8)
47#define VMAC_NHBYTES 128/* Must 2^i for any 3 < i < 13 Standard = 128*/
48#define VMAC_NONCEBYTES 16
49
50/* per-transform (per-key) context */
51struct vmac_tfm_ctx {
52 struct crypto_cipher *cipher;
53 u64 nhkey[(VMAC_NHBYTES/8)+2*(VMAC_TAG_LEN/64-1)];
54 u64 polykey[2*VMAC_TAG_LEN/64];
55 u64 l3key[2*VMAC_TAG_LEN/64];
56};
57
58/* per-request context */
59struct vmac_desc_ctx {
60 union {
61 u8 partial[VMAC_NHBYTES]; /* partial block */
62 __le64 partial_words[VMAC_NHBYTES / 8];
63 };
64 unsigned int partial_size; /* size of the partial block */
65 bool first_block_processed;
66 u64 polytmp[2*VMAC_TAG_LEN/64]; /* running total of L2-hash */
67 union {
68 u8 bytes[VMAC_NONCEBYTES];
69 __be64 pads[VMAC_NONCEBYTES / 8];
70 } nonce;
71 unsigned int nonce_size; /* nonce bytes filled so far */
72};
73
74/*
75 * Constants and masks
76 */
77#define UINT64_C(x) x##ULL
78static const u64 p64 = UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
79static const u64 m62 = UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
80static const u64 m63 = UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
81static const u64 m64 = UINT64_C(0xffffffffffffffff); /* 64-bit mask */
82static const u64 mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */
83
84#define pe64_to_cpup le64_to_cpup /* Prefer little endian */
85
86#ifdef __LITTLE_ENDIAN
87#define INDEX_HIGH 1
88#define INDEX_LOW 0
89#else
90#define INDEX_HIGH 0
91#define INDEX_LOW 1
92#endif
93
94/*
95 * The following routines are used in this implementation. They are
96 * written via macros to simulate zero-overhead call-by-reference.
97 *
98 * MUL64: 64x64->128-bit multiplication
99 * PMUL64: assumes top bits cleared on inputs
100 * ADD128: 128x128->128-bit addition
101 */
102
103#define ADD128(rh, rl, ih, il) \
104 do { \
105 u64 _il = (il); \
106 (rl) += (_il); \
107 if ((rl) < (_il)) \
108 (rh)++; \
109 (rh) += (ih); \
110 } while (0)
111
112#define MUL32(i1, i2) ((u64)(u32)(i1)*(u32)(i2))
113
114#define PMUL64(rh, rl, i1, i2) /* Assumes m doesn't overflow */ \
115 do { \
116 u64 _i1 = (i1), _i2 = (i2); \
117 u64 m = MUL32(_i1, _i2>>32) + MUL32(_i1>>32, _i2); \
118 rh = MUL32(_i1>>32, _i2>>32); \
119 rl = MUL32(_i1, _i2); \
120 ADD128(rh, rl, (m >> 32), (m << 32)); \
121 } while (0)
122
123#define MUL64(rh, rl, i1, i2) \
124 do { \
125 u64 _i1 = (i1), _i2 = (i2); \
126 u64 m1 = MUL32(_i1, _i2>>32); \
127 u64 m2 = MUL32(_i1>>32, _i2); \
128 rh = MUL32(_i1>>32, _i2>>32); \
129 rl = MUL32(_i1, _i2); \
130 ADD128(rh, rl, (m1 >> 32), (m1 << 32)); \
131 ADD128(rh, rl, (m2 >> 32), (m2 << 32)); \
132 } while (0)
133
134/*
135 * For highest performance the L1 NH and L2 polynomial hashes should be
136 * carefully implemented to take advantage of one's target architecture.
137 * Here these two hash functions are defined multiple time; once for
138 * 64-bit architectures, once for 32-bit SSE2 architectures, and once
139 * for the rest (32-bit) architectures.
140 * For each, nh_16 *must* be defined (works on multiples of 16 bytes).
141 * Optionally, nh_vmac_nhbytes can be defined (for multiples of
142 * VMAC_NHBYTES), and nh_16_2 and nh_vmac_nhbytes_2 (versions that do two
143 * NH computations at once).
144 */
145
146#ifdef CONFIG_64BIT
147
148#define nh_16(mp, kp, nw, rh, rl) \
149 do { \
150 int i; u64 th, tl; \
151 rh = rl = 0; \
152 for (i = 0; i < nw; i += 2) { \
153 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
154 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
155 ADD128(rh, rl, th, tl); \
156 } \
157 } while (0)
158
159#define nh_16_2(mp, kp, nw, rh, rl, rh1, rl1) \
160 do { \
161 int i; u64 th, tl; \
162 rh1 = rl1 = rh = rl = 0; \
163 for (i = 0; i < nw; i += 2) { \
164 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
165 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
166 ADD128(rh, rl, th, tl); \
167 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
168 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
169 ADD128(rh1, rl1, th, tl); \
170 } \
171 } while (0)
172
173#if (VMAC_NHBYTES >= 64) /* These versions do 64-bytes of message at a time */
174#define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
175 do { \
176 int i; u64 th, tl; \
177 rh = rl = 0; \
178 for (i = 0; i < nw; i += 8) { \
179 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
180 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
181 ADD128(rh, rl, th, tl); \
182 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
183 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
184 ADD128(rh, rl, th, tl); \
185 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
186 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
187 ADD128(rh, rl, th, tl); \
188 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
189 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
190 ADD128(rh, rl, th, tl); \
191 } \
192 } while (0)
193
194#define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh1, rl1) \
195 do { \
196 int i; u64 th, tl; \
197 rh1 = rl1 = rh = rl = 0; \
198 for (i = 0; i < nw; i += 8) { \
199 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
200 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
201 ADD128(rh, rl, th, tl); \
202 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
203 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
204 ADD128(rh1, rl1, th, tl); \
205 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
206 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
207 ADD128(rh, rl, th, tl); \
208 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+4], \
209 pe64_to_cpup((mp)+i+3)+(kp)[i+5]); \
210 ADD128(rh1, rl1, th, tl); \
211 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
212 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
213 ADD128(rh, rl, th, tl); \
214 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+6], \
215 pe64_to_cpup((mp)+i+5)+(kp)[i+7]); \
216 ADD128(rh1, rl1, th, tl); \
217 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
218 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
219 ADD128(rh, rl, th, tl); \
220 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+8], \
221 pe64_to_cpup((mp)+i+7)+(kp)[i+9]); \
222 ADD128(rh1, rl1, th, tl); \
223 } \
224 } while (0)
225#endif
226
227#define poly_step(ah, al, kh, kl, mh, ml) \
228 do { \
229 u64 t1h, t1l, t2h, t2l, t3h, t3l, z = 0; \
230 /* compute ab*cd, put bd into result registers */ \
231 PMUL64(t3h, t3l, al, kh); \
232 PMUL64(t2h, t2l, ah, kl); \
233 PMUL64(t1h, t1l, ah, 2*kh); \
234 PMUL64(ah, al, al, kl); \
235 /* add 2 * ac to result */ \
236 ADD128(ah, al, t1h, t1l); \
237 /* add together ad + bc */ \
238 ADD128(t2h, t2l, t3h, t3l); \
239 /* now (ah,al), (t2l,2*t2h) need summing */ \
240 /* first add the high registers, carrying into t2h */ \
241 ADD128(t2h, ah, z, t2l); \
242 /* double t2h and add top bit of ah */ \
243 t2h = 2 * t2h + (ah >> 63); \
244 ah &= m63; \
245 /* now add the low registers */ \
246 ADD128(ah, al, mh, ml); \
247 ADD128(ah, al, z, t2h); \
248 } while (0)
249
250#else /* ! CONFIG_64BIT */
251
252#ifndef nh_16
253#define nh_16(mp, kp, nw, rh, rl) \
254 do { \
255 u64 t1, t2, m1, m2, t; \
256 int i; \
257 rh = rl = t = 0; \
258 for (i = 0; i < nw; i += 2) { \
259 t1 = pe64_to_cpup(mp+i) + kp[i]; \
260 t2 = pe64_to_cpup(mp+i+1) + kp[i+1]; \
261 m2 = MUL32(t1 >> 32, t2); \
262 m1 = MUL32(t1, t2 >> 32); \
263 ADD128(rh, rl, MUL32(t1 >> 32, t2 >> 32), \
264 MUL32(t1, t2)); \
265 rh += (u64)(u32)(m1 >> 32) \
266 + (u32)(m2 >> 32); \
267 t += (u64)(u32)m1 + (u32)m2; \
268 } \
269 ADD128(rh, rl, (t >> 32), (t << 32)); \
270 } while (0)
271#endif
272
273static void poly_step_func(u64 *ahi, u64 *alo,
274 const u64 *kh, const u64 *kl,
275 const u64 *mh, const u64 *ml)
276{
277#define a0 (*(((u32 *)alo)+INDEX_LOW))
278#define a1 (*(((u32 *)alo)+INDEX_HIGH))
279#define a2 (*(((u32 *)ahi)+INDEX_LOW))
280#define a3 (*(((u32 *)ahi)+INDEX_HIGH))
281#define k0 (*(((u32 *)kl)+INDEX_LOW))
282#define k1 (*(((u32 *)kl)+INDEX_HIGH))
283#define k2 (*(((u32 *)kh)+INDEX_LOW))
284#define k3 (*(((u32 *)kh)+INDEX_HIGH))
285
286 u64 p, q, t;
287 u32 t2;
288
289 p = MUL32(a3, k3);
290 p += p;
291 p += *(u64 *)mh;
292 p += MUL32(a0, k2);
293 p += MUL32(a1, k1);
294 p += MUL32(a2, k0);
295 t = (u32)(p);
296 p >>= 32;
297 p += MUL32(a0, k3);
298 p += MUL32(a1, k2);
299 p += MUL32(a2, k1);
300 p += MUL32(a3, k0);
301 t |= ((u64)((u32)p & 0x7fffffff)) << 32;
302 p >>= 31;
303 p += (u64)(((u32 *)ml)[INDEX_LOW]);
304 p += MUL32(a0, k0);
305 q = MUL32(a1, k3);
306 q += MUL32(a2, k2);
307 q += MUL32(a3, k1);
308 q += q;
309 p += q;
310 t2 = (u32)(p);
311 p >>= 32;
312 p += (u64)(((u32 *)ml)[INDEX_HIGH]);
313 p += MUL32(a0, k1);
314 p += MUL32(a1, k0);
315 q = MUL32(a2, k3);
316 q += MUL32(a3, k2);
317 q += q;
318 p += q;
319 *(u64 *)(alo) = (p << 32) | t2;
320 p >>= 32;
321 *(u64 *)(ahi) = p + t;
322
323#undef a0
324#undef a1
325#undef a2
326#undef a3
327#undef k0
328#undef k1
329#undef k2
330#undef k3
331}
332
333#define poly_step(ah, al, kh, kl, mh, ml) \
334 poly_step_func(&(ah), &(al), &(kh), &(kl), &(mh), &(ml))
335
336#endif /* end of specialized NH and poly definitions */
337
338/* At least nh_16 is defined. Defined others as needed here */
339#ifndef nh_16_2
340#define nh_16_2(mp, kp, nw, rh, rl, rh2, rl2) \
341 do { \
342 nh_16(mp, kp, nw, rh, rl); \
343 nh_16(mp, ((kp)+2), nw, rh2, rl2); \
344 } while (0)
345#endif
346#ifndef nh_vmac_nhbytes
347#define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
348 nh_16(mp, kp, nw, rh, rl)
349#endif
350#ifndef nh_vmac_nhbytes_2
351#define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh2, rl2) \
352 do { \
353 nh_vmac_nhbytes(mp, kp, nw, rh, rl); \
354 nh_vmac_nhbytes(mp, ((kp)+2), nw, rh2, rl2); \
355 } while (0)
356#endif
357
358static u64 l3hash(u64 p1, u64 p2, u64 k1, u64 k2, u64 len)
359{
360 u64 rh, rl, t, z = 0;
361
362 /* fully reduce (p1,p2)+(len,0) mod p127 */
363 t = p1 >> 63;
364 p1 &= m63;
365 ADD128(p1, p2, len, t);
366 /* At this point, (p1,p2) is at most 2^127+(len<<64) */
367 t = (p1 > m63) + ((p1 == m63) && (p2 == m64));
368 ADD128(p1, p2, z, t);
369 p1 &= m63;
370
371 /* compute (p1,p2)/(2^64-2^32) and (p1,p2)%(2^64-2^32) */
372 t = p1 + (p2 >> 32);
373 t += (t >> 32);
374 t += (u32)t > 0xfffffffeu;
375 p1 += (t >> 32);
376 p2 += (p1 << 32);
377
378 /* compute (p1+k1)%p64 and (p2+k2)%p64 */
379 p1 += k1;
380 p1 += (0 - (p1 < k1)) & 257;
381 p2 += k2;
382 p2 += (0 - (p2 < k2)) & 257;
383
384 /* compute (p1+k1)*(p2+k2)%p64 */
385 MUL64(rh, rl, p1, p2);
386 t = rh >> 56;
387 ADD128(t, rl, z, rh);
388 rh <<= 8;
389 ADD128(t, rl, z, rh);
390 t += t << 8;
391 rl += t;
392 rl += (0 - (rl < t)) & 257;
393 rl += (0 - (rl > p64-1)) & 257;
394 return rl;
395}
396
397/* L1 and L2-hash one or more VMAC_NHBYTES-byte blocks */
398static void vhash_blocks(const struct vmac_tfm_ctx *tctx,
399 struct vmac_desc_ctx *dctx,
400 const __le64 *mptr, unsigned int blocks)
401{
402 const u64 *kptr = tctx->nhkey;
403 const u64 pkh = tctx->polykey[0];
404 const u64 pkl = tctx->polykey[1];
405 u64 ch = dctx->polytmp[0];
406 u64 cl = dctx->polytmp[1];
407 u64 rh, rl;
408
409 if (!dctx->first_block_processed) {
410 dctx->first_block_processed = true;
411 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
412 rh &= m62;
413 ADD128(ch, cl, rh, rl);
414 mptr += (VMAC_NHBYTES/sizeof(u64));
415 blocks--;
416 }
417
418 while (blocks--) {
419 nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
420 rh &= m62;
421 poly_step(ch, cl, pkh, pkl, rh, rl);
422 mptr += (VMAC_NHBYTES/sizeof(u64));
423 }
424
425 dctx->polytmp[0] = ch;
426 dctx->polytmp[1] = cl;
427}
428
429static int vmac_setkey(struct crypto_shash *tfm,
430 const u8 *key, unsigned int keylen)
431{
432 struct vmac_tfm_ctx *tctx = crypto_shash_ctx(tfm);
433 __be64 out[2];
434 u8 in[16] = { 0 };
435 unsigned int i;
436 int err;
437
438 if (keylen != VMAC_KEY_LEN) {
439 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
440 return -EINVAL;
441 }
442
443 err = crypto_cipher_setkey(tctx->cipher, key, keylen);
444 if (err)
445 return err;
446
447 /* Fill nh key */
448 in[0] = 0x80;
449 for (i = 0; i < ARRAY_SIZE(tctx->nhkey); i += 2) {
450 crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
451 tctx->nhkey[i] = be64_to_cpu(out[0]);
452 tctx->nhkey[i+1] = be64_to_cpu(out[1]);
453 in[15]++;
454 }
455
456 /* Fill poly key */
457 in[0] = 0xC0;
458 in[15] = 0;
459 for (i = 0; i < ARRAY_SIZE(tctx->polykey); i += 2) {
460 crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
461 tctx->polykey[i] = be64_to_cpu(out[0]) & mpoly;
462 tctx->polykey[i+1] = be64_to_cpu(out[1]) & mpoly;
463 in[15]++;
464 }
465
466 /* Fill ip key */
467 in[0] = 0xE0;
468 in[15] = 0;
469 for (i = 0; i < ARRAY_SIZE(tctx->l3key); i += 2) {
470 do {
471 crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
472 tctx->l3key[i] = be64_to_cpu(out[0]);
473 tctx->l3key[i+1] = be64_to_cpu(out[1]);
474 in[15]++;
475 } while (tctx->l3key[i] >= p64 || tctx->l3key[i+1] >= p64);
476 }
477
478 return 0;
479}
480
481static int vmac_init(struct shash_desc *desc)
482{
483 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
484 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
485
486 dctx->partial_size = 0;
487 dctx->first_block_processed = false;
488 memcpy(dctx->polytmp, tctx->polykey, sizeof(dctx->polytmp));
489 dctx->nonce_size = 0;
490 return 0;
491}
492
493static int vmac_update(struct shash_desc *desc, const u8 *p, unsigned int len)
494{
495 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
496 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
497 unsigned int n;
498
499 /* Nonce is passed as first VMAC_NONCEBYTES bytes of data */
500 if (dctx->nonce_size < VMAC_NONCEBYTES) {
501 n = min(len, VMAC_NONCEBYTES - dctx->nonce_size);
502 memcpy(&dctx->nonce.bytes[dctx->nonce_size], p, n);
503 dctx->nonce_size += n;
504 p += n;
505 len -= n;
506 }
507
508 if (dctx->partial_size) {
509 n = min(len, VMAC_NHBYTES - dctx->partial_size);
510 memcpy(&dctx->partial[dctx->partial_size], p, n);
511 dctx->partial_size += n;
512 p += n;
513 len -= n;
514 if (dctx->partial_size == VMAC_NHBYTES) {
515 vhash_blocks(tctx, dctx, dctx->partial_words, 1);
516 dctx->partial_size = 0;
517 }
518 }
519
520 if (len >= VMAC_NHBYTES) {
521 n = round_down(len, VMAC_NHBYTES);
522 /* TODO: 'p' may be misaligned here */
523 vhash_blocks(tctx, dctx, (const __le64 *)p, n / VMAC_NHBYTES);
524 p += n;
525 len -= n;
526 }
527
528 if (len) {
529 memcpy(dctx->partial, p, len);
530 dctx->partial_size = len;
531 }
532
533 return 0;
534}
535
536static u64 vhash_final(const struct vmac_tfm_ctx *tctx,
537 struct vmac_desc_ctx *dctx)
538{
539 unsigned int partial = dctx->partial_size;
540 u64 ch = dctx->polytmp[0];
541 u64 cl = dctx->polytmp[1];
542
543 /* L1 and L2-hash the final block if needed */
544 if (partial) {
545 /* Zero-pad to next 128-bit boundary */
546 unsigned int n = round_up(partial, 16);
547 u64 rh, rl;
548
549 memset(&dctx->partial[partial], 0, n - partial);
550 nh_16(dctx->partial_words, tctx->nhkey, n / 8, rh, rl);
551 rh &= m62;
552 if (dctx->first_block_processed)
553 poly_step(ch, cl, tctx->polykey[0], tctx->polykey[1],
554 rh, rl);
555 else
556 ADD128(ch, cl, rh, rl);
557 }
558
559 /* L3-hash the 128-bit output of L2-hash */
560 return l3hash(ch, cl, tctx->l3key[0], tctx->l3key[1], partial * 8);
561}
562
563static int vmac_final(struct shash_desc *desc, u8 *out)
564{
565 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
566 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
567 int index;
568 u64 hash, pad;
569
570 if (dctx->nonce_size != VMAC_NONCEBYTES)
571 return -EINVAL;
572
573 /*
574 * The VMAC specification requires a nonce at least 1 bit shorter than
575 * the block cipher's block length, so we actually only accept a 127-bit
576 * nonce. We define the unused bit to be the first one and require that
577 * it be 0, so the needed prepending of a 0 bit is implicit.
578 */
579 if (dctx->nonce.bytes[0] & 0x80)
580 return -EINVAL;
581
582 /* Finish calculating the VHASH of the message */
583 hash = vhash_final(tctx, dctx);
584
585 /* Generate pseudorandom pad by encrypting the nonce */
586 BUILD_BUG_ON(VMAC_NONCEBYTES != 2 * (VMAC_TAG_LEN / 8));
587 index = dctx->nonce.bytes[VMAC_NONCEBYTES - 1] & 1;
588 dctx->nonce.bytes[VMAC_NONCEBYTES - 1] &= ~1;
589 crypto_cipher_encrypt_one(tctx->cipher, dctx->nonce.bytes,
590 dctx->nonce.bytes);
591 pad = be64_to_cpu(dctx->nonce.pads[index]);
592
593 /* The VMAC is the sum of VHASH and the pseudorandom pad */
594 put_unaligned_be64(hash + pad, out);
595 return 0;
596}
597
598static int vmac_init_tfm(struct crypto_tfm *tfm)
599{
600 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
601 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
602 struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
603 struct crypto_cipher *cipher;
604
605 cipher = crypto_spawn_cipher(spawn);
606 if (IS_ERR(cipher))
607 return PTR_ERR(cipher);
608
609 tctx->cipher = cipher;
610 return 0;
611}
612
613static void vmac_exit_tfm(struct crypto_tfm *tfm)
614{
615 struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
616
617 crypto_free_cipher(tctx->cipher);
618}
619
620static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
621{
622 struct shash_instance *inst;
623 struct crypto_alg *alg;
624 int err;
625
626 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
627 if (err)
628 return err;
629
630 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
631 CRYPTO_ALG_TYPE_MASK);
632 if (IS_ERR(alg))
633 return PTR_ERR(alg);
634
635 err = -EINVAL;
636 if (alg->cra_blocksize != VMAC_NONCEBYTES)
637 goto out_put_alg;
638
639 inst = shash_alloc_instance(tmpl->name, alg);
640 err = PTR_ERR(inst);
641 if (IS_ERR(inst))
642 goto out_put_alg;
643
644 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
645 shash_crypto_instance(inst),
646 CRYPTO_ALG_TYPE_MASK);
647 if (err)
648 goto out_free_inst;
649
650 inst->alg.base.cra_priority = alg->cra_priority;
651 inst->alg.base.cra_blocksize = alg->cra_blocksize;
652 inst->alg.base.cra_alignmask = alg->cra_alignmask;
653
654 inst->alg.base.cra_ctxsize = sizeof(struct vmac_tfm_ctx);
655 inst->alg.base.cra_init = vmac_init_tfm;
656 inst->alg.base.cra_exit = vmac_exit_tfm;
657
658 inst->alg.descsize = sizeof(struct vmac_desc_ctx);
659 inst->alg.digestsize = VMAC_TAG_LEN / 8;
660 inst->alg.init = vmac_init;
661 inst->alg.update = vmac_update;
662 inst->alg.final = vmac_final;
663 inst->alg.setkey = vmac_setkey;
664
665 err = shash_register_instance(tmpl, inst);
666 if (err) {
667out_free_inst:
668 shash_free_instance(shash_crypto_instance(inst));
669 }
670
671out_put_alg:
672 crypto_mod_put(alg);
673 return err;
674}
675
676static struct crypto_template vmac64_tmpl = {
677 .name = "vmac64",
678 .create = vmac_create,
679 .free = shash_free_instance,
680 .module = THIS_MODULE,
681};
682
683static int __init vmac_module_init(void)
684{
685 return crypto_register_template(&vmac64_tmpl);
686}
687
688static void __exit vmac_module_exit(void)
689{
690 crypto_unregister_template(&vmac64_tmpl);
691}
692
693subsys_initcall(vmac_module_init);
694module_exit(vmac_module_exit);
695
696MODULE_LICENSE("GPL");
697MODULE_DESCRIPTION("VMAC hash algorithm");
698MODULE_ALIAS_CRYPTO("vmac64");