aes_cbc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * AES CBC routines supporting VMX instructions on the Power 8
  3. *
  4. * Copyright (C) 2015 International Business Machines Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 only.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
  20. */
  21. #include <linux/types.h>
  22. #include <linux/err.h>
  23. #include <linux/crypto.h>
  24. #include <linux/delay.h>
  25. #include <linux/hardirq.h>
  26. #include <asm/switch_to.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/scatterwalk.h>
  29. #include <crypto/skcipher.h>
  30. #include "aesp8-ppc.h"
  31. struct p8_aes_cbc_ctx {
  32. struct crypto_skcipher *fallback;
  33. struct aes_key enc_key;
  34. struct aes_key dec_key;
  35. };
  36. static int p8_aes_cbc_init(struct crypto_tfm *tfm)
  37. {
  38. const char *alg = crypto_tfm_alg_name(tfm);
  39. struct crypto_skcipher *fallback;
  40. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  41. fallback = crypto_alloc_skcipher(alg, 0,
  42. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  43. if (IS_ERR(fallback)) {
  44. printk(KERN_ERR
  45. "Failed to allocate transformation for '%s': %ld\n",
  46. alg, PTR_ERR(fallback));
  47. return PTR_ERR(fallback);
  48. }
  49. crypto_skcipher_set_flags(
  50. fallback,
  51. crypto_skcipher_get_flags((struct crypto_skcipher *)tfm));
  52. ctx->fallback = fallback;
  53. return 0;
  54. }
  55. static void p8_aes_cbc_exit(struct crypto_tfm *tfm)
  56. {
  57. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  58. if (ctx->fallback) {
  59. crypto_free_skcipher(ctx->fallback);
  60. ctx->fallback = NULL;
  61. }
  62. }
  63. static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key,
  64. unsigned int keylen)
  65. {
  66. int ret;
  67. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  68. preempt_disable();
  69. pagefault_disable();
  70. enable_kernel_vsx();
  71. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  72. ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  73. disable_kernel_vsx();
  74. pagefault_enable();
  75. preempt_enable();
  76. ret += crypto_skcipher_setkey(ctx->fallback, key, keylen);
  77. return ret;
  78. }
  79. static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc,
  80. struct scatterlist *dst,
  81. struct scatterlist *src, unsigned int nbytes)
  82. {
  83. int ret;
  84. struct blkcipher_walk walk;
  85. struct p8_aes_cbc_ctx *ctx =
  86. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  87. if (in_interrupt()) {
  88. SKCIPHER_REQUEST_ON_STACK(req, ctx->fallback);
  89. skcipher_request_set_tfm(req, ctx->fallback);
  90. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  91. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  92. ret = crypto_skcipher_encrypt(req);
  93. skcipher_request_zero(req);
  94. } else {
  95. blkcipher_walk_init(&walk, dst, src, nbytes);
  96. ret = blkcipher_walk_virt(desc, &walk);
  97. while ((nbytes = walk.nbytes)) {
  98. preempt_disable();
  99. pagefault_disable();
  100. enable_kernel_vsx();
  101. aes_p8_cbc_encrypt(walk.src.virt.addr,
  102. walk.dst.virt.addr,
  103. nbytes & AES_BLOCK_MASK,
  104. &ctx->enc_key, walk.iv, 1);
  105. disable_kernel_vsx();
  106. pagefault_enable();
  107. preempt_enable();
  108. nbytes &= AES_BLOCK_SIZE - 1;
  109. ret = blkcipher_walk_done(desc, &walk, nbytes);
  110. }
  111. }
  112. return ret;
  113. }
  114. static int p8_aes_cbc_decrypt(struct blkcipher_desc *desc,
  115. struct scatterlist *dst,
  116. struct scatterlist *src, unsigned int nbytes)
  117. {
  118. int ret;
  119. struct blkcipher_walk walk;
  120. struct p8_aes_cbc_ctx *ctx =
  121. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  122. if (in_interrupt()) {
  123. SKCIPHER_REQUEST_ON_STACK(req, ctx->fallback);
  124. skcipher_request_set_tfm(req, ctx->fallback);
  125. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  126. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  127. ret = crypto_skcipher_decrypt(req);
  128. skcipher_request_zero(req);
  129. } else {
  130. blkcipher_walk_init(&walk, dst, src, nbytes);
  131. ret = blkcipher_walk_virt(desc, &walk);
  132. while ((nbytes = walk.nbytes)) {
  133. preempt_disable();
  134. pagefault_disable();
  135. enable_kernel_vsx();
  136. aes_p8_cbc_encrypt(walk.src.virt.addr,
  137. walk.dst.virt.addr,
  138. nbytes & AES_BLOCK_MASK,
  139. &ctx->dec_key, walk.iv, 0);
  140. disable_kernel_vsx();
  141. pagefault_enable();
  142. preempt_enable();
  143. nbytes &= AES_BLOCK_SIZE - 1;
  144. ret = blkcipher_walk_done(desc, &walk, nbytes);
  145. }
  146. }
  147. return ret;
  148. }
  149. struct crypto_alg p8_aes_cbc_alg = {
  150. .cra_name = "cbc(aes)",
  151. .cra_driver_name = "p8_aes_cbc",
  152. .cra_module = THIS_MODULE,
  153. .cra_priority = 2000,
  154. .cra_type = &crypto_blkcipher_type,
  155. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
  156. .cra_alignmask = 0,
  157. .cra_blocksize = AES_BLOCK_SIZE,
  158. .cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
  159. .cra_init = p8_aes_cbc_init,
  160. .cra_exit = p8_aes_cbc_exit,
  161. .cra_blkcipher = {
  162. .ivsize = AES_BLOCK_SIZE,
  163. .min_keysize = AES_MIN_KEY_SIZE,
  164. .max_keysize = AES_MAX_KEY_SIZE,
  165. .setkey = p8_aes_cbc_setkey,
  166. .encrypt = p8_aes_cbc_encrypt,
  167. .decrypt = p8_aes_cbc_decrypt,
  168. },
  169. };