cryptd.c 36 KB

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