cryptd.c 28 KB

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