aes.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * AES 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 "aesp8-ppc.h"
  29. struct p8_aes_ctx {
  30. struct crypto_cipher *fallback;
  31. struct aes_key enc_key;
  32. struct aes_key dec_key;
  33. };
  34. static int p8_aes_init(struct crypto_tfm *tfm)
  35. {
  36. const char *alg = crypto_tfm_alg_name(tfm);
  37. struct crypto_cipher *fallback;
  38. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  39. fallback = crypto_alloc_cipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  40. if (IS_ERR(fallback)) {
  41. printk(KERN_ERR
  42. "Failed to allocate transformation for '%s': %ld\n",
  43. alg, PTR_ERR(fallback));
  44. return PTR_ERR(fallback);
  45. }
  46. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  47. crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
  48. crypto_cipher_set_flags(fallback,
  49. crypto_cipher_get_flags((struct
  50. crypto_cipher *)
  51. tfm));
  52. ctx->fallback = fallback;
  53. return 0;
  54. }
  55. static void p8_aes_exit(struct crypto_tfm *tfm)
  56. {
  57. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  58. if (ctx->fallback) {
  59. crypto_free_cipher(ctx->fallback);
  60. ctx->fallback = NULL;
  61. }
  62. }
  63. static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
  64. unsigned int keylen)
  65. {
  66. int ret;
  67. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  68. preempt_disable();
  69. pagefault_disable();
  70. enable_kernel_vsx();
  71. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  72. ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  73. disable_kernel_vsx();
  74. pagefault_enable();
  75. preempt_enable();
  76. ret += crypto_cipher_setkey(ctx->fallback, key, keylen);
  77. return ret;
  78. }
  79. static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  80. {
  81. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  82. if (in_interrupt()) {
  83. crypto_cipher_encrypt_one(ctx->fallback, dst, src);
  84. } else {
  85. preempt_disable();
  86. pagefault_disable();
  87. enable_kernel_vsx();
  88. aes_p8_encrypt(src, dst, &ctx->enc_key);
  89. disable_kernel_vsx();
  90. pagefault_enable();
  91. preempt_enable();
  92. }
  93. }
  94. static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  95. {
  96. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  97. if (in_interrupt()) {
  98. crypto_cipher_decrypt_one(ctx->fallback, dst, src);
  99. } else {
  100. preempt_disable();
  101. pagefault_disable();
  102. enable_kernel_vsx();
  103. aes_p8_decrypt(src, dst, &ctx->dec_key);
  104. disable_kernel_vsx();
  105. pagefault_enable();
  106. preempt_enable();
  107. }
  108. }
  109. struct crypto_alg p8_aes_alg = {
  110. .cra_name = "aes",
  111. .cra_driver_name = "p8_aes",
  112. .cra_module = THIS_MODULE,
  113. .cra_priority = 1000,
  114. .cra_type = NULL,
  115. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
  116. .cra_alignmask = 0,
  117. .cra_blocksize = AES_BLOCK_SIZE,
  118. .cra_ctxsize = sizeof(struct p8_aes_ctx),
  119. .cra_init = p8_aes_init,
  120. .cra_exit = p8_aes_exit,
  121. .cra_cipher = {
  122. .cia_min_keysize = AES_MIN_KEY_SIZE,
  123. .cia_max_keysize = AES_MAX_KEY_SIZE,
  124. .cia_setkey = p8_aes_setkey,
  125. .cia_encrypt = p8_aes_encrypt,
  126. .cia_decrypt = p8_aes_decrypt,
  127. },
  128. };