aes.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. crypto_cipher_set_flags(fallback,
  47. crypto_cipher_get_flags((struct
  48. crypto_cipher *)
  49. tfm));
  50. ctx->fallback = fallback;
  51. return 0;
  52. }
  53. static void p8_aes_exit(struct crypto_tfm *tfm)
  54. {
  55. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  56. if (ctx->fallback) {
  57. crypto_free_cipher(ctx->fallback);
  58. ctx->fallback = NULL;
  59. }
  60. }
  61. static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
  62. unsigned int keylen)
  63. {
  64. int ret;
  65. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  66. preempt_disable();
  67. pagefault_disable();
  68. enable_kernel_vsx();
  69. ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
  70. ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
  71. disable_kernel_vsx();
  72. pagefault_enable();
  73. preempt_enable();
  74. ret += crypto_cipher_setkey(ctx->fallback, key, keylen);
  75. return ret;
  76. }
  77. static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  78. {
  79. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  80. if (in_interrupt()) {
  81. crypto_cipher_encrypt_one(ctx->fallback, dst, src);
  82. } else {
  83. preempt_disable();
  84. pagefault_disable();
  85. enable_kernel_vsx();
  86. aes_p8_encrypt(src, dst, &ctx->enc_key);
  87. disable_kernel_vsx();
  88. pagefault_enable();
  89. preempt_enable();
  90. }
  91. }
  92. static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  93. {
  94. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  95. if (in_interrupt()) {
  96. crypto_cipher_decrypt_one(ctx->fallback, dst, src);
  97. } else {
  98. preempt_disable();
  99. pagefault_disable();
  100. enable_kernel_vsx();
  101. aes_p8_decrypt(src, dst, &ctx->dec_key);
  102. disable_kernel_vsx();
  103. pagefault_enable();
  104. preempt_enable();
  105. }
  106. }
  107. struct crypto_alg p8_aes_alg = {
  108. .cra_name = "aes",
  109. .cra_driver_name = "p8_aes",
  110. .cra_module = THIS_MODULE,
  111. .cra_priority = 1000,
  112. .cra_type = NULL,
  113. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
  114. .cra_alignmask = 0,
  115. .cra_blocksize = AES_BLOCK_SIZE,
  116. .cra_ctxsize = sizeof(struct p8_aes_ctx),
  117. .cra_init = p8_aes_init,
  118. .cra_exit = p8_aes_exit,
  119. .cra_cipher = {
  120. .cia_min_keysize = AES_MIN_KEY_SIZE,
  121. .cia_max_keysize = AES_MAX_KEY_SIZE,
  122. .cia_setkey = p8_aes_setkey,
  123. .cia_encrypt = p8_aes_encrypt,
  124. .cia_decrypt = p8_aes_decrypt,
  125. },
  126. };