aes.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "Failed to allocate transformation for '%s': %ld\n",
  46. alg, PTR_ERR(fallback));
  47. return PTR_ERR(fallback);
  48. }
  49. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  50. crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
  51. crypto_cipher_set_flags(fallback,
  52. crypto_cipher_get_flags((struct crypto_cipher *) tfm));
  53. ctx->fallback = fallback;
  54. return 0;
  55. }
  56. static void p8_aes_exit(struct crypto_tfm *tfm)
  57. {
  58. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  59. if (ctx->fallback) {
  60. crypto_free_cipher(ctx->fallback);
  61. ctx->fallback = NULL;
  62. }
  63. }
  64. static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
  65. unsigned int keylen)
  66. {
  67. int ret;
  68. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  69. pagefault_disable();
  70. enable_kernel_altivec();
  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. pagefault_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. pagefault_disable();
  84. enable_kernel_altivec();
  85. aes_p8_encrypt(src, dst, &ctx->enc_key);
  86. pagefault_enable();
  87. }
  88. }
  89. static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  90. {
  91. struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  92. if (in_interrupt()) {
  93. crypto_cipher_decrypt_one(ctx->fallback, dst, src);
  94. } else {
  95. pagefault_disable();
  96. enable_kernel_altivec();
  97. aes_p8_decrypt(src, dst, &ctx->dec_key);
  98. pagefault_enable();
  99. }
  100. }
  101. struct crypto_alg p8_aes_alg = {
  102. .cra_name = "aes",
  103. .cra_driver_name = "p8_aes",
  104. .cra_module = THIS_MODULE,
  105. .cra_priority = 1000,
  106. .cra_type = NULL,
  107. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
  108. .cra_alignmask = 0,
  109. .cra_blocksize = AES_BLOCK_SIZE,
  110. .cra_ctxsize = sizeof(struct p8_aes_ctx),
  111. .cra_init = p8_aes_init,
  112. .cra_exit = p8_aes_exit,
  113. .cra_cipher = {
  114. .cia_min_keysize = AES_MIN_KEY_SIZE,
  115. .cia_max_keysize = AES_MAX_KEY_SIZE,
  116. .cia_setkey = p8_aes_setkey,
  117. .cia_encrypt = p8_aes_encrypt,
  118. .cia_decrypt = p8_aes_decrypt,
  119. },
  120. };