cast5_avx_glue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue Code for the AVX assembler implementation of the Cast5 Cipher
  4. *
  5. * Copyright (C) 2012 Johannes Goetzfried
  6. * <[email protected]>
  7. */
  8. #include <crypto/algapi.h>
  9. #include <crypto/cast5.h>
  10. #include <crypto/internal/simd.h>
  11. #include <linux/crypto.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include "ecb_cbc_helpers.h"
  16. #define CAST5_PARALLEL_BLOCKS 16
  17. asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
  18. const u8 *src);
  19. asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  20. const u8 *src);
  21. asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  22. const u8 *src);
  23. static int cast5_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. return cast5_setkey(&tfm->base, key, keylen);
  27. }
  28. static int ecb_encrypt(struct skcipher_request *req)
  29. {
  30. ECB_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
  31. ECB_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_ecb_enc_16way);
  32. ECB_BLOCK(1, __cast5_encrypt);
  33. ECB_WALK_END();
  34. }
  35. static int ecb_decrypt(struct skcipher_request *req)
  36. {
  37. ECB_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
  38. ECB_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_ecb_dec_16way);
  39. ECB_BLOCK(1, __cast5_decrypt);
  40. ECB_WALK_END();
  41. }
  42. static int cbc_encrypt(struct skcipher_request *req)
  43. {
  44. CBC_WALK_START(req, CAST5_BLOCK_SIZE, -1);
  45. CBC_ENC_BLOCK(__cast5_encrypt);
  46. CBC_WALK_END();
  47. }
  48. static int cbc_decrypt(struct skcipher_request *req)
  49. {
  50. CBC_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
  51. CBC_DEC_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_cbc_dec_16way);
  52. CBC_DEC_BLOCK(1, __cast5_decrypt);
  53. CBC_WALK_END();
  54. }
  55. static struct skcipher_alg cast5_algs[] = {
  56. {
  57. .base.cra_name = "__ecb(cast5)",
  58. .base.cra_driver_name = "__ecb-cast5-avx",
  59. .base.cra_priority = 200,
  60. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  61. .base.cra_blocksize = CAST5_BLOCK_SIZE,
  62. .base.cra_ctxsize = sizeof(struct cast5_ctx),
  63. .base.cra_module = THIS_MODULE,
  64. .min_keysize = CAST5_MIN_KEY_SIZE,
  65. .max_keysize = CAST5_MAX_KEY_SIZE,
  66. .setkey = cast5_setkey_skcipher,
  67. .encrypt = ecb_encrypt,
  68. .decrypt = ecb_decrypt,
  69. }, {
  70. .base.cra_name = "__cbc(cast5)",
  71. .base.cra_driver_name = "__cbc-cast5-avx",
  72. .base.cra_priority = 200,
  73. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  74. .base.cra_blocksize = CAST5_BLOCK_SIZE,
  75. .base.cra_ctxsize = sizeof(struct cast5_ctx),
  76. .base.cra_module = THIS_MODULE,
  77. .min_keysize = CAST5_MIN_KEY_SIZE,
  78. .max_keysize = CAST5_MAX_KEY_SIZE,
  79. .ivsize = CAST5_BLOCK_SIZE,
  80. .setkey = cast5_setkey_skcipher,
  81. .encrypt = cbc_encrypt,
  82. .decrypt = cbc_decrypt,
  83. }
  84. };
  85. static struct simd_skcipher_alg *cast5_simd_algs[ARRAY_SIZE(cast5_algs)];
  86. static int __init cast5_init(void)
  87. {
  88. const char *feature_name;
  89. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
  90. &feature_name)) {
  91. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  92. return -ENODEV;
  93. }
  94. return simd_register_skciphers_compat(cast5_algs,
  95. ARRAY_SIZE(cast5_algs),
  96. cast5_simd_algs);
  97. }
  98. static void __exit cast5_exit(void)
  99. {
  100. simd_unregister_skciphers(cast5_algs, ARRAY_SIZE(cast5_algs),
  101. cast5_simd_algs);
  102. }
  103. module_init(cast5_init);
  104. module_exit(cast5_exit);
  105. MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
  106. MODULE_LICENSE("GPL");
  107. MODULE_ALIAS_CRYPTO("cast5");