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