1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Twofish for CryptoAPI
5 * Originally Twofish for GPG
6 * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
7 * 256-bit key length added March 20, 1999
8 * Some modifications to reduce the text size by Werner Koch, April, 1998
9 * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
10 * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
12 * The original author has disclaimed all copyright interest in this
13 * code and thus put it in the public domain. The subsequent authors
14 * have put this under the GNU General Public License.
16 * This code is a "clean room" implementation, written from the paper
17 * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
18 * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
19 * through http://www.counterpane.com/twofish.html
21 * For background information on multiplication in finite fields, used for
22 * the matrix operations in the key schedule, see the book _Contemporary
23 * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
27 #include <asm/unaligned.h>
28 #include <crypto/algapi.h>
29 #include <crypto/twofish.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/bitops.h>
36 /* Macros to compute the g() function in the encryption and decryption
37 * rounds. G1 is the straight g() function; G2 includes the 8-bit
38 * rotation for the high 32-bit word. */
41 (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
42 ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
45 (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
46 ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
48 /* Encryption and decryption Feistel rounds. Each one calls the two g()
49 * macros, does the PHT, and performs the XOR and the appropriate bit
50 * rotations. The parameters are the round number (used to select subkeys),
51 * and the four 32-bit chunks of the text. */
53 #define ENCROUND(n, a, b, c, d) \
54 x = G1 (a); y = G2 (b); \
55 x += y; y += x + ctx->k[2 * (n) + 1]; \
56 (c) ^= x + ctx->k[2 * (n)]; \
57 (c) = ror32((c), 1); \
58 (d) = rol32((d), 1) ^ y
60 #define DECROUND(n, a, b, c, d) \
61 x = G1 (a); y = G2 (b); \
63 (d) ^= y + ctx->k[2 * (n) + 1]; \
64 (d) = ror32((d), 1); \
65 (c) = rol32((c), 1); \
66 (c) ^= (x + ctx->k[2 * (n)])
68 /* Encryption and decryption cycles; each one is simply two Feistel rounds
69 * with the 32-bit chunks re-ordered to simulate the "swap" */
72 ENCROUND (2 * (n), a, b, c, d); \
73 ENCROUND (2 * (n) + 1, c, d, a, b)
76 DECROUND (2 * (n) + 1, c, d, a, b); \
77 DECROUND (2 * (n), a, b, c, d)
79 /* Macros to convert the input and output bytes into 32-bit words,
80 * and simultaneously perform the whitening step. INPACK packs word
81 * number n into the variable named by x, using whitening subkey number m.
82 * OUTUNPACK unpacks word number n from the variable named by x, using
83 * whitening subkey number m. */
85 #define INPACK(n, x, m) \
86 x = get_unaligned_le32(in + (n) * 4) ^ ctx->w[m]
88 #define OUTUNPACK(n, x, m) \
90 put_unaligned_le32(x, out + (n) * 4)
94 /* Encrypt one block. in and out may be the same. */
95 static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
97 struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
99 /* The four 32-bit chunks of the text. */
102 /* Temporaries used by the round function. */
105 /* Input whitening and packing. */
111 /* Encryption Feistel cycles. */
121 /* Output whitening and unpacking. */
129 /* Decrypt one block. in and out may be the same. */
130 static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
132 struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
134 /* The four 32-bit chunks of the text. */
137 /* Temporaries used by the round function. */
140 /* Input whitening and packing. */
146 /* Encryption Feistel cycles. */
156 /* Output whitening and unpacking. */
164 static struct crypto_alg alg = {
165 .cra_name = "twofish",
166 .cra_driver_name = "twofish-generic",
168 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
169 .cra_blocksize = TF_BLOCK_SIZE,
170 .cra_ctxsize = sizeof(struct twofish_ctx),
171 .cra_module = THIS_MODULE,
172 .cra_u = { .cipher = {
173 .cia_min_keysize = TF_MIN_KEY_SIZE,
174 .cia_max_keysize = TF_MAX_KEY_SIZE,
175 .cia_setkey = twofish_setkey,
176 .cia_encrypt = twofish_encrypt,
177 .cia_decrypt = twofish_decrypt } }
180 static int __init twofish_mod_init(void)
182 return crypto_register_alg(&alg);
185 static void __exit twofish_mod_fini(void)
187 crypto_unregister_alg(&alg);
190 subsys_initcall(twofish_mod_init);
191 module_exit(twofish_mod_fini);
193 MODULE_LICENSE("GPL");
194 MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
195 MODULE_ALIAS_CRYPTO("twofish");
196 MODULE_ALIAS_CRYPTO("twofish-generic");