aes_ctr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * AES CTR 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_ctr_ctx {
  16. struct crypto_skcipher *fallback;
  17. struct aes_key enc_key;
  18. };
  19. static int p8_aes_ctr_init(struct crypto_skcipher *tfm)
  20. {
  21. struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  22. struct crypto_skcipher *fallback;
  23. fallback = crypto_alloc_skcipher("ctr(aes)", 0,
  24. CRYPTO_ALG_NEED_FALLBACK |
  25. CRYPTO_ALG_ASYNC);
  26. if (IS_ERR(fallback)) {
  27. pr_err("Failed to allocate ctr(aes) fallback: %ld\n",
  28. PTR_ERR(fallback));
  29. return PTR_ERR(fallback);
  30. }
  31. crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
  32. crypto_skcipher_reqsize(fallback));
  33. ctx->fallback = fallback;
  34. return 0;
  35. }
  36. static void p8_aes_ctr_exit(struct crypto_skcipher *tfm)
  37. {
  38. struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  39. crypto_free_skcipher(ctx->fallback);
  40. }
  41. static int p8_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key,
  42. unsigned int keylen)
  43. {
  44. struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  45. int ret;
  46. preempt_disable();
  47. pagefault_disable();
  48. enable_kernel_vsx();
  49. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  50. disable_kernel_vsx();
  51. pagefault_enable();
  52. preempt_enable();
  53. ret |= crypto_skcipher_setkey(ctx->fallback, key, keylen);
  54. return ret ? -EINVAL : 0;
  55. }
  56. static void p8_aes_ctr_final(const struct p8_aes_ctr_ctx *ctx,
  57. struct skcipher_walk *walk)
  58. {
  59. u8 *ctrblk = walk->iv;
  60. u8 keystream[AES_BLOCK_SIZE];
  61. u8 *src = walk->src.virt.addr;
  62. u8 *dst = walk->dst.virt.addr;
  63. unsigned int nbytes = walk->nbytes;
  64. preempt_disable();
  65. pagefault_disable();
  66. enable_kernel_vsx();
  67. aes_p8_encrypt(ctrblk, keystream, &ctx->enc_key);
  68. disable_kernel_vsx();
  69. pagefault_enable();
  70. preempt_enable();
  71. crypto_xor_cpy(dst, keystream, src, nbytes);
  72. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  73. }
  74. static int p8_aes_ctr_crypt(struct skcipher_request *req)
  75. {
  76. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  77. const struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
  78. struct skcipher_walk walk;
  79. unsigned int nbytes;
  80. int ret;
  81. if (!crypto_simd_usable()) {
  82. struct skcipher_request *subreq = skcipher_request_ctx(req);
  83. *subreq = *req;
  84. skcipher_request_set_tfm(subreq, ctx->fallback);
  85. return crypto_skcipher_encrypt(subreq);
  86. }
  87. ret = skcipher_walk_virt(&walk, req, false);
  88. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  89. preempt_disable();
  90. pagefault_disable();
  91. enable_kernel_vsx();
  92. aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr,
  93. walk.dst.virt.addr,
  94. nbytes / AES_BLOCK_SIZE,
  95. &ctx->enc_key, walk.iv);
  96. disable_kernel_vsx();
  97. pagefault_enable();
  98. preempt_enable();
  99. do {
  100. crypto_inc(walk.iv, AES_BLOCK_SIZE);
  101. } while ((nbytes -= AES_BLOCK_SIZE) >= AES_BLOCK_SIZE);
  102. ret = skcipher_walk_done(&walk, nbytes);
  103. }
  104. if (nbytes) {
  105. p8_aes_ctr_final(ctx, &walk);
  106. ret = skcipher_walk_done(&walk, 0);
  107. }
  108. return ret;
  109. }
  110. struct skcipher_alg p8_aes_ctr_alg = {
  111. .base.cra_name = "ctr(aes)",
  112. .base.cra_driver_name = "p8_aes_ctr",
  113. .base.cra_module = THIS_MODULE,
  114. .base.cra_priority = 2000,
  115. .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  116. .base.cra_blocksize = 1,
  117. .base.cra_ctxsize = sizeof(struct p8_aes_ctr_ctx),
  118. .setkey = p8_aes_ctr_setkey,
  119. .encrypt = p8_aes_ctr_crypt,
  120. .decrypt = p8_aes_ctr_crypt,
  121. .init = p8_aes_ctr_init,
  122. .exit = p8_aes_ctr_exit,
  123. .min_keysize = AES_MIN_KEY_SIZE,
  124. .max_keysize = AES_MAX_KEY_SIZE,
  125. .ivsize = AES_BLOCK_SIZE,
  126. .chunksize = AES_BLOCK_SIZE,
  127. };