simd.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Shared crypto simd helpers
  3. *
  4. * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. * Copyright (c) 2016 Herbert Xu <herbert@gondor.apana.org.au>
  6. *
  7. * Based on aesni-intel_glue.c by:
  8. * Copyright (C) 2008, Intel Corp.
  9. * Author: Huang Ying <ying.huang@intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. #include <crypto/cryptd.h>
  26. #include <crypto/internal/simd.h>
  27. #include <crypto/internal/skcipher.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/preempt.h>
  31. #include <asm/simd.h>
  32. struct simd_skcipher_alg {
  33. const char *ialg_name;
  34. struct skcipher_alg alg;
  35. };
  36. struct simd_skcipher_ctx {
  37. struct cryptd_skcipher *cryptd_tfm;
  38. };
  39. static int simd_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
  40. unsigned int key_len)
  41. {
  42. struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  43. struct crypto_skcipher *child = &ctx->cryptd_tfm->base;
  44. int err;
  45. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  46. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(tfm) &
  47. CRYPTO_TFM_REQ_MASK);
  48. err = crypto_skcipher_setkey(child, key, key_len);
  49. crypto_skcipher_set_flags(tfm, crypto_skcipher_get_flags(child) &
  50. CRYPTO_TFM_RES_MASK);
  51. return err;
  52. }
  53. static int simd_skcipher_encrypt(struct skcipher_request *req)
  54. {
  55. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  56. struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  57. struct skcipher_request *subreq;
  58. struct crypto_skcipher *child;
  59. subreq = skcipher_request_ctx(req);
  60. *subreq = *req;
  61. if (!may_use_simd() ||
  62. (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
  63. child = &ctx->cryptd_tfm->base;
  64. else
  65. child = cryptd_skcipher_child(ctx->cryptd_tfm);
  66. skcipher_request_set_tfm(subreq, child);
  67. return crypto_skcipher_encrypt(subreq);
  68. }
  69. static int simd_skcipher_decrypt(struct skcipher_request *req)
  70. {
  71. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  72. struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  73. struct skcipher_request *subreq;
  74. struct crypto_skcipher *child;
  75. subreq = skcipher_request_ctx(req);
  76. *subreq = *req;
  77. if (!may_use_simd() ||
  78. (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
  79. child = &ctx->cryptd_tfm->base;
  80. else
  81. child = cryptd_skcipher_child(ctx->cryptd_tfm);
  82. skcipher_request_set_tfm(subreq, child);
  83. return crypto_skcipher_decrypt(subreq);
  84. }
  85. static void simd_skcipher_exit(struct crypto_skcipher *tfm)
  86. {
  87. struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  88. cryptd_free_skcipher(ctx->cryptd_tfm);
  89. }
  90. static int simd_skcipher_init(struct crypto_skcipher *tfm)
  91. {
  92. struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  93. struct cryptd_skcipher *cryptd_tfm;
  94. struct simd_skcipher_alg *salg;
  95. struct skcipher_alg *alg;
  96. unsigned reqsize;
  97. alg = crypto_skcipher_alg(tfm);
  98. salg = container_of(alg, struct simd_skcipher_alg, alg);
  99. cryptd_tfm = cryptd_alloc_skcipher(salg->ialg_name,
  100. CRYPTO_ALG_INTERNAL,
  101. CRYPTO_ALG_INTERNAL);
  102. if (IS_ERR(cryptd_tfm))
  103. return PTR_ERR(cryptd_tfm);
  104. ctx->cryptd_tfm = cryptd_tfm;
  105. reqsize = crypto_skcipher_reqsize(cryptd_skcipher_child(cryptd_tfm));
  106. reqsize = max(reqsize, crypto_skcipher_reqsize(&cryptd_tfm->base));
  107. reqsize += sizeof(struct skcipher_request);
  108. crypto_skcipher_set_reqsize(tfm, reqsize);
  109. return 0;
  110. }
  111. struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname,
  112. const char *drvname,
  113. const char *basename)
  114. {
  115. struct simd_skcipher_alg *salg;
  116. struct crypto_skcipher *tfm;
  117. struct skcipher_alg *ialg;
  118. struct skcipher_alg *alg;
  119. int err;
  120. tfm = crypto_alloc_skcipher(basename, CRYPTO_ALG_INTERNAL,
  121. CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
  122. if (IS_ERR(tfm))
  123. return ERR_CAST(tfm);
  124. ialg = crypto_skcipher_alg(tfm);
  125. salg = kzalloc(sizeof(*salg), GFP_KERNEL);
  126. if (!salg) {
  127. salg = ERR_PTR(-ENOMEM);
  128. goto out_put_tfm;
  129. }
  130. salg->ialg_name = basename;
  131. alg = &salg->alg;
  132. err = -ENAMETOOLONG;
  133. if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", algname) >=
  134. CRYPTO_MAX_ALG_NAME)
  135. goto out_free_salg;
  136. if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  137. drvname) >= CRYPTO_MAX_ALG_NAME)
  138. goto out_free_salg;
  139. alg->base.cra_flags = CRYPTO_ALG_ASYNC;
  140. alg->base.cra_priority = ialg->base.cra_priority;
  141. alg->base.cra_blocksize = ialg->base.cra_blocksize;
  142. alg->base.cra_alignmask = ialg->base.cra_alignmask;
  143. alg->base.cra_module = ialg->base.cra_module;
  144. alg->base.cra_ctxsize = sizeof(struct simd_skcipher_ctx);
  145. alg->ivsize = ialg->ivsize;
  146. alg->chunksize = ialg->chunksize;
  147. alg->min_keysize = ialg->min_keysize;
  148. alg->max_keysize = ialg->max_keysize;
  149. alg->init = simd_skcipher_init;
  150. alg->exit = simd_skcipher_exit;
  151. alg->setkey = simd_skcipher_setkey;
  152. alg->encrypt = simd_skcipher_encrypt;
  153. alg->decrypt = simd_skcipher_decrypt;
  154. err = crypto_register_skcipher(alg);
  155. if (err)
  156. goto out_free_salg;
  157. out_put_tfm:
  158. crypto_free_skcipher(tfm);
  159. return salg;
  160. out_free_salg:
  161. kfree(salg);
  162. salg = ERR_PTR(err);
  163. goto out_put_tfm;
  164. }
  165. EXPORT_SYMBOL_GPL(simd_skcipher_create_compat);
  166. struct simd_skcipher_alg *simd_skcipher_create(const char *algname,
  167. const char *basename)
  168. {
  169. char drvname[CRYPTO_MAX_ALG_NAME];
  170. if (snprintf(drvname, CRYPTO_MAX_ALG_NAME, "simd-%s", basename) >=
  171. CRYPTO_MAX_ALG_NAME)
  172. return ERR_PTR(-ENAMETOOLONG);
  173. return simd_skcipher_create_compat(algname, drvname, basename);
  174. }
  175. EXPORT_SYMBOL_GPL(simd_skcipher_create);
  176. void simd_skcipher_free(struct simd_skcipher_alg *salg)
  177. {
  178. crypto_unregister_skcipher(&salg->alg);
  179. kfree(salg);
  180. }
  181. EXPORT_SYMBOL_GPL(simd_skcipher_free);
  182. int simd_register_skciphers_compat(struct skcipher_alg *algs, int count,
  183. struct simd_skcipher_alg **simd_algs)
  184. {
  185. int err;
  186. int i;
  187. const char *algname;
  188. const char *drvname;
  189. const char *basename;
  190. struct simd_skcipher_alg *simd;
  191. err = crypto_register_skciphers(algs, count);
  192. if (err)
  193. return err;
  194. for (i = 0; i < count; i++) {
  195. WARN_ON(strncmp(algs[i].base.cra_name, "__", 2));
  196. WARN_ON(strncmp(algs[i].base.cra_driver_name, "__", 2));
  197. algname = algs[i].base.cra_name + 2;
  198. drvname = algs[i].base.cra_driver_name + 2;
  199. basename = algs[i].base.cra_driver_name;
  200. simd = simd_skcipher_create_compat(algname, drvname, basename);
  201. err = PTR_ERR(simd);
  202. if (IS_ERR(simd))
  203. goto err_unregister;
  204. simd_algs[i] = simd;
  205. }
  206. return 0;
  207. err_unregister:
  208. simd_unregister_skciphers(algs, count, simd_algs);
  209. return err;
  210. }
  211. EXPORT_SYMBOL_GPL(simd_register_skciphers_compat);
  212. void simd_unregister_skciphers(struct skcipher_alg *algs, int count,
  213. struct simd_skcipher_alg **simd_algs)
  214. {
  215. int i;
  216. crypto_unregister_skciphers(algs, count);
  217. for (i = 0; i < count; i++) {
  218. if (simd_algs[i]) {
  219. simd_skcipher_free(simd_algs[i]);
  220. simd_algs[i] = NULL;
  221. }
  222. }
  223. }
  224. EXPORT_SYMBOL_GPL(simd_unregister_skciphers);
  225. MODULE_LICENSE("GPL");