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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Twofish for CryptoAPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Originally Twofish for GPG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * 256-bit key length added March 20, 1999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Some modifications to reduce the text size by Werner Koch, April, 1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * The original author has disclaimed all copyright interest in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * code and thus put it in the public domain. The subsequent authors 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * have put this under the GNU General Public License.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This code is a "clean room" implementation, written from the paper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * through http://www.counterpane.com/twofish.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * For background information on multiplication in finite fields, used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * the matrix operations in the key schedule, see the book _Contemporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Third Edition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <crypto/twofish.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* Macros to compute the g() function in the encryption and decryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * rounds.  G1 is the straight g() function; G2 includes the 8-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * rotation for the high 32-bit word. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define G1(a) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)      (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)    ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define G2(b) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)      (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)    ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* Encryption and decryption Feistel rounds.  Each one calls the two g()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * macros, does the PHT, and performs the XOR and the appropriate bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * rotations.  The parameters are the round number (used to select subkeys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * and the four 32-bit chunks of the text. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define ENCROUND(n, a, b, c, d) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)    x = G1 (a); y = G2 (b); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)    x += y; y += x + ctx->k[2 * (n) + 1]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)    (c) ^= x + ctx->k[2 * (n)]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)    (c) = ror32((c), 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)    (d) = rol32((d), 1) ^ y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define DECROUND(n, a, b, c, d) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)    x = G1 (a); y = G2 (b); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)    x += y; y += x; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)    (d) ^= y + ctx->k[2 * (n) + 1]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)    (d) = ror32((d), 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)    (c) = rol32((c), 1); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)    (c) ^= (x + ctx->k[2 * (n)])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /* Encryption and decryption cycles; each one is simply two Feistel rounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * with the 32-bit chunks re-ordered to simulate the "swap" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define ENCCYCLE(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)    ENCROUND (2 * (n), a, b, c, d); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)    ENCROUND (2 * (n) + 1, c, d, a, b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define DECCYCLE(n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)    DECROUND (2 * (n) + 1, c, d, a, b); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)    DECROUND (2 * (n), a, b, c, d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /* Macros to convert the input and output bytes into 32-bit words,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * and simultaneously perform the whitening step.  INPACK packs word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * number n into the variable named by x, using whitening subkey number m.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * OUTUNPACK unpacks word number n from the variable named by x, using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * whitening subkey number m. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define INPACK(n, x, m) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)    x = le32_to_cpu(src[n]) ^ ctx->w[m]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define OUTUNPACK(n, x, m) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)    x ^= ctx->w[m]; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)    dst[n] = cpu_to_le32(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /* Encrypt one block.  in and out may be the same. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	const __le32 *src = (const __le32 *)in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	__le32 *dst = (__le32 *)out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	/* The four 32-bit chunks of the text. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	u32 a, b, c, d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* Temporaries used by the round function. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	u32 x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	/* Input whitening and packing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	INPACK (0, a, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	INPACK (1, b, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	INPACK (2, c, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	INPACK (3, d, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* Encryption Feistel cycles. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ENCCYCLE (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ENCCYCLE (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ENCCYCLE (2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ENCCYCLE (3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	ENCCYCLE (4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ENCCYCLE (5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	ENCCYCLE (6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	ENCCYCLE (7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Output whitening and unpacking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	OUTUNPACK (0, c, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	OUTUNPACK (1, d, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	OUTUNPACK (2, a, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	OUTUNPACK (3, b, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Decrypt one block.  in and out may be the same. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	const __le32 *src = (const __le32 *)in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	__le32 *dst = (__le32 *)out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)   
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/* The four 32-bit chunks of the text. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	u32 a, b, c, d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* Temporaries used by the round function. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	u32 x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/* Input whitening and packing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	INPACK (0, c, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	INPACK (1, d, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	INPACK (2, a, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	INPACK (3, b, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* Encryption Feistel cycles. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	DECCYCLE (7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	DECCYCLE (6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	DECCYCLE (5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	DECCYCLE (4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	DECCYCLE (3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	DECCYCLE (2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	DECCYCLE (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	DECCYCLE (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* Output whitening and unpacking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	OUTUNPACK (0, a, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	OUTUNPACK (1, b, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	OUTUNPACK (2, c, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	OUTUNPACK (3, d, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static struct crypto_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.cra_name           =   "twofish",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	.cra_driver_name    =   "twofish-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.cra_priority       =   100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.cra_flags          =   CRYPTO_ALG_TYPE_CIPHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.cra_blocksize      =   TF_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.cra_ctxsize        =   sizeof(struct twofish_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.cra_alignmask      =	3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.cra_module         =   THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.cra_u              =   { .cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	.cia_min_keysize    =   TF_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	.cia_max_keysize    =   TF_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	.cia_setkey         =   twofish_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	.cia_encrypt        =   twofish_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	.cia_decrypt        =   twofish_decrypt } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int __init twofish_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return crypto_register_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void __exit twofish_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	crypto_unregister_alg(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) subsys_initcall(twofish_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) module_exit(twofish_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_ALIAS_CRYPTO("twofish");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_ALIAS_CRYPTO("twofish-generic");