Back to home page

OSCL-LXR

 
 

    


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