blowfish_generic.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Blowfish Cipher Algorithm, by Bruce Schneier.
  5. * http://www.counterpane.com/blowfish.html
  6. *
  7. * Adapted from Kerneli implementation.
  8. *
  9. * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org>
  10. * Copyright (c) Kyle McMartin <kyle@debian.org>
  11. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <asm/byteorder.h>
  23. #include <linux/crypto.h>
  24. #include <linux/types.h>
  25. #include <crypto/blowfish.h>
  26. /*
  27. * Round loop unrolling macros, S is a pointer to a S-Box array
  28. * organized in 4 unsigned longs at a row.
  29. */
  30. #define GET32_3(x) (((x) & 0xff))
  31. #define GET32_2(x) (((x) >> (8)) & (0xff))
  32. #define GET32_1(x) (((x) >> (16)) & (0xff))
  33. #define GET32_0(x) (((x) >> (24)) & (0xff))
  34. #define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \
  35. S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])
  36. #define ROUND(a, b, n) ({ b ^= P[n]; a ^= bf_F(b); })
  37. static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  38. {
  39. struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
  40. const __be32 *in_blk = (const __be32 *)src;
  41. __be32 *const out_blk = (__be32 *)dst;
  42. const u32 *P = ctx->p;
  43. const u32 *S = ctx->s;
  44. u32 yl = be32_to_cpu(in_blk[0]);
  45. u32 yr = be32_to_cpu(in_blk[1]);
  46. ROUND(yr, yl, 0);
  47. ROUND(yl, yr, 1);
  48. ROUND(yr, yl, 2);
  49. ROUND(yl, yr, 3);
  50. ROUND(yr, yl, 4);
  51. ROUND(yl, yr, 5);
  52. ROUND(yr, yl, 6);
  53. ROUND(yl, yr, 7);
  54. ROUND(yr, yl, 8);
  55. ROUND(yl, yr, 9);
  56. ROUND(yr, yl, 10);
  57. ROUND(yl, yr, 11);
  58. ROUND(yr, yl, 12);
  59. ROUND(yl, yr, 13);
  60. ROUND(yr, yl, 14);
  61. ROUND(yl, yr, 15);
  62. yl ^= P[16];
  63. yr ^= P[17];
  64. out_blk[0] = cpu_to_be32(yr);
  65. out_blk[1] = cpu_to_be32(yl);
  66. }
  67. static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  68. {
  69. struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
  70. const __be32 *in_blk = (const __be32 *)src;
  71. __be32 *const out_blk = (__be32 *)dst;
  72. const u32 *P = ctx->p;
  73. const u32 *S = ctx->s;
  74. u32 yl = be32_to_cpu(in_blk[0]);
  75. u32 yr = be32_to_cpu(in_blk[1]);
  76. ROUND(yr, yl, 17);
  77. ROUND(yl, yr, 16);
  78. ROUND(yr, yl, 15);
  79. ROUND(yl, yr, 14);
  80. ROUND(yr, yl, 13);
  81. ROUND(yl, yr, 12);
  82. ROUND(yr, yl, 11);
  83. ROUND(yl, yr, 10);
  84. ROUND(yr, yl, 9);
  85. ROUND(yl, yr, 8);
  86. ROUND(yr, yl, 7);
  87. ROUND(yl, yr, 6);
  88. ROUND(yr, yl, 5);
  89. ROUND(yl, yr, 4);
  90. ROUND(yr, yl, 3);
  91. ROUND(yl, yr, 2);
  92. yl ^= P[1];
  93. yr ^= P[0];
  94. out_blk[0] = cpu_to_be32(yr);
  95. out_blk[1] = cpu_to_be32(yl);
  96. }
  97. static struct crypto_alg alg = {
  98. .cra_name = "blowfish",
  99. .cra_driver_name = "blowfish-generic",
  100. .cra_priority = 100,
  101. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  102. .cra_blocksize = BF_BLOCK_SIZE,
  103. .cra_ctxsize = sizeof(struct bf_ctx),
  104. .cra_alignmask = 3,
  105. .cra_module = THIS_MODULE,
  106. .cra_u = { .cipher = {
  107. .cia_min_keysize = BF_MIN_KEY_SIZE,
  108. .cia_max_keysize = BF_MAX_KEY_SIZE,
  109. .cia_setkey = blowfish_setkey,
  110. .cia_encrypt = bf_encrypt,
  111. .cia_decrypt = bf_decrypt } }
  112. };
  113. static int __init blowfish_mod_init(void)
  114. {
  115. return crypto_register_alg(&alg);
  116. }
  117. static void __exit blowfish_mod_fini(void)
  118. {
  119. crypto_unregister_alg(&alg);
  120. }
  121. module_init(blowfish_mod_init);
  122. module_exit(blowfish_mod_fini);
  123. MODULE_LICENSE("GPL");
  124. MODULE_DESCRIPTION("Blowfish Cipher Algorithm");
  125. MODULE_ALIAS_CRYPTO("blowfish");
  126. MODULE_ALIAS_CRYPTO("blowfish-generic");