aes_xts.c 5.1 KB

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