Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Twofish for CryptoAPI |
| 4 | * |
| 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> |
| 11 | * |
| 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. |
| 15 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 20 | * |
| 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 |
| 24 | * Third Edition. |
| 25 | */ |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 26 | |
| 27 | #include <asm/byteorder.h> |
Joachim Fritschi | 2729bb4 | 2006-06-20 20:37:23 +1000 | [diff] [blame] | 28 | #include <crypto/twofish.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/crypto.h> |
Denis Vlasenko | a5f8c47 | 2006-01-16 17:42:28 +1100 | [diff] [blame] | 34 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
| 39 | |
| 40 | #define G1(a) \ |
| 41 | (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \ |
| 42 | ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24]) |
| 43 | |
| 44 | #define G2(b) \ |
| 45 | (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \ |
| 46 | ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24]) |
| 47 | |
| 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. */ |
| 52 | |
| 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)]; \ |
Denis Vlasenko | a5f8c47 | 2006-01-16 17:42:28 +1100 | [diff] [blame] | 57 | (c) = ror32((c), 1); \ |
| 58 | (d) = rol32((d), 1) ^ y |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | #define DECROUND(n, a, b, c, d) \ |
| 61 | x = G1 (a); y = G2 (b); \ |
| 62 | x += y; y += x; \ |
| 63 | (d) ^= y + ctx->k[2 * (n) + 1]; \ |
Denis Vlasenko | a5f8c47 | 2006-01-16 17:42:28 +1100 | [diff] [blame] | 64 | (d) = ror32((d), 1); \ |
| 65 | (c) = rol32((c), 1); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | (c) ^= (x + ctx->k[2 * (n)]) |
| 67 | |
| 68 | /* Encryption and decryption cycles; each one is simply two Feistel rounds |
| 69 | * with the 32-bit chunks re-ordered to simulate the "swap" */ |
| 70 | |
| 71 | #define ENCCYCLE(n) \ |
| 72 | ENCROUND (2 * (n), a, b, c, d); \ |
| 73 | ENCROUND (2 * (n) + 1, c, d, a, b) |
| 74 | |
| 75 | #define DECCYCLE(n) \ |
| 76 | DECROUND (2 * (n) + 1, c, d, a, b); \ |
| 77 | DECROUND (2 * (n), a, b, c, d) |
| 78 | |
| 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. */ |
| 84 | |
| 85 | #define INPACK(n, x, m) \ |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 86 | x = le32_to_cpu(src[n]) ^ ctx->w[m] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | #define OUTUNPACK(n, x, m) \ |
| 89 | x ^= ctx->w[m]; \ |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 90 | dst[n] = cpu_to_le32(x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* Encrypt one block. in and out may be the same. */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 95 | static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 97 | struct twofish_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 98 | const __le32 *src = (const __le32 *)in; |
| 99 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* The four 32-bit chunks of the text. */ |
| 102 | u32 a, b, c, d; |
| 103 | |
| 104 | /* Temporaries used by the round function. */ |
| 105 | u32 x, y; |
| 106 | |
| 107 | /* Input whitening and packing. */ |
| 108 | INPACK (0, a, 0); |
| 109 | INPACK (1, b, 1); |
| 110 | INPACK (2, c, 2); |
| 111 | INPACK (3, d, 3); |
| 112 | |
| 113 | /* Encryption Feistel cycles. */ |
| 114 | ENCCYCLE (0); |
| 115 | ENCCYCLE (1); |
| 116 | ENCCYCLE (2); |
| 117 | ENCCYCLE (3); |
| 118 | ENCCYCLE (4); |
| 119 | ENCCYCLE (5); |
| 120 | ENCCYCLE (6); |
| 121 | ENCCYCLE (7); |
| 122 | |
| 123 | /* Output whitening and unpacking. */ |
| 124 | OUTUNPACK (0, c, 4); |
| 125 | OUTUNPACK (1, d, 5); |
| 126 | OUTUNPACK (2, a, 6); |
| 127 | OUTUNPACK (3, b, 7); |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /* Decrypt one block. in and out may be the same. */ |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 132 | static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 134 | struct twofish_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 135 | const __le32 *src = (const __le32 *)in; |
| 136 | __le32 *dst = (__le32 *)out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /* The four 32-bit chunks of the text. */ |
| 139 | u32 a, b, c, d; |
| 140 | |
| 141 | /* Temporaries used by the round function. */ |
| 142 | u32 x, y; |
| 143 | |
| 144 | /* Input whitening and packing. */ |
| 145 | INPACK (0, c, 4); |
| 146 | INPACK (1, d, 5); |
| 147 | INPACK (2, a, 6); |
| 148 | INPACK (3, b, 7); |
| 149 | |
| 150 | /* Encryption Feistel cycles. */ |
| 151 | DECCYCLE (7); |
| 152 | DECCYCLE (6); |
| 153 | DECCYCLE (5); |
| 154 | DECCYCLE (4); |
| 155 | DECCYCLE (3); |
| 156 | DECCYCLE (2); |
| 157 | DECCYCLE (1); |
| 158 | DECCYCLE (0); |
| 159 | |
| 160 | /* Output whitening and unpacking. */ |
| 161 | OUTUNPACK (0, a, 0); |
| 162 | OUTUNPACK (1, b, 1); |
| 163 | OUTUNPACK (2, c, 2); |
| 164 | OUTUNPACK (3, d, 3); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | static struct crypto_alg alg = { |
| 169 | .cra_name = "twofish", |
Joachim Fritschi | 758f570 | 2006-06-20 20:39:29 +1000 | [diff] [blame] | 170 | .cra_driver_name = "twofish-generic", |
| 171 | .cra_priority = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 173 | .cra_blocksize = TF_BLOCK_SIZE, |
| 174 | .cra_ctxsize = sizeof(struct twofish_ctx), |
Herbert Xu | a429d26 | 2006-01-07 16:38:15 +1100 | [diff] [blame] | 175 | .cra_alignmask = 3, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | .cra_module = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | .cra_u = { .cipher = { |
| 178 | .cia_min_keysize = TF_MIN_KEY_SIZE, |
| 179 | .cia_max_keysize = TF_MAX_KEY_SIZE, |
| 180 | .cia_setkey = twofish_setkey, |
| 181 | .cia_encrypt = twofish_encrypt, |
| 182 | .cia_decrypt = twofish_decrypt } } |
| 183 | }; |
| 184 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 185 | static int __init twofish_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
| 187 | return crypto_register_alg(&alg); |
| 188 | } |
| 189 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 190 | static void __exit twofish_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | crypto_unregister_alg(&alg); |
| 193 | } |
| 194 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 195 | subsys_initcall(twofish_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 196 | module_exit(twofish_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | MODULE_LICENSE("GPL"); |
| 199 | MODULE_DESCRIPTION ("Twofish Cipher Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 200 | MODULE_ALIAS_CRYPTO("twofish"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 201 | MODULE_ALIAS_CRYPTO("twofish-generic"); |