aes_cbc.c 5.4 KB

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