0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
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
0037
0038
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
0049
0050
0051
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
0069
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
0080
0081
0082
0083
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
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
0100 u32 a, b, c, d;
0101
0102
0103 u32 x, y;
0104
0105
0106 INPACK (0, a, 0);
0107 INPACK (1, b, 1);
0108 INPACK (2, c, 2);
0109 INPACK (3, d, 3);
0110
0111
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
0122 OUTUNPACK (0, c, 4);
0123 OUTUNPACK (1, d, 5);
0124 OUTUNPACK (2, a, 6);
0125 OUTUNPACK (3, b, 7);
0126
0127 }
0128
0129
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
0135 u32 a, b, c, d;
0136
0137
0138 u32 x, y;
0139
0140
0141 INPACK (0, c, 4);
0142 INPACK (1, d, 5);
0143 INPACK (2, a, 6);
0144 INPACK (3, b, 7);
0145
0146
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
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");