aes_cbc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * AES CBC routines supporting VMX instructions on the Power 8
  4. *
  5. * Copyright (C) 2015 International Business Machines Inc.
  6. *
  7. * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
  8. */
  9. #include <asm/simd.h>
  10. #include <asm/switch_to.h>
  11. #include <crypto/aes.h>
  12. #include <crypto/internal/simd.h>
  13. #include <crypto/internal/skcipher.h>
  14. #include "aesp8-ppc.h"
  15. struct p8_aes_cbc_ctx {
  16. struct crypto_skcipher *fallback;
  17. struct aes_key enc_key;
  18. struct aes_key dec_key;
  19. };
  20. static int p8_aes_cbc_init(struct crypto_skcipher *tfm)
  21. {
  22. struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  23. struct crypto_skcipher *fallback;
  24. fallback = crypto_alloc_skcipher("cbc(aes)", 0,
  25. CRYPTO_ALG_NEED_FALLBACK |
  26. CRYPTO_ALG_ASYNC);
  27. if (IS_ERR(fallback)) {
  28. pr_err("Failed to allocate cbc(aes) fallback: %ld\n",
  29. PTR_ERR(fallback));
  30. return PTR_ERR(fallback);
  31. }
  32. crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
  33. crypto_skcipher_reqsize(fallback));
  34. ctx->fallback = fallback;
  35. return 0;
  36. }
  37. static void p8_aes_cbc_exit(struct crypto_skcipher *tfm)
  38. {
  39. struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  40. crypto_free_skcipher(ctx->fallback);
  41. }
  42. static int p8_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  46. int ret;
  47. preempt_disable();
  48. pagefault_disable();
  49. enable_kernel_vsx();
  50. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  51. ret |= aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  52. disable_kernel_vsx();
  53. pagefault_enable();
  54. preempt_enable();
  55. ret |= crypto_skcipher_setkey(ctx->fallback, key, keylen);
  56. return ret ? -EINVAL : 0;
  57. }
  58. static int p8_aes_cbc_crypt(struct skcipher_request *req, int enc)
  59. {
  60. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  61. const struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  62. struct skcipher_walk walk;
  63. unsigned int nbytes;
  64. int ret;
  65. if (!crypto_simd_usable()) {
  66. struct skcipher_request *subreq = skcipher_request_ctx(req);
  67. *subreq = *req;
  68. skcipher_request_set_tfm(subreq, ctx->fallback);
  69. return enc ? crypto_skcipher_encrypt(subreq) :
  70. crypto_skcipher_decrypt(subreq);
  71. }
  72. ret = skcipher_walk_virt(&walk, req, false);
  73. while ((nbytes = walk.nbytes) != 0) {
  74. preempt_disable();
  75. pagefault_disable();
  76. enable_kernel_vsx();
  77. aes_p8_cbc_encrypt(walk.src.virt.addr,
  78. walk.dst.virt.addr,
  79. round_down(nbytes, AES_BLOCK_SIZE),
  80. enc ? &ctx->enc_key : &ctx->dec_key,
  81. walk.iv, enc);
  82. disable_kernel_vsx();
  83. pagefault_enable();
  84. preempt_enable();
  85. ret = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
  86. }
  87. return ret;
  88. }
  89. static int p8_aes_cbc_encrypt(struct skcipher_request *req)
  90. {
  91. return p8_aes_cbc_crypt(req, 1);
  92. }
  93. static int p8_aes_cbc_decrypt(struct skcipher_request *req)
  94. {
  95. return p8_aes_cbc_crypt(req, 0);
  96. }
  97. struct skcipher_alg p8_aes_cbc_alg = {
  98. .base.cra_name = "cbc(aes)",
  99. .base.cra_driver_name = "p8_aes_cbc",
  100. .base.cra_module = THIS_MODULE,
  101. .base.cra_priority = 2000,
  102. .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  103. .base.cra_blocksize = AES_BLOCK_SIZE,
  104. .base.cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
  105. .setkey = p8_aes_cbc_setkey,
  106. .encrypt = p8_aes_cbc_encrypt,
  107. .decrypt = p8_aes_cbc_decrypt,
  108. .init = p8_aes_cbc_init,
  109. .exit = p8_aes_cbc_exit,
  110. .min_keysize = AES_MIN_KEY_SIZE,
  111. .max_keysize = AES_MAX_KEY_SIZE,
  112. .ivsize = AES_BLOCK_SIZE,
  113. };