aes.c 3.8 KB

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