pcrypt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * pcrypt - Parallel crypto wrapper.
  3. *
  4. * Copyright (C) 2009 secunet Security Networks AG
  5. * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <crypto/algapi.h>
  21. #include <crypto/internal/aead.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <crypto/pcrypt.h>
  27. static struct padata_instance *pcrypt_enc_padata;
  28. static struct padata_instance *pcrypt_dec_padata;
  29. static struct workqueue_struct *encwq;
  30. static struct workqueue_struct *decwq;
  31. struct pcrypt_instance_ctx {
  32. struct crypto_spawn spawn;
  33. unsigned int tfm_count;
  34. };
  35. struct pcrypt_aead_ctx {
  36. struct crypto_aead *child;
  37. unsigned int cb_cpu;
  38. };
  39. static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu,
  40. struct padata_instance *pinst)
  41. {
  42. unsigned int cpu_index, cpu, i;
  43. cpu = *cb_cpu;
  44. if (cpumask_test_cpu(cpu, cpu_active_mask))
  45. goto out;
  46. cpu_index = cpu % cpumask_weight(cpu_active_mask);
  47. cpu = cpumask_first(cpu_active_mask);
  48. for (i = 0; i < cpu_index; i++)
  49. cpu = cpumask_next(cpu, cpu_active_mask);
  50. *cb_cpu = cpu;
  51. out:
  52. return padata_do_parallel(pinst, padata, cpu);
  53. }
  54. static int pcrypt_aead_setkey(struct crypto_aead *parent,
  55. const u8 *key, unsigned int keylen)
  56. {
  57. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  58. return crypto_aead_setkey(ctx->child, key, keylen);
  59. }
  60. static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
  61. unsigned int authsize)
  62. {
  63. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  64. return crypto_aead_setauthsize(ctx->child, authsize);
  65. }
  66. static void pcrypt_aead_serial(struct padata_priv *padata)
  67. {
  68. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  69. struct aead_request *req = pcrypt_request_ctx(preq);
  70. aead_request_complete(req->base.data, padata->info);
  71. }
  72. static void pcrypt_aead_giv_serial(struct padata_priv *padata)
  73. {
  74. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  75. struct aead_givcrypt_request *req = pcrypt_request_ctx(preq);
  76. aead_request_complete(req->areq.base.data, padata->info);
  77. }
  78. static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
  79. {
  80. struct aead_request *req = areq->data;
  81. struct pcrypt_request *preq = aead_request_ctx(req);
  82. struct padata_priv *padata = pcrypt_request_padata(preq);
  83. padata->info = err;
  84. req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  85. padata_do_serial(padata);
  86. }
  87. static void pcrypt_aead_enc(struct padata_priv *padata)
  88. {
  89. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  90. struct aead_request *req = pcrypt_request_ctx(preq);
  91. padata->info = crypto_aead_encrypt(req);
  92. if (padata->info == -EINPROGRESS)
  93. return;
  94. padata_do_serial(padata);
  95. }
  96. static int pcrypt_aead_encrypt(struct aead_request *req)
  97. {
  98. int err;
  99. struct pcrypt_request *preq = aead_request_ctx(req);
  100. struct aead_request *creq = pcrypt_request_ctx(preq);
  101. struct padata_priv *padata = pcrypt_request_padata(preq);
  102. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  103. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  104. u32 flags = aead_request_flags(req);
  105. memset(padata, 0, sizeof(struct padata_priv));
  106. padata->parallel = pcrypt_aead_enc;
  107. padata->serial = pcrypt_aead_serial;
  108. aead_request_set_tfm(creq, ctx->child);
  109. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  110. pcrypt_aead_done, req);
  111. aead_request_set_crypt(creq, req->src, req->dst,
  112. req->cryptlen, req->iv);
  113. aead_request_set_assoc(creq, req->assoc, req->assoclen);
  114. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_enc_padata);
  115. if (err)
  116. return err;
  117. else
  118. err = crypto_aead_encrypt(creq);
  119. return err;
  120. }
  121. static void pcrypt_aead_dec(struct padata_priv *padata)
  122. {
  123. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  124. struct aead_request *req = pcrypt_request_ctx(preq);
  125. padata->info = crypto_aead_decrypt(req);
  126. if (padata->info == -EINPROGRESS)
  127. return;
  128. padata_do_serial(padata);
  129. }
  130. static int pcrypt_aead_decrypt(struct aead_request *req)
  131. {
  132. int err;
  133. struct pcrypt_request *preq = aead_request_ctx(req);
  134. struct aead_request *creq = pcrypt_request_ctx(preq);
  135. struct padata_priv *padata = pcrypt_request_padata(preq);
  136. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  137. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  138. u32 flags = aead_request_flags(req);
  139. memset(padata, 0, sizeof(struct padata_priv));
  140. padata->parallel = pcrypt_aead_dec;
  141. padata->serial = pcrypt_aead_serial;
  142. aead_request_set_tfm(creq, ctx->child);
  143. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  144. pcrypt_aead_done, req);
  145. aead_request_set_crypt(creq, req->src, req->dst,
  146. req->cryptlen, req->iv);
  147. aead_request_set_assoc(creq, req->assoc, req->assoclen);
  148. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_dec_padata);
  149. if (err)
  150. return err;
  151. else
  152. err = crypto_aead_decrypt(creq);
  153. return err;
  154. }
  155. static void pcrypt_aead_givenc(struct padata_priv *padata)
  156. {
  157. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  158. struct aead_givcrypt_request *req = pcrypt_request_ctx(preq);
  159. padata->info = crypto_aead_givencrypt(req);
  160. if (padata->info == -EINPROGRESS)
  161. return;
  162. padata_do_serial(padata);
  163. }
  164. static int pcrypt_aead_givencrypt(struct aead_givcrypt_request *req)
  165. {
  166. int err;
  167. struct aead_request *areq = &req->areq;
  168. struct pcrypt_request *preq = aead_request_ctx(areq);
  169. struct aead_givcrypt_request *creq = pcrypt_request_ctx(preq);
  170. struct padata_priv *padata = pcrypt_request_padata(preq);
  171. struct crypto_aead *aead = aead_givcrypt_reqtfm(req);
  172. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  173. u32 flags = aead_request_flags(areq);
  174. memset(padata, 0, sizeof(struct padata_priv));
  175. padata->parallel = pcrypt_aead_givenc;
  176. padata->serial = pcrypt_aead_giv_serial;
  177. aead_givcrypt_set_tfm(creq, ctx->child);
  178. aead_givcrypt_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  179. pcrypt_aead_done, areq);
  180. aead_givcrypt_set_crypt(creq, areq->src, areq->dst,
  181. areq->cryptlen, areq->iv);
  182. aead_givcrypt_set_assoc(creq, areq->assoc, areq->assoclen);
  183. aead_givcrypt_set_giv(creq, req->giv, req->seq);
  184. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_enc_padata);
  185. if (err)
  186. return err;
  187. else
  188. err = crypto_aead_givencrypt(creq);
  189. return err;
  190. }
  191. static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm)
  192. {
  193. int cpu, cpu_index;
  194. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  195. struct pcrypt_instance_ctx *ictx = crypto_instance_ctx(inst);
  196. struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  197. struct crypto_aead *cipher;
  198. ictx->tfm_count++;
  199. cpu_index = ictx->tfm_count % cpumask_weight(cpu_active_mask);
  200. ctx->cb_cpu = cpumask_first(cpu_active_mask);
  201. for (cpu = 0; cpu < cpu_index; cpu++)
  202. ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_active_mask);
  203. cipher = crypto_spawn_aead(crypto_instance_ctx(inst));
  204. if (IS_ERR(cipher))
  205. return PTR_ERR(cipher);
  206. ctx->child = cipher;
  207. tfm->crt_aead.reqsize = sizeof(struct pcrypt_request)
  208. + sizeof(struct aead_givcrypt_request)
  209. + crypto_aead_reqsize(cipher);
  210. return 0;
  211. }
  212. static void pcrypt_aead_exit_tfm(struct crypto_tfm *tfm)
  213. {
  214. struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  215. crypto_free_aead(ctx->child);
  216. }
  217. static struct crypto_instance *pcrypt_alloc_instance(struct crypto_alg *alg)
  218. {
  219. struct crypto_instance *inst;
  220. struct pcrypt_instance_ctx *ctx;
  221. int err;
  222. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  223. if (!inst) {
  224. inst = ERR_PTR(-ENOMEM);
  225. goto out;
  226. }
  227. err = -ENAMETOOLONG;
  228. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  229. "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  230. goto out_free_inst;
  231. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  232. ctx = crypto_instance_ctx(inst);
  233. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  234. CRYPTO_ALG_TYPE_MASK);
  235. if (err)
  236. goto out_free_inst;
  237. inst->alg.cra_priority = alg->cra_priority + 100;
  238. inst->alg.cra_blocksize = alg->cra_blocksize;
  239. inst->alg.cra_alignmask = alg->cra_alignmask;
  240. out:
  241. return inst;
  242. out_free_inst:
  243. kfree(inst);
  244. inst = ERR_PTR(err);
  245. goto out;
  246. }
  247. static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb)
  248. {
  249. struct crypto_instance *inst;
  250. struct crypto_alg *alg;
  251. struct crypto_attr_type *algt;
  252. algt = crypto_get_attr_type(tb);
  253. alg = crypto_get_attr_alg(tb, algt->type,
  254. (algt->mask & CRYPTO_ALG_TYPE_MASK));
  255. if (IS_ERR(alg))
  256. return ERR_CAST(alg);
  257. inst = pcrypt_alloc_instance(alg);
  258. if (IS_ERR(inst))
  259. goto out_put_alg;
  260. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  261. inst->alg.cra_type = &crypto_aead_type;
  262. inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
  263. inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
  264. inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
  265. inst->alg.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
  266. inst->alg.cra_init = pcrypt_aead_init_tfm;
  267. inst->alg.cra_exit = pcrypt_aead_exit_tfm;
  268. inst->alg.cra_aead.setkey = pcrypt_aead_setkey;
  269. inst->alg.cra_aead.setauthsize = pcrypt_aead_setauthsize;
  270. inst->alg.cra_aead.encrypt = pcrypt_aead_encrypt;
  271. inst->alg.cra_aead.decrypt = pcrypt_aead_decrypt;
  272. inst->alg.cra_aead.givencrypt = pcrypt_aead_givencrypt;
  273. out_put_alg:
  274. crypto_mod_put(alg);
  275. return inst;
  276. }
  277. static struct crypto_instance *pcrypt_alloc(struct rtattr **tb)
  278. {
  279. struct crypto_attr_type *algt;
  280. algt = crypto_get_attr_type(tb);
  281. if (IS_ERR(algt))
  282. return ERR_CAST(algt);
  283. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  284. case CRYPTO_ALG_TYPE_AEAD:
  285. return pcrypt_alloc_aead(tb);
  286. }
  287. return ERR_PTR(-EINVAL);
  288. }
  289. static void pcrypt_free(struct crypto_instance *inst)
  290. {
  291. struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst);
  292. crypto_drop_spawn(&ctx->spawn);
  293. kfree(inst);
  294. }
  295. static struct crypto_template pcrypt_tmpl = {
  296. .name = "pcrypt",
  297. .alloc = pcrypt_alloc,
  298. .free = pcrypt_free,
  299. .module = THIS_MODULE,
  300. };
  301. static int __init pcrypt_init(void)
  302. {
  303. encwq = create_workqueue("pencrypt");
  304. if (!encwq)
  305. goto err;
  306. decwq = create_workqueue("pdecrypt");
  307. if (!decwq)
  308. goto err_destroy_encwq;
  309. pcrypt_enc_padata = padata_alloc(cpu_possible_mask, encwq);
  310. if (!pcrypt_enc_padata)
  311. goto err_destroy_decwq;
  312. pcrypt_dec_padata = padata_alloc(cpu_possible_mask, decwq);
  313. if (!pcrypt_dec_padata)
  314. goto err_free_padata;
  315. padata_start(pcrypt_enc_padata);
  316. padata_start(pcrypt_dec_padata);
  317. return crypto_register_template(&pcrypt_tmpl);
  318. err_free_padata:
  319. padata_free(pcrypt_enc_padata);
  320. err_destroy_decwq:
  321. destroy_workqueue(decwq);
  322. err_destroy_encwq:
  323. destroy_workqueue(encwq);
  324. err:
  325. return -ENOMEM;
  326. }
  327. static void __exit pcrypt_exit(void)
  328. {
  329. padata_stop(pcrypt_enc_padata);
  330. padata_stop(pcrypt_dec_padata);
  331. destroy_workqueue(encwq);
  332. destroy_workqueue(decwq);
  333. padata_free(pcrypt_enc_padata);
  334. padata_free(pcrypt_dec_padata);
  335. crypto_unregister_template(&pcrypt_tmpl);
  336. }
  337. module_init(pcrypt_init);
  338. module_exit(pcrypt_exit);
  339. MODULE_LICENSE("GPL");
  340. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  341. MODULE_DESCRIPTION("Parallel crypto wrapper");