twofish_generic.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Twofish for CryptoAPI
  4. *
  5. * Originally Twofish for GPG
  6. * By Matthew Skala <[email protected]>, July 26, 1998
  7. * 256-bit key length added March 20, 1999
  8. * Some modifications to reduce the text size by Werner Koch, April, 1998
  9. * Ported to the kerneli patch by Marc Mutz <[email protected]>
  10. * Ported to CryptoAPI by Colin Slater <[email protected]>
  11. *
  12. * The original author has disclaimed all copyright interest in this
  13. * code and thus put it in the public domain. The subsequent authors
  14. * have put this under the GNU General Public License.
  15. *
  16. * This code is a "clean room" implementation, written from the paper
  17. * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
  18. * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
  19. * through http://www.counterpane.com/twofish.html
  20. *
  21. * For background information on multiplication in finite fields, used for
  22. * the matrix operations in the key schedule, see the book _Contemporary
  23. * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
  24. * Third Edition.
  25. */
  26. #include <asm/unaligned.h>
  27. #include <crypto/twofish.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/errno.h>
  32. #include <linux/crypto.h>
  33. #include <linux/bitops.h>
  34. /* Macros to compute the g() function in the encryption and decryption
  35. * rounds. G1 is the straight g() function; G2 includes the 8-bit
  36. * rotation for the high 32-bit word. */
  37. #define G1(a) \
  38. (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
  39. ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
  40. #define G2(b) \
  41. (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
  42. ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
  43. /* Encryption and decryption Feistel rounds. Each one calls the two g()
  44. * macros, does the PHT, and performs the XOR and the appropriate bit
  45. * rotations. The parameters are the round number (used to select subkeys),
  46. * and the four 32-bit chunks of the text. */
  47. #define ENCROUND(n, a, b, c, d) \
  48. x = G1 (a); y = G2 (b); \
  49. x += y; y += x + ctx->k[2 * (n) + 1]; \
  50. (c) ^= x + ctx->k[2 * (n)]; \
  51. (c) = ror32((c), 1); \
  52. (d) = rol32((d), 1) ^ y
  53. #define DECROUND(n, a, b, c, d) \
  54. x = G1 (a); y = G2 (b); \
  55. x += y; y += x; \
  56. (d) ^= y + ctx->k[2 * (n) + 1]; \
  57. (d) = ror32((d), 1); \
  58. (c) = rol32((c), 1); \
  59. (c) ^= (x + ctx->k[2 * (n)])
  60. /* Encryption and decryption cycles; each one is simply two Feistel rounds
  61. * with the 32-bit chunks re-ordered to simulate the "swap" */
  62. #define ENCCYCLE(n) \
  63. ENCROUND (2 * (n), a, b, c, d); \
  64. ENCROUND (2 * (n) + 1, c, d, a, b)
  65. #define DECCYCLE(n) \
  66. DECROUND (2 * (n) + 1, c, d, a, b); \
  67. DECROUND (2 * (n), a, b, c, d)
  68. /* Macros to convert the input and output bytes into 32-bit words,
  69. * and simultaneously perform the whitening step. INPACK packs word
  70. * number n into the variable named by x, using whitening subkey number m.
  71. * OUTUNPACK unpacks word number n from the variable named by x, using
  72. * whitening subkey number m. */
  73. #define INPACK(n, x, m) \
  74. x = get_unaligned_le32(in + (n) * 4) ^ ctx->w[m]
  75. #define OUTUNPACK(n, x, m) \
  76. x ^= ctx->w[m]; \
  77. put_unaligned_le32(x, out + (n) * 4)
  78. /* Encrypt one block. in and out may be the same. */
  79. static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  80. {
  81. struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
  82. /* The four 32-bit chunks of the text. */
  83. u32 a, b, c, d;
  84. /* Temporaries used by the round function. */
  85. u32 x, y;
  86. /* Input whitening and packing. */
  87. INPACK (0, a, 0);
  88. INPACK (1, b, 1);
  89. INPACK (2, c, 2);
  90. INPACK (3, d, 3);
  91. /* Encryption Feistel cycles. */
  92. ENCCYCLE (0);
  93. ENCCYCLE (1);
  94. ENCCYCLE (2);
  95. ENCCYCLE (3);
  96. ENCCYCLE (4);
  97. ENCCYCLE (5);
  98. ENCCYCLE (6);
  99. ENCCYCLE (7);
  100. /* Output whitening and unpacking. */
  101. OUTUNPACK (0, c, 4);
  102. OUTUNPACK (1, d, 5);
  103. OUTUNPACK (2, a, 6);
  104. OUTUNPACK (3, b, 7);
  105. }
  106. /* Decrypt one block. in and out may be the same. */
  107. static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  108. {
  109. struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
  110. /* The four 32-bit chunks of the text. */
  111. u32 a, b, c, d;
  112. /* Temporaries used by the round function. */
  113. u32 x, y;
  114. /* Input whitening and packing. */
  115. INPACK (0, c, 4);
  116. INPACK (1, d, 5);
  117. INPACK (2, a, 6);
  118. INPACK (3, b, 7);
  119. /* Encryption Feistel cycles. */
  120. DECCYCLE (7);
  121. DECCYCLE (6);
  122. DECCYCLE (5);
  123. DECCYCLE (4);
  124. DECCYCLE (3);
  125. DECCYCLE (2);
  126. DECCYCLE (1);
  127. DECCYCLE (0);
  128. /* Output whitening and unpacking. */
  129. OUTUNPACK (0, a, 0);
  130. OUTUNPACK (1, b, 1);
  131. OUTUNPACK (2, c, 2);
  132. OUTUNPACK (3, d, 3);
  133. }
  134. static struct crypto_alg alg = {
  135. .cra_name = "twofish",
  136. .cra_driver_name = "twofish-generic",
  137. .cra_priority = 100,
  138. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  139. .cra_blocksize = TF_BLOCK_SIZE,
  140. .cra_ctxsize = sizeof(struct twofish_ctx),
  141. .cra_module = THIS_MODULE,
  142. .cra_u = { .cipher = {
  143. .cia_min_keysize = TF_MIN_KEY_SIZE,
  144. .cia_max_keysize = TF_MAX_KEY_SIZE,
  145. .cia_setkey = twofish_setkey,
  146. .cia_encrypt = twofish_encrypt,
  147. .cia_decrypt = twofish_decrypt } }
  148. };
  149. static int __init twofish_mod_init(void)
  150. {
  151. return crypto_register_alg(&alg);
  152. }
  153. static void __exit twofish_mod_fini(void)
  154. {
  155. crypto_unregister_alg(&alg);
  156. }
  157. subsys_initcall(twofish_mod_init);
  158. module_exit(twofish_mod_fini);
  159. MODULE_LICENSE("GPL");
  160. MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
  161. MODULE_ALIAS_CRYPTO("twofish");
  162. MODULE_ALIAS_CRYPTO("twofish-generic");