twofish_generic.c 6.2 KB

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