twofish_avx_glue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue Code for AVX assembler version of Twofish Cipher
  4. *
  5. * Copyright (C) 2012 Johannes Goetzfried
  6. * <[email protected]>
  7. *
  8. * Copyright © 2013 Jussi Kivilinna <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/crypto.h>
  13. #include <linux/err.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/internal/simd.h>
  16. #include <crypto/twofish.h>
  17. #include "twofish.h"
  18. #include "ecb_cbc_helpers.h"
  19. #define TWOFISH_PARALLEL_BLOCKS 8
  20. /* 8-way parallel cipher functions */
  21. asmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
  22. asmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
  23. asmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
  24. static int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
  25. const u8 *key, unsigned int keylen)
  26. {
  27. return twofish_setkey(&tfm->base, key, keylen);
  28. }
  29. static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
  30. {
  31. __twofish_enc_blk_3way(ctx, dst, src, false);
  32. }
  33. static int ecb_encrypt(struct skcipher_request *req)
  34. {
  35. ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
  36. ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_enc_8way);
  37. ECB_BLOCK(3, twofish_enc_blk_3way);
  38. ECB_BLOCK(1, twofish_enc_blk);
  39. ECB_WALK_END();
  40. }
  41. static int ecb_decrypt(struct skcipher_request *req)
  42. {
  43. ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
  44. ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_dec_8way);
  45. ECB_BLOCK(3, twofish_dec_blk_3way);
  46. ECB_BLOCK(1, twofish_dec_blk);
  47. ECB_WALK_END();
  48. }
  49. static int cbc_encrypt(struct skcipher_request *req)
  50. {
  51. CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
  52. CBC_ENC_BLOCK(twofish_enc_blk);
  53. CBC_WALK_END();
  54. }
  55. static int cbc_decrypt(struct skcipher_request *req)
  56. {
  57. CBC_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
  58. CBC_DEC_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_cbc_dec_8way);
  59. CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way);
  60. CBC_DEC_BLOCK(1, twofish_dec_blk);
  61. CBC_WALK_END();
  62. }
  63. static struct skcipher_alg twofish_algs[] = {
  64. {
  65. .base.cra_name = "__ecb(twofish)",
  66. .base.cra_driver_name = "__ecb-twofish-avx",
  67. .base.cra_priority = 400,
  68. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  69. .base.cra_blocksize = TF_BLOCK_SIZE,
  70. .base.cra_ctxsize = sizeof(struct twofish_ctx),
  71. .base.cra_module = THIS_MODULE,
  72. .min_keysize = TF_MIN_KEY_SIZE,
  73. .max_keysize = TF_MAX_KEY_SIZE,
  74. .setkey = twofish_setkey_skcipher,
  75. .encrypt = ecb_encrypt,
  76. .decrypt = ecb_decrypt,
  77. }, {
  78. .base.cra_name = "__cbc(twofish)",
  79. .base.cra_driver_name = "__cbc-twofish-avx",
  80. .base.cra_priority = 400,
  81. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  82. .base.cra_blocksize = TF_BLOCK_SIZE,
  83. .base.cra_ctxsize = sizeof(struct twofish_ctx),
  84. .base.cra_module = THIS_MODULE,
  85. .min_keysize = TF_MIN_KEY_SIZE,
  86. .max_keysize = TF_MAX_KEY_SIZE,
  87. .ivsize = TF_BLOCK_SIZE,
  88. .setkey = twofish_setkey_skcipher,
  89. .encrypt = cbc_encrypt,
  90. .decrypt = cbc_decrypt,
  91. },
  92. };
  93. static struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
  94. static int __init twofish_init(void)
  95. {
  96. const char *feature_name;
  97. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
  98. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  99. return -ENODEV;
  100. }
  101. return simd_register_skciphers_compat(twofish_algs,
  102. ARRAY_SIZE(twofish_algs),
  103. twofish_simd_algs);
  104. }
  105. static void __exit twofish_exit(void)
  106. {
  107. simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
  108. twofish_simd_algs);
  109. }
  110. module_init(twofish_init);
  111. module_exit(twofish_exit);
  112. MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
  113. MODULE_LICENSE("GPL");
  114. MODULE_ALIAS_CRYPTO("twofish");