aes.c 3.9 KB

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