2 * Modified to interface to the Linux kernel
3 * Copyright (c) 2009, Intel Corporation.
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.
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
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.
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 * ----------------------------------------------------------------------- */
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/crypto.h>
30 #include <linux/scatterlist.h>
31 #include <asm/byteorder.h>
32 #include <crypto/scatterwalk.h>
33 #include <crypto/vmac.h>
34 #include <crypto/internal/hash.h>
39 #define UINT64_C(x) x##ULL
40 const u64 p64
= UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
41 const u64 m62
= UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
42 const u64 m63
= UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
43 const u64 m64
= UINT64_C(0xffffffffffffffff); /* 64-bit mask */
44 const u64 mpoly
= UINT64_C(0x1fffffff1fffffff); /* Poly key mask */
46 #ifdef __LITTLE_ENDIAN
55 * The following routines are used in this implementation. They are
56 * written via macros to simulate zero-overhead call-by-reference.
58 * MUL64: 64x64->128-bit multiplication
59 * PMUL64: assumes top bits cleared on inputs
60 * ADD128: 128x128->128-bit addition
63 #define ADD128(rh, rl, ih, il) \
72 #define MUL32(i1, i2) ((u64)(u32)(i1)*(u32)(i2))
74 #define PMUL64(rh, rl, i1, i2) /* Assumes m doesn't overflow */ \
76 u64 _i1 = (i1), _i2 = (i2); \
77 u64 m = MUL32(_i1, _i2>>32) + MUL32(_i1>>32, _i2); \
78 rh = MUL32(_i1>>32, _i2>>32); \
79 rl = MUL32(_i1, _i2); \
80 ADD128(rh, rl, (m >> 32), (m << 32)); \
83 #define MUL64(rh, rl, i1, i2) \
85 u64 _i1 = (i1), _i2 = (i2); \
86 u64 m1 = MUL32(_i1, _i2>>32); \
87 u64 m2 = MUL32(_i1>>32, _i2); \
88 rh = MUL32(_i1>>32, _i2>>32); \
89 rl = MUL32(_i1, _i2); \
90 ADD128(rh, rl, (m1 >> 32), (m1 << 32)); \
91 ADD128(rh, rl, (m2 >> 32), (m2 << 32)); \
95 * For highest performance the L1 NH and L2 polynomial hashes should be
96 * carefully implemented to take advantage of one's target architechture.
97 * Here these two hash functions are defined multiple time; once for
98 * 64-bit architectures, once for 32-bit SSE2 architectures, and once
99 * for the rest (32-bit) architectures.
100 * For each, nh_16 *must* be defined (works on multiples of 16 bytes).
101 * Optionally, nh_vmac_nhbytes can be defined (for multiples of
102 * VMAC_NHBYTES), and nh_16_2 and nh_vmac_nhbytes_2 (versions that do two
103 * NH computations at once).
108 #define nh_16(mp, kp, nw, rh, rl) \
112 for (i = 0; i < nw; i += 2) { \
113 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i], \
114 le64_to_cpup((mp)+i+1)+(kp)[i+1]); \
115 ADD128(rh, rl, th, tl); \
119 #define nh_16_2(mp, kp, nw, rh, rl, rh1, rl1) \
122 rh1 = rl1 = rh = rl = 0; \
123 for (i = 0; i < nw; i += 2) { \
124 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i], \
125 le64_to_cpup((mp)+i+1)+(kp)[i+1]); \
126 ADD128(rh, rl, th, tl); \
127 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i+2], \
128 le64_to_cpup((mp)+i+1)+(kp)[i+3]); \
129 ADD128(rh1, rl1, th, tl); \
133 #if (VMAC_NHBYTES >= 64) /* These versions do 64-bytes of message at a time */
134 #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
138 for (i = 0; i < nw; i += 8) { \
139 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i], \
140 le64_to_cpup((mp)+i+1)+(kp)[i+1]); \
141 ADD128(rh, rl, th, tl); \
142 MUL64(th, tl, le64_to_cpup((mp)+i+2)+(kp)[i+2], \
143 le64_to_cpup((mp)+i+3)+(kp)[i+3]); \
144 ADD128(rh, rl, th, tl); \
145 MUL64(th, tl, le64_to_cpup((mp)+i+4)+(kp)[i+4], \
146 le64_to_cpup((mp)+i+5)+(kp)[i+5]); \
147 ADD128(rh, rl, th, tl); \
148 MUL64(th, tl, le64_to_cpup((mp)+i+6)+(kp)[i+6], \
149 le64_to_cpup((mp)+i+7)+(kp)[i+7]); \
150 ADD128(rh, rl, th, tl); \
154 #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh1, rl1) \
157 rh1 = rl1 = rh = rl = 0; \
158 for (i = 0; i < nw; i += 8) { \
159 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i], \
160 le64_to_cpup((mp)+i+1)+(kp)[i+1]); \
161 ADD128(rh, rl, th, tl); \
162 MUL64(th, tl, le64_to_cpup((mp)+i)+(kp)[i+2], \
163 le64_to_cpup((mp)+i+1)+(kp)[i+3]); \
164 ADD128(rh1, rl1, th, tl); \
165 MUL64(th, tl, le64_to_cpup((mp)+i+2)+(kp)[i+2], \
166 le64_to_cpup((mp)+i+3)+(kp)[i+3]); \
167 ADD128(rh, rl, th, tl); \
168 MUL64(th, tl, le64_to_cpup((mp)+i+2)+(kp)[i+4], \
169 le64_to_cpup((mp)+i+3)+(kp)[i+5]); \
170 ADD128(rh1, rl1, th, tl); \
171 MUL64(th, tl, le64_to_cpup((mp)+i+4)+(kp)[i+4], \
172 le64_to_cpup((mp)+i+5)+(kp)[i+5]); \
173 ADD128(rh, rl, th, tl); \
174 MUL64(th, tl, le64_to_cpup((mp)+i+4)+(kp)[i+6], \
175 le64_to_cpup((mp)+i+5)+(kp)[i+7]); \
176 ADD128(rh1, rl1, th, tl); \
177 MUL64(th, tl, le64_to_cpup((mp)+i+6)+(kp)[i+6], \
178 le64_to_cpup((mp)+i+7)+(kp)[i+7]); \
179 ADD128(rh, rl, th, tl); \
180 MUL64(th, tl, le64_to_cpup((mp)+i+6)+(kp)[i+8], \
181 le64_to_cpup((mp)+i+7)+(kp)[i+9]); \
182 ADD128(rh1, rl1, th, tl); \
187 #define poly_step(ah, al, kh, kl, mh, ml) \
189 u64 t1h, t1l, t2h, t2l, t3h, t3l, z = 0; \
190 /* compute ab*cd, put bd into result registers */ \
191 PMUL64(t3h, t3l, al, kh); \
192 PMUL64(t2h, t2l, ah, kl); \
193 PMUL64(t1h, t1l, ah, 2*kh); \
194 PMUL64(ah, al, al, kl); \
195 /* add 2 * ac to result */ \
196 ADD128(ah, al, t1h, t1l); \
197 /* add together ad + bc */ \
198 ADD128(t2h, t2l, t3h, t3l); \
199 /* now (ah,al), (t2l,2*t2h) need summing */ \
200 /* first add the high registers, carrying into t2h */ \
201 ADD128(t2h, ah, z, t2l); \
202 /* double t2h and add top bit of ah */ \
203 t2h = 2 * t2h + (ah >> 63); \
205 /* now add the low registers */ \
206 ADD128(ah, al, mh, ml); \
207 ADD128(ah, al, z, t2h); \
210 #else /* ! CONFIG_64BIT */
213 #define nh_16(mp, kp, nw, rh, rl) \
215 u64 t1, t2, m1, m2, t; \
218 for (i = 0; i < nw; i += 2) { \
219 t1 = le64_to_cpup(mp+i) + kp[i]; \
220 t2 = le64_to_cpup(mp+i+1) + kp[i+1]; \
221 m2 = MUL32(t1 >> 32, t2); \
222 m1 = MUL32(t1, t2 >> 32); \
223 ADD128(rh, rl, MUL32(t1 >> 32, t2 >> 32), \
225 rh += (u64)(u32)(m1 >> 32) \
227 t += (u64)(u32)m1 + (u32)m2; \
229 ADD128(rh, rl, (t >> 32), (t << 32)); \
233 static void poly_step_func(u64
*ahi
, u64
*alo
,
234 const u64
*kh
, const u64
*kl
,
235 const u64
*mh
, const u64
*ml
)
237 #define a0 (*(((u32 *)alo)+INDEX_LOW))
238 #define a1 (*(((u32 *)alo)+INDEX_HIGH))
239 #define a2 (*(((u32 *)ahi)+INDEX_LOW))
240 #define a3 (*(((u32 *)ahi)+INDEX_HIGH))
241 #define k0 (*(((u32 *)kl)+INDEX_LOW))
242 #define k1 (*(((u32 *)kl)+INDEX_HIGH))
243 #define k2 (*(((u32 *)kh)+INDEX_LOW))
244 #define k3 (*(((u32 *)kh)+INDEX_HIGH))
261 t
|= ((u64
)((u32
)p
& 0x7fffffff)) << 32;
263 p
+= (u64
)(((u32
*)ml
)[INDEX_LOW
]);
272 p
+= (u64
)(((u32
*)ml
)[INDEX_HIGH
]);
279 *(u64
*)(alo
) = (p
<< 32) | t2
;
281 *(u64
*)(ahi
) = p
+ t
;
293 #define poly_step(ah, al, kh, kl, mh, ml) \
294 poly_step_func(&(ah), &(al), &(kh), &(kl), &(mh), &(ml))
296 #endif /* end of specialized NH and poly definitions */
298 /* At least nh_16 is defined. Defined others as needed here */
300 #define nh_16_2(mp, kp, nw, rh, rl, rh2, rl2) \
302 nh_16(mp, kp, nw, rh, rl); \
303 nh_16(mp, ((kp)+2), nw, rh2, rl2); \
306 #ifndef nh_vmac_nhbytes
307 #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
308 nh_16(mp, kp, nw, rh, rl)
310 #ifndef nh_vmac_nhbytes_2
311 #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh2, rl2) \
313 nh_vmac_nhbytes(mp, kp, nw, rh, rl); \
314 nh_vmac_nhbytes(mp, ((kp)+2), nw, rh2, rl2); \
318 static void vhash_abort(struct vmac_ctx
*ctx
)
320 ctx
->polytmp
[0] = ctx
->polykey
[0] ;
321 ctx
->polytmp
[1] = ctx
->polykey
[1] ;
322 ctx
->first_block_processed
= 0;
325 static u64
l3hash(u64 p1
, u64 p2
,
326 u64 k1
, u64 k2
, u64 len
)
328 u64 rh
, rl
, t
, z
= 0;
330 /* fully reduce (p1,p2)+(len,0) mod p127 */
333 ADD128(p1
, p2
, len
, t
);
334 /* At this point, (p1,p2) is at most 2^127+(len<<64) */
335 t
= (p1
> m63
) + ((p1
== m63
) && (p2
== m64
));
336 ADD128(p1
, p2
, z
, t
);
339 /* compute (p1,p2)/(2^64-2^32) and (p1,p2)%(2^64-2^32) */
342 t
+= (u32
)t
> 0xfffffffeu
;
346 /* compute (p1+k1)%p64 and (p2+k2)%p64 */
348 p1
+= (0 - (p1
< k1
)) & 257;
350 p2
+= (0 - (p2
< k2
)) & 257;
352 /* compute (p1+k1)*(p2+k2)%p64 */
353 MUL64(rh
, rl
, p1
, p2
);
355 ADD128(t
, rl
, z
, rh
);
357 ADD128(t
, rl
, z
, rh
);
360 rl
+= (0 - (rl
< t
)) & 257;
361 rl
+= (0 - (rl
> p64
-1)) & 257;
365 static void vhash_update(const unsigned char *m
,
366 unsigned int mbytes
, /* Pos multiple of VMAC_NHBYTES */
367 struct vmac_ctx
*ctx
)
370 const u64
*kptr
= (u64
*)ctx
->nhkey
;
373 u64 pkh
= ctx
->polykey
[0];
374 u64 pkl
= ctx
->polykey
[1];
377 i
= mbytes
/ VMAC_NHBYTES
; /* Must be non-zero */
379 ch
= ctx
->polytmp
[0];
380 cl
= ctx
->polytmp
[1];
382 if (!ctx
->first_block_processed
) {
383 ctx
->first_block_processed
= 1;
384 nh_vmac_nhbytes(mptr
, kptr
, VMAC_NHBYTES
/8, rh
, rl
);
386 ADD128(ch
, cl
, rh
, rl
);
387 mptr
+= (VMAC_NHBYTES
/sizeof(u64
));
392 nh_vmac_nhbytes(mptr
, kptr
, VMAC_NHBYTES
/8, rh
, rl
);
394 poly_step(ch
, cl
, pkh
, pkl
, rh
, rl
);
395 mptr
+= (VMAC_NHBYTES
/sizeof(u64
));
398 ctx
->polytmp
[0] = ch
;
399 ctx
->polytmp
[1] = cl
;
402 static u64
vhash(unsigned char m
[], unsigned int mbytes
,
403 u64
*tagl
, struct vmac_ctx
*ctx
)
406 const u64
*kptr
= (u64
*)ctx
->nhkey
;
409 u64 pkh
= ctx
->polykey
[0];
410 u64 pkl
= ctx
->polykey
[1];
413 i
= mbytes
/ VMAC_NHBYTES
;
414 remaining
= mbytes
% VMAC_NHBYTES
;
416 if (ctx
->first_block_processed
) {
417 ch
= ctx
->polytmp
[0];
418 cl
= ctx
->polytmp
[1];
420 nh_vmac_nhbytes(mptr
, kptr
, VMAC_NHBYTES
/8, ch
, cl
);
422 ADD128(ch
, cl
, pkh
, pkl
);
423 mptr
+= (VMAC_NHBYTES
/sizeof(u64
));
425 } else if (remaining
) {
426 nh_16(mptr
, kptr
, 2*((remaining
+15)/16), ch
, cl
);
428 ADD128(ch
, cl
, pkh
, pkl
);
429 mptr
+= (VMAC_NHBYTES
/sizeof(u64
));
431 } else {/* Empty String */
437 nh_vmac_nhbytes(mptr
, kptr
, VMAC_NHBYTES
/8, rh
, rl
);
439 poly_step(ch
, cl
, pkh
, pkl
, rh
, rl
);
440 mptr
+= (VMAC_NHBYTES
/sizeof(u64
));
443 nh_16(mptr
, kptr
, 2*((remaining
+15)/16), rh
, rl
);
445 poly_step(ch
, cl
, pkh
, pkl
, rh
, rl
);
451 return l3hash(ch
, cl
, ctx
->l3key
[0], ctx
->l3key
[1], remaining
);
454 static u64
vmac(unsigned char m
[], unsigned int mbytes
,
455 unsigned char n
[16], u64
*tagl
,
456 struct vmac_ctx_t
*ctx
)
462 in_n
= ctx
->__vmac_ctx
.cached_nonce
;
463 out_p
= ctx
->__vmac_ctx
.cached_aes
;
466 if ((*(u64
*)(n
+8) != in_n
[1]) || (*(u64
*)(n
) != in_n
[0])) {
467 in_n
[0] = *(u64
*)(n
);
468 in_n
[1] = *(u64
*)(n
+8);
469 ((unsigned char *)in_n
)[15] &= 0xFE;
470 crypto_cipher_encrypt_one(ctx
->child
,
471 (unsigned char *)out_p
, (unsigned char *)in_n
);
473 ((unsigned char *)in_n
)[15] |= (unsigned char)(1-i
);
475 p
= be64_to_cpup(out_p
+ i
);
476 h
= vhash(m
, mbytes
, (u64
*)0, &ctx
->__vmac_ctx
);
480 static int vmac_set_key(unsigned char user_key
[], struct vmac_ctx_t
*ctx
)
482 u64 in
[2] = {0}, out
[2];
486 err
= crypto_cipher_setkey(ctx
->child
, user_key
, VMAC_KEY_LEN
);
491 ((unsigned char *)in
)[0] = 0x80;
492 for (i
= 0; i
< sizeof(ctx
->__vmac_ctx
.nhkey
)/8; i
+= 2) {
493 crypto_cipher_encrypt_one(ctx
->child
,
494 (unsigned char *)out
, (unsigned char *)in
);
495 ctx
->__vmac_ctx
.nhkey
[i
] = be64_to_cpup(out
);
496 ctx
->__vmac_ctx
.nhkey
[i
+1] = be64_to_cpup(out
+1);
497 ((unsigned char *)in
)[15] += 1;
501 ((unsigned char *)in
)[0] = 0xC0;
503 for (i
= 0; i
< sizeof(ctx
->__vmac_ctx
.polykey
)/8; i
+= 2) {
504 crypto_cipher_encrypt_one(ctx
->child
,
505 (unsigned char *)out
, (unsigned char *)in
);
506 ctx
->__vmac_ctx
.polytmp
[i
] =
507 ctx
->__vmac_ctx
.polykey
[i
] =
508 be64_to_cpup(out
) & mpoly
;
509 ctx
->__vmac_ctx
.polytmp
[i
+1] =
510 ctx
->__vmac_ctx
.polykey
[i
+1] =
511 be64_to_cpup(out
+1) & mpoly
;
512 ((unsigned char *)in
)[15] += 1;
516 ((unsigned char *)in
)[0] = 0xE0;
518 for (i
= 0; i
< sizeof(ctx
->__vmac_ctx
.l3key
)/8; i
+= 2) {
520 crypto_cipher_encrypt_one(ctx
->child
,
521 (unsigned char *)out
, (unsigned char *)in
);
522 ctx
->__vmac_ctx
.l3key
[i
] = be64_to_cpup(out
);
523 ctx
->__vmac_ctx
.l3key
[i
+1] = be64_to_cpup(out
+1);
524 ((unsigned char *)in
)[15] += 1;
525 } while (ctx
->__vmac_ctx
.l3key
[i
] >= p64
526 || ctx
->__vmac_ctx
.l3key
[i
+1] >= p64
);
529 /* Invalidate nonce/aes cache and reset other elements */
530 ctx
->__vmac_ctx
.cached_nonce
[0] = (u64
)-1; /* Ensure illegal nonce */
531 ctx
->__vmac_ctx
.cached_nonce
[1] = (u64
)0; /* Ensure illegal nonce */
532 ctx
->__vmac_ctx
.first_block_processed
= 0;
537 static int vmac_setkey(struct crypto_shash
*parent
,
538 const u8
*key
, unsigned int keylen
)
540 struct vmac_ctx_t
*ctx
= crypto_shash_ctx(parent
);
542 if (keylen
!= VMAC_KEY_LEN
) {
543 crypto_shash_set_flags(parent
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
547 return vmac_set_key((u8
*)key
, ctx
);
550 static int vmac_init(struct shash_desc
*pdesc
)
552 struct crypto_shash
*parent
= pdesc
->tfm
;
553 struct vmac_ctx_t
*ctx
= crypto_shash_ctx(parent
);
555 memset(&ctx
->__vmac_ctx
, 0, sizeof(struct vmac_ctx
));
559 static int vmac_update(struct shash_desc
*pdesc
, const u8
*p
,
562 struct crypto_shash
*parent
= pdesc
->tfm
;
563 struct vmac_ctx_t
*ctx
= crypto_shash_ctx(parent
);
565 vhash_update(p
, len
, &ctx
->__vmac_ctx
);
570 static int vmac_final(struct shash_desc
*pdesc
, u8
*out
)
572 struct crypto_shash
*parent
= pdesc
->tfm
;
573 struct vmac_ctx_t
*ctx
= crypto_shash_ctx(parent
);
577 mac
= vmac(NULL
, 0, nonce
, NULL
, ctx
);
578 memcpy(out
, &mac
, sizeof(vmac_t
));
579 memset(&mac
, 0, sizeof(vmac_t
));
580 memset(&ctx
->__vmac_ctx
, 0, sizeof(struct vmac_ctx
));
584 static int vmac_init_tfm(struct crypto_tfm
*tfm
)
586 struct crypto_cipher
*cipher
;
587 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
588 struct crypto_spawn
*spawn
= crypto_instance_ctx(inst
);
589 struct vmac_ctx_t
*ctx
= crypto_tfm_ctx(tfm
);
591 cipher
= crypto_spawn_cipher(spawn
);
593 return PTR_ERR(cipher
);
599 static void vmac_exit_tfm(struct crypto_tfm
*tfm
)
601 struct vmac_ctx_t
*ctx
= crypto_tfm_ctx(tfm
);
602 crypto_free_cipher(ctx
->child
);
605 static int vmac_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
607 struct shash_instance
*inst
;
608 struct crypto_alg
*alg
;
611 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SHASH
);
615 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
,
616 CRYPTO_ALG_TYPE_MASK
);
620 inst
= shash_alloc_instance("vmac", alg
);
625 err
= crypto_init_spawn(shash_instance_ctx(inst
), alg
,
626 shash_crypto_instance(inst
),
627 CRYPTO_ALG_TYPE_MASK
);
631 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
632 inst
->alg
.base
.cra_blocksize
= alg
->cra_blocksize
;
633 inst
->alg
.base
.cra_alignmask
= alg
->cra_alignmask
;
635 inst
->alg
.digestsize
= sizeof(vmac_t
);
636 inst
->alg
.base
.cra_ctxsize
= sizeof(struct vmac_ctx_t
);
637 inst
->alg
.base
.cra_init
= vmac_init_tfm
;
638 inst
->alg
.base
.cra_exit
= vmac_exit_tfm
;
640 inst
->alg
.init
= vmac_init
;
641 inst
->alg
.update
= vmac_update
;
642 inst
->alg
.final
= vmac_final
;
643 inst
->alg
.setkey
= vmac_setkey
;
645 err
= shash_register_instance(tmpl
, inst
);
648 shash_free_instance(shash_crypto_instance(inst
));
656 static struct crypto_template vmac_tmpl
= {
658 .create
= vmac_create
,
659 .free
= shash_free_instance
,
660 .module
= THIS_MODULE
,
663 static int __init
vmac_module_init(void)
665 return crypto_register_template(&vmac_tmpl
);
668 static void __exit
vmac_module_exit(void)
670 crypto_unregister_template(&vmac_tmpl
);
673 module_init(vmac_module_init
);
674 module_exit(vmac_module_exit
);
676 MODULE_LICENSE("GPL");
677 MODULE_DESCRIPTION("VMAC hash algorithm");