pcrypt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * pcrypt - Parallel crypto wrapper.
  4. *
  5. * Copyright (C) 2009 secunet Security Networks AG
  6. * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
  7. */
  8. #include <crypto/algapi.h>
  9. #include <crypto/internal/aead.h>
  10. #include <linux/atomic.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/kobject.h>
  16. #include <linux/cpu.h>
  17. #include <crypto/pcrypt.h>
  18. static struct padata_instance *pencrypt;
  19. static struct padata_instance *pdecrypt;
  20. static struct kset *pcrypt_kset;
  21. struct pcrypt_instance_ctx {
  22. struct crypto_aead_spawn spawn;
  23. struct padata_shell *psenc;
  24. struct padata_shell *psdec;
  25. atomic_t tfm_count;
  26. };
  27. struct pcrypt_aead_ctx {
  28. struct crypto_aead *child;
  29. unsigned int cb_cpu;
  30. };
  31. static inline struct pcrypt_instance_ctx *pcrypt_tfm_ictx(
  32. struct crypto_aead *tfm)
  33. {
  34. return aead_instance_ctx(aead_alg_instance(tfm));
  35. }
  36. static int pcrypt_aead_setkey(struct crypto_aead *parent,
  37. const u8 *key, unsigned int keylen)
  38. {
  39. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  40. return crypto_aead_setkey(ctx->child, key, keylen);
  41. }
  42. static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
  43. unsigned int authsize)
  44. {
  45. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  46. return crypto_aead_setauthsize(ctx->child, authsize);
  47. }
  48. static void pcrypt_aead_serial(struct padata_priv *padata)
  49. {
  50. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  51. struct aead_request *req = pcrypt_request_ctx(preq);
  52. aead_request_complete(req->base.data, padata->info);
  53. }
  54. static void pcrypt_aead_done(void *data, int err)
  55. {
  56. struct aead_request *req = data;
  57. struct pcrypt_request *preq = aead_request_ctx(req);
  58. struct padata_priv *padata = pcrypt_request_padata(preq);
  59. padata->info = err;
  60. padata_do_serial(padata);
  61. }
  62. static void pcrypt_aead_enc(struct padata_priv *padata)
  63. {
  64. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  65. struct aead_request *req = pcrypt_request_ctx(preq);
  66. int ret;
  67. ret = crypto_aead_encrypt(req);
  68. if (ret == -EINPROGRESS)
  69. return;
  70. padata->info = ret;
  71. padata_do_serial(padata);
  72. }
  73. static int pcrypt_aead_encrypt(struct aead_request *req)
  74. {
  75. int err;
  76. struct pcrypt_request *preq = aead_request_ctx(req);
  77. struct aead_request *creq = pcrypt_request_ctx(preq);
  78. struct padata_priv *padata = pcrypt_request_padata(preq);
  79. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  80. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  81. u32 flags = aead_request_flags(req);
  82. struct pcrypt_instance_ctx *ictx;
  83. ictx = pcrypt_tfm_ictx(aead);
  84. memset(padata, 0, sizeof(struct padata_priv));
  85. padata->parallel = pcrypt_aead_enc;
  86. padata->serial = pcrypt_aead_serial;
  87. aead_request_set_tfm(creq, ctx->child);
  88. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  89. pcrypt_aead_done, req);
  90. aead_request_set_crypt(creq, req->src, req->dst,
  91. req->cryptlen, req->iv);
  92. aead_request_set_ad(creq, req->assoclen);
  93. err = padata_do_parallel(ictx->psenc, padata, &ctx->cb_cpu);
  94. if (!err)
  95. return -EINPROGRESS;
  96. if (err == -EBUSY) {
  97. /* try non-parallel mode */
  98. return crypto_aead_encrypt(creq);
  99. }
  100. return err;
  101. }
  102. static void pcrypt_aead_dec(struct padata_priv *padata)
  103. {
  104. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  105. struct aead_request *req = pcrypt_request_ctx(preq);
  106. int ret;
  107. ret = crypto_aead_decrypt(req);
  108. if (ret == -EINPROGRESS)
  109. return;
  110. padata->info = ret;
  111. padata_do_serial(padata);
  112. }
  113. static int pcrypt_aead_decrypt(struct aead_request *req)
  114. {
  115. int err;
  116. struct pcrypt_request *preq = aead_request_ctx(req);
  117. struct aead_request *creq = pcrypt_request_ctx(preq);
  118. struct padata_priv *padata = pcrypt_request_padata(preq);
  119. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  120. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  121. u32 flags = aead_request_flags(req);
  122. struct pcrypt_instance_ctx *ictx;
  123. ictx = pcrypt_tfm_ictx(aead);
  124. memset(padata, 0, sizeof(struct padata_priv));
  125. padata->parallel = pcrypt_aead_dec;
  126. padata->serial = pcrypt_aead_serial;
  127. aead_request_set_tfm(creq, ctx->child);
  128. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  129. pcrypt_aead_done, req);
  130. aead_request_set_crypt(creq, req->src, req->dst,
  131. req->cryptlen, req->iv);
  132. aead_request_set_ad(creq, req->assoclen);
  133. err = padata_do_parallel(ictx->psdec, padata, &ctx->cb_cpu);
  134. if (!err)
  135. return -EINPROGRESS;
  136. if (err == -EBUSY) {
  137. /* try non-parallel mode */
  138. return crypto_aead_decrypt(creq);
  139. }
  140. return err;
  141. }
  142. static int pcrypt_aead_init_tfm(struct crypto_aead *tfm)
  143. {
  144. int cpu, cpu_index;
  145. struct aead_instance *inst = aead_alg_instance(tfm);
  146. struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst);
  147. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
  148. struct crypto_aead *cipher;
  149. cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
  150. cpumask_weight(cpu_online_mask);
  151. ctx->cb_cpu = cpumask_first(cpu_online_mask);
  152. for (cpu = 0; cpu < cpu_index; cpu++)
  153. ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask);
  154. cipher = crypto_spawn_aead(&ictx->spawn);
  155. if (IS_ERR(cipher))
  156. return PTR_ERR(cipher);
  157. ctx->child = cipher;
  158. crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) +
  159. sizeof(struct aead_request) +
  160. crypto_aead_reqsize(cipher));
  161. return 0;
  162. }
  163. static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm)
  164. {
  165. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
  166. crypto_free_aead(ctx->child);
  167. }
  168. static void pcrypt_free(struct aead_instance *inst)
  169. {
  170. struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst);
  171. crypto_drop_aead(&ctx->spawn);
  172. padata_free_shell(ctx->psdec);
  173. padata_free_shell(ctx->psenc);
  174. kfree(inst);
  175. }
  176. static int pcrypt_init_instance(struct crypto_instance *inst,
  177. struct crypto_alg *alg)
  178. {
  179. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  180. "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  181. return -ENAMETOOLONG;
  182. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  183. inst->alg.cra_priority = alg->cra_priority + 100;
  184. inst->alg.cra_blocksize = alg->cra_blocksize;
  185. inst->alg.cra_alignmask = alg->cra_alignmask;
  186. return 0;
  187. }
  188. static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb,
  189. struct crypto_attr_type *algt)
  190. {
  191. struct pcrypt_instance_ctx *ctx;
  192. struct aead_instance *inst;
  193. struct aead_alg *alg;
  194. u32 mask = crypto_algt_inherited_mask(algt);
  195. int err;
  196. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  197. if (!inst)
  198. return -ENOMEM;
  199. err = -ENOMEM;
  200. ctx = aead_instance_ctx(inst);
  201. ctx->psenc = padata_alloc_shell(pencrypt);
  202. if (!ctx->psenc)
  203. goto err_free_inst;
  204. ctx->psdec = padata_alloc_shell(pdecrypt);
  205. if (!ctx->psdec)
  206. goto err_free_inst;
  207. err = crypto_grab_aead(&ctx->spawn, aead_crypto_instance(inst),
  208. crypto_attr_alg_name(tb[1]), 0, mask);
  209. if (err)
  210. goto err_free_inst;
  211. alg = crypto_spawn_aead_alg(&ctx->spawn);
  212. err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base);
  213. if (err)
  214. goto err_free_inst;
  215. inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC;
  216. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  217. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  218. inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
  219. inst->alg.init = pcrypt_aead_init_tfm;
  220. inst->alg.exit = pcrypt_aead_exit_tfm;
  221. inst->alg.setkey = pcrypt_aead_setkey;
  222. inst->alg.setauthsize = pcrypt_aead_setauthsize;
  223. inst->alg.encrypt = pcrypt_aead_encrypt;
  224. inst->alg.decrypt = pcrypt_aead_decrypt;
  225. inst->free = pcrypt_free;
  226. err = aead_register_instance(tmpl, inst);
  227. if (err) {
  228. err_free_inst:
  229. pcrypt_free(inst);
  230. }
  231. return err;
  232. }
  233. static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb)
  234. {
  235. struct crypto_attr_type *algt;
  236. algt = crypto_get_attr_type(tb);
  237. if (IS_ERR(algt))
  238. return PTR_ERR(algt);
  239. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  240. case CRYPTO_ALG_TYPE_AEAD:
  241. return pcrypt_create_aead(tmpl, tb, algt);
  242. }
  243. return -EINVAL;
  244. }
  245. static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name)
  246. {
  247. int ret;
  248. pinst->kobj.kset = pcrypt_kset;
  249. ret = kobject_add(&pinst->kobj, NULL, "%s", name);
  250. if (!ret)
  251. kobject_uevent(&pinst->kobj, KOBJ_ADD);
  252. return ret;
  253. }
  254. static int pcrypt_init_padata(struct padata_instance **pinst, const char *name)
  255. {
  256. int ret = -ENOMEM;
  257. *pinst = padata_alloc(name);
  258. if (!*pinst)
  259. return ret;
  260. ret = pcrypt_sysfs_add(*pinst, name);
  261. if (ret)
  262. padata_free(*pinst);
  263. return ret;
  264. }
  265. static struct crypto_template pcrypt_tmpl = {
  266. .name = "pcrypt",
  267. .create = pcrypt_create,
  268. .module = THIS_MODULE,
  269. };
  270. static int __init pcrypt_init(void)
  271. {
  272. int err = -ENOMEM;
  273. pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj);
  274. if (!pcrypt_kset)
  275. goto err;
  276. err = pcrypt_init_padata(&pencrypt, "pencrypt");
  277. if (err)
  278. goto err_unreg_kset;
  279. err = pcrypt_init_padata(&pdecrypt, "pdecrypt");
  280. if (err)
  281. goto err_deinit_pencrypt;
  282. return crypto_register_template(&pcrypt_tmpl);
  283. err_deinit_pencrypt:
  284. padata_free(pencrypt);
  285. err_unreg_kset:
  286. kset_unregister(pcrypt_kset);
  287. err:
  288. return err;
  289. }
  290. static void __exit pcrypt_exit(void)
  291. {
  292. crypto_unregister_template(&pcrypt_tmpl);
  293. padata_free(pencrypt);
  294. padata_free(pdecrypt);
  295. kset_unregister(pcrypt_kset);
  296. }
  297. subsys_initcall(pcrypt_init);
  298. module_exit(pcrypt_exit);
  299. MODULE_LICENSE("GPL");
  300. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  301. MODULE_DESCRIPTION("Parallel crypto wrapper");
  302. MODULE_ALIAS_CRYPTO("pcrypt");