sm4-ce-glue.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <asm/neon.h>
  3. #include <asm/simd.h>
  4. #include <crypto/sm4.h>
  5. #include <linux/module.h>
  6. #include <linux/cpufeature.h>
  7. #include <linux/crypto.h>
  8. #include <linux/types.h>
  9. MODULE_ALIAS_CRYPTO("sm4");
  10. MODULE_ALIAS_CRYPTO("sm4-ce");
  11. MODULE_DESCRIPTION("SM4 symmetric cipher using ARMv8 Crypto Extensions");
  12. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  13. MODULE_LICENSE("GPL v2");
  14. asmlinkage void sm4_ce_do_crypt(const u32 *rk, void *out, const void *in);
  15. static void sm4_ce_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  16. {
  17. const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
  18. if (!may_use_simd()) {
  19. crypto_sm4_encrypt(tfm, out, in);
  20. } else {
  21. kernel_neon_begin();
  22. sm4_ce_do_crypt(ctx->rkey_enc, out, in);
  23. kernel_neon_end();
  24. }
  25. }
  26. static void sm4_ce_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  27. {
  28. const struct crypto_sm4_ctx *ctx = crypto_tfm_ctx(tfm);
  29. if (!may_use_simd()) {
  30. crypto_sm4_decrypt(tfm, out, in);
  31. } else {
  32. kernel_neon_begin();
  33. sm4_ce_do_crypt(ctx->rkey_dec, out, in);
  34. kernel_neon_end();
  35. }
  36. }
  37. static struct crypto_alg sm4_ce_alg = {
  38. .cra_name = "sm4",
  39. .cra_driver_name = "sm4-ce",
  40. .cra_priority = 200,
  41. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  42. .cra_blocksize = SM4_BLOCK_SIZE,
  43. .cra_ctxsize = sizeof(struct crypto_sm4_ctx),
  44. .cra_module = THIS_MODULE,
  45. .cra_u.cipher = {
  46. .cia_min_keysize = SM4_KEY_SIZE,
  47. .cia_max_keysize = SM4_KEY_SIZE,
  48. .cia_setkey = crypto_sm4_set_key,
  49. .cia_encrypt = sm4_ce_encrypt,
  50. .cia_decrypt = sm4_ce_decrypt
  51. }
  52. };
  53. static int __init sm4_ce_mod_init(void)
  54. {
  55. return crypto_register_alg(&sm4_ce_alg);
  56. }
  57. static void __exit sm4_ce_mod_fini(void)
  58. {
  59. crypto_unregister_alg(&sm4_ce_alg);
  60. }
  61. module_cpu_feature_match(SM4, sm4_ce_mod_init);
  62. module_exit(sm4_ce_mod_fini);