18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Twofish for CryptoAPI
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Originally Twofish for GPG
68c2ecf20Sopenharmony_ci * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
78c2ecf20Sopenharmony_ci * 256-bit key length added March 20, 1999
88c2ecf20Sopenharmony_ci * Some modifications to reduce the text size by Werner Koch, April, 1998
98c2ecf20Sopenharmony_ci * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
108c2ecf20Sopenharmony_ci * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The original author has disclaimed all copyright interest in this
138c2ecf20Sopenharmony_ci * code and thus put it in the public domain. The subsequent authors
148c2ecf20Sopenharmony_ci * have put this under the GNU General Public License.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This code is a "clean room" implementation, written from the paper
178c2ecf20Sopenharmony_ci * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
188c2ecf20Sopenharmony_ci * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
198c2ecf20Sopenharmony_ci * through http://www.counterpane.com/twofish.html
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * For background information on multiplication in finite fields, used for
228c2ecf20Sopenharmony_ci * the matrix operations in the key schedule, see the book _Contemporary
238c2ecf20Sopenharmony_ci * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
248c2ecf20Sopenharmony_ci * Third Edition.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
288c2ecf20Sopenharmony_ci#include <crypto/twofish.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/init.h>
318c2ecf20Sopenharmony_ci#include <linux/types.h>
328c2ecf20Sopenharmony_ci#include <linux/errno.h>
338c2ecf20Sopenharmony_ci#include <linux/crypto.h>
348c2ecf20Sopenharmony_ci#include <linux/bitops.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Macros to compute the g() function in the encryption and decryption
378c2ecf20Sopenharmony_ci * rounds.  G1 is the straight g() function; G2 includes the 8-bit
388c2ecf20Sopenharmony_ci * rotation for the high 32-bit word. */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define G1(a) \
418c2ecf20Sopenharmony_ci     (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
428c2ecf20Sopenharmony_ci   ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define G2(b) \
458c2ecf20Sopenharmony_ci     (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
468c2ecf20Sopenharmony_ci   ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Encryption and decryption Feistel rounds.  Each one calls the two g()
498c2ecf20Sopenharmony_ci * macros, does the PHT, and performs the XOR and the appropriate bit
508c2ecf20Sopenharmony_ci * rotations.  The parameters are the round number (used to select subkeys),
518c2ecf20Sopenharmony_ci * and the four 32-bit chunks of the text. */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define ENCROUND(n, a, b, c, d) \
548c2ecf20Sopenharmony_ci   x = G1 (a); y = G2 (b); \
558c2ecf20Sopenharmony_ci   x += y; y += x + ctx->k[2 * (n) + 1]; \
568c2ecf20Sopenharmony_ci   (c) ^= x + ctx->k[2 * (n)]; \
578c2ecf20Sopenharmony_ci   (c) = ror32((c), 1); \
588c2ecf20Sopenharmony_ci   (d) = rol32((d), 1) ^ y
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define DECROUND(n, a, b, c, d) \
618c2ecf20Sopenharmony_ci   x = G1 (a); y = G2 (b); \
628c2ecf20Sopenharmony_ci   x += y; y += x; \
638c2ecf20Sopenharmony_ci   (d) ^= y + ctx->k[2 * (n) + 1]; \
648c2ecf20Sopenharmony_ci   (d) = ror32((d), 1); \
658c2ecf20Sopenharmony_ci   (c) = rol32((c), 1); \
668c2ecf20Sopenharmony_ci   (c) ^= (x + ctx->k[2 * (n)])
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Encryption and decryption cycles; each one is simply two Feistel rounds
698c2ecf20Sopenharmony_ci * with the 32-bit chunks re-ordered to simulate the "swap" */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define ENCCYCLE(n) \
728c2ecf20Sopenharmony_ci   ENCROUND (2 * (n), a, b, c, d); \
738c2ecf20Sopenharmony_ci   ENCROUND (2 * (n) + 1, c, d, a, b)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define DECCYCLE(n) \
768c2ecf20Sopenharmony_ci   DECROUND (2 * (n) + 1, c, d, a, b); \
778c2ecf20Sopenharmony_ci   DECROUND (2 * (n), a, b, c, d)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Macros to convert the input and output bytes into 32-bit words,
808c2ecf20Sopenharmony_ci * and simultaneously perform the whitening step.  INPACK packs word
818c2ecf20Sopenharmony_ci * number n into the variable named by x, using whitening subkey number m.
828c2ecf20Sopenharmony_ci * OUTUNPACK unpacks word number n from the variable named by x, using
838c2ecf20Sopenharmony_ci * whitening subkey number m. */
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define INPACK(n, x, m) \
868c2ecf20Sopenharmony_ci   x = le32_to_cpu(src[n]) ^ ctx->w[m]
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define OUTUNPACK(n, x, m) \
898c2ecf20Sopenharmony_ci   x ^= ctx->w[m]; \
908c2ecf20Sopenharmony_ci   dst[n] = cpu_to_le32(x)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* Encrypt one block.  in and out may be the same. */
958c2ecf20Sopenharmony_cistatic void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
988c2ecf20Sopenharmony_ci	const __le32 *src = (const __le32 *)in;
998c2ecf20Sopenharmony_ci	__le32 *dst = (__le32 *)out;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* The four 32-bit chunks of the text. */
1028c2ecf20Sopenharmony_ci	u32 a, b, c, d;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Temporaries used by the round function. */
1058c2ecf20Sopenharmony_ci	u32 x, y;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* Input whitening and packing. */
1088c2ecf20Sopenharmony_ci	INPACK (0, a, 0);
1098c2ecf20Sopenharmony_ci	INPACK (1, b, 1);
1108c2ecf20Sopenharmony_ci	INPACK (2, c, 2);
1118c2ecf20Sopenharmony_ci	INPACK (3, d, 3);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* Encryption Feistel cycles. */
1148c2ecf20Sopenharmony_ci	ENCCYCLE (0);
1158c2ecf20Sopenharmony_ci	ENCCYCLE (1);
1168c2ecf20Sopenharmony_ci	ENCCYCLE (2);
1178c2ecf20Sopenharmony_ci	ENCCYCLE (3);
1188c2ecf20Sopenharmony_ci	ENCCYCLE (4);
1198c2ecf20Sopenharmony_ci	ENCCYCLE (5);
1208c2ecf20Sopenharmony_ci	ENCCYCLE (6);
1218c2ecf20Sopenharmony_ci	ENCCYCLE (7);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Output whitening and unpacking. */
1248c2ecf20Sopenharmony_ci	OUTUNPACK (0, c, 4);
1258c2ecf20Sopenharmony_ci	OUTUNPACK (1, d, 5);
1268c2ecf20Sopenharmony_ci	OUTUNPACK (2, a, 6);
1278c2ecf20Sopenharmony_ci	OUTUNPACK (3, b, 7);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* Decrypt one block.  in and out may be the same. */
1328c2ecf20Sopenharmony_cistatic void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
1358c2ecf20Sopenharmony_ci	const __le32 *src = (const __le32 *)in;
1368c2ecf20Sopenharmony_ci	__le32 *dst = (__le32 *)out;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/* The four 32-bit chunks of the text. */
1398c2ecf20Sopenharmony_ci	u32 a, b, c, d;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* Temporaries used by the round function. */
1428c2ecf20Sopenharmony_ci	u32 x, y;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Input whitening and packing. */
1458c2ecf20Sopenharmony_ci	INPACK (0, c, 4);
1468c2ecf20Sopenharmony_ci	INPACK (1, d, 5);
1478c2ecf20Sopenharmony_ci	INPACK (2, a, 6);
1488c2ecf20Sopenharmony_ci	INPACK (3, b, 7);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/* Encryption Feistel cycles. */
1518c2ecf20Sopenharmony_ci	DECCYCLE (7);
1528c2ecf20Sopenharmony_ci	DECCYCLE (6);
1538c2ecf20Sopenharmony_ci	DECCYCLE (5);
1548c2ecf20Sopenharmony_ci	DECCYCLE (4);
1558c2ecf20Sopenharmony_ci	DECCYCLE (3);
1568c2ecf20Sopenharmony_ci	DECCYCLE (2);
1578c2ecf20Sopenharmony_ci	DECCYCLE (1);
1588c2ecf20Sopenharmony_ci	DECCYCLE (0);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* Output whitening and unpacking. */
1618c2ecf20Sopenharmony_ci	OUTUNPACK (0, a, 0);
1628c2ecf20Sopenharmony_ci	OUTUNPACK (1, b, 1);
1638c2ecf20Sopenharmony_ci	OUTUNPACK (2, c, 2);
1648c2ecf20Sopenharmony_ci	OUTUNPACK (3, d, 3);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic struct crypto_alg alg = {
1698c2ecf20Sopenharmony_ci	.cra_name           =   "twofish",
1708c2ecf20Sopenharmony_ci	.cra_driver_name    =   "twofish-generic",
1718c2ecf20Sopenharmony_ci	.cra_priority       =   100,
1728c2ecf20Sopenharmony_ci	.cra_flags          =   CRYPTO_ALG_TYPE_CIPHER,
1738c2ecf20Sopenharmony_ci	.cra_blocksize      =   TF_BLOCK_SIZE,
1748c2ecf20Sopenharmony_ci	.cra_ctxsize        =   sizeof(struct twofish_ctx),
1758c2ecf20Sopenharmony_ci	.cra_alignmask      =	3,
1768c2ecf20Sopenharmony_ci	.cra_module         =   THIS_MODULE,
1778c2ecf20Sopenharmony_ci	.cra_u              =   { .cipher = {
1788c2ecf20Sopenharmony_ci	.cia_min_keysize    =   TF_MIN_KEY_SIZE,
1798c2ecf20Sopenharmony_ci	.cia_max_keysize    =   TF_MAX_KEY_SIZE,
1808c2ecf20Sopenharmony_ci	.cia_setkey         =   twofish_setkey,
1818c2ecf20Sopenharmony_ci	.cia_encrypt        =   twofish_encrypt,
1828c2ecf20Sopenharmony_ci	.cia_decrypt        =   twofish_decrypt } }
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int __init twofish_mod_init(void)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return crypto_register_alg(&alg);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void __exit twofish_mod_fini(void)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	crypto_unregister_alg(&alg);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cisubsys_initcall(twofish_mod_init);
1968c2ecf20Sopenharmony_cimodule_exit(twofish_mod_fini);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1998c2ecf20Sopenharmony_ciMODULE_DESCRIPTION ("Twofish Cipher Algorithm");
2008c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish");
2018c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish-generic");
202