cryptd.c 37 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_sync_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_sync_skcipher *child = ctx->child;
  364. int err;
  365. crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  366. crypto_sync_skcipher_set_flags(child,
  367. crypto_skcipher_get_flags(parent) &
  368. CRYPTO_TFM_REQ_MASK);
  369. err = crypto_sync_skcipher_setkey(child, key, keylen);
  370. crypto_skcipher_set_flags(parent,
  371. crypto_sync_skcipher_get_flags(child) &
  372. CRYPTO_TFM_RES_MASK);
  373. return err;
  374. }
  375. static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
  376. {
  377. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  378. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  379. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  380. int refcnt = atomic_read(&ctx->refcnt);
  381. local_bh_disable();
  382. rctx->complete(&req->base, err);
  383. local_bh_enable();
  384. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  385. crypto_free_skcipher(tfm);
  386. }
  387. static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
  388. int err)
  389. {
  390. struct skcipher_request *req = skcipher_request_cast(base);
  391. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  392. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  393. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  394. struct crypto_sync_skcipher *child = ctx->child;
  395. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
  396. if (unlikely(err == -EINPROGRESS))
  397. goto out;
  398. skcipher_request_set_sync_tfm(subreq, child);
  399. skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  400. NULL, NULL);
  401. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  402. req->iv);
  403. err = crypto_skcipher_encrypt(subreq);
  404. skcipher_request_zero(subreq);
  405. req->base.complete = rctx->complete;
  406. out:
  407. cryptd_skcipher_complete(req, err);
  408. }
  409. static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
  410. int err)
  411. {
  412. struct skcipher_request *req = skcipher_request_cast(base);
  413. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  414. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  415. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  416. struct crypto_sync_skcipher *child = ctx->child;
  417. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
  418. if (unlikely(err == -EINPROGRESS))
  419. goto out;
  420. skcipher_request_set_sync_tfm(subreq, child);
  421. skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  422. NULL, NULL);
  423. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  424. req->iv);
  425. err = crypto_skcipher_decrypt(subreq);
  426. skcipher_request_zero(subreq);
  427. req->base.complete = rctx->complete;
  428. out:
  429. cryptd_skcipher_complete(req, err);
  430. }
  431. static int cryptd_skcipher_enqueue(struct skcipher_request *req,
  432. crypto_completion_t compl)
  433. {
  434. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  435. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  436. struct cryptd_queue *queue;
  437. queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
  438. rctx->complete = req->base.complete;
  439. req->base.complete = compl;
  440. return cryptd_enqueue_request(queue, &req->base);
  441. }
  442. static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
  443. {
  444. return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
  445. }
  446. static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
  447. {
  448. return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
  449. }
  450. static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
  451. {
  452. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  453. struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
  454. struct crypto_skcipher_spawn *spawn = &ictx->spawn;
  455. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  456. struct crypto_skcipher *cipher;
  457. cipher = crypto_spawn_skcipher(spawn);
  458. if (IS_ERR(cipher))
  459. return PTR_ERR(cipher);
  460. ctx->child = (struct crypto_sync_skcipher *)cipher;
  461. crypto_skcipher_set_reqsize(
  462. tfm, sizeof(struct cryptd_skcipher_request_ctx));
  463. return 0;
  464. }
  465. static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
  466. {
  467. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  468. crypto_free_sync_skcipher(ctx->child);
  469. }
  470. static void cryptd_skcipher_free(struct skcipher_instance *inst)
  471. {
  472. struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
  473. crypto_drop_skcipher(&ctx->spawn);
  474. }
  475. static int cryptd_create_skcipher(struct crypto_template *tmpl,
  476. struct rtattr **tb,
  477. struct cryptd_queue *queue)
  478. {
  479. struct skcipherd_instance_ctx *ctx;
  480. struct skcipher_instance *inst;
  481. struct skcipher_alg *alg;
  482. const char *name;
  483. u32 type;
  484. u32 mask;
  485. int err;
  486. type = 0;
  487. mask = CRYPTO_ALG_ASYNC;
  488. cryptd_check_internal(tb, &type, &mask);
  489. name = crypto_attr_alg_name(tb[1]);
  490. if (IS_ERR(name))
  491. return PTR_ERR(name);
  492. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  493. if (!inst)
  494. return -ENOMEM;
  495. ctx = skcipher_instance_ctx(inst);
  496. ctx->queue = queue;
  497. crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
  498. err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
  499. if (err)
  500. goto out_free_inst;
  501. alg = crypto_spawn_skcipher_alg(&ctx->spawn);
  502. err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
  503. if (err)
  504. goto out_drop_skcipher;
  505. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
  506. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  507. inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
  508. inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
  509. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
  510. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
  511. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
  512. inst->alg.init = cryptd_skcipher_init_tfm;
  513. inst->alg.exit = cryptd_skcipher_exit_tfm;
  514. inst->alg.setkey = cryptd_skcipher_setkey;
  515. inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
  516. inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
  517. inst->free = cryptd_skcipher_free;
  518. err = skcipher_register_instance(tmpl, inst);
  519. if (err) {
  520. out_drop_skcipher:
  521. crypto_drop_skcipher(&ctx->spawn);
  522. out_free_inst:
  523. kfree(inst);
  524. }
  525. return err;
  526. }
  527. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  528. {
  529. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  530. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  531. struct crypto_shash_spawn *spawn = &ictx->spawn;
  532. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  533. struct crypto_shash *hash;
  534. hash = crypto_spawn_shash(spawn);
  535. if (IS_ERR(hash))
  536. return PTR_ERR(hash);
  537. ctx->child = hash;
  538. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  539. sizeof(struct cryptd_hash_request_ctx) +
  540. crypto_shash_descsize(hash));
  541. return 0;
  542. }
  543. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  544. {
  545. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  546. crypto_free_shash(ctx->child);
  547. }
  548. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  549. const u8 *key, unsigned int keylen)
  550. {
  551. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  552. struct crypto_shash *child = ctx->child;
  553. int err;
  554. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  555. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  556. CRYPTO_TFM_REQ_MASK);
  557. err = crypto_shash_setkey(child, key, keylen);
  558. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  559. CRYPTO_TFM_RES_MASK);
  560. return err;
  561. }
  562. static int cryptd_hash_enqueue(struct ahash_request *req,
  563. crypto_completion_t compl)
  564. {
  565. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  566. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  567. struct cryptd_queue *queue =
  568. cryptd_get_queue(crypto_ahash_tfm(tfm));
  569. rctx->complete = req->base.complete;
  570. req->base.complete = compl;
  571. return cryptd_enqueue_request(queue, &req->base);
  572. }
  573. static void cryptd_hash_complete(struct ahash_request *req, int err)
  574. {
  575. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  576. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  577. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  578. int refcnt = atomic_read(&ctx->refcnt);
  579. local_bh_disable();
  580. rctx->complete(&req->base, err);
  581. local_bh_enable();
  582. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  583. crypto_free_ahash(tfm);
  584. }
  585. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  586. {
  587. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  588. struct crypto_shash *child = ctx->child;
  589. struct ahash_request *req = ahash_request_cast(req_async);
  590. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  591. struct shash_desc *desc = &rctx->desc;
  592. if (unlikely(err == -EINPROGRESS))
  593. goto out;
  594. desc->tfm = child;
  595. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  596. err = crypto_shash_init(desc);
  597. req->base.complete = rctx->complete;
  598. out:
  599. cryptd_hash_complete(req, err);
  600. }
  601. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  602. {
  603. return cryptd_hash_enqueue(req, cryptd_hash_init);
  604. }
  605. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  606. {
  607. struct ahash_request *req = ahash_request_cast(req_async);
  608. struct cryptd_hash_request_ctx *rctx;
  609. rctx = ahash_request_ctx(req);
  610. if (unlikely(err == -EINPROGRESS))
  611. goto out;
  612. err = shash_ahash_update(req, &rctx->desc);
  613. req->base.complete = rctx->complete;
  614. out:
  615. cryptd_hash_complete(req, err);
  616. }
  617. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  618. {
  619. return cryptd_hash_enqueue(req, cryptd_hash_update);
  620. }
  621. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  622. {
  623. struct ahash_request *req = ahash_request_cast(req_async);
  624. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  625. if (unlikely(err == -EINPROGRESS))
  626. goto out;
  627. err = crypto_shash_final(&rctx->desc, req->result);
  628. req->base.complete = rctx->complete;
  629. out:
  630. cryptd_hash_complete(req, err);
  631. }
  632. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  633. {
  634. return cryptd_hash_enqueue(req, cryptd_hash_final);
  635. }
  636. static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
  637. {
  638. struct ahash_request *req = ahash_request_cast(req_async);
  639. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  640. if (unlikely(err == -EINPROGRESS))
  641. goto out;
  642. err = shash_ahash_finup(req, &rctx->desc);
  643. req->base.complete = rctx->complete;
  644. out:
  645. cryptd_hash_complete(req, err);
  646. }
  647. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  648. {
  649. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  650. }
  651. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  652. {
  653. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  654. struct crypto_shash *child = ctx->child;
  655. struct ahash_request *req = ahash_request_cast(req_async);
  656. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  657. struct shash_desc *desc = &rctx->desc;
  658. if (unlikely(err == -EINPROGRESS))
  659. goto out;
  660. desc->tfm = child;
  661. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  662. err = shash_ahash_digest(req, desc);
  663. req->base.complete = rctx->complete;
  664. out:
  665. cryptd_hash_complete(req, err);
  666. }
  667. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  668. {
  669. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  670. }
  671. static int cryptd_hash_export(struct ahash_request *req, void *out)
  672. {
  673. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  674. return crypto_shash_export(&rctx->desc, out);
  675. }
  676. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  677. {
  678. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  679. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  680. struct shash_desc *desc = cryptd_shash_desc(req);
  681. desc->tfm = ctx->child;
  682. desc->flags = req->base.flags;
  683. return crypto_shash_import(desc, in);
  684. }
  685. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  686. struct cryptd_queue *queue)
  687. {
  688. struct hashd_instance_ctx *ctx;
  689. struct ahash_instance *inst;
  690. struct shash_alg *salg;
  691. struct crypto_alg *alg;
  692. u32 type = 0;
  693. u32 mask = 0;
  694. int err;
  695. cryptd_check_internal(tb, &type, &mask);
  696. salg = shash_attr_alg(tb[1], type, mask);
  697. if (IS_ERR(salg))
  698. return PTR_ERR(salg);
  699. alg = &salg->base;
  700. inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
  701. sizeof(*ctx));
  702. err = PTR_ERR(inst);
  703. if (IS_ERR(inst))
  704. goto out_put_alg;
  705. ctx = ahash_instance_ctx(inst);
  706. ctx->queue = queue;
  707. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  708. ahash_crypto_instance(inst));
  709. if (err)
  710. goto out_free_inst;
  711. inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
  712. (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
  713. CRYPTO_ALG_OPTIONAL_KEY));
  714. inst->alg.halg.digestsize = salg->digestsize;
  715. inst->alg.halg.statesize = salg->statesize;
  716. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  717. inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
  718. inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
  719. inst->alg.init = cryptd_hash_init_enqueue;
  720. inst->alg.update = cryptd_hash_update_enqueue;
  721. inst->alg.final = cryptd_hash_final_enqueue;
  722. inst->alg.finup = cryptd_hash_finup_enqueue;
  723. inst->alg.export = cryptd_hash_export;
  724. inst->alg.import = cryptd_hash_import;
  725. if (crypto_shash_alg_has_setkey(salg))
  726. inst->alg.setkey = cryptd_hash_setkey;
  727. inst->alg.digest = cryptd_hash_digest_enqueue;
  728. err = ahash_register_instance(tmpl, inst);
  729. if (err) {
  730. crypto_drop_shash(&ctx->spawn);
  731. out_free_inst:
  732. kfree(inst);
  733. }
  734. out_put_alg:
  735. crypto_mod_put(alg);
  736. return err;
  737. }
  738. static int cryptd_aead_setkey(struct crypto_aead *parent,
  739. const u8 *key, unsigned int keylen)
  740. {
  741. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  742. struct crypto_aead *child = ctx->child;
  743. return crypto_aead_setkey(child, key, keylen);
  744. }
  745. static int cryptd_aead_setauthsize(struct crypto_aead *parent,
  746. unsigned int authsize)
  747. {
  748. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  749. struct crypto_aead *child = ctx->child;
  750. return crypto_aead_setauthsize(child, authsize);
  751. }
  752. static void cryptd_aead_crypt(struct aead_request *req,
  753. struct crypto_aead *child,
  754. int err,
  755. int (*crypt)(struct aead_request *req))
  756. {
  757. struct cryptd_aead_request_ctx *rctx;
  758. struct cryptd_aead_ctx *ctx;
  759. crypto_completion_t compl;
  760. struct crypto_aead *tfm;
  761. int refcnt;
  762. rctx = aead_request_ctx(req);
  763. compl = rctx->complete;
  764. tfm = crypto_aead_reqtfm(req);
  765. if (unlikely(err == -EINPROGRESS))
  766. goto out;
  767. aead_request_set_tfm(req, child);
  768. err = crypt( req );
  769. out:
  770. ctx = crypto_aead_ctx(tfm);
  771. refcnt = atomic_read(&ctx->refcnt);
  772. local_bh_disable();
  773. compl(&req->base, err);
  774. local_bh_enable();
  775. if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
  776. crypto_free_aead(tfm);
  777. }
  778. static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
  779. {
  780. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  781. struct crypto_aead *child = ctx->child;
  782. struct aead_request *req;
  783. req = container_of(areq, struct aead_request, base);
  784. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
  785. }
  786. static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
  787. {
  788. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  789. struct crypto_aead *child = ctx->child;
  790. struct aead_request *req;
  791. req = container_of(areq, struct aead_request, base);
  792. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
  793. }
  794. static int cryptd_aead_enqueue(struct aead_request *req,
  795. crypto_completion_t compl)
  796. {
  797. struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
  798. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  799. struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
  800. rctx->complete = req->base.complete;
  801. req->base.complete = compl;
  802. return cryptd_enqueue_request(queue, &req->base);
  803. }
  804. static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
  805. {
  806. return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
  807. }
  808. static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
  809. {
  810. return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
  811. }
  812. static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
  813. {
  814. struct aead_instance *inst = aead_alg_instance(tfm);
  815. struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
  816. struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
  817. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  818. struct crypto_aead *cipher;
  819. cipher = crypto_spawn_aead(spawn);
  820. if (IS_ERR(cipher))
  821. return PTR_ERR(cipher);
  822. ctx->child = cipher;
  823. crypto_aead_set_reqsize(
  824. tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
  825. crypto_aead_reqsize(cipher)));
  826. return 0;
  827. }
  828. static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
  829. {
  830. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  831. crypto_free_aead(ctx->child);
  832. }
  833. static int cryptd_create_aead(struct crypto_template *tmpl,
  834. struct rtattr **tb,
  835. struct cryptd_queue *queue)
  836. {
  837. struct aead_instance_ctx *ctx;
  838. struct aead_instance *inst;
  839. struct aead_alg *alg;
  840. const char *name;
  841. u32 type = 0;
  842. u32 mask = CRYPTO_ALG_ASYNC;
  843. int err;
  844. cryptd_check_internal(tb, &type, &mask);
  845. name = crypto_attr_alg_name(tb[1]);
  846. if (IS_ERR(name))
  847. return PTR_ERR(name);
  848. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  849. if (!inst)
  850. return -ENOMEM;
  851. ctx = aead_instance_ctx(inst);
  852. ctx->queue = queue;
  853. crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
  854. err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
  855. if (err)
  856. goto out_free_inst;
  857. alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
  858. err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
  859. if (err)
  860. goto out_drop_aead;
  861. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
  862. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  863. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
  864. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  865. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  866. inst->alg.init = cryptd_aead_init_tfm;
  867. inst->alg.exit = cryptd_aead_exit_tfm;
  868. inst->alg.setkey = cryptd_aead_setkey;
  869. inst->alg.setauthsize = cryptd_aead_setauthsize;
  870. inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
  871. inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
  872. err = aead_register_instance(tmpl, inst);
  873. if (err) {
  874. out_drop_aead:
  875. crypto_drop_aead(&ctx->aead_spawn);
  876. out_free_inst:
  877. kfree(inst);
  878. }
  879. return err;
  880. }
  881. static struct cryptd_queue queue;
  882. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  883. {
  884. struct crypto_attr_type *algt;
  885. algt = crypto_get_attr_type(tb);
  886. if (IS_ERR(algt))
  887. return PTR_ERR(algt);
  888. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  889. case CRYPTO_ALG_TYPE_BLKCIPHER:
  890. if ((algt->type & CRYPTO_ALG_TYPE_MASK) ==
  891. CRYPTO_ALG_TYPE_BLKCIPHER)
  892. return cryptd_create_blkcipher(tmpl, tb, &queue);
  893. return cryptd_create_skcipher(tmpl, tb, &queue);
  894. case CRYPTO_ALG_TYPE_DIGEST:
  895. return cryptd_create_hash(tmpl, tb, &queue);
  896. case CRYPTO_ALG_TYPE_AEAD:
  897. return cryptd_create_aead(tmpl, tb, &queue);
  898. }
  899. return -EINVAL;
  900. }
  901. static void cryptd_free(struct crypto_instance *inst)
  902. {
  903. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  904. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  905. struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
  906. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  907. case CRYPTO_ALG_TYPE_AHASH:
  908. crypto_drop_shash(&hctx->spawn);
  909. kfree(ahash_instance(inst));
  910. return;
  911. case CRYPTO_ALG_TYPE_AEAD:
  912. crypto_drop_aead(&aead_ctx->aead_spawn);
  913. kfree(aead_instance(inst));
  914. return;
  915. default:
  916. crypto_drop_spawn(&ctx->spawn);
  917. kfree(inst);
  918. }
  919. }
  920. static struct crypto_template cryptd_tmpl = {
  921. .name = "cryptd",
  922. .create = cryptd_create,
  923. .free = cryptd_free,
  924. .module = THIS_MODULE,
  925. };
  926. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  927. u32 type, u32 mask)
  928. {
  929. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  930. struct cryptd_blkcipher_ctx *ctx;
  931. struct crypto_tfm *tfm;
  932. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  933. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  934. return ERR_PTR(-EINVAL);
  935. type = crypto_skcipher_type(type);
  936. mask &= ~CRYPTO_ALG_TYPE_MASK;
  937. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  938. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  939. if (IS_ERR(tfm))
  940. return ERR_CAST(tfm);
  941. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  942. crypto_free_tfm(tfm);
  943. return ERR_PTR(-EINVAL);
  944. }
  945. ctx = crypto_tfm_ctx(tfm);
  946. atomic_set(&ctx->refcnt, 1);
  947. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  948. }
  949. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  950. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  951. {
  952. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  953. return ctx->child;
  954. }
  955. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  956. bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm)
  957. {
  958. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  959. return atomic_read(&ctx->refcnt) - 1;
  960. }
  961. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued);
  962. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  963. {
  964. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  965. if (atomic_dec_and_test(&ctx->refcnt))
  966. crypto_free_ablkcipher(&tfm->base);
  967. }
  968. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  969. struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
  970. u32 type, u32 mask)
  971. {
  972. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  973. struct cryptd_skcipher_ctx *ctx;
  974. struct crypto_skcipher *tfm;
  975. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  976. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  977. return ERR_PTR(-EINVAL);
  978. tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
  979. if (IS_ERR(tfm))
  980. return ERR_CAST(tfm);
  981. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  982. crypto_free_skcipher(tfm);
  983. return ERR_PTR(-EINVAL);
  984. }
  985. ctx = crypto_skcipher_ctx(tfm);
  986. atomic_set(&ctx->refcnt, 1);
  987. return container_of(tfm, struct cryptd_skcipher, base);
  988. }
  989. EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
  990. struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
  991. {
  992. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  993. return &ctx->child->base;
  994. }
  995. EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
  996. bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
  997. {
  998. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  999. return atomic_read(&ctx->refcnt) - 1;
  1000. }
  1001. EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
  1002. void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
  1003. {
  1004. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  1005. if (atomic_dec_and_test(&ctx->refcnt))
  1006. crypto_free_skcipher(&tfm->base);
  1007. }
  1008. EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
  1009. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  1010. u32 type, u32 mask)
  1011. {
  1012. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  1013. struct cryptd_hash_ctx *ctx;
  1014. struct crypto_ahash *tfm;
  1015. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  1016. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  1017. return ERR_PTR(-EINVAL);
  1018. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  1019. if (IS_ERR(tfm))
  1020. return ERR_CAST(tfm);
  1021. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  1022. crypto_free_ahash(tfm);
  1023. return ERR_PTR(-EINVAL);
  1024. }
  1025. ctx = crypto_ahash_ctx(tfm);
  1026. atomic_set(&ctx->refcnt, 1);
  1027. return __cryptd_ahash_cast(tfm);
  1028. }
  1029. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  1030. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  1031. {
  1032. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1033. return ctx->child;
  1034. }
  1035. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  1036. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  1037. {
  1038. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  1039. return &rctx->desc;
  1040. }
  1041. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  1042. bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
  1043. {
  1044. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1045. return atomic_read(&ctx->refcnt) - 1;
  1046. }
  1047. EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
  1048. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  1049. {
  1050. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  1051. if (atomic_dec_and_test(&ctx->refcnt))
  1052. crypto_free_ahash(&tfm->base);
  1053. }
  1054. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  1055. struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
  1056. u32 type, u32 mask)
  1057. {
  1058. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  1059. struct cryptd_aead_ctx *ctx;
  1060. struct crypto_aead *tfm;
  1061. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  1062. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  1063. return ERR_PTR(-EINVAL);
  1064. tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
  1065. if (IS_ERR(tfm))
  1066. return ERR_CAST(tfm);
  1067. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  1068. crypto_free_aead(tfm);
  1069. return ERR_PTR(-EINVAL);
  1070. }
  1071. ctx = crypto_aead_ctx(tfm);
  1072. atomic_set(&ctx->refcnt, 1);
  1073. return __cryptd_aead_cast(tfm);
  1074. }
  1075. EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
  1076. struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
  1077. {
  1078. struct cryptd_aead_ctx *ctx;
  1079. ctx = crypto_aead_ctx(&tfm->base);
  1080. return ctx->child;
  1081. }
  1082. EXPORT_SYMBOL_GPL(cryptd_aead_child);
  1083. bool cryptd_aead_queued(struct cryptd_aead *tfm)
  1084. {
  1085. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  1086. return atomic_read(&ctx->refcnt) - 1;
  1087. }
  1088. EXPORT_SYMBOL_GPL(cryptd_aead_queued);
  1089. void cryptd_free_aead(struct cryptd_aead *tfm)
  1090. {
  1091. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  1092. if (atomic_dec_and_test(&ctx->refcnt))
  1093. crypto_free_aead(&tfm->base);
  1094. }
  1095. EXPORT_SYMBOL_GPL(cryptd_free_aead);
  1096. static int __init cryptd_init(void)
  1097. {
  1098. int err;
  1099. err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
  1100. if (err)
  1101. return err;
  1102. err = crypto_register_template(&cryptd_tmpl);
  1103. if (err)
  1104. cryptd_fini_queue(&queue);
  1105. return err;
  1106. }
  1107. static void __exit cryptd_exit(void)
  1108. {
  1109. cryptd_fini_queue(&queue);
  1110. crypto_unregister_template(&cryptd_tmpl);
  1111. }
  1112. subsys_initcall(cryptd_init);
  1113. module_exit(cryptd_exit);
  1114. MODULE_LICENSE("GPL");
  1115. MODULE_DESCRIPTION("Software async crypto daemon");
  1116. MODULE_ALIAS_CRYPTO("cryptd");