123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #include <asm/byteorder.h>
- #include <crypto/twofish.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/errno.h>
- #include <linux/crypto.h>
- #include <linux/bitops.h>
- #define G1(a) \
- (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
- ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
- #define G2(b) \
- (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
- ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
- #define ENCROUND(n, a, b, c, d) \
- x = G1 (a); y = G2 (b); \
- x += y; y += x + ctx->k[2 * (n) + 1]; \
- (c) ^= x + ctx->k[2 * (n)]; \
- (c) = ror32((c), 1); \
- (d) = rol32((d), 1) ^ y
- #define DECROUND(n, a, b, c, d) \
- x = G1 (a); y = G2 (b); \
- x += y; y += x; \
- (d) ^= y + ctx->k[2 * (n) + 1]; \
- (d) = ror32((d), 1); \
- (c) = rol32((c), 1); \
- (c) ^= (x + ctx->k[2 * (n)])
- #define ENCCYCLE(n) \
- ENCROUND (2 * (n), a, b, c, d); \
- ENCROUND (2 * (n) + 1, c, d, a, b)
- #define DECCYCLE(n) \
- DECROUND (2 * (n) + 1, c, d, a, b); \
- DECROUND (2 * (n), a, b, c, d)
- #define INPACK(n, x, m) \
- x = le32_to_cpu(src[n]) ^ ctx->w[m]
- #define OUTUNPACK(n, x, m) \
- x ^= ctx->w[m]; \
- dst[n] = cpu_to_le32(x)
- static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
- {
- struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
- const __le32 *src = (const __le32 *)in;
- __le32 *dst = (__le32 *)out;
-
- u32 a, b, c, d;
-
-
- u32 x, y;
-
- INPACK (0, a, 0);
- INPACK (1, b, 1);
- INPACK (2, c, 2);
- INPACK (3, d, 3);
-
-
- ENCCYCLE (0);
- ENCCYCLE (1);
- ENCCYCLE (2);
- ENCCYCLE (3);
- ENCCYCLE (4);
- ENCCYCLE (5);
- ENCCYCLE (6);
- ENCCYCLE (7);
-
-
- OUTUNPACK (0, c, 4);
- OUTUNPACK (1, d, 5);
- OUTUNPACK (2, a, 6);
- OUTUNPACK (3, b, 7);
-
- }
- static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
- {
- struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
- const __le32 *src = (const __le32 *)in;
- __le32 *dst = (__le32 *)out;
-
-
- u32 a, b, c, d;
-
-
- u32 x, y;
-
-
- INPACK (0, c, 4);
- INPACK (1, d, 5);
- INPACK (2, a, 6);
- INPACK (3, b, 7);
-
-
- DECCYCLE (7);
- DECCYCLE (6);
- DECCYCLE (5);
- DECCYCLE (4);
- DECCYCLE (3);
- DECCYCLE (2);
- DECCYCLE (1);
- DECCYCLE (0);
-
- OUTUNPACK (0, a, 0);
- OUTUNPACK (1, b, 1);
- OUTUNPACK (2, c, 2);
- OUTUNPACK (3, d, 3);
- }
- static struct crypto_alg alg = {
- .cra_name = "twofish",
- .cra_driver_name = "twofish-generic",
- .cra_priority = 100,
- .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
- .cra_blocksize = TF_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct twofish_ctx),
- .cra_alignmask = 3,
- .cra_module = THIS_MODULE,
- .cra_u = { .cipher = {
- .cia_min_keysize = TF_MIN_KEY_SIZE,
- .cia_max_keysize = TF_MAX_KEY_SIZE,
- .cia_setkey = twofish_setkey,
- .cia_encrypt = twofish_encrypt,
- .cia_decrypt = twofish_decrypt } }
- };
- static int __init twofish_mod_init(void)
- {
- return crypto_register_alg(&alg);
- }
- static void __exit twofish_mod_fini(void)
- {
- crypto_unregister_alg(&alg);
- }
- module_init(twofish_mod_init);
- module_exit(twofish_mod_fini);
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
- MODULE_ALIAS_CRYPTO("twofish");
- MODULE_ALIAS_CRYPTO("twofish-generic");
|