cryptd.c 36 KB

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