ablk_helper.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. * USA
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/crypto.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/hardirq.h>
  31. #include <crypto/algapi.h>
  32. #include <crypto/cryptd.h>
  33. #include <crypto/ablk_helper.h>
  34. #include <asm/simd.h>
  35. int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key,
  36. unsigned int key_len)
  37. {
  38. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  39. struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base;
  40. int err;
  41. crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  42. crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm)
  43. & CRYPTO_TFM_REQ_MASK);
  44. err = crypto_ablkcipher_setkey(child, key, key_len);
  45. crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child)
  46. & CRYPTO_TFM_RES_MASK);
  47. return err;
  48. }
  49. EXPORT_SYMBOL_GPL(ablk_set_key);
  50. int __ablk_encrypt(struct ablkcipher_request *req)
  51. {
  52. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  53. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  54. struct blkcipher_desc desc;
  55. desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
  56. desc.info = req->info;
  57. desc.flags = 0;
  58. return crypto_blkcipher_crt(desc.tfm)->encrypt(
  59. &desc, req->dst, req->src, req->nbytes);
  60. }
  61. EXPORT_SYMBOL_GPL(__ablk_encrypt);
  62. int ablk_encrypt(struct ablkcipher_request *req)
  63. {
  64. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  65. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  66. if (!may_use_simd() ||
  67. (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
  68. struct ablkcipher_request *cryptd_req =
  69. ablkcipher_request_ctx(req);
  70. *cryptd_req = *req;
  71. ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  72. return crypto_ablkcipher_encrypt(cryptd_req);
  73. } else {
  74. return __ablk_encrypt(req);
  75. }
  76. }
  77. EXPORT_SYMBOL_GPL(ablk_encrypt);
  78. int ablk_decrypt(struct ablkcipher_request *req)
  79. {
  80. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  81. struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  82. if (!may_use_simd() ||
  83. (in_atomic() && cryptd_ablkcipher_queued(ctx->cryptd_tfm))) {
  84. struct ablkcipher_request *cryptd_req =
  85. ablkcipher_request_ctx(req);
  86. *cryptd_req = *req;
  87. ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  88. return crypto_ablkcipher_decrypt(cryptd_req);
  89. } else {
  90. struct blkcipher_desc desc;
  91. desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm);
  92. desc.info = req->info;
  93. desc.flags = 0;
  94. return crypto_blkcipher_crt(desc.tfm)->decrypt(
  95. &desc, req->dst, req->src, req->nbytes);
  96. }
  97. }
  98. EXPORT_SYMBOL_GPL(ablk_decrypt);
  99. void ablk_exit(struct crypto_tfm *tfm)
  100. {
  101. struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
  102. cryptd_free_ablkcipher(ctx->cryptd_tfm);
  103. }
  104. EXPORT_SYMBOL_GPL(ablk_exit);
  105. int ablk_init_common(struct crypto_tfm *tfm, const char *drv_name)
  106. {
  107. struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm);
  108. struct cryptd_ablkcipher *cryptd_tfm;
  109. cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, CRYPTO_ALG_INTERNAL,
  110. CRYPTO_ALG_INTERNAL);
  111. if (IS_ERR(cryptd_tfm))
  112. return PTR_ERR(cryptd_tfm);
  113. ctx->cryptd_tfm = cryptd_tfm;
  114. tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) +
  115. crypto_ablkcipher_reqsize(&cryptd_tfm->base);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(ablk_init_common);
  119. int ablk_init(struct crypto_tfm *tfm)
  120. {
  121. char drv_name[CRYPTO_MAX_ALG_NAME];
  122. snprintf(drv_name, sizeof(drv_name), "__driver-%s",
  123. crypto_tfm_alg_driver_name(tfm));
  124. return ablk_init_common(tfm, drv_name);
  125. }
  126. EXPORT_SYMBOL_GPL(ablk_init);
  127. MODULE_LICENSE("GPL");