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