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