blowfish_generic.c 3.1 KB

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