cryptd.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * Software async crypto daemon.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * Added AEAD support to cryptd.
  7. * Authors: Tadeusz Struk (tadeusz.struk@intel.com)
  8. * Adrian Hoban <adrian.hoban@intel.com>
  9. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  10. * Aidan O'Mahony (aidan.o.mahony@intel.com)
  11. * Copyright (c) 2010, Intel Corporation.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. */
  19. #include <crypto/internal/hash.h>
  20. #include <crypto/internal/aead.h>
  21. #include <crypto/internal/skcipher.h>
  22. #include <crypto/cryptd.h>
  23. #include <crypto/crypto_wq.h>
  24. #include <linux/atomic.h>
  25. #include <linux/err.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/module.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. static unsigned int cryptd_max_cpu_qlen = 1000;
  34. module_param(cryptd_max_cpu_qlen, uint, 0);
  35. MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
  36. struct cryptd_cpu_queue {
  37. struct crypto_queue queue;
  38. struct work_struct work;
  39. };
  40. struct cryptd_queue {
  41. struct cryptd_cpu_queue __percpu *cpu_queue;
  42. };
  43. struct cryptd_instance_ctx {
  44. struct crypto_spawn spawn;
  45. struct cryptd_queue *queue;
  46. };
  47. struct skcipherd_instance_ctx {
  48. struct crypto_skcipher_spawn spawn;
  49. struct cryptd_queue *queue;
  50. };
  51. struct hashd_instance_ctx {
  52. struct crypto_shash_spawn spawn;
  53. struct cryptd_queue *queue;
  54. };
  55. struct aead_instance_ctx {
  56. struct crypto_aead_spawn aead_spawn;
  57. struct cryptd_queue *queue;
  58. };
  59. struct cryptd_blkcipher_ctx {
  60. atomic_t refcnt;
  61. struct crypto_blkcipher *child;
  62. };
  63. struct cryptd_blkcipher_request_ctx {
  64. crypto_completion_t complete;
  65. };
  66. struct cryptd_skcipher_ctx {
  67. atomic_t refcnt;
  68. struct crypto_skcipher *child;
  69. };
  70. struct cryptd_skcipher_request_ctx {
  71. crypto_completion_t complete;
  72. };
  73. struct cryptd_hash_ctx {
  74. atomic_t refcnt;
  75. struct crypto_shash *child;
  76. };
  77. struct cryptd_hash_request_ctx {
  78. crypto_completion_t complete;
  79. struct shash_desc desc;
  80. };
  81. struct cryptd_aead_ctx {
  82. atomic_t refcnt;
  83. struct crypto_aead *child;
  84. };
  85. struct cryptd_aead_request_ctx {
  86. crypto_completion_t complete;
  87. };
  88. static void cryptd_queue_worker(struct work_struct *work);
  89. static int cryptd_init_queue(struct cryptd_queue *queue,
  90. unsigned int max_cpu_qlen)
  91. {
  92. int cpu;
  93. struct cryptd_cpu_queue *cpu_queue;
  94. queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
  95. if (!queue->cpu_queue)
  96. return -ENOMEM;
  97. for_each_possible_cpu(cpu) {
  98. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  99. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  100. INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
  101. }
  102. pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
  103. return 0;
  104. }
  105. static void cryptd_fini_queue(struct cryptd_queue *queue)
  106. {
  107. int cpu;
  108. struct cryptd_cpu_queue *cpu_queue;
  109. for_each_possible_cpu(cpu) {
  110. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  111. BUG_ON(cpu_queue->queue.qlen);
  112. }
  113. free_percpu(queue->cpu_queue);
  114. }
  115. static int cryptd_enqueue_request(struct cryptd_queue *queue,
  116. struct crypto_async_request *request)
  117. {
  118. int cpu, err;
  119. struct cryptd_cpu_queue *cpu_queue;
  120. atomic_t *refcnt;
  121. cpu = get_cpu();
  122. cpu_queue = this_cpu_ptr(queue->cpu_queue);
  123. err = crypto_enqueue_request(&cpu_queue->queue, request);
  124. refcnt = crypto_tfm_ctx(request->tfm);
  125. if (err == -ENOSPC)
  126. goto out_put_cpu;
  127. queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
  128. if (!atomic_read(refcnt))
  129. goto out_put_cpu;
  130. atomic_inc(refcnt);
  131. out_put_cpu:
  132. put_cpu();
  133. return err;
  134. }
  135. /* Called in workqueue context, do one real cryption work (via
  136. * req->complete) and reschedule itself if there are more work to
  137. * do. */
  138. static void cryptd_queue_worker(struct work_struct *work)
  139. {
  140. struct cryptd_cpu_queue *cpu_queue;
  141. struct crypto_async_request *req, *backlog;
  142. cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
  143. /*
  144. * Only handle one request at a time to avoid hogging crypto workqueue.
  145. * preempt_disable/enable is used to prevent being preempted by
  146. * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
  147. * cryptd_enqueue_request() being accessed from software interrupts.
  148. */
  149. local_bh_disable();
  150. preempt_disable();
  151. backlog = crypto_get_backlog(&cpu_queue->queue);
  152. req = crypto_dequeue_request(&cpu_queue->queue);
  153. preempt_enable();
  154. local_bh_enable();
  155. if (!req)
  156. return;
  157. if (backlog)
  158. backlog->complete(backlog, -EINPROGRESS);
  159. req->complete(req, 0);
  160. if (cpu_queue->queue.qlen)
  161. queue_work(kcrypto_wq, &cpu_queue->work);
  162. }
  163. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  164. {
  165. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  166. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  167. return ictx->queue;
  168. }
  169. static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
  170. u32 *mask)
  171. {
  172. struct crypto_attr_type *algt;
  173. algt = crypto_get_attr_type(tb);
  174. if (IS_ERR(algt))
  175. return;
  176. *type |= algt->type & CRYPTO_ALG_INTERNAL;
  177. *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
  178. }
  179. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  180. const u8 *key, unsigned int keylen)
  181. {
  182. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  183. struct crypto_blkcipher *child = ctx->child;
  184. int err;
  185. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  186. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  187. CRYPTO_TFM_REQ_MASK);
  188. err = crypto_blkcipher_setkey(child, key, keylen);
  189. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  190. CRYPTO_TFM_RES_MASK);
  191. return err;
  192. }
  193. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  194. struct crypto_blkcipher *child,
  195. int err,
  196. int (*crypt)(struct blkcipher_desc *desc,
  197. struct scatterlist *dst,
  198. struct scatterlist *src,
  199. unsigned int len))
  200. {
  201. struct cryptd_blkcipher_request_ctx *rctx;
  202. struct cryptd_blkcipher_ctx *ctx;
  203. struct crypto_ablkcipher *tfm;
  204. struct blkcipher_desc desc;
  205. int refcnt;
  206. rctx = ablkcipher_request_ctx(req);
  207. if (unlikely(err == -EINPROGRESS))
  208. goto out;
  209. desc.tfm = child;
  210. desc.info = req->info;
  211. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  212. err = crypt(&desc, req->dst, req->src, req->nbytes);
  213. req->base.complete = rctx->complete;
  214. out:
  215. tfm = crypto_ablkcipher_reqtfm(req);
  216. ctx = crypto_ablkcipher_ctx(tfm);
  217. refcnt = atomic_read(&ctx->refcnt);
  218. local_bh_disable();
  219. rctx->complete(&req->base, err);
  220. local_bh_enable();
  221. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  222. crypto_free_ablkcipher(tfm);
  223. }
  224. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  225. {
  226. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  227. struct crypto_blkcipher *child = ctx->child;
  228. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  229. crypto_blkcipher_crt(child)->encrypt);
  230. }
  231. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  232. {
  233. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  234. struct crypto_blkcipher *child = ctx->child;
  235. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  236. crypto_blkcipher_crt(child)->decrypt);
  237. }
  238. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  239. crypto_completion_t compl)
  240. {
  241. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  242. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  243. struct cryptd_queue *queue;
  244. queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
  245. rctx->complete = req->base.complete;
  246. req->base.complete = compl;
  247. return cryptd_enqueue_request(queue, &req->base);
  248. }
  249. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  250. {
  251. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  252. }
  253. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  254. {
  255. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  256. }
  257. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  258. {
  259. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  260. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  261. struct crypto_spawn *spawn = &ictx->spawn;
  262. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  263. struct crypto_blkcipher *cipher;
  264. cipher = crypto_spawn_blkcipher(spawn);
  265. if (IS_ERR(cipher))
  266. return PTR_ERR(cipher);
  267. ctx->child = cipher;
  268. tfm->crt_ablkcipher.reqsize =
  269. sizeof(struct cryptd_blkcipher_request_ctx);
  270. return 0;
  271. }
  272. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  273. {
  274. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  275. crypto_free_blkcipher(ctx->child);
  276. }
  277. static int cryptd_init_instance(struct crypto_instance *inst,
  278. struct crypto_alg *alg)
  279. {
  280. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  281. "cryptd(%s)",
  282. alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  283. return -ENAMETOOLONG;
  284. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  285. inst->alg.cra_priority = alg->cra_priority + 50;
  286. inst->alg.cra_blocksize = alg->cra_blocksize;
  287. inst->alg.cra_alignmask = alg->cra_alignmask;
  288. return 0;
  289. }
  290. static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  291. unsigned int tail)
  292. {
  293. char *p;
  294. struct crypto_instance *inst;
  295. int err;
  296. p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
  297. if (!p)
  298. return ERR_PTR(-ENOMEM);
  299. inst = (void *)(p + head);
  300. err = cryptd_init_instance(inst, alg);
  301. if (err)
  302. goto out_free_inst;
  303. out:
  304. return p;
  305. out_free_inst:
  306. kfree(p);
  307. p = ERR_PTR(err);
  308. goto out;
  309. }
  310. static int cryptd_create_blkcipher(struct crypto_template *tmpl,
  311. struct rtattr **tb,
  312. struct cryptd_queue *queue)
  313. {
  314. struct cryptd_instance_ctx *ctx;
  315. struct crypto_instance *inst;
  316. struct crypto_alg *alg;
  317. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  318. u32 mask = CRYPTO_ALG_TYPE_MASK;
  319. int err;
  320. cryptd_check_internal(tb, &type, &mask);
  321. alg = crypto_get_attr_alg(tb, type, mask);
  322. if (IS_ERR(alg))
  323. return PTR_ERR(alg);
  324. inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
  325. err = PTR_ERR(inst);
  326. if (IS_ERR(inst))
  327. goto out_put_alg;
  328. ctx = crypto_instance_ctx(inst);
  329. ctx->queue = queue;
  330. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  331. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  332. if (err)
  333. goto out_free_inst;
  334. type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  335. if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
  336. type |= CRYPTO_ALG_INTERNAL;
  337. inst->alg.cra_flags = type;
  338. inst->alg.cra_type = &crypto_ablkcipher_type;
  339. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  340. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  341. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  342. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  343. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  344. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  345. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  346. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  347. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  348. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  349. err = crypto_register_instance(tmpl, inst);
  350. if (err) {
  351. crypto_drop_spawn(&ctx->spawn);
  352. out_free_inst:
  353. kfree(inst);
  354. }
  355. out_put_alg:
  356. crypto_mod_put(alg);
  357. return err;
  358. }
  359. static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
  360. const u8 *key, unsigned int keylen)
  361. {
  362. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
  363. struct crypto_skcipher *child = ctx->child;
  364. int err;
  365. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  366. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  367. CRYPTO_TFM_REQ_MASK);
  368. err = crypto_skcipher_setkey(child, key, keylen);
  369. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  370. CRYPTO_TFM_RES_MASK);
  371. return err;
  372. }
  373. static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
  374. {
  375. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  376. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  377. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  378. int refcnt = atomic_read(&ctx->refcnt);
  379. local_bh_disable();
  380. rctx->complete(&req->base, err);
  381. local_bh_enable();
  382. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  383. crypto_free_skcipher(tfm);
  384. }
  385. static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
  386. int err)
  387. {
  388. struct skcipher_request *req = skcipher_request_cast(base);
  389. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  390. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  391. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  392. struct crypto_skcipher *child = ctx->child;
  393. SKCIPHER_REQUEST_ON_STACK(subreq, child);
  394. if (unlikely(err == -EINPROGRESS))
  395. goto out;
  396. skcipher_request_set_tfm(subreq, child);
  397. skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  398. NULL, NULL);
  399. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  400. req->iv);
  401. err = crypto_skcipher_encrypt(subreq);
  402. skcipher_request_zero(subreq);
  403. req->base.complete = rctx->complete;
  404. out:
  405. cryptd_skcipher_complete(req, err);
  406. }
  407. static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
  408. int err)
  409. {
  410. struct skcipher_request *req = skcipher_request_cast(base);
  411. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  412. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  413. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  414. struct crypto_skcipher *child = ctx->child;
  415. SKCIPHER_REQUEST_ON_STACK(subreq, child);
  416. if (unlikely(err == -EINPROGRESS))
  417. goto out;
  418. skcipher_request_set_tfm(subreq, child);
  419. skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  420. NULL, NULL);
  421. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  422. req->iv);
  423. err = crypto_skcipher_decrypt(subreq);
  424. skcipher_request_zero(subreq);
  425. req->base.complete = rctx->complete;
  426. out:
  427. cryptd_skcipher_complete(req, err);
  428. }
  429. static int cryptd_skcipher_enqueue(struct skcipher_request *req,
  430. crypto_completion_t compl)
  431. {
  432. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  433. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  434. struct cryptd_queue *queue;
  435. queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
  436. rctx->complete = req->base.complete;
  437. req->base.complete = compl;
  438. return cryptd_enqueue_request(queue, &req->base);
  439. }
  440. static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
  441. {
  442. return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
  443. }
  444. static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
  445. {
  446. return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
  447. }
  448. static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
  449. {
  450. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  451. struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
  452. struct crypto_skcipher_spawn *spawn = &ictx->spawn;
  453. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  454. struct crypto_skcipher *cipher;
  455. cipher = crypto_spawn_skcipher(spawn);
  456. if (IS_ERR(cipher))
  457. return PTR_ERR(cipher);
  458. ctx->child = cipher;
  459. crypto_skcipher_set_reqsize(
  460. tfm, sizeof(struct cryptd_skcipher_request_ctx));
  461. return 0;
  462. }
  463. static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
  464. {
  465. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  466. crypto_free_skcipher(ctx->child);
  467. }
  468. static void cryptd_skcipher_free(struct skcipher_instance *inst)
  469. {
  470. struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
  471. crypto_drop_skcipher(&ctx->spawn);
  472. kfree(inst);
  473. }
  474. static int cryptd_create_skcipher(struct crypto_template *tmpl,
  475. struct rtattr **tb,
  476. struct cryptd_queue *queue)
  477. {
  478. struct skcipherd_instance_ctx *ctx;
  479. struct skcipher_instance *inst;
  480. struct skcipher_alg *alg;
  481. const char *name;
  482. u32 type;
  483. u32 mask;
  484. int err;
  485. type = 0;
  486. mask = CRYPTO_ALG_ASYNC;
  487. cryptd_check_internal(tb, &type, &mask);
  488. name = crypto_attr_alg_name(tb[1]);
  489. if (IS_ERR(name))
  490. return PTR_ERR(name);
  491. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  492. if (!inst)
  493. return -ENOMEM;
  494. ctx = skcipher_instance_ctx(inst);
  495. ctx->queue = queue;
  496. crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
  497. err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
  498. if (err)
  499. goto out_free_inst;
  500. alg = crypto_spawn_skcipher_alg(&ctx->spawn);
  501. err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
  502. if (err)
  503. goto out_drop_skcipher;
  504. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
  505. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  506. inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
  507. inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
  508. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
  509. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
  510. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
  511. inst->alg.init = cryptd_skcipher_init_tfm;
  512. inst->alg.exit = cryptd_skcipher_exit_tfm;
  513. inst->alg.setkey = cryptd_skcipher_setkey;
  514. inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
  515. inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
  516. inst->free = cryptd_skcipher_free;
  517. err = skcipher_register_instance(tmpl, inst);
  518. if (err) {
  519. out_drop_skcipher:
  520. crypto_drop_skcipher(&ctx->spawn);
  521. out_free_inst:
  522. kfree(inst);
  523. }
  524. return err;
  525. }
  526. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  527. {
  528. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  529. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  530. struct crypto_shash_spawn *spawn = &ictx->spawn;
  531. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  532. struct crypto_shash *hash;
  533. hash = crypto_spawn_shash(spawn);
  534. if (IS_ERR(hash))
  535. return PTR_ERR(hash);
  536. ctx->child = hash;
  537. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  538. sizeof(struct cryptd_hash_request_ctx) +
  539. crypto_shash_descsize(hash));
  540. return 0;
  541. }
  542. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  543. {
  544. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  545. crypto_free_shash(ctx->child);
  546. }
  547. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  548. const u8 *key, unsigned int keylen)
  549. {
  550. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  551. struct crypto_shash *child = ctx->child;
  552. int err;
  553. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  554. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  555. CRYPTO_TFM_REQ_MASK);
  556. err = crypto_shash_setkey(child, key, keylen);
  557. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  558. CRYPTO_TFM_RES_MASK);
  559. return err;
  560. }
  561. static int cryptd_hash_enqueue(struct ahash_request *req,
  562. crypto_completion_t compl)
  563. {
  564. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  565. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  566. struct cryptd_queue *queue =
  567. cryptd_get_queue(crypto_ahash_tfm(tfm));
  568. rctx->complete = req->base.complete;
  569. req->base.complete = compl;
  570. return cryptd_enqueue_request(queue, &req->base);
  571. }
  572. static void cryptd_hash_complete(struct ahash_request *req, int err)
  573. {
  574. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  575. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  576. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  577. int refcnt = atomic_read(&ctx->refcnt);
  578. local_bh_disable();
  579. rctx->complete(&req->base, err);
  580. local_bh_enable();
  581. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  582. crypto_free_ahash(tfm);
  583. }
  584. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  585. {
  586. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  587. struct crypto_shash *child = ctx->child;
  588. struct ahash_request *req = ahash_request_cast(req_async);
  589. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  590. struct shash_desc *desc = &rctx->desc;
  591. if (unlikely(err == -EINPROGRESS))
  592. goto out;
  593. desc->tfm = child;
  594. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  595. err = crypto_shash_init(desc);
  596. req->base.complete = rctx->complete;
  597. out:
  598. cryptd_hash_complete(req, err);
  599. }
  600. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  601. {
  602. return cryptd_hash_enqueue(req, cryptd_hash_init);
  603. }
  604. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  605. {
  606. struct ahash_request *req = ahash_request_cast(req_async);
  607. struct cryptd_hash_request_ctx *rctx;
  608. rctx = ahash_request_ctx(req);
  609. if (unlikely(err == -EINPROGRESS))
  610. goto out;
  611. err = shash_ahash_update(req, &rctx->desc);
  612. req->base.complete = rctx->complete;
  613. out:
  614. cryptd_hash_complete(req, err);
  615. }
  616. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  617. {
  618. return cryptd_hash_enqueue(req, cryptd_hash_update);
  619. }
  620. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  621. {
  622. struct ahash_request *req = ahash_request_cast(req_async);
  623. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  624. if (unlikely(err == -EINPROGRESS))
  625. goto out;
  626. err = crypto_shash_final(&rctx->desc, req->result);
  627. req->base.complete = rctx->complete;
  628. out:
  629. cryptd_hash_complete(req, err);
  630. }
  631. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  632. {
  633. return cryptd_hash_enqueue(req, cryptd_hash_final);
  634. }
  635. static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
  636. {
  637. struct ahash_request *req = ahash_request_cast(req_async);
  638. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  639. if (unlikely(err == -EINPROGRESS))
  640. goto out;
  641. err = shash_ahash_finup(req, &rctx->desc);
  642. req->base.complete = rctx->complete;
  643. out:
  644. cryptd_hash_complete(req, err);
  645. }
  646. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  647. {
  648. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  649. }
  650. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  651. {
  652. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  653. struct crypto_shash *child = ctx->child;
  654. struct ahash_request *req = ahash_request_cast(req_async);
  655. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  656. struct shash_desc *desc = &rctx->desc;
  657. if (unlikely(err == -EINPROGRESS))
  658. goto out;
  659. desc->tfm = child;
  660. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  661. err = shash_ahash_digest(req, desc);
  662. req->base.complete = rctx->complete;
  663. out:
  664. cryptd_hash_complete(req, err);
  665. }
  666. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  667. {
  668. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  669. }
  670. static int cryptd_hash_export(struct ahash_request *req, void *out)
  671. {
  672. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  673. return crypto_shash_export(&rctx->desc, out);
  674. }
  675. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  676. {
  677. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  678. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  679. struct shash_desc *desc = cryptd_shash_desc(req);
  680. desc->tfm = ctx->child;
  681. desc->flags = req->base.flags;
  682. return crypto_shash_import(desc, in);
  683. }
  684. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  685. struct cryptd_queue *queue)
  686. {
  687. struct hashd_instance_ctx *ctx;
  688. struct ahash_instance *inst;
  689. struct shash_alg *salg;
  690. struct crypto_alg *alg;
  691. u32 type = 0;
  692. u32 mask = 0;
  693. int err;
  694. cryptd_check_internal(tb, &type, &mask);
  695. salg = shash_attr_alg(tb[1], type, mask);
  696. if (IS_ERR(salg))
  697. return PTR_ERR(salg);
  698. alg = &salg->base;
  699. inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
  700. sizeof(*ctx));
  701. err = PTR_ERR(inst);
  702. if (IS_ERR(inst))
  703. goto out_put_alg;
  704. ctx = ahash_instance_ctx(inst);
  705. ctx->queue = queue;
  706. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  707. ahash_crypto_instance(inst));
  708. if (err)
  709. goto out_free_inst;
  710. inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
  711. (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
  712. CRYPTO_ALG_OPTIONAL_KEY));
  713. inst->alg.halg.digestsize = salg->digestsize;
  714. inst->alg.halg.statesize = salg->statesize;
  715. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  716. inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
  717. inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
  718. inst->alg.init = cryptd_hash_init_enqueue;
  719. inst->alg.update = cryptd_hash_update_enqueue;
  720. inst->alg.final = cryptd_hash_final_enqueue;
  721. inst->alg.finup = cryptd_hash_finup_enqueue;
  722. inst->alg.export = cryptd_hash_export;
  723. inst->alg.import = cryptd_hash_import;
  724. if (crypto_shash_alg_has_setkey(salg))
  725. inst->alg.setkey = cryptd_hash_setkey;
  726. inst->alg.digest = cryptd_hash_digest_enqueue;
  727. err = ahash_register_instance(tmpl, inst);
  728. if (err) {
  729. crypto_drop_shash(&ctx->spawn);
  730. out_free_inst:
  731. kfree(inst);
  732. }
  733. out_put_alg:
  734. crypto_mod_put(alg);
  735. return err;
  736. }
  737. static int cryptd_aead_setkey(struct crypto_aead *parent,
  738. const u8 *key, unsigned int keylen)
  739. {
  740. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  741. struct crypto_aead *child = ctx->child;
  742. return crypto_aead_setkey(child, key, keylen);
  743. }
  744. static int cryptd_aead_setauthsize(struct crypto_aead *parent,
  745. unsigned int authsize)
  746. {
  747. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  748. struct crypto_aead *child = ctx->child;
  749. return crypto_aead_setauthsize(child, authsize);
  750. }
  751. static void cryptd_aead_crypt(struct aead_request *req,
  752. struct crypto_aead *child,
  753. int err,
  754. int (*crypt)(struct aead_request *req))
  755. {
  756. struct cryptd_aead_request_ctx *rctx;
  757. struct cryptd_aead_ctx *ctx;
  758. crypto_completion_t compl;
  759. struct crypto_aead *tfm;
  760. int refcnt;
  761. rctx = aead_request_ctx(req);
  762. compl = rctx->complete;
  763. tfm = crypto_aead_reqtfm(req);
  764. if (unlikely(err == -EINPROGRESS))
  765. goto out;
  766. aead_request_set_tfm(req, child);
  767. err = crypt( req );
  768. out:
  769. ctx = crypto_aead_ctx(tfm);
  770. refcnt = atomic_read(&ctx->refcnt);
  771. local_bh_disable();
  772. compl(&req->base, err);
  773. local_bh_enable();
  774. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  775. crypto_free_aead(tfm);
  776. }
  777. static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
  778. {
  779. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  780. struct crypto_aead *child = ctx->child;
  781. struct aead_request *req;
  782. req = container_of(areq, struct aead_request, base);
  783. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
  784. }
  785. static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
  786. {
  787. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  788. struct crypto_aead *child = ctx->child;
  789. struct aead_request *req;
  790. req = container_of(areq, struct aead_request, base);
  791. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
  792. }
  793. static int cryptd_aead_enqueue(struct aead_request *req,
  794. crypto_completion_t compl)
  795. {
  796. struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
  797. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  798. struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
  799. rctx->complete = req->base.complete;
  800. req->base.complete = compl;
  801. return cryptd_enqueue_request(queue, &req->base);
  802. }
  803. static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
  804. {
  805. return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
  806. }
  807. static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
  808. {
  809. return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
  810. }
  811. static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
  812. {
  813. struct aead_instance *inst = aead_alg_instance(tfm);
  814. struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
  815. struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
  816. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  817. struct crypto_aead *cipher;
  818. cipher = crypto_spawn_aead(spawn);
  819. if (IS_ERR(cipher))
  820. return PTR_ERR(cipher);
  821. ctx->child = cipher;
  822. crypto_aead_set_reqsize(
  823. tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
  824. crypto_aead_reqsize(cipher)));
  825. return 0;
  826. }
  827. static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
  828. {
  829. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  830. crypto_free_aead(ctx->child);
  831. }
  832. static int cryptd_create_aead(struct crypto_template *tmpl,
  833. struct rtattr **tb,
  834. struct cryptd_queue *queue)
  835. {
  836. struct aead_instance_ctx *ctx;
  837. struct aead_instance *inst;
  838. struct aead_alg *alg;
  839. const char *name;
  840. u32 type = 0;
  841. u32 mask = CRYPTO_ALG_ASYNC;
  842. int err;
  843. cryptd_check_internal(tb, &type, &mask);
  844. name = crypto_attr_alg_name(tb[1]);
  845. if (IS_ERR(name))
  846. return PTR_ERR(name);
  847. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  848. if (!inst)
  849. return -ENOMEM;
  850. ctx = aead_instance_ctx(inst);
  851. ctx->queue = queue;
  852. crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
  853. err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
  854. if (err)
  855. goto out_free_inst;
  856. alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
  857. err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
  858. if (err)
  859. goto out_drop_aead;
  860. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
  861. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  862. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
  863. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  864. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  865. inst->alg.init = cryptd_aead_init_tfm;
  866. inst->alg.exit = cryptd_aead_exit_tfm;
  867. inst->alg.setkey = cryptd_aead_setkey;
  868. inst->alg.setauthsize = cryptd_aead_setauthsize;
  869. inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
  870. inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
  871. err = aead_register_instance(tmpl, inst);
  872. if (err) {
  873. out_drop_aead:
  874. crypto_drop_aead(&ctx->aead_spawn);
  875. out_free_inst:
  876. kfree(inst);
  877. }
  878. return err;
  879. }
  880. static struct cryptd_queue queue;
  881. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  882. {
  883. struct crypto_attr_type *algt;
  884. algt = crypto_get_attr_type(tb);
  885. if (IS_ERR(algt))
  886. return PTR_ERR(algt);
  887. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  888. case CRYPTO_ALG_TYPE_BLKCIPHER:
  889. if ((algt->type & CRYPTO_ALG_TYPE_MASK) ==
  890. CRYPTO_ALG_TYPE_BLKCIPHER)
  891. return cryptd_create_blkcipher(tmpl, tb, &queue);
  892. return cryptd_create_skcipher(tmpl, tb, &queue);
  893. case CRYPTO_ALG_TYPE_DIGEST:
  894. return cryptd_create_hash(tmpl, tb, &queue);
  895. case CRYPTO_ALG_TYPE_AEAD:
  896. return cryptd_create_aead(tmpl, tb, &queue);
  897. }
  898. return -EINVAL;
  899. }
  900. static void cryptd_free(struct crypto_instance *inst)
  901. {
  902. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  903. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  904. struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
  905. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  906. case CRYPTO_ALG_TYPE_AHASH:
  907. crypto_drop_shash(&hctx->spawn);
  908. kfree(ahash_instance(inst));
  909. return;
  910. case CRYPTO_ALG_TYPE_AEAD:
  911. crypto_drop_aead(&aead_ctx->aead_spawn);
  912. kfree(aead_instance(inst));
  913. return;
  914. default:
  915. crypto_drop_spawn(&ctx->spawn);
  916. kfree(inst);
  917. }
  918. }
  919. static struct crypto_template cryptd_tmpl = {
  920. .name = "cryptd",
  921. .create = cryptd_create,
  922. .free = cryptd_free,
  923. .module = THIS_MODULE,
  924. };
  925. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  926. u32 type, u32 mask)
  927. {
  928. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  929. struct cryptd_blkcipher_ctx *ctx;
  930. struct crypto_tfm *tfm;
  931. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  932. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  933. return ERR_PTR(-EINVAL);
  934. type = crypto_skcipher_type(type);
  935. mask &= ~CRYPTO_ALG_TYPE_MASK;
  936. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  937. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  938. if (IS_ERR(tfm))
  939. return ERR_CAST(tfm);
  940. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  941. crypto_free_tfm(tfm);
  942. return ERR_PTR(-EINVAL);
  943. }
  944. ctx = crypto_tfm_ctx(tfm);
  945. atomic_set(&ctx->refcnt, 1);
  946. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  947. }
  948. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  949. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  950. {
  951. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  952. return ctx->child;
  953. }
  954. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  955. bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm)
  956. {
  957. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  958. return atomic_read(&ctx->refcnt) - 1;
  959. }
  960. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued);
  961. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  962. {
  963. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  964. if (atomic_dec_and_test(&ctx->refcnt))
  965. crypto_free_ablkcipher(&tfm->base);
  966. }
  967. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  968. struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
  969. u32 type, u32 mask)
  970. {
  971. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  972. struct cryptd_skcipher_ctx *ctx;
  973. struct crypto_skcipher *tfm;
  974. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  975. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  976. return ERR_PTR(-EINVAL);
  977. tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
  978. if (IS_ERR(tfm))
  979. return ERR_CAST(tfm);
  980. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  981. crypto_free_skcipher(tfm);
  982. return ERR_PTR(-EINVAL);
  983. }
  984. ctx = crypto_skcipher_ctx(tfm);
  985. atomic_set(&ctx->refcnt, 1);
  986. return container_of(tfm, struct cryptd_skcipher, base);
  987. }
  988. EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
  989. struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
  990. {
  991. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  992. return ctx->child;
  993. }
  994. EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
  995. bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
  996. {
  997. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  998. return atomic_read(&ctx->refcnt) - 1;
  999. }
  1000. EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
  1001. void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
  1002. {
  1003. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  1004. if (atomic_dec_and_test(&ctx->refcnt))
  1005. crypto_free_skcipher(&tfm->base);
  1006. }
  1007. EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
  1008. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  1009. u32 type, u32 mask)
  1010. {
  1011. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  1012. struct cryptd_hash_ctx *ctx;
  1013. struct crypto_ahash *tfm;
  1014. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  1015. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  1016. return ERR_PTR(-EINVAL);
  1017. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  1018. if (IS_ERR(tfm))
  1019. return ERR_CAST(tfm);
  1020. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  1021. crypto_free_ahash(tfm);
  1022. return ERR_PTR(-EINVAL);
  1023. }
  1024. ctx = crypto_ahash_ctx(tfm);
  1025. atomic_set(&ctx->refcnt, 1);
  1026. return __cryptd_ahash_cast(tfm);
  1027. }
  1028. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  1029. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  1030. {
  1031. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1032. return ctx->child;
  1033. }
  1034. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  1035. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  1036. {
  1037. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  1038. return &rctx->desc;
  1039. }
  1040. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  1041. bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
  1042. {
  1043. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1044. return atomic_read(&ctx->refcnt) - 1;
  1045. }
  1046. EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
  1047. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  1048. {
  1049. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1050. if (atomic_dec_and_test(&ctx->refcnt))
  1051. crypto_free_ahash(&tfm->base);
  1052. }
  1053. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  1054. struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
  1055. u32 type, u32 mask)
  1056. {
  1057. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  1058. struct cryptd_aead_ctx *ctx;
  1059. struct crypto_aead *tfm;
  1060. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  1061. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  1062. return ERR_PTR(-EINVAL);
  1063. tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
  1064. if (IS_ERR(tfm))
  1065. return ERR_CAST(tfm);
  1066. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  1067. crypto_free_aead(tfm);
  1068. return ERR_PTR(-EINVAL);
  1069. }
  1070. ctx = crypto_aead_ctx(tfm);
  1071. atomic_set(&ctx->refcnt, 1);
  1072. return __cryptd_aead_cast(tfm);
  1073. }
  1074. EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
  1075. struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
  1076. {
  1077. struct cryptd_aead_ctx *ctx;
  1078. ctx = crypto_aead_ctx(&tfm->base);
  1079. return ctx->child;
  1080. }
  1081. EXPORT_SYMBOL_GPL(cryptd_aead_child);
  1082. bool cryptd_aead_queued(struct cryptd_aead *tfm)
  1083. {
  1084. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  1085. return atomic_read(&ctx->refcnt) - 1;
  1086. }
  1087. EXPORT_SYMBOL_GPL(cryptd_aead_queued);
  1088. void cryptd_free_aead(struct cryptd_aead *tfm)
  1089. {
  1090. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  1091. if (atomic_dec_and_test(&ctx->refcnt))
  1092. crypto_free_aead(&tfm->base);
  1093. }
  1094. EXPORT_SYMBOL_GPL(cryptd_free_aead);
  1095. static int __init cryptd_init(void)
  1096. {
  1097. int err;
  1098. err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
  1099. if (err)
  1100. return err;
  1101. err = crypto_register_template(&cryptd_tmpl);
  1102. if (err)
  1103. cryptd_fini_queue(&queue);
  1104. return err;
  1105. }
  1106. static void __exit cryptd_exit(void)
  1107. {
  1108. cryptd_fini_queue(&queue);
  1109. crypto_unregister_template(&cryptd_tmpl);
  1110. }
  1111. subsys_initcall(cryptd_init);
  1112. module_exit(cryptd_exit);
  1113. MODULE_LICENSE("GPL");
  1114. MODULE_DESCRIPTION("Software async crypto daemon");
  1115. MODULE_ALIAS_CRYPTO("cryptd");