Back to home page

LXR

 
 

    


0001 /*
0002  * Twofish for CryptoAPI
0003  *
0004  * Originally Twofish for GPG
0005  * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
0006  * 256-bit key length added March 20, 1999
0007  * Some modifications to reduce the text size by Werner Koch, April, 1998
0008  * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
0009  * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
0010  *
0011  * The original author has disclaimed all copyright interest in this
0012  * code and thus put it in the public domain. The subsequent authors 
0013  * have put this under the GNU General Public License.
0014  *
0015  * This program is free software; you can redistribute it and/or modify
0016  * it under the terms of the GNU General Public License as published by
0017  * the Free Software Foundation; either version 2 of the License, or
0018  * (at your option) any later version.
0019  *
0020  * This program is distributed in the hope that it will be useful,
0021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0023  * GNU General Public License for more details.
0024  * 
0025  * You should have received a copy of the GNU General Public License
0026  * along with this program; if not, write to the Free Software
0027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
0028  * USA
0029  *
0030  * This code is a "clean room" implementation, written from the paper
0031  * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
0032  * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
0033  * through http://www.counterpane.com/twofish.html
0034  *
0035  * For background information on multiplication in finite fields, used for
0036  * the matrix operations in the key schedule, see the book _Contemporary
0037  * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
0038  * Third Edition.
0039  */
0040 
0041 #include <asm/byteorder.h>
0042 #include <crypto/twofish.h>
0043 #include <linux/module.h>
0044 #include <linux/init.h>
0045 #include <linux/types.h>
0046 #include <linux/errno.h>
0047 #include <linux/crypto.h>
0048 #include <linux/bitops.h>
0049 
0050 /* Macros to compute the g() function in the encryption and decryption
0051  * rounds.  G1 is the straight g() function; G2 includes the 8-bit
0052  * rotation for the high 32-bit word. */
0053 
0054 #define G1(a) \
0055      (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
0056    ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
0057 
0058 #define G2(b) \
0059      (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
0060    ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
0061 
0062 /* Encryption and decryption Feistel rounds.  Each one calls the two g()
0063  * macros, does the PHT, and performs the XOR and the appropriate bit
0064  * rotations.  The parameters are the round number (used to select subkeys),
0065  * and the four 32-bit chunks of the text. */
0066 
0067 #define ENCROUND(n, a, b, c, d) \
0068    x = G1 (a); y = G2 (b); \
0069    x += y; y += x + ctx->k[2 * (n) + 1]; \
0070    (c) ^= x + ctx->k[2 * (n)]; \
0071    (c) = ror32((c), 1); \
0072    (d) = rol32((d), 1) ^ y
0073 
0074 #define DECROUND(n, a, b, c, d) \
0075    x = G1 (a); y = G2 (b); \
0076    x += y; y += x; \
0077    (d) ^= y + ctx->k[2 * (n) + 1]; \
0078    (d) = ror32((d), 1); \
0079    (c) = rol32((c), 1); \
0080    (c) ^= (x + ctx->k[2 * (n)])
0081 
0082 /* Encryption and decryption cycles; each one is simply two Feistel rounds
0083  * with the 32-bit chunks re-ordered to simulate the "swap" */
0084 
0085 #define ENCCYCLE(n) \
0086    ENCROUND (2 * (n), a, b, c, d); \
0087    ENCROUND (2 * (n) + 1, c, d, a, b)
0088 
0089 #define DECCYCLE(n) \
0090    DECROUND (2 * (n) + 1, c, d, a, b); \
0091    DECROUND (2 * (n), a, b, c, d)
0092 
0093 /* Macros to convert the input and output bytes into 32-bit words,
0094  * and simultaneously perform the whitening step.  INPACK packs word
0095  * number n into the variable named by x, using whitening subkey number m.
0096  * OUTUNPACK unpacks word number n from the variable named by x, using
0097  * whitening subkey number m. */
0098 
0099 #define INPACK(n, x, m) \
0100    x = le32_to_cpu(src[n]) ^ ctx->w[m]
0101 
0102 #define OUTUNPACK(n, x, m) \
0103    x ^= ctx->w[m]; \
0104    dst[n] = cpu_to_le32(x)
0105 
0106 
0107 
0108 /* Encrypt one block.  in and out may be the same. */
0109 static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
0110 {
0111     struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
0112     const __le32 *src = (const __le32 *)in;
0113     __le32 *dst = (__le32 *)out;
0114 
0115     /* The four 32-bit chunks of the text. */
0116     u32 a, b, c, d;
0117     
0118     /* Temporaries used by the round function. */
0119     u32 x, y;
0120 
0121     /* Input whitening and packing. */
0122     INPACK (0, a, 0);
0123     INPACK (1, b, 1);
0124     INPACK (2, c, 2);
0125     INPACK (3, d, 3);
0126     
0127     /* Encryption Feistel cycles. */
0128     ENCCYCLE (0);
0129     ENCCYCLE (1);
0130     ENCCYCLE (2);
0131     ENCCYCLE (3);
0132     ENCCYCLE (4);
0133     ENCCYCLE (5);
0134     ENCCYCLE (6);
0135     ENCCYCLE (7);
0136     
0137     /* Output whitening and unpacking. */
0138     OUTUNPACK (0, c, 4);
0139     OUTUNPACK (1, d, 5);
0140     OUTUNPACK (2, a, 6);
0141     OUTUNPACK (3, b, 7);
0142     
0143 }
0144 
0145 /* Decrypt one block.  in and out may be the same. */
0146 static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
0147 {
0148     struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
0149     const __le32 *src = (const __le32 *)in;
0150     __le32 *dst = (__le32 *)out;
0151   
0152     /* The four 32-bit chunks of the text. */
0153     u32 a, b, c, d;
0154     
0155     /* Temporaries used by the round function. */
0156     u32 x, y;
0157     
0158     /* Input whitening and packing. */
0159     INPACK (0, c, 4);
0160     INPACK (1, d, 5);
0161     INPACK (2, a, 6);
0162     INPACK (3, b, 7);
0163     
0164     /* Encryption Feistel cycles. */
0165     DECCYCLE (7);
0166     DECCYCLE (6);
0167     DECCYCLE (5);
0168     DECCYCLE (4);
0169     DECCYCLE (3);
0170     DECCYCLE (2);
0171     DECCYCLE (1);
0172     DECCYCLE (0);
0173 
0174     /* Output whitening and unpacking. */
0175     OUTUNPACK (0, a, 0);
0176     OUTUNPACK (1, b, 1);
0177     OUTUNPACK (2, c, 2);
0178     OUTUNPACK (3, d, 3);
0179 
0180 }
0181 
0182 static struct crypto_alg alg = {
0183     .cra_name           =   "twofish",
0184     .cra_driver_name    =   "twofish-generic",
0185     .cra_priority       =   100,
0186     .cra_flags          =   CRYPTO_ALG_TYPE_CIPHER,
0187     .cra_blocksize      =   TF_BLOCK_SIZE,
0188     .cra_ctxsize        =   sizeof(struct twofish_ctx),
0189     .cra_alignmask      =   3,
0190     .cra_module         =   THIS_MODULE,
0191     .cra_u              =   { .cipher = {
0192     .cia_min_keysize    =   TF_MIN_KEY_SIZE,
0193     .cia_max_keysize    =   TF_MAX_KEY_SIZE,
0194     .cia_setkey         =   twofish_setkey,
0195     .cia_encrypt        =   twofish_encrypt,
0196     .cia_decrypt        =   twofish_decrypt } }
0197 };
0198 
0199 static int __init twofish_mod_init(void)
0200 {
0201     return crypto_register_alg(&alg);
0202 }
0203 
0204 static void __exit twofish_mod_fini(void)
0205 {
0206     crypto_unregister_alg(&alg);
0207 }
0208 
0209 module_init(twofish_mod_init);
0210 module_exit(twofish_mod_fini);
0211 
0212 MODULE_LICENSE("GPL");
0213 MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
0214 MODULE_ALIAS_CRYPTO("twofish");
0215 MODULE_ALIAS_CRYPTO("twofish-generic");