aes_cbc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  50. crypto_skcipher_driver_name(fallback));
  51. crypto_skcipher_set_flags(
  52. fallback,
  53. crypto_skcipher_get_flags((struct crypto_skcipher *)tfm));
  54. ctx->fallback = fallback;
  55. return 0;
  56. }
  57. static void p8_aes_cbc_exit(struct crypto_tfm *tfm)
  58. {
  59. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  60. if (ctx->fallback) {
  61. crypto_free_skcipher(ctx->fallback);
  62. ctx->fallback = NULL;
  63. }
  64. }
  65. static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key,
  66. unsigned int keylen)
  67. {
  68. int ret;
  69. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  70. preempt_disable();
  71. pagefault_disable();
  72. enable_kernel_vsx();
  73. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  74. ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  75. disable_kernel_vsx();
  76. pagefault_enable();
  77. preempt_enable();
  78. ret += crypto_skcipher_setkey(ctx->fallback, key, keylen);
  79. return ret;
  80. }
  81. static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc,
  82. struct scatterlist *dst,
  83. struct scatterlist *src, unsigned int nbytes)
  84. {
  85. int ret;
  86. struct blkcipher_walk walk;
  87. struct p8_aes_cbc_ctx *ctx =
  88. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  89. if (in_interrupt()) {
  90. SKCIPHER_REQUEST_ON_STACK(req, ctx->fallback);
  91. skcipher_request_set_tfm(req, ctx->fallback);
  92. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  93. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  94. ret = crypto_skcipher_encrypt(req);
  95. skcipher_request_zero(req);
  96. } else {
  97. preempt_disable();
  98. pagefault_disable();
  99. enable_kernel_vsx();
  100. blkcipher_walk_init(&walk, dst, src, nbytes);
  101. ret = blkcipher_walk_virt(desc, &walk);
  102. while ((nbytes = walk.nbytes)) {
  103. aes_p8_cbc_encrypt(walk.src.virt.addr,
  104. walk.dst.virt.addr,
  105. nbytes & AES_BLOCK_MASK,
  106. &ctx->enc_key, walk.iv, 1);
  107. nbytes &= AES_BLOCK_SIZE - 1;
  108. ret = blkcipher_walk_done(desc, &walk, nbytes);
  109. }
  110. disable_kernel_vsx();
  111. pagefault_enable();
  112. preempt_enable();
  113. }
  114. return ret;
  115. }
  116. static int p8_aes_cbc_decrypt(struct blkcipher_desc *desc,
  117. struct scatterlist *dst,
  118. struct scatterlist *src, unsigned int nbytes)
  119. {
  120. int ret;
  121. struct blkcipher_walk walk;
  122. struct p8_aes_cbc_ctx *ctx =
  123. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  124. if (in_interrupt()) {
  125. SKCIPHER_REQUEST_ON_STACK(req, ctx->fallback);
  126. skcipher_request_set_tfm(req, ctx->fallback);
  127. skcipher_request_set_callback(req, desc->flags, NULL, NULL);
  128. skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
  129. ret = crypto_skcipher_decrypt(req);
  130. skcipher_request_zero(req);
  131. } else {
  132. preempt_disable();
  133. pagefault_disable();
  134. enable_kernel_vsx();
  135. blkcipher_walk_init(&walk, dst, src, nbytes);
  136. ret = blkcipher_walk_virt(desc, &walk);
  137. while ((nbytes = walk.nbytes)) {
  138. aes_p8_cbc_encrypt(walk.src.virt.addr,
  139. walk.dst.virt.addr,
  140. nbytes & AES_BLOCK_MASK,
  141. &ctx->dec_key, walk.iv, 0);
  142. nbytes &= AES_BLOCK_SIZE - 1;
  143. ret = blkcipher_walk_done(desc, &walk, nbytes);
  144. }
  145. disable_kernel_vsx();
  146. pagefault_enable();
  147. preempt_enable();
  148. }
  149. return ret;
  150. }
  151. struct crypto_alg p8_aes_cbc_alg = {
  152. .cra_name = "cbc(aes)",
  153. .cra_driver_name = "p8_aes_cbc",
  154. .cra_module = THIS_MODULE,
  155. .cra_priority = 2000,
  156. .cra_type = &crypto_blkcipher_type,
  157. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
  158. .cra_alignmask = 0,
  159. .cra_blocksize = AES_BLOCK_SIZE,
  160. .cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
  161. .cra_init = p8_aes_cbc_init,
  162. .cra_exit = p8_aes_cbc_exit,
  163. .cra_blkcipher = {
  164. .ivsize = AES_BLOCK_SIZE,
  165. .min_keysize = AES_MIN_KEY_SIZE,
  166. .max_keysize = AES_MAX_KEY_SIZE,
  167. .setkey = p8_aes_cbc_setkey,
  168. .encrypt = p8_aes_cbc_encrypt,
  169. .decrypt = p8_aes_cbc_decrypt,
  170. },
  171. };