2 * Twofish for CryptoAPI
4 * Originally Twofish for GPG
5 * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
6 * 256-bit key length added March 20, 1999
7 * Some modifications to reduce the text size by Werner Koch, April, 1998
8 * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
9 * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
11 * The original author has disclaimed all copyright interest in this
12 * code and thus put it in the public domain. The subsequent authors
13 * have put this under the GNU General Public License.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * This code is a "clean room" implementation, written from the paper
30 * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
31 * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
32 * through http://www.counterpane.com/twofish.html
34 * For background information on multiplication in finite fields, used for
35 * the matrix operations in the key schedule, see the book _Contemporary
36 * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
40 #include <asm/byteorder.h>
41 #include <crypto/twofish.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/types.h>
45 #include <linux/errno.h>
46 #include <linux/crypto.h>
47 #include <linux/bitops.h>
49 /* Macros to compute the g() function in the encryption and decryption
50 * rounds. G1 is the straight g() function; G2 includes the 8-bit
51 * rotation for the high 32-bit word. */
54 (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
55 ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
58 (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
59 ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
61 /* Encryption and decryption Feistel rounds. Each one calls the two g()
62 * macros, does the PHT, and performs the XOR and the appropriate bit
63 * rotations. The parameters are the round number (used to select subkeys),
64 * and the four 32-bit chunks of the text. */
66 #define ENCROUND(n, a, b, c, d) \
67 x = G1 (a); y = G2 (b); \
68 x += y; y += x + ctx->k[2 * (n) + 1]; \
69 (c) ^= x + ctx->k[2 * (n)]; \
70 (c) = ror32((c), 1); \
71 (d) = rol32((d), 1) ^ y
73 #define DECROUND(n, a, b, c, d) \
74 x = G1 (a); y = G2 (b); \
76 (d) ^= y + ctx->k[2 * (n) + 1]; \
77 (d) = ror32((d), 1); \
78 (c) = rol32((c), 1); \
79 (c) ^= (x + ctx->k[2 * (n)])
81 /* Encryption and decryption cycles; each one is simply two Feistel rounds
82 * with the 32-bit chunks re-ordered to simulate the "swap" */
85 ENCROUND (2 * (n), a, b, c, d); \
86 ENCROUND (2 * (n) + 1, c, d, a, b)
89 DECROUND (2 * (n) + 1, c, d, a, b); \
90 DECROUND (2 * (n), a, b, c, d)
92 /* Macros to convert the input and output bytes into 32-bit words,
93 * and simultaneously perform the whitening step. INPACK packs word
94 * number n into the variable named by x, using whitening subkey number m.
95 * OUTUNPACK unpacks word number n from the variable named by x, using
96 * whitening subkey number m. */
98 #define INPACK(n, x, m) \
99 x = le32_to_cpu(src[n]) ^ ctx->w[m]
101 #define OUTUNPACK(n, x, m) \
103 dst[n] = cpu_to_le32(x)
107 /* Encrypt one block. in and out may be the same. */
108 static void twofish_encrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
110 struct twofish_ctx
*ctx
= crypto_tfm_ctx(tfm
);
111 const __le32
*src
= (const __le32
*)in
;
112 __le32
*dst
= (__le32
*)out
;
114 /* The four 32-bit chunks of the text. */
117 /* Temporaries used by the round function. */
120 /* Input whitening and packing. */
126 /* Encryption Feistel cycles. */
136 /* Output whitening and unpacking. */
144 /* Decrypt one block. in and out may be the same. */
145 static void twofish_decrypt(struct crypto_tfm
*tfm
, u8
*out
, const u8
*in
)
147 struct twofish_ctx
*ctx
= crypto_tfm_ctx(tfm
);
148 const __le32
*src
= (const __le32
*)in
;
149 __le32
*dst
= (__le32
*)out
;
151 /* The four 32-bit chunks of the text. */
154 /* Temporaries used by the round function. */
157 /* Input whitening and packing. */
163 /* Encryption Feistel cycles. */
173 /* Output whitening and unpacking. */
181 static struct crypto_alg alg
= {
182 .cra_name
= "twofish",
183 .cra_driver_name
= "twofish-generic",
185 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
186 .cra_blocksize
= TF_BLOCK_SIZE
,
187 .cra_ctxsize
= sizeof(struct twofish_ctx
),
189 .cra_module
= THIS_MODULE
,
190 .cra_u
= { .cipher
= {
191 .cia_min_keysize
= TF_MIN_KEY_SIZE
,
192 .cia_max_keysize
= TF_MAX_KEY_SIZE
,
193 .cia_setkey
= twofish_setkey
,
194 .cia_encrypt
= twofish_encrypt
,
195 .cia_decrypt
= twofish_decrypt
} }
198 static int __init
twofish_mod_init(void)
200 return crypto_register_alg(&alg
);
203 static void __exit
twofish_mod_fini(void)
205 crypto_unregister_alg(&alg
);
208 module_init(twofish_mod_init
);
209 module_exit(twofish_mod_fini
);
211 MODULE_LICENSE("GPL");
212 MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
213 MODULE_ALIAS_CRYPTO("twofish");
214 MODULE_ALIAS_CRYPTO("twofish-generic");