pcrypt.c 15 KB

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