aes.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AES routines supporting VMX instructions on the Power 8
  4. *
  5. * Copyright (C) 2015 International Business Machines Inc.
  6. *
  7. * Author: Marcelo Henrique Cerri <[email protected]>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/err.h>
  11. #include <linux/crypto.h>
  12. #include <linux/delay.h>
  13. #include <asm/simd.h>
  14. #include <asm/switch_to.h>
  15. #include <crypto/aes.h>
  16. #include <crypto/internal/cipher.h>
  17. #include <crypto/internal/simd.h>
  18. #include "aesp8-ppc.h"
  19. struct p8_aes_ctx {
  20. struct crypto_cipher *fallback;
  21. struct aes_key enc_key;
  22. struct aes_key dec_key;
  23. };
  24. static int p8_aes_init(struct crypto_tfm *tfm)
  25. {
  26. const char *alg = crypto_tfm_alg_name(tfm);
  27. struct crypto_cipher *fallback;
  28. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  29. fallback = crypto_alloc_cipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  30. if (IS_ERR(fallback)) {
  31. printk(KERN_ERR
  32. "Failed to allocate transformation for '%s': %ld\n",
  33. alg, PTR_ERR(fallback));
  34. return PTR_ERR(fallback);
  35. }
  36. crypto_cipher_set_flags(fallback,
  37. crypto_cipher_get_flags((struct
  38. crypto_cipher *)
  39. tfm));
  40. ctx->fallback = fallback;
  41. return 0;
  42. }
  43. static void p8_aes_exit(struct crypto_tfm *tfm)
  44. {
  45. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  46. if (ctx->fallback) {
  47. crypto_free_cipher(ctx->fallback);
  48. ctx->fallback = NULL;
  49. }
  50. }
  51. static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
  52. unsigned int keylen)
  53. {
  54. int ret;
  55. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  56. preempt_disable();
  57. pagefault_disable();
  58. enable_kernel_vsx();
  59. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  60. ret |= aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  61. disable_kernel_vsx();
  62. pagefault_enable();
  63. preempt_enable();
  64. ret |= crypto_cipher_setkey(ctx->fallback, key, keylen);
  65. return ret ? -EINVAL : 0;
  66. }
  67. static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  68. {
  69. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  70. if (!crypto_simd_usable()) {
  71. crypto_cipher_encrypt_one(ctx->fallback, dst, src);
  72. } else {
  73. preempt_disable();
  74. pagefault_disable();
  75. enable_kernel_vsx();
  76. aes_p8_encrypt(src, dst, &ctx->enc_key);
  77. disable_kernel_vsx();
  78. pagefault_enable();
  79. preempt_enable();
  80. }
  81. }
  82. static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  83. {
  84. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  85. if (!crypto_simd_usable()) {
  86. crypto_cipher_decrypt_one(ctx->fallback, dst, src);
  87. } else {
  88. preempt_disable();
  89. pagefault_disable();
  90. enable_kernel_vsx();
  91. aes_p8_decrypt(src, dst, &ctx->dec_key);
  92. disable_kernel_vsx();
  93. pagefault_enable();
  94. preempt_enable();
  95. }
  96. }
  97. struct crypto_alg p8_aes_alg = {
  98. .cra_name = "aes",
  99. .cra_driver_name = "p8_aes",
  100. .cra_module = THIS_MODULE,
  101. .cra_priority = 1000,
  102. .cra_type = NULL,
  103. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
  104. .cra_alignmask = 0,
  105. .cra_blocksize = AES_BLOCK_SIZE,
  106. .cra_ctxsize = sizeof(struct p8_aes_ctx),
  107. .cra_init = p8_aes_init,
  108. .cra_exit = p8_aes_exit,
  109. .cra_cipher = {
  110. .cia_min_keysize = AES_MIN_KEY_SIZE,
  111. .cia_max_keysize = AES_MAX_KEY_SIZE,
  112. .cia_setkey = p8_aes_setkey,
  113. .cia_encrypt = p8_aes_encrypt,
  114. .cia_decrypt = p8_aes_decrypt,
  115. },
  116. };