Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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