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 -EINPROGRESS;
  117. return err;
  118. }
  119. static void pcrypt_aead_dec(struct padata_priv *padata)
  120. {
  121. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  122. struct aead_request *req = pcrypt_request_ctx(preq);
  123. padata->info = crypto_aead_decrypt(req);
  124. if (padata->info == -EINPROGRESS)
  125. return;
  126. padata_do_serial(padata);
  127. }
  128. static int pcrypt_aead_decrypt(struct aead_request *req)
  129. {
  130. int err;
  131. struct pcrypt_request *preq = aead_request_ctx(req);
  132. struct aead_request *creq = pcrypt_request_ctx(preq);
  133. struct padata_priv *padata = pcrypt_request_padata(preq);
  134. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  135. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  136. u32 flags = aead_request_flags(req);
  137. memset(padata, 0, sizeof(struct padata_priv));
  138. padata->parallel = pcrypt_aead_dec;
  139. padata->serial = pcrypt_aead_serial;
  140. aead_request_set_tfm(creq, ctx->child);
  141. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  142. pcrypt_aead_done, req);
  143. aead_request_set_crypt(creq, req->src, req->dst,
  144. req->cryptlen, req->iv);
  145. aead_request_set_assoc(creq, req->assoc, req->assoclen);
  146. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_dec_padata);
  147. if (!err)
  148. return -EINPROGRESS;
  149. return err;
  150. }
  151. static void pcrypt_aead_givenc(struct padata_priv *padata)
  152. {
  153. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  154. struct aead_givcrypt_request *req = pcrypt_request_ctx(preq);
  155. padata->info = crypto_aead_givencrypt(req);
  156. if (padata->info == -EINPROGRESS)
  157. return;
  158. padata_do_serial(padata);
  159. }
  160. static int pcrypt_aead_givencrypt(struct aead_givcrypt_request *req)
  161. {
  162. int err;
  163. struct aead_request *areq = &req->areq;
  164. struct pcrypt_request *preq = aead_request_ctx(areq);
  165. struct aead_givcrypt_request *creq = pcrypt_request_ctx(preq);
  166. struct padata_priv *padata = pcrypt_request_padata(preq);
  167. struct crypto_aead *aead = aead_givcrypt_reqtfm(req);
  168. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  169. u32 flags = aead_request_flags(areq);
  170. memset(padata, 0, sizeof(struct padata_priv));
  171. padata->parallel = pcrypt_aead_givenc;
  172. padata->serial = pcrypt_aead_giv_serial;
  173. aead_givcrypt_set_tfm(creq, ctx->child);
  174. aead_givcrypt_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  175. pcrypt_aead_done, areq);
  176. aead_givcrypt_set_crypt(creq, areq->src, areq->dst,
  177. areq->cryptlen, areq->iv);
  178. aead_givcrypt_set_assoc(creq, areq->assoc, areq->assoclen);
  179. aead_givcrypt_set_giv(creq, req->giv, req->seq);
  180. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, pcrypt_enc_padata);
  181. if (!err)
  182. return -EINPROGRESS;
  183. return err;
  184. }
  185. static int pcrypt_aead_init_tfm(struct crypto_tfm *tfm)
  186. {
  187. int cpu, cpu_index;
  188. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  189. struct pcrypt_instance_ctx *ictx = crypto_instance_ctx(inst);
  190. struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  191. struct crypto_aead *cipher;
  192. ictx->tfm_count++;
  193. cpu_index = ictx->tfm_count % cpumask_weight(cpu_active_mask);
  194. ctx->cb_cpu = cpumask_first(cpu_active_mask);
  195. for (cpu = 0; cpu < cpu_index; cpu++)
  196. ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_active_mask);
  197. cipher = crypto_spawn_aead(crypto_instance_ctx(inst));
  198. if (IS_ERR(cipher))
  199. return PTR_ERR(cipher);
  200. ctx->child = cipher;
  201. tfm->crt_aead.reqsize = sizeof(struct pcrypt_request)
  202. + sizeof(struct aead_givcrypt_request)
  203. + crypto_aead_reqsize(cipher);
  204. return 0;
  205. }
  206. static void pcrypt_aead_exit_tfm(struct crypto_tfm *tfm)
  207. {
  208. struct pcrypt_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  209. crypto_free_aead(ctx->child);
  210. }
  211. static struct crypto_instance *pcrypt_alloc_instance(struct crypto_alg *alg)
  212. {
  213. struct crypto_instance *inst;
  214. struct pcrypt_instance_ctx *ctx;
  215. int err;
  216. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  217. if (!inst) {
  218. inst = ERR_PTR(-ENOMEM);
  219. goto out;
  220. }
  221. err = -ENAMETOOLONG;
  222. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  223. "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  224. goto out_free_inst;
  225. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  226. ctx = crypto_instance_ctx(inst);
  227. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  228. CRYPTO_ALG_TYPE_MASK);
  229. if (err)
  230. goto out_free_inst;
  231. inst->alg.cra_priority = alg->cra_priority + 100;
  232. inst->alg.cra_blocksize = alg->cra_blocksize;
  233. inst->alg.cra_alignmask = alg->cra_alignmask;
  234. out:
  235. return inst;
  236. out_free_inst:
  237. kfree(inst);
  238. inst = ERR_PTR(err);
  239. goto out;
  240. }
  241. static struct crypto_instance *pcrypt_alloc_aead(struct rtattr **tb,
  242. u32 type, u32 mask)
  243. {
  244. struct crypto_instance *inst;
  245. struct crypto_alg *alg;
  246. alg = crypto_get_attr_alg(tb, type, (mask & CRYPTO_ALG_TYPE_MASK));
  247. if (IS_ERR(alg))
  248. return ERR_CAST(alg);
  249. inst = pcrypt_alloc_instance(alg);
  250. if (IS_ERR(inst))
  251. goto out_put_alg;
  252. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  253. inst->alg.cra_type = &crypto_aead_type;
  254. inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
  255. inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
  256. inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
  257. inst->alg.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
  258. inst->alg.cra_init = pcrypt_aead_init_tfm;
  259. inst->alg.cra_exit = pcrypt_aead_exit_tfm;
  260. inst->alg.cra_aead.setkey = pcrypt_aead_setkey;
  261. inst->alg.cra_aead.setauthsize = pcrypt_aead_setauthsize;
  262. inst->alg.cra_aead.encrypt = pcrypt_aead_encrypt;
  263. inst->alg.cra_aead.decrypt = pcrypt_aead_decrypt;
  264. inst->alg.cra_aead.givencrypt = pcrypt_aead_givencrypt;
  265. out_put_alg:
  266. crypto_mod_put(alg);
  267. return inst;
  268. }
  269. static struct crypto_instance *pcrypt_alloc(struct rtattr **tb)
  270. {
  271. struct crypto_attr_type *algt;
  272. algt = crypto_get_attr_type(tb);
  273. if (IS_ERR(algt))
  274. return ERR_CAST(algt);
  275. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  276. case CRYPTO_ALG_TYPE_AEAD:
  277. return pcrypt_alloc_aead(tb, algt->type, algt->mask);
  278. }
  279. return ERR_PTR(-EINVAL);
  280. }
  281. static void pcrypt_free(struct crypto_instance *inst)
  282. {
  283. struct pcrypt_instance_ctx *ctx = crypto_instance_ctx(inst);
  284. crypto_drop_spawn(&ctx->spawn);
  285. kfree(inst);
  286. }
  287. static struct crypto_template pcrypt_tmpl = {
  288. .name = "pcrypt",
  289. .alloc = pcrypt_alloc,
  290. .free = pcrypt_free,
  291. .module = THIS_MODULE,
  292. };
  293. static int __init pcrypt_init(void)
  294. {
  295. int err = -ENOMEM;
  296. encwq = create_workqueue("pencrypt");
  297. if (!encwq)
  298. goto err;
  299. decwq = create_workqueue("pdecrypt");
  300. if (!decwq)
  301. goto err_destroy_encwq;
  302. pcrypt_enc_padata = padata_alloc(cpu_possible_mask, encwq);
  303. if (!pcrypt_enc_padata)
  304. goto err_destroy_decwq;
  305. pcrypt_dec_padata = padata_alloc(cpu_possible_mask, decwq);
  306. if (!pcrypt_dec_padata)
  307. goto err_free_enc_padata;
  308. err = padata_start(pcrypt_enc_padata);
  309. if (err)
  310. goto err_free_dec_padata;
  311. err = padata_start(pcrypt_dec_padata);
  312. if (err)
  313. goto err_free_dec_padata;
  314. return crypto_register_template(&pcrypt_tmpl);
  315. err_free_dec_padata:
  316. padata_free(pcrypt_dec_padata);
  317. err_free_enc_padata:
  318. padata_free(pcrypt_enc_padata);
  319. err_destroy_decwq:
  320. destroy_workqueue(decwq);
  321. err_destroy_encwq:
  322. destroy_workqueue(encwq);
  323. err:
  324. return err;
  325. }
  326. static void __exit pcrypt_exit(void)
  327. {
  328. padata_stop(pcrypt_enc_padata);
  329. padata_stop(pcrypt_dec_padata);
  330. destroy_workqueue(encwq);
  331. destroy_workqueue(decwq);
  332. padata_free(pcrypt_enc_padata);
  333. padata_free(pcrypt_dec_padata);
  334. crypto_unregister_template(&pcrypt_tmpl);
  335. }
  336. module_init(pcrypt_init);
  337. module_exit(pcrypt_exit);
  338. MODULE_LICENSE("GPL");
  339. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  340. MODULE_DESCRIPTION("Parallel crypto wrapper");