twofish_generic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Twofish for CryptoAPI
  4. *
  5. * Originally Twofish for GPG
  6. * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, 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 <Marc@Mutz.com>
  10. * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
  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/byteorder.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 = le32_to_cpu(src[n]) ^ ctx->w[m]
  75. #define OUTUNPACK(n, x, m) \
  76. x ^= ctx->w[m]; \
  77. dst[n] = cpu_to_le32(x)
  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. const __le32 *src = (const __le32 *)in;
  83. __le32 *dst = (__le32 *)out;
  84. /* The four 32-bit chunks of the text. */
  85. u32 a, b, c, d;
  86. /* Temporaries used by the round function. */
  87. u32 x, y;
  88. /* Input whitening and packing. */
  89. INPACK (0, a, 0);
  90. INPACK (1, b, 1);
  91. INPACK (2, c, 2);
  92. INPACK (3, d, 3);
  93. /* Encryption Feistel cycles. */
  94. ENCCYCLE (0);
  95. ENCCYCLE (1);
  96. ENCCYCLE (2);
  97. ENCCYCLE (3);
  98. ENCCYCLE (4);
  99. ENCCYCLE (5);
  100. ENCCYCLE (6);
  101. ENCCYCLE (7);
  102. /* Output whitening and unpacking. */
  103. OUTUNPACK (0, c, 4);
  104. OUTUNPACK (1, d, 5);
  105. OUTUNPACK (2, a, 6);
  106. OUTUNPACK (3, b, 7);
  107. }
  108. /* Decrypt one block. in and out may be the same. */
  109. static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  110. {
  111. struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
  112. const __le32 *src = (const __le32 *)in;
  113. __le32 *dst = (__le32 *)out;
  114. /* The four 32-bit chunks of the text. */
  115. u32 a, b, c, d;
  116. /* Temporaries used by the round function. */
  117. u32 x, y;
  118. /* Input whitening and packing. */
  119. INPACK (0, c, 4);
  120. INPACK (1, d, 5);
  121. INPACK (2, a, 6);
  122. INPACK (3, b, 7);
  123. /* Encryption Feistel cycles. */
  124. DECCYCLE (7);
  125. DECCYCLE (6);
  126. DECCYCLE (5);
  127. DECCYCLE (4);
  128. DECCYCLE (3);
  129. DECCYCLE (2);
  130. DECCYCLE (1);
  131. DECCYCLE (0);
  132. /* Output whitening and unpacking. */
  133. OUTUNPACK (0, a, 0);
  134. OUTUNPACK (1, b, 1);
  135. OUTUNPACK (2, c, 2);
  136. OUTUNPACK (3, d, 3);
  137. }
  138. static struct crypto_alg alg = {
  139. .cra_name = "twofish",
  140. .cra_driver_name = "twofish-generic",
  141. .cra_priority = 100,
  142. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  143. .cra_blocksize = TF_BLOCK_SIZE,
  144. .cra_ctxsize = sizeof(struct twofish_ctx),
  145. .cra_alignmask = 3,
  146. .cra_module = THIS_MODULE,
  147. .cra_u = { .cipher = {
  148. .cia_min_keysize = TF_MIN_KEY_SIZE,
  149. .cia_max_keysize = TF_MAX_KEY_SIZE,
  150. .cia_setkey = twofish_setkey,
  151. .cia_encrypt = twofish_encrypt,
  152. .cia_decrypt = twofish_decrypt } }
  153. };
  154. static int __init twofish_mod_init(void)
  155. {
  156. return crypto_register_alg(&alg);
  157. }
  158. static void __exit twofish_mod_fini(void)
  159. {
  160. crypto_unregister_alg(&alg);
  161. }
  162. subsys_initcall(twofish_mod_init);
  163. module_exit(twofish_mod_fini);
  164. MODULE_LICENSE("GPL");
  165. MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
  166. MODULE_ALIAS_CRYPTO("twofish");
  167. MODULE_ALIAS_CRYPTO("twofish-generic");