aes_xts.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * AES XTS routines supporting VMX In-core instructions on 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 Foundations; 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 of FITNESS FOR A PARTICUPAR 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: Leonidas S. Barbosa <leosilva@linux.vnet.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/xts.h>
  30. #include "aesp8-ppc.h"
  31. struct p8_aes_xts_ctx {
  32. struct crypto_blkcipher *fallback;
  33. struct aes_key enc_key;
  34. struct aes_key dec_key;
  35. struct aes_key tweak_key;
  36. };
  37. static int p8_aes_xts_init(struct crypto_tfm *tfm)
  38. {
  39. const char *alg;
  40. struct crypto_blkcipher *fallback;
  41. struct p8_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  42. if (!(alg = crypto_tfm_alg_name(tfm))) {
  43. printk(KERN_ERR "Failed to get algorithm name.\n");
  44. return -ENOENT;
  45. }
  46. fallback =
  47. crypto_alloc_blkcipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  48. if (IS_ERR(fallback)) {
  49. printk(KERN_ERR
  50. "Failed to allocate transformation for '%s': %ld\n",
  51. alg, PTR_ERR(fallback));
  52. return PTR_ERR(fallback);
  53. }
  54. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  55. crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
  56. crypto_blkcipher_set_flags(
  57. fallback,
  58. crypto_blkcipher_get_flags((struct crypto_blkcipher *)tfm));
  59. ctx->fallback = fallback;
  60. return 0;
  61. }
  62. static void p8_aes_xts_exit(struct crypto_tfm *tfm)
  63. {
  64. struct p8_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  65. if (ctx->fallback) {
  66. crypto_free_blkcipher(ctx->fallback);
  67. ctx->fallback = NULL;
  68. }
  69. }
  70. static int p8_aes_xts_setkey(struct crypto_tfm *tfm, const u8 *key,
  71. unsigned int keylen)
  72. {
  73. int ret;
  74. struct p8_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  75. ret = xts_check_key(tfm, key, keylen);
  76. if (ret)
  77. return ret;
  78. preempt_disable();
  79. pagefault_disable();
  80. enable_kernel_vsx();
  81. ret = aes_p8_set_encrypt_key(key + keylen/2, (keylen/2) * 8, &ctx->tweak_key);
  82. ret += aes_p8_set_encrypt_key(key, (keylen/2) * 8, &ctx->enc_key);
  83. ret += aes_p8_set_decrypt_key(key, (keylen/2) * 8, &ctx->dec_key);
  84. disable_kernel_vsx();
  85. pagefault_enable();
  86. preempt_enable();
  87. ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen);
  88. return ret;
  89. }
  90. static int p8_aes_xts_crypt(struct blkcipher_desc *desc,
  91. struct scatterlist *dst,
  92. struct scatterlist *src,
  93. unsigned int nbytes, int enc)
  94. {
  95. int ret;
  96. u8 tweak[AES_BLOCK_SIZE];
  97. u8 *iv;
  98. struct blkcipher_walk walk;
  99. struct p8_aes_xts_ctx *ctx =
  100. crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
  101. struct blkcipher_desc fallback_desc = {
  102. .tfm = ctx->fallback,
  103. .info = desc->info,
  104. .flags = desc->flags
  105. };
  106. if (in_interrupt()) {
  107. ret = enc ? crypto_blkcipher_encrypt(&fallback_desc, dst, src, nbytes) :
  108. crypto_blkcipher_decrypt(&fallback_desc, dst, src, nbytes);
  109. } else {
  110. preempt_disable();
  111. pagefault_disable();
  112. enable_kernel_vsx();
  113. blkcipher_walk_init(&walk, dst, src, nbytes);
  114. ret = blkcipher_walk_virt(desc, &walk);
  115. iv = walk.iv;
  116. memset(tweak, 0, AES_BLOCK_SIZE);
  117. aes_p8_encrypt(iv, tweak, &ctx->tweak_key);
  118. while ((nbytes = walk.nbytes)) {
  119. if (enc)
  120. aes_p8_xts_encrypt(walk.src.virt.addr, walk.dst.virt.addr,
  121. nbytes & AES_BLOCK_MASK, &ctx->enc_key, NULL, tweak);
  122. else
  123. aes_p8_xts_decrypt(walk.src.virt.addr, walk.dst.virt.addr,
  124. nbytes & AES_BLOCK_MASK, &ctx->dec_key, NULL, tweak);
  125. nbytes &= AES_BLOCK_SIZE - 1;
  126. ret = blkcipher_walk_done(desc, &walk, nbytes);
  127. }
  128. disable_kernel_vsx();
  129. pagefault_enable();
  130. preempt_enable();
  131. }
  132. return ret;
  133. }
  134. static int p8_aes_xts_encrypt(struct blkcipher_desc *desc,
  135. struct scatterlist *dst,
  136. struct scatterlist *src, unsigned int nbytes)
  137. {
  138. return p8_aes_xts_crypt(desc, dst, src, nbytes, 1);
  139. }
  140. static int p8_aes_xts_decrypt(struct blkcipher_desc *desc,
  141. struct scatterlist *dst,
  142. struct scatterlist *src, unsigned int nbytes)
  143. {
  144. return p8_aes_xts_crypt(desc, dst, src, nbytes, 0);
  145. }
  146. struct crypto_alg p8_aes_xts_alg = {
  147. .cra_name = "xts(aes)",
  148. .cra_driver_name = "p8_aes_xts",
  149. .cra_module = THIS_MODULE,
  150. .cra_priority = 2000,
  151. .cra_type = &crypto_blkcipher_type,
  152. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
  153. .cra_alignmask = 0,
  154. .cra_blocksize = AES_BLOCK_SIZE,
  155. .cra_ctxsize = sizeof(struct p8_aes_xts_ctx),
  156. .cra_init = p8_aes_xts_init,
  157. .cra_exit = p8_aes_xts_exit,
  158. .cra_blkcipher = {
  159. .ivsize = AES_BLOCK_SIZE,
  160. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  161. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  162. .setkey = p8_aes_xts_setkey,
  163. .encrypt = p8_aes_xts_encrypt,
  164. .decrypt = p8_aes_xts_decrypt,
  165. }
  166. };