pcrypt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/atomic.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/notifier.h>
  28. #include <linux/kobject.h>
  29. #include <linux/cpu.h>
  30. #include <crypto/pcrypt.h>
  31. struct padata_pcrypt {
  32. struct padata_instance *pinst;
  33. struct workqueue_struct *wq;
  34. /*
  35. * Cpumask for callback CPUs. It should be
  36. * equal to serial cpumask of corresponding padata instance,
  37. * so it is updated when padata notifies us about serial
  38. * cpumask change.
  39. *
  40. * cb_cpumask is protected by RCU. This fact prevents us from
  41. * using cpumask_var_t directly because the actual type of
  42. * cpumsak_var_t depends on kernel configuration(particularly on
  43. * CONFIG_CPUMASK_OFFSTACK macro). Depending on the configuration
  44. * cpumask_var_t may be either a pointer to the struct cpumask
  45. * or a variable allocated on the stack. Thus we can not safely use
  46. * cpumask_var_t with RCU operations such as rcu_assign_pointer or
  47. * rcu_dereference. So cpumask_var_t is wrapped with struct
  48. * pcrypt_cpumask which makes possible to use it with RCU.
  49. */
  50. struct pcrypt_cpumask {
  51. cpumask_var_t mask;
  52. } *cb_cpumask;
  53. struct notifier_block nblock;
  54. };
  55. static struct padata_pcrypt pencrypt;
  56. static struct padata_pcrypt pdecrypt;
  57. static struct kset *pcrypt_kset;
  58. struct pcrypt_instance_ctx {
  59. struct crypto_aead_spawn spawn;
  60. atomic_t tfm_count;
  61. };
  62. struct pcrypt_aead_ctx {
  63. struct crypto_aead *child;
  64. unsigned int cb_cpu;
  65. };
  66. static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu,
  67. struct padata_pcrypt *pcrypt)
  68. {
  69. unsigned int cpu_index, cpu, i;
  70. struct pcrypt_cpumask *cpumask;
  71. cpu = *cb_cpu;
  72. rcu_read_lock_bh();
  73. cpumask = rcu_dereference_bh(pcrypt->cb_cpumask);
  74. if (cpumask_test_cpu(cpu, cpumask->mask))
  75. goto out;
  76. if (!cpumask_weight(cpumask->mask))
  77. goto out;
  78. cpu_index = cpu % cpumask_weight(cpumask->mask);
  79. cpu = cpumask_first(cpumask->mask);
  80. for (i = 0; i < cpu_index; i++)
  81. cpu = cpumask_next(cpu, cpumask->mask);
  82. *cb_cpu = cpu;
  83. out:
  84. rcu_read_unlock_bh();
  85. return padata_do_parallel(pcrypt->pinst, padata, cpu);
  86. }
  87. static int pcrypt_aead_setkey(struct crypto_aead *parent,
  88. const u8 *key, unsigned int keylen)
  89. {
  90. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  91. return crypto_aead_setkey(ctx->child, key, keylen);
  92. }
  93. static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
  94. unsigned int authsize)
  95. {
  96. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
  97. return crypto_aead_setauthsize(ctx->child, authsize);
  98. }
  99. static void pcrypt_aead_serial(struct padata_priv *padata)
  100. {
  101. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  102. struct aead_request *req = pcrypt_request_ctx(preq);
  103. aead_request_complete(req->base.data, padata->info);
  104. }
  105. static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
  106. {
  107. struct aead_request *req = areq->data;
  108. struct pcrypt_request *preq = aead_request_ctx(req);
  109. struct padata_priv *padata = pcrypt_request_padata(preq);
  110. padata->info = err;
  111. req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  112. padata_do_serial(padata);
  113. }
  114. static void pcrypt_aead_enc(struct padata_priv *padata)
  115. {
  116. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  117. struct aead_request *req = pcrypt_request_ctx(preq);
  118. padata->info = crypto_aead_encrypt(req);
  119. if (padata->info == -EINPROGRESS)
  120. return;
  121. padata_do_serial(padata);
  122. }
  123. static int pcrypt_aead_encrypt(struct aead_request *req)
  124. {
  125. int err;
  126. struct pcrypt_request *preq = aead_request_ctx(req);
  127. struct aead_request *creq = pcrypt_request_ctx(preq);
  128. struct padata_priv *padata = pcrypt_request_padata(preq);
  129. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  130. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  131. u32 flags = aead_request_flags(req);
  132. memset(padata, 0, sizeof(struct padata_priv));
  133. padata->parallel = pcrypt_aead_enc;
  134. padata->serial = pcrypt_aead_serial;
  135. aead_request_set_tfm(creq, ctx->child);
  136. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  137. pcrypt_aead_done, req);
  138. aead_request_set_crypt(creq, req->src, req->dst,
  139. req->cryptlen, req->iv);
  140. aead_request_set_ad(creq, req->assoclen);
  141. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt);
  142. if (!err)
  143. return -EINPROGRESS;
  144. return err;
  145. }
  146. static void pcrypt_aead_dec(struct padata_priv *padata)
  147. {
  148. struct pcrypt_request *preq = pcrypt_padata_request(padata);
  149. struct aead_request *req = pcrypt_request_ctx(preq);
  150. padata->info = crypto_aead_decrypt(req);
  151. if (padata->info == -EINPROGRESS)
  152. return;
  153. padata_do_serial(padata);
  154. }
  155. static int pcrypt_aead_decrypt(struct aead_request *req)
  156. {
  157. int err;
  158. struct pcrypt_request *preq = aead_request_ctx(req);
  159. struct aead_request *creq = pcrypt_request_ctx(preq);
  160. struct padata_priv *padata = pcrypt_request_padata(preq);
  161. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  162. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
  163. u32 flags = aead_request_flags(req);
  164. memset(padata, 0, sizeof(struct padata_priv));
  165. padata->parallel = pcrypt_aead_dec;
  166. padata->serial = pcrypt_aead_serial;
  167. aead_request_set_tfm(creq, ctx->child);
  168. aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
  169. pcrypt_aead_done, req);
  170. aead_request_set_crypt(creq, req->src, req->dst,
  171. req->cryptlen, req->iv);
  172. aead_request_set_ad(creq, req->assoclen);
  173. err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pdecrypt);
  174. if (!err)
  175. return -EINPROGRESS;
  176. return err;
  177. }
  178. static int pcrypt_aead_init_tfm(struct crypto_aead *tfm)
  179. {
  180. int cpu, cpu_index;
  181. struct aead_instance *inst = aead_alg_instance(tfm);
  182. struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst);
  183. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
  184. struct crypto_aead *cipher;
  185. cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
  186. cpumask_weight(cpu_online_mask);
  187. ctx->cb_cpu = cpumask_first(cpu_online_mask);
  188. for (cpu = 0; cpu < cpu_index; cpu++)
  189. ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask);
  190. cipher = crypto_spawn_aead(&ictx->spawn);
  191. if (IS_ERR(cipher))
  192. return PTR_ERR(cipher);
  193. ctx->child = cipher;
  194. crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) +
  195. sizeof(struct aead_request) +
  196. crypto_aead_reqsize(cipher));
  197. return 0;
  198. }
  199. static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm)
  200. {
  201. struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
  202. crypto_free_aead(ctx->child);
  203. }
  204. static void pcrypt_free(struct aead_instance *inst)
  205. {
  206. struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst);
  207. crypto_drop_aead(&ctx->spawn);
  208. kfree(inst);
  209. }
  210. static int pcrypt_init_instance(struct crypto_instance *inst,
  211. struct crypto_alg *alg)
  212. {
  213. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  214. "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  215. return -ENAMETOOLONG;
  216. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  217. inst->alg.cra_priority = alg->cra_priority + 100;
  218. inst->alg.cra_blocksize = alg->cra_blocksize;
  219. inst->alg.cra_alignmask = alg->cra_alignmask;
  220. return 0;
  221. }
  222. static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb,
  223. u32 type, u32 mask)
  224. {
  225. struct pcrypt_instance_ctx *ctx;
  226. struct crypto_attr_type *algt;
  227. struct aead_instance *inst;
  228. struct aead_alg *alg;
  229. const char *name;
  230. int err;
  231. algt = crypto_get_attr_type(tb);
  232. if (IS_ERR(algt))
  233. return PTR_ERR(algt);
  234. name = crypto_attr_alg_name(tb[1]);
  235. if (IS_ERR(name))
  236. return PTR_ERR(name);
  237. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  238. if (!inst)
  239. return -ENOMEM;
  240. ctx = aead_instance_ctx(inst);
  241. crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst));
  242. err = crypto_grab_aead(&ctx->spawn, name, 0, 0);
  243. if (err)
  244. goto out_free_inst;
  245. alg = crypto_spawn_aead_alg(&ctx->spawn);
  246. err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base);
  247. if (err)
  248. goto out_drop_aead;
  249. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC;
  250. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  251. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  252. inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
  253. inst->alg.init = pcrypt_aead_init_tfm;
  254. inst->alg.exit = pcrypt_aead_exit_tfm;
  255. inst->alg.setkey = pcrypt_aead_setkey;
  256. inst->alg.setauthsize = pcrypt_aead_setauthsize;
  257. inst->alg.encrypt = pcrypt_aead_encrypt;
  258. inst->alg.decrypt = pcrypt_aead_decrypt;
  259. inst->free = pcrypt_free;
  260. err = aead_register_instance(tmpl, inst);
  261. if (err)
  262. goto out_drop_aead;
  263. out:
  264. return err;
  265. out_drop_aead:
  266. crypto_drop_aead(&ctx->spawn);
  267. out_free_inst:
  268. kfree(inst);
  269. goto out;
  270. }
  271. static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb)
  272. {
  273. struct crypto_attr_type *algt;
  274. algt = crypto_get_attr_type(tb);
  275. if (IS_ERR(algt))
  276. return PTR_ERR(algt);
  277. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  278. case CRYPTO_ALG_TYPE_AEAD:
  279. return pcrypt_create_aead(tmpl, tb, algt->type, algt->mask);
  280. }
  281. return -EINVAL;
  282. }
  283. static int pcrypt_cpumask_change_notify(struct notifier_block *self,
  284. unsigned long val, void *data)
  285. {
  286. struct padata_pcrypt *pcrypt;
  287. struct pcrypt_cpumask *new_mask, *old_mask;
  288. struct padata_cpumask *cpumask = (struct padata_cpumask *)data;
  289. if (!(val & PADATA_CPU_SERIAL))
  290. return 0;
  291. pcrypt = container_of(self, struct padata_pcrypt, nblock);
  292. new_mask = kmalloc(sizeof(*new_mask), GFP_KERNEL);
  293. if (!new_mask)
  294. return -ENOMEM;
  295. if (!alloc_cpumask_var(&new_mask->mask, GFP_KERNEL)) {
  296. kfree(new_mask);
  297. return -ENOMEM;
  298. }
  299. old_mask = pcrypt->cb_cpumask;
  300. cpumask_copy(new_mask->mask, cpumask->cbcpu);
  301. rcu_assign_pointer(pcrypt->cb_cpumask, new_mask);
  302. synchronize_rcu_bh();
  303. free_cpumask_var(old_mask->mask);
  304. kfree(old_mask);
  305. return 0;
  306. }
  307. static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name)
  308. {
  309. int ret;
  310. pinst->kobj.kset = pcrypt_kset;
  311. ret = kobject_add(&pinst->kobj, NULL, name);
  312. if (!ret)
  313. kobject_uevent(&pinst->kobj, KOBJ_ADD);
  314. return ret;
  315. }
  316. static int pcrypt_init_padata(struct padata_pcrypt *pcrypt,
  317. const char *name)
  318. {
  319. int ret = -ENOMEM;
  320. struct pcrypt_cpumask *mask;
  321. get_online_cpus();
  322. pcrypt->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
  323. 1, name);
  324. if (!pcrypt->wq)
  325. goto err;
  326. pcrypt->pinst = padata_alloc_possible(pcrypt->wq);
  327. if (!pcrypt->pinst)
  328. goto err_destroy_workqueue;
  329. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  330. if (!mask)
  331. goto err_free_padata;
  332. if (!alloc_cpumask_var(&mask->mask, GFP_KERNEL)) {
  333. kfree(mask);
  334. goto err_free_padata;
  335. }
  336. cpumask_and(mask->mask, cpu_possible_mask, cpu_online_mask);
  337. rcu_assign_pointer(pcrypt->cb_cpumask, mask);
  338. pcrypt->nblock.notifier_call = pcrypt_cpumask_change_notify;
  339. ret = padata_register_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
  340. if (ret)
  341. goto err_free_cpumask;
  342. ret = pcrypt_sysfs_add(pcrypt->pinst, name);
  343. if (ret)
  344. goto err_unregister_notifier;
  345. put_online_cpus();
  346. return ret;
  347. err_unregister_notifier:
  348. padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
  349. err_free_cpumask:
  350. free_cpumask_var(mask->mask);
  351. kfree(mask);
  352. err_free_padata:
  353. padata_free(pcrypt->pinst);
  354. err_destroy_workqueue:
  355. destroy_workqueue(pcrypt->wq);
  356. err:
  357. put_online_cpus();
  358. return ret;
  359. }
  360. static void pcrypt_fini_padata(struct padata_pcrypt *pcrypt)
  361. {
  362. free_cpumask_var(pcrypt->cb_cpumask->mask);
  363. kfree(pcrypt->cb_cpumask);
  364. padata_stop(pcrypt->pinst);
  365. padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
  366. destroy_workqueue(pcrypt->wq);
  367. padata_free(pcrypt->pinst);
  368. }
  369. static struct crypto_template pcrypt_tmpl = {
  370. .name = "pcrypt",
  371. .create = pcrypt_create,
  372. .module = THIS_MODULE,
  373. };
  374. static int __init pcrypt_init(void)
  375. {
  376. int err = -ENOMEM;
  377. pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj);
  378. if (!pcrypt_kset)
  379. goto err;
  380. err = pcrypt_init_padata(&pencrypt, "pencrypt");
  381. if (err)
  382. goto err_unreg_kset;
  383. err = pcrypt_init_padata(&pdecrypt, "pdecrypt");
  384. if (err)
  385. goto err_deinit_pencrypt;
  386. padata_start(pencrypt.pinst);
  387. padata_start(pdecrypt.pinst);
  388. return crypto_register_template(&pcrypt_tmpl);
  389. err_deinit_pencrypt:
  390. pcrypt_fini_padata(&pencrypt);
  391. err_unreg_kset:
  392. kset_unregister(pcrypt_kset);
  393. err:
  394. return err;
  395. }
  396. static void __exit pcrypt_exit(void)
  397. {
  398. pcrypt_fini_padata(&pencrypt);
  399. pcrypt_fini_padata(&pdecrypt);
  400. kset_unregister(pcrypt_kset);
  401. crypto_unregister_template(&pcrypt_tmpl);
  402. }
  403. module_init(pcrypt_init);
  404. module_exit(pcrypt_exit);
  405. MODULE_LICENSE("GPL");
  406. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  407. MODULE_DESCRIPTION("Parallel crypto wrapper");
  408. MODULE_ALIAS_CRYPTO("pcrypt");