blob: bf1e385bc6847f73c4ad0a4cf43abd6cb32537e6 [file] [log] [blame]
Shane Wangf1939f72009-09-02 20:05:22 +10001/*
Eric Biggersbb296482018-06-18 10:22:38 -07002 * VMAC: Message Authentication Code using Universal Hashing
3 *
4 * Reference: https://tools.ietf.org/html/draft-krovetz-vmac-01
5 *
Shane Wangf1939f72009-09-02 20:05:22 +10006 * Copyright (c) 2009, Intel Corporation.
Eric Biggersbb296482018-06-18 10:22:38 -07007 * Copyright (c) 2018, Google Inc.
Shane Wangf1939f72009-09-02 20:05:22 +10008 *
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
Eric Biggersbb296482018-06-18 10:22:38 -070023/*
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 */
Shane Wangf1939f72009-09-02 20:05:22 +100030
Eric Biggersbb296482018-06-18 10:22:38 -070031#include <asm/unaligned.h>
Shane Wangf1939f72009-09-02 20:05:22 +100032#include <linux/init.h>
33#include <linux/types.h>
34#include <linux/crypto.h>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040035#include <linux/module.h>
Shane Wangf1939f72009-09-02 20:05:22 +100036#include <linux/scatterlist.h>
37#include <asm/byteorder.h>
38#include <crypto/scatterwalk.h>
Shane Wangf1939f72009-09-02 20:05:22 +100039#include <crypto/internal/hash.h>
40
41/*
Eric Biggersbb296482018-06-18 10:22:38 -070042 * 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*/
Eric Biggersed331ad2018-06-18 10:22:39 -070048#define VMAC_NONCEBYTES 16
Eric Biggersbb296482018-06-18 10:22:38 -070049
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 */
Eric Biggersed331ad2018-06-18 10:22:39 -070067 union {
68 u8 bytes[VMAC_NONCEBYTES];
69 __be64 pads[VMAC_NONCEBYTES / 8];
70 } nonce;
71 unsigned int nonce_size; /* nonce bytes filled so far */
Eric Biggersbb296482018-06-18 10:22:38 -070072};
73
74/*
Shane Wangf1939f72009-09-02 20:05:22 +100075 * Constants and masks
76 */
77#define UINT64_C(x) x##ULL
Jussi Kivilinna66ce0b02012-08-28 16:46:54 +030078static 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 */
Shane Wangf1939f72009-09-02 20:05:22 +100083
Shane Wang304a2042010-03-18 20:22:55 +080084#define pe64_to_cpup le64_to_cpup /* Prefer little endian */
85
Shane Wangf1939f72009-09-02 20:05:22 +100086#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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300136 * carefully implemented to take advantage of one's target architecture.
Shane Wangf1939f72009-09-02 20:05:22 +1000137 * 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) { \
Shane Wang304a2042010-03-18 20:22:55 +0800153 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
154 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000155 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) { \
Shane Wang304a2042010-03-18 20:22:55 +0800164 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
165 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000166 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800167 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
168 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000169 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) { \
Shane Wang304a2042010-03-18 20:22:55 +0800179 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
180 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000181 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800182 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
183 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000184 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800185 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
186 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000187 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800188 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
189 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000190 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) { \
Shane Wang304a2042010-03-18 20:22:55 +0800199 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
200 pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000201 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800202 MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
203 pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000204 ADD128(rh1, rl1, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800205 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
206 pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000207 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800208 MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+4], \
209 pe64_to_cpup((mp)+i+3)+(kp)[i+5]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000210 ADD128(rh1, rl1, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800211 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
212 pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000213 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800214 MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+6], \
215 pe64_to_cpup((mp)+i+5)+(kp)[i+7]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000216 ADD128(rh1, rl1, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800217 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
218 pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000219 ADD128(rh, rl, th, tl); \
Shane Wang304a2042010-03-18 20:22:55 +0800220 MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+8], \
221 pe64_to_cpup((mp)+i+7)+(kp)[i+9]); \
Shane Wangf1939f72009-09-02 20:05:22 +1000222 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) { \
Shane Wang304a2042010-03-18 20:22:55 +0800259 t1 = pe64_to_cpup(mp+i) + kp[i]; \
260 t2 = pe64_to_cpup(mp+i+1) + kp[i+1]; \
Shane Wangf1939f72009-09-02 20:05:22 +1000261 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
Shane Wang304a2042010-03-18 20:22:55 +0800358static u64 l3hash(u64 p1, u64 p2, u64 k1, u64 k2, u64 len)
Shane Wangf1939f72009-09-02 20:05:22 +1000359{
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
Eric Biggersbb296482018-06-18 10:22:38 -0700397/* 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)
Shane Wangf1939f72009-09-02 20:05:22 +1000401{
Eric Biggersbb296482018-06-18 10:22:38 -0700402 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;
Shane Wangf1939f72009-09-02 20:05:22 +1000408
Eric Biggersbb296482018-06-18 10:22:38 -0700409 if (!dctx->first_block_processed) {
410 dctx->first_block_processed = true;
Shane Wangf1939f72009-09-02 20:05:22 +1000411 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));
Eric Biggersbb296482018-06-18 10:22:38 -0700415 blocks--;
Shane Wangf1939f72009-09-02 20:05:22 +1000416 }
417
Eric Biggersbb296482018-06-18 10:22:38 -0700418 while (blocks--) {
Shane Wangf1939f72009-09-02 20:05:22 +1000419 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
Eric Biggersbb296482018-06-18 10:22:38 -0700425 dctx->polytmp[0] = ch;
426 dctx->polytmp[1] = cl;
Shane Wangf1939f72009-09-02 20:05:22 +1000427}
428
Eric Biggersbb296482018-06-18 10:22:38 -0700429static int vmac_setkey(struct crypto_shash *tfm,
430 const u8 *key, unsigned int keylen)
Shane Wangf1939f72009-09-02 20:05:22 +1000431{
Eric Biggersbb296482018-06-18 10:22:38 -0700432 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;
Shane Wangf1939f72009-09-02 20:05:22 +1000437
Eric Biggersbb296482018-06-18 10:22:38 -0700438 if (keylen != VMAC_KEY_LEN) {
439 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
440 return -EINVAL;
Shane Wangf1939f72009-09-02 20:05:22 +1000441 }
442
Eric Biggersbb296482018-06-18 10:22:38 -0700443 err = crypto_cipher_setkey(tctx->cipher, key, keylen);
Shane Wangf1939f72009-09-02 20:05:22 +1000444 if (err)
445 return err;
446
447 /* Fill nh key */
Eric Biggersbb296482018-06-18 10:22:38 -0700448 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]++;
Shane Wangf1939f72009-09-02 20:05:22 +1000454 }
455
456 /* Fill poly key */
Eric Biggersbb296482018-06-18 10:22:38 -0700457 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]++;
Shane Wangf1939f72009-09-02 20:05:22 +1000464 }
465
466 /* Fill ip key */
Eric Biggersbb296482018-06-18 10:22:38 -0700467 in[0] = 0xE0;
468 in[15] = 0;
469 for (i = 0; i < ARRAY_SIZE(tctx->l3key); i += 2) {
Shane Wangf1939f72009-09-02 20:05:22 +1000470 do {
Eric Biggersbb296482018-06-18 10:22:38 -0700471 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);
Shane Wangf1939f72009-09-02 20:05:22 +1000476 }
477
Shane Wangf1939f72009-09-02 20:05:22 +1000478 return 0;
479}
480
Eric Biggersbb296482018-06-18 10:22:38 -0700481static int vmac_init(struct shash_desc *desc)
Shane Wangf1939f72009-09-02 20:05:22 +1000482{
Eric Biggersbb296482018-06-18 10:22:38 -0700483 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
484 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
Shane Wangf1939f72009-09-02 20:05:22 +1000485
Eric Biggersbb296482018-06-18 10:22:38 -0700486 dctx->partial_size = 0;
487 dctx->first_block_processed = false;
488 memcpy(dctx->polytmp, tctx->polykey, sizeof(dctx->polytmp));
Eric Biggersed331ad2018-06-18 10:22:39 -0700489 dctx->nonce_size = 0;
490 return 0;
491}
492
493static int vmac_init_with_hardcoded_nonce(struct shash_desc *desc)
494{
495 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
496
497 vmac_init(desc);
498 memset(&dctx->nonce, 0, VMAC_NONCEBYTES);
499 dctx->nonce_size = VMAC_NONCEBYTES;
Eric Biggersbb296482018-06-18 10:22:38 -0700500 return 0;
501}
502
503static int vmac_update(struct shash_desc *desc, const u8 *p, unsigned int len)
504{
505 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
506 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
507 unsigned int n;
508
Eric Biggersed331ad2018-06-18 10:22:39 -0700509 /* Nonce is passed as first VMAC_NONCEBYTES bytes of data */
510 if (dctx->nonce_size < VMAC_NONCEBYTES) {
511 n = min(len, VMAC_NONCEBYTES - dctx->nonce_size);
512 memcpy(&dctx->nonce.bytes[dctx->nonce_size], p, n);
513 dctx->nonce_size += n;
514 p += n;
515 len -= n;
516 }
517
Eric Biggersbb296482018-06-18 10:22:38 -0700518 if (dctx->partial_size) {
519 n = min(len, VMAC_NHBYTES - dctx->partial_size);
520 memcpy(&dctx->partial[dctx->partial_size], p, n);
521 dctx->partial_size += n;
522 p += n;
523 len -= n;
524 if (dctx->partial_size == VMAC_NHBYTES) {
525 vhash_blocks(tctx, dctx, dctx->partial_words, 1);
526 dctx->partial_size = 0;
527 }
Salman Qaziba1ee072012-10-05 14:24:14 -0700528 }
Eric Biggersbb296482018-06-18 10:22:38 -0700529
530 if (len >= VMAC_NHBYTES) {
531 n = round_down(len, VMAC_NHBYTES);
532 /* TODO: 'p' may be misaligned here */
533 vhash_blocks(tctx, dctx, (const __le64 *)p, n / VMAC_NHBYTES);
534 p += n;
535 len -= n;
536 }
537
538 if (len) {
539 memcpy(dctx->partial, p, len);
540 dctx->partial_size = len;
541 }
542
543 return 0;
544}
545
546static u64 vhash_final(const struct vmac_tfm_ctx *tctx,
547 struct vmac_desc_ctx *dctx)
548{
549 unsigned int partial = dctx->partial_size;
550 u64 ch = dctx->polytmp[0];
551 u64 cl = dctx->polytmp[1];
552
553 /* L1 and L2-hash the final block if needed */
554 if (partial) {
555 /* Zero-pad to next 128-bit boundary */
556 unsigned int n = round_up(partial, 16);
557 u64 rh, rl;
558
559 memset(&dctx->partial[partial], 0, n - partial);
560 nh_16(dctx->partial_words, tctx->nhkey, n / 8, rh, rl);
561 rh &= m62;
562 if (dctx->first_block_processed)
563 poly_step(ch, cl, tctx->polykey[0], tctx->polykey[1],
564 rh, rl);
565 else
566 ADD128(ch, cl, rh, rl);
567 }
568
569 /* L3-hash the 128-bit output of L2-hash */
570 return l3hash(ch, cl, tctx->l3key[0], tctx->l3key[1], partial * 8);
571}
572
Eric Biggersed331ad2018-06-18 10:22:39 -0700573static int __vmac_final(struct shash_desc *desc, u64 *mac)
Eric Biggersbb296482018-06-18 10:22:38 -0700574{
575 const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
576 struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
Eric Biggersbb296482018-06-18 10:22:38 -0700577 int index;
578 u64 hash, pad;
579
Eric Biggersed331ad2018-06-18 10:22:39 -0700580 if (dctx->nonce_size != VMAC_NONCEBYTES)
581 return -EINVAL;
582
583 /*
584 * The VMAC specification requires a nonce at least 1 bit shorter than
585 * the block cipher's block length, so we actually only accept a 127-bit
586 * nonce. We define the unused bit to be the first one and require that
587 * it be 0, so the needed prepending of a 0 bit is implicit.
588 */
589 if (dctx->nonce.bytes[0] & 0x80)
590 return -EINVAL;
591
Eric Biggersbb296482018-06-18 10:22:38 -0700592 /* Finish calculating the VHASH of the message */
593 hash = vhash_final(tctx, dctx);
594
595 /* Generate pseudorandom pad by encrypting the nonce */
Eric Biggersed331ad2018-06-18 10:22:39 -0700596 BUILD_BUG_ON(VMAC_NONCEBYTES != 2 * (VMAC_TAG_LEN / 8));
597 index = dctx->nonce.bytes[VMAC_NONCEBYTES - 1] & 1;
598 dctx->nonce.bytes[VMAC_NONCEBYTES - 1] &= ~1;
599 crypto_cipher_encrypt_one(tctx->cipher, dctx->nonce.bytes,
600 dctx->nonce.bytes);
601 pad = be64_to_cpu(dctx->nonce.pads[index]);
Eric Biggersbb296482018-06-18 10:22:38 -0700602
603 /* The VMAC is the sum of VHASH and the pseudorandom pad */
Eric Biggersed331ad2018-06-18 10:22:39 -0700604 *mac = hash + pad;
605 return 0;
606}
607
608static int vmac_final_le(struct shash_desc *desc, u8 *out)
609{
610 u64 mac;
611 int err;
612
613 err = __vmac_final(desc, &mac);
614 if (err)
615 return err;
616 put_unaligned_le64(mac, out);
617 return 0;
618}
619
620static int vmac_final_be(struct shash_desc *desc, u8 *out)
621{
622 u64 mac;
623 int err;
624
625 err = __vmac_final(desc, &mac);
626 if (err)
627 return err;
628 put_unaligned_be64(mac, out);
Shane Wangf1939f72009-09-02 20:05:22 +1000629 return 0;
630}
631
632static int vmac_init_tfm(struct crypto_tfm *tfm)
633{
Eric Biggersbb296482018-06-18 10:22:38 -0700634 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Shane Wangf1939f72009-09-02 20:05:22 +1000635 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Eric Biggersbb296482018-06-18 10:22:38 -0700636 struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
637 struct crypto_cipher *cipher;
Shane Wangf1939f72009-09-02 20:05:22 +1000638
639 cipher = crypto_spawn_cipher(spawn);
640 if (IS_ERR(cipher))
641 return PTR_ERR(cipher);
642
Eric Biggersbb296482018-06-18 10:22:38 -0700643 tctx->cipher = cipher;
Shane Wangf1939f72009-09-02 20:05:22 +1000644 return 0;
645}
646
647static void vmac_exit_tfm(struct crypto_tfm *tfm)
648{
Eric Biggersbb296482018-06-18 10:22:38 -0700649 struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
650
651 crypto_free_cipher(tctx->cipher);
Shane Wangf1939f72009-09-02 20:05:22 +1000652}
653
Eric Biggersed331ad2018-06-18 10:22:39 -0700654static int vmac_create_common(struct crypto_template *tmpl, struct rtattr **tb,
655 bool vmac64)
Shane Wangf1939f72009-09-02 20:05:22 +1000656{
657 struct shash_instance *inst;
658 struct crypto_alg *alg;
659 int err;
660
661 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
662 if (err)
663 return err;
664
665 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
666 CRYPTO_ALG_TYPE_MASK);
667 if (IS_ERR(alg))
668 return PTR_ERR(alg);
669
Eric Biggers73bf20e2018-06-18 10:22:37 -0700670 err = -EINVAL;
Eric Biggersed331ad2018-06-18 10:22:39 -0700671 if (alg->cra_blocksize != VMAC_NONCEBYTES)
Eric Biggers73bf20e2018-06-18 10:22:37 -0700672 goto out_put_alg;
673
Eric Biggersed331ad2018-06-18 10:22:39 -0700674 inst = shash_alloc_instance(tmpl->name, alg);
Shane Wangf1939f72009-09-02 20:05:22 +1000675 err = PTR_ERR(inst);
676 if (IS_ERR(inst))
677 goto out_put_alg;
678
679 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
680 shash_crypto_instance(inst),
681 CRYPTO_ALG_TYPE_MASK);
682 if (err)
683 goto out_free_inst;
684
685 inst->alg.base.cra_priority = alg->cra_priority;
686 inst->alg.base.cra_blocksize = alg->cra_blocksize;
687 inst->alg.base.cra_alignmask = alg->cra_alignmask;
688
Eric Biggersbb296482018-06-18 10:22:38 -0700689 inst->alg.base.cra_ctxsize = sizeof(struct vmac_tfm_ctx);
Shane Wangf1939f72009-09-02 20:05:22 +1000690 inst->alg.base.cra_init = vmac_init_tfm;
691 inst->alg.base.cra_exit = vmac_exit_tfm;
692
Eric Biggersbb296482018-06-18 10:22:38 -0700693 inst->alg.descsize = sizeof(struct vmac_desc_ctx);
694 inst->alg.digestsize = VMAC_TAG_LEN / 8;
Eric Biggersed331ad2018-06-18 10:22:39 -0700695 if (vmac64) {
696 inst->alg.init = vmac_init;
697 inst->alg.final = vmac_final_be;
698 } else {
699 pr_warn("vmac: using insecure hardcoded nonce\n");
700 inst->alg.init = vmac_init_with_hardcoded_nonce;
701 inst->alg.final = vmac_final_le;
702 }
Shane Wangf1939f72009-09-02 20:05:22 +1000703 inst->alg.update = vmac_update;
Shane Wangf1939f72009-09-02 20:05:22 +1000704 inst->alg.setkey = vmac_setkey;
705
706 err = shash_register_instance(tmpl, inst);
707 if (err) {
708out_free_inst:
709 shash_free_instance(shash_crypto_instance(inst));
710 }
711
712out_put_alg:
713 crypto_mod_put(alg);
714 return err;
715}
716
Eric Biggersed331ad2018-06-18 10:22:39 -0700717static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
718{
719 return vmac_create_common(tmpl, tb, false);
720}
721
722static int vmac64_create(struct crypto_template *tmpl, struct rtattr **tb)
723{
724 return vmac_create_common(tmpl, tb, true);
725}
726
Shane Wangf1939f72009-09-02 20:05:22 +1000727static struct crypto_template vmac_tmpl = {
728 .name = "vmac",
729 .create = vmac_create,
730 .free = shash_free_instance,
731 .module = THIS_MODULE,
732};
733
Eric Biggersed331ad2018-06-18 10:22:39 -0700734static struct crypto_template vmac64_tmpl = {
735 .name = "vmac64",
736 .create = vmac64_create,
737 .free = shash_free_instance,
738 .module = THIS_MODULE,
739};
740
Shane Wangf1939f72009-09-02 20:05:22 +1000741static int __init vmac_module_init(void)
742{
Eric Biggersed331ad2018-06-18 10:22:39 -0700743 int err;
744
745 err = crypto_register_template(&vmac_tmpl);
746 if (err)
747 return err;
748
749 err = crypto_register_template(&vmac64_tmpl);
750 if (err)
751 crypto_unregister_template(&vmac_tmpl);
752
753 return err;
Shane Wangf1939f72009-09-02 20:05:22 +1000754}
755
756static void __exit vmac_module_exit(void)
757{
758 crypto_unregister_template(&vmac_tmpl);
Eric Biggersed331ad2018-06-18 10:22:39 -0700759 crypto_unregister_template(&vmac64_tmpl);
Shane Wangf1939f72009-09-02 20:05:22 +1000760}
761
762module_init(vmac_module_init);
763module_exit(vmac_module_exit);
764
765MODULE_LICENSE("GPL");
766MODULE_DESCRIPTION("VMAC hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800767MODULE_ALIAS_CRYPTO("vmac");
Eric Biggersed331ad2018-06-18 10:22:39 -0700768MODULE_ALIAS_CRYPTO("vmac64");