aes_cbc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "aesp8-ppc.h"
  30. struct p8_aes_cbc_ctx {
  31. struct crypto_blkcipher *fallback;
  32. struct aes_key enc_key;
  33. struct aes_key dec_key;
  34. };
  35. static int p8_aes_cbc_init(struct crypto_tfm *tfm)
  36. {
  37. const char *alg;
  38. struct crypto_blkcipher *fallback;
  39. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  40. if (!(alg = crypto_tfm_alg_name(tfm))) {
  41. printk(KERN_ERR "Failed to get algorithm name.\n");
  42. return -ENOENT;
  43. }
  44. fallback =
  45. crypto_alloc_blkcipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  46. if (IS_ERR(fallback)) {
  47. printk(KERN_ERR
  48. "Failed to allocate transformation for '%s': %ld\n",
  49. alg, PTR_ERR(fallback));
  50. return PTR_ERR(fallback);
  51. }
  52. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  53. crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
  54. crypto_blkcipher_set_flags(
  55. fallback,
  56. crypto_blkcipher_get_flags((struct crypto_blkcipher *)tfm));
  57. ctx->fallback = fallback;
  58. return 0;
  59. }
  60. static void p8_aes_cbc_exit(struct crypto_tfm *tfm)
  61. {
  62. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  63. if (ctx->fallback) {
  64. crypto_free_blkcipher(ctx->fallback);
  65. ctx->fallback = NULL;
  66. }
  67. }
  68. static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key,
  69. unsigned int keylen)
  70. {
  71. int ret;
  72. struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  73. preempt_disable();
  74. pagefault_disable();
  75. enable_kernel_vsx();
  76. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  77. ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  78. disable_kernel_vsx();
  79. pagefault_enable();
  80. preempt_enable();
  81. ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen);
  82. return ret;
  83. }
  84. static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc,
  85. struct scatterlist *dst,
  86. struct scatterlist *src, unsigned int nbytes)
  87. {
  88. int ret;
  89. struct blkcipher_walk walk;
  90. struct p8_aes_cbc_ctx *ctx =
  91. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  92. struct blkcipher_desc fallback_desc = {
  93. .tfm = ctx->fallback,
  94. .info = desc->info,
  95. .flags = desc->flags
  96. };
  97. if (in_interrupt()) {
  98. ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src,
  99. nbytes);
  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. struct blkcipher_desc fallback_desc = {
  129. .tfm = ctx->fallback,
  130. .info = desc->info,
  131. .flags = desc->flags
  132. };
  133. if (in_interrupt()) {
  134. ret = crypto_blkcipher_decrypt(&fallback_desc, dst, src,
  135. nbytes);
  136. } else {
  137. preempt_disable();
  138. pagefault_disable();
  139. enable_kernel_vsx();
  140. blkcipher_walk_init(&walk, dst, src, nbytes);
  141. ret = blkcipher_walk_virt(desc, &walk);
  142. while ((nbytes = walk.nbytes)) {
  143. aes_p8_cbc_encrypt(walk.src.virt.addr,
  144. walk.dst.virt.addr,
  145. nbytes & AES_BLOCK_MASK,
  146. &ctx->dec_key, walk.iv, 0);
  147. nbytes &= AES_BLOCK_SIZE - 1;
  148. ret = blkcipher_walk_done(desc, &walk, nbytes);
  149. }
  150. disable_kernel_vsx();
  151. pagefault_enable();
  152. preempt_enable();
  153. }
  154. return ret;
  155. }
  156. struct crypto_alg p8_aes_cbc_alg = {
  157. .cra_name = "cbc(aes)",
  158. .cra_driver_name = "p8_aes_cbc",
  159. .cra_module = THIS_MODULE,
  160. .cra_priority = 2000,
  161. .cra_type = &crypto_blkcipher_type,
  162. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
  163. .cra_alignmask = 0,
  164. .cra_blocksize = AES_BLOCK_SIZE,
  165. .cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
  166. .cra_init = p8_aes_cbc_init,
  167. .cra_exit = p8_aes_cbc_exit,
  168. .cra_blkcipher = {
  169. .ivsize = AES_BLOCK_SIZE,
  170. .min_keysize = AES_MIN_KEY_SIZE,
  171. .max_keysize = AES_MAX_KEY_SIZE,
  172. .setkey = p8_aes_cbc_setkey,
  173. .encrypt = p8_aes_cbc_encrypt,
  174. .decrypt = p8_aes_cbc_decrypt,
  175. },
  176. };