ablk_helper.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Shared async block cipher helpers
  3. *
  4. * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * Based on aesni-intel_glue.c by:
  7. * Copyright (C) 2008, Intel Corp.
  8. * Author: Huang Ying <ying.huang@intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/crypto.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/hardirq.h>
  29. #include <crypto/algapi.h>
  30. #include <crypto/cryptd.h>
  31. #include <crypto/ablk_helper.h>
  32. #include <asm/simd.h>
  33. int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key,
  34. unsigned int key_len)
  35. {
  36. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  37. struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base;
  38. int err;
  39. crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  40. crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm)
  41. & CRYPTO_TFM_REQ_MASK);
  42. err = crypto_ablkcipher_setkey(child, key, key_len);
  43. crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child)
  44. & CRYPTO_TFM_RES_MASK);
  45. return err;
  46. }
  47. EXPORT_SYMBOL_GPL(ablk_set_key);
  48. int __ablk_encrypt(struct ablkcipher_request *req)
  49. {
  50. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  51. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  52. struct blkcipher_desc desc;
  53. desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
  54. desc.info = req->info;
  55. desc.flags = 0;
  56. return crypto_blkcipher_crt(desc.tfm)->encrypt(
  57. &desc, req->dst, req->src, req->nbytes);
  58. }
  59. EXPORT_SYMBOL_GPL(__ablk_encrypt);
  60. int ablk_encrypt(struct ablkcipher_request *req)
  61. {
  62. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  63. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  64. if (!may_use_simd() ||
  65. (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
  66. struct ablkcipher_request *cryptd_req =
  67. ablkcipher_request_ctx(req);
  68. *cryptd_req = *req;
  69. ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  70. return crypto_ablkcipher_encrypt(cryptd_req);
  71. } else {
  72. return __ablk_encrypt(req);
  73. }
  74. }
  75. EXPORT_SYMBOL_GPL(ablk_encrypt);
  76. int ablk_decrypt(struct ablkcipher_request *req)
  77. {
  78. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  79. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  80. if (!may_use_simd() ||
  81. (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
  82. struct ablkcipher_request *cryptd_req =
  83. ablkcipher_request_ctx(req);
  84. *cryptd_req = *req;
  85. ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  86. return crypto_ablkcipher_decrypt(cryptd_req);
  87. } else {
  88. struct blkcipher_desc desc;
  89. desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
  90. desc.info = req->info;
  91. desc.flags = 0;
  92. return crypto_blkcipher_crt(desc.tfm)->decrypt(
  93. &desc, req->dst, req->src, req->nbytes);
  94. }
  95. }
  96. EXPORT_SYMBOL_GPL(ablk_decrypt);
  97. void ablk_exit(struct crypto_tfm *tfm)
  98. {
  99. struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
  100. cryptd_free_ablkcipher(ctx->cryptd_tfm);
  101. }
  102. EXPORT_SYMBOL_GPL(ablk_exit);
  103. int ablk_init_common(struct crypto_tfm *tfm, const char *drv_name)
  104. {
  105. struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
  106. struct cryptd_ablkcipher *cryptd_tfm;
  107. cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, CRYPTO_ALG_INTERNAL,
  108. CRYPTO_ALG_INTERNAL);
  109. if (IS_ERR(cryptd_tfm))
  110. return PTR_ERR(cryptd_tfm);
  111. ctx->cryptd_tfm = cryptd_tfm;
  112. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) +
  113. crypto_ablkcipher_reqsize(&cryptd_tfm->base);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL_GPL(ablk_init_common);
  117. int ablk_init(struct crypto_tfm *tfm)
  118. {
  119. char drv_name[CRYPTO_MAX_ALG_NAME];
  120. snprintf(drv_name, sizeof(drv_name), "__driver-%s",
  121. crypto_tfm_alg_driver_name(tfm));
  122. return ablk_init_common(tfm, drv_name);
  123. }
  124. EXPORT_SYMBOL_GPL(ablk_init);
  125. MODULE_LICENSE("GPL");