cryptd.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Software async crypto daemon.
  4. *
  5. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. *
  7. * Added AEAD support to cryptd.
  8. * Authors: Tadeusz Struk (tadeusz.struk@intel.com)
  9. * Adrian Hoban <adrian.hoban@intel.com>
  10. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  11. * Aidan O'Mahony (aidan.o.mahony@intel.com)
  12. * Copyright (c) 2010, Intel Corporation.
  13. */
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/internal/aead.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/cryptd.h>
  18. #include <linux/refcount.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/workqueue.h>
  28. static unsigned int cryptd_max_cpu_qlen = 1000;
  29. module_param(cryptd_max_cpu_qlen, uint, 0);
  30. MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
  31. static struct workqueue_struct *cryptd_wq;
  32. struct cryptd_cpu_queue {
  33. local_lock_t bh_lock;
  34. struct crypto_queue queue;
  35. struct work_struct work;
  36. };
  37. struct cryptd_queue {
  38. /*
  39. * Protected by disabling BH to allow enqueueing from softinterrupt and
  40. * dequeuing from kworker (cryptd_queue_worker()).
  41. */
  42. struct cryptd_cpu_queue __percpu *cpu_queue;
  43. };
  44. struct cryptd_instance_ctx {
  45. struct crypto_spawn spawn;
  46. struct cryptd_queue *queue;
  47. };
  48. struct skcipherd_instance_ctx {
  49. struct crypto_skcipher_spawn spawn;
  50. struct cryptd_queue *queue;
  51. };
  52. struct hashd_instance_ctx {
  53. struct crypto_shash_spawn spawn;
  54. struct cryptd_queue *queue;
  55. };
  56. struct aead_instance_ctx {
  57. struct crypto_aead_spawn aead_spawn;
  58. struct cryptd_queue *queue;
  59. };
  60. struct cryptd_skcipher_ctx {
  61. refcount_t refcnt;
  62. struct crypto_skcipher *child;
  63. };
  64. struct cryptd_skcipher_request_ctx {
  65. struct skcipher_request req;
  66. };
  67. struct cryptd_hash_ctx {
  68. refcount_t refcnt;
  69. struct crypto_shash *child;
  70. };
  71. struct cryptd_hash_request_ctx {
  72. crypto_completion_t complete;
  73. void *data;
  74. struct shash_desc desc;
  75. };
  76. struct cryptd_aead_ctx {
  77. refcount_t refcnt;
  78. struct crypto_aead *child;
  79. };
  80. struct cryptd_aead_request_ctx {
  81. struct aead_request req;
  82. };
  83. static void cryptd_queue_worker(struct work_struct *work);
  84. static int cryptd_init_queue(struct cryptd_queue *queue,
  85. unsigned int max_cpu_qlen)
  86. {
  87. int cpu;
  88. struct cryptd_cpu_queue *cpu_queue;
  89. queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
  90. if (!queue->cpu_queue)
  91. return -ENOMEM;
  92. for_each_possible_cpu(cpu) {
  93. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  94. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  95. INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
  96. local_lock_init(&cpu_queue->bh_lock);
  97. }
  98. pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
  99. return 0;
  100. }
  101. static void cryptd_fini_queue(struct cryptd_queue *queue)
  102. {
  103. int cpu;
  104. struct cryptd_cpu_queue *cpu_queue;
  105. for_each_possible_cpu(cpu) {
  106. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  107. BUG_ON(cpu_queue->queue.qlen);
  108. }
  109. free_percpu(queue->cpu_queue);
  110. }
  111. static int cryptd_enqueue_request(struct cryptd_queue *queue,
  112. struct crypto_async_request *request)
  113. {
  114. int err;
  115. struct cryptd_cpu_queue *cpu_queue;
  116. refcount_t *refcnt;
  117. local_bh_disable();
  118. local_lock_nested_bh(&queue->cpu_queue->bh_lock);
  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;
  124. queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
  125. if (!refcount_read(refcnt))
  126. goto out;
  127. refcount_inc(refcnt);
  128. out:
  129. local_unlock_nested_bh(&queue->cpu_queue->bh_lock);
  130. local_bh_enable();
  131. return err;
  132. }
  133. /* Called in workqueue context, do one real cryption work (via
  134. * req->complete) and reschedule itself if there are more work to
  135. * do. */
  136. static void cryptd_queue_worker(struct work_struct *work)
  137. {
  138. struct cryptd_cpu_queue *cpu_queue;
  139. struct crypto_async_request *req, *backlog;
  140. cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
  141. /*
  142. * Only handle one request at a time to avoid hogging crypto workqueue.
  143. */
  144. local_bh_disable();
  145. __local_lock_nested_bh(&cpu_queue->bh_lock);
  146. backlog = crypto_get_backlog(&cpu_queue->queue);
  147. req = crypto_dequeue_request(&cpu_queue->queue);
  148. __local_unlock_nested_bh(&cpu_queue->bh_lock);
  149. local_bh_enable();
  150. if (!req)
  151. return;
  152. if (backlog)
  153. crypto_request_complete(backlog, -EINPROGRESS);
  154. crypto_request_complete(req, 0);
  155. if (cpu_queue->queue.qlen)
  156. queue_work(cryptd_wq, &cpu_queue->work);
  157. }
  158. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  159. {
  160. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  161. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  162. return ictx->queue;
  163. }
  164. static void cryptd_type_and_mask(struct crypto_attr_type *algt,
  165. u32 *type, u32 *mask)
  166. {
  167. /*
  168. * cryptd is allowed to wrap internal algorithms, but in that case the
  169. * resulting cryptd instance will be marked as internal as well.
  170. */
  171. *type = algt->type & CRYPTO_ALG_INTERNAL;
  172. *mask = algt->mask & CRYPTO_ALG_INTERNAL;
  173. /* No point in cryptd wrapping an algorithm that's already async. */
  174. *mask |= CRYPTO_ALG_ASYNC;
  175. *mask |= crypto_algt_inherited_mask(algt);
  176. }
  177. static int cryptd_init_instance(struct crypto_instance *inst,
  178. struct crypto_alg *alg)
  179. {
  180. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  181. "cryptd(%s)",
  182. alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  183. return -ENAMETOOLONG;
  184. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  185. inst->alg.cra_priority = alg->cra_priority + 50;
  186. inst->alg.cra_blocksize = alg->cra_blocksize;
  187. inst->alg.cra_alignmask = alg->cra_alignmask;
  188. return 0;
  189. }
  190. static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
  191. const u8 *key, unsigned int keylen)
  192. {
  193. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
  194. struct crypto_skcipher *child = ctx->child;
  195. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  196. crypto_skcipher_set_flags(child,
  197. crypto_skcipher_get_flags(parent) &
  198. CRYPTO_TFM_REQ_MASK);
  199. return crypto_skcipher_setkey(child, key, keylen);
  200. }
  201. static struct skcipher_request *cryptd_skcipher_prepare(
  202. struct skcipher_request *req, int err)
  203. {
  204. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  205. struct skcipher_request *subreq = &rctx->req;
  206. struct cryptd_skcipher_ctx *ctx;
  207. struct crypto_skcipher *child;
  208. req->base.complete = subreq->base.complete;
  209. req->base.data = subreq->base.data;
  210. if (unlikely(err == -EINPROGRESS))
  211. return NULL;
  212. ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  213. child = ctx->child;
  214. skcipher_request_set_tfm(subreq, child);
  215. skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  216. NULL, NULL);
  217. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  218. req->iv);
  219. return subreq;
  220. }
  221. static void cryptd_skcipher_complete(struct skcipher_request *req, int err,
  222. crypto_completion_t complete)
  223. {
  224. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  225. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  226. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  227. struct skcipher_request *subreq = &rctx->req;
  228. int refcnt = refcount_read(&ctx->refcnt);
  229. local_bh_disable();
  230. skcipher_request_complete(req, err);
  231. local_bh_enable();
  232. if (unlikely(err == -EINPROGRESS)) {
  233. subreq->base.complete = req->base.complete;
  234. subreq->base.data = req->base.data;
  235. req->base.complete = complete;
  236. req->base.data = req;
  237. } else if (refcnt && refcount_dec_and_test(&ctx->refcnt))
  238. crypto_free_skcipher(tfm);
  239. }
  240. static void cryptd_skcipher_encrypt(void *data, int err)
  241. {
  242. struct skcipher_request *req = data;
  243. struct skcipher_request *subreq;
  244. subreq = cryptd_skcipher_prepare(req, err);
  245. if (likely(subreq))
  246. err = crypto_skcipher_encrypt(subreq);
  247. cryptd_skcipher_complete(req, err, cryptd_skcipher_encrypt);
  248. }
  249. static void cryptd_skcipher_decrypt(void *data, int err)
  250. {
  251. struct skcipher_request *req = data;
  252. struct skcipher_request *subreq;
  253. subreq = cryptd_skcipher_prepare(req, err);
  254. if (likely(subreq))
  255. err = crypto_skcipher_decrypt(subreq);
  256. cryptd_skcipher_complete(req, err, cryptd_skcipher_decrypt);
  257. }
  258. static int cryptd_skcipher_enqueue(struct skcipher_request *req,
  259. crypto_completion_t compl)
  260. {
  261. struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
  262. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  263. struct skcipher_request *subreq = &rctx->req;
  264. struct cryptd_queue *queue;
  265. queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
  266. subreq->base.complete = req->base.complete;
  267. subreq->base.data = req->base.data;
  268. req->base.complete = compl;
  269. req->base.data = req;
  270. return cryptd_enqueue_request(queue, &req->base);
  271. }
  272. static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
  273. {
  274. return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
  275. }
  276. static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
  277. {
  278. return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
  279. }
  280. static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
  281. {
  282. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  283. struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
  284. struct crypto_skcipher_spawn *spawn = &ictx->spawn;
  285. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  286. struct crypto_skcipher *cipher;
  287. cipher = crypto_spawn_skcipher(spawn);
  288. if (IS_ERR(cipher))
  289. return PTR_ERR(cipher);
  290. ctx->child = cipher;
  291. crypto_skcipher_set_reqsize(
  292. tfm, sizeof(struct cryptd_skcipher_request_ctx) +
  293. crypto_skcipher_reqsize(cipher));
  294. return 0;
  295. }
  296. static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
  297. {
  298. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  299. crypto_free_skcipher(ctx->child);
  300. }
  301. static void cryptd_skcipher_free(struct skcipher_instance *inst)
  302. {
  303. struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
  304. crypto_drop_skcipher(&ctx->spawn);
  305. kfree(inst);
  306. }
  307. static int cryptd_create_skcipher(struct crypto_template *tmpl,
  308. struct rtattr **tb,
  309. struct crypto_attr_type *algt,
  310. struct cryptd_queue *queue)
  311. {
  312. struct skcipherd_instance_ctx *ctx;
  313. struct skcipher_instance *inst;
  314. struct skcipher_alg_common *alg;
  315. u32 type;
  316. u32 mask;
  317. int err;
  318. cryptd_type_and_mask(algt, &type, &mask);
  319. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  320. if (!inst)
  321. return -ENOMEM;
  322. ctx = skcipher_instance_ctx(inst);
  323. ctx->queue = queue;
  324. err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
  325. crypto_attr_alg_name(tb[1]), type, mask);
  326. if (err)
  327. goto err_free_inst;
  328. alg = crypto_spawn_skcipher_alg_common(&ctx->spawn);
  329. err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
  330. if (err)
  331. goto err_free_inst;
  332. inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
  333. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  334. inst->alg.ivsize = alg->ivsize;
  335. inst->alg.chunksize = alg->chunksize;
  336. inst->alg.min_keysize = alg->min_keysize;
  337. inst->alg.max_keysize = alg->max_keysize;
  338. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
  339. inst->alg.init = cryptd_skcipher_init_tfm;
  340. inst->alg.exit = cryptd_skcipher_exit_tfm;
  341. inst->alg.setkey = cryptd_skcipher_setkey;
  342. inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
  343. inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
  344. inst->free = cryptd_skcipher_free;
  345. err = skcipher_register_instance(tmpl, inst);
  346. if (err) {
  347. err_free_inst:
  348. cryptd_skcipher_free(inst);
  349. }
  350. return err;
  351. }
  352. static int cryptd_hash_init_tfm(struct crypto_ahash *tfm)
  353. {
  354. struct ahash_instance *inst = ahash_alg_instance(tfm);
  355. struct hashd_instance_ctx *ictx = ahash_instance_ctx(inst);
  356. struct crypto_shash_spawn *spawn = &ictx->spawn;
  357. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  358. struct crypto_shash *hash;
  359. hash = crypto_spawn_shash(spawn);
  360. if (IS_ERR(hash))
  361. return PTR_ERR(hash);
  362. ctx->child = hash;
  363. crypto_ahash_set_reqsize(tfm,
  364. sizeof(struct cryptd_hash_request_ctx) +
  365. crypto_shash_descsize(hash));
  366. return 0;
  367. }
  368. static int cryptd_hash_clone_tfm(struct crypto_ahash *ntfm,
  369. struct crypto_ahash *tfm)
  370. {
  371. struct cryptd_hash_ctx *nctx = crypto_ahash_ctx(ntfm);
  372. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  373. struct crypto_shash *hash;
  374. hash = crypto_clone_shash(ctx->child);
  375. if (IS_ERR(hash))
  376. return PTR_ERR(hash);
  377. nctx->child = hash;
  378. return 0;
  379. }
  380. static void cryptd_hash_exit_tfm(struct crypto_ahash *tfm)
  381. {
  382. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  383. crypto_free_shash(ctx->child);
  384. }
  385. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  386. const u8 *key, unsigned int keylen)
  387. {
  388. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  389. struct crypto_shash *child = ctx->child;
  390. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  391. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  392. CRYPTO_TFM_REQ_MASK);
  393. return crypto_shash_setkey(child, key, keylen);
  394. }
  395. static int cryptd_hash_enqueue(struct ahash_request *req,
  396. crypto_completion_t compl)
  397. {
  398. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  399. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  400. struct cryptd_queue *queue =
  401. cryptd_get_queue(crypto_ahash_tfm(tfm));
  402. rctx->complete = req->base.complete;
  403. rctx->data = req->base.data;
  404. req->base.complete = compl;
  405. req->base.data = req;
  406. return cryptd_enqueue_request(queue, &req->base);
  407. }
  408. static struct shash_desc *cryptd_hash_prepare(struct ahash_request *req,
  409. int err)
  410. {
  411. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  412. req->base.complete = rctx->complete;
  413. req->base.data = rctx->data;
  414. if (unlikely(err == -EINPROGRESS))
  415. return NULL;
  416. return &rctx->desc;
  417. }
  418. static void cryptd_hash_complete(struct ahash_request *req, int err,
  419. crypto_completion_t complete)
  420. {
  421. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  422. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  423. int refcnt = refcount_read(&ctx->refcnt);
  424. local_bh_disable();
  425. ahash_request_complete(req, err);
  426. local_bh_enable();
  427. if (err == -EINPROGRESS) {
  428. req->base.complete = complete;
  429. req->base.data = req;
  430. } else if (refcnt && refcount_dec_and_test(&ctx->refcnt))
  431. crypto_free_ahash(tfm);
  432. }
  433. static void cryptd_hash_init(void *data, int err)
  434. {
  435. struct ahash_request *req = data;
  436. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  437. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  438. struct crypto_shash *child = ctx->child;
  439. struct shash_desc *desc;
  440. desc = cryptd_hash_prepare(req, err);
  441. if (unlikely(!desc))
  442. goto out;
  443. desc->tfm = child;
  444. err = crypto_shash_init(desc);
  445. out:
  446. cryptd_hash_complete(req, err, cryptd_hash_init);
  447. }
  448. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  449. {
  450. return cryptd_hash_enqueue(req, cryptd_hash_init);
  451. }
  452. static void cryptd_hash_update(void *data, int err)
  453. {
  454. struct ahash_request *req = data;
  455. struct shash_desc *desc;
  456. desc = cryptd_hash_prepare(req, err);
  457. if (likely(desc))
  458. err = shash_ahash_update(req, desc);
  459. cryptd_hash_complete(req, err, cryptd_hash_update);
  460. }
  461. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  462. {
  463. return cryptd_hash_enqueue(req, cryptd_hash_update);
  464. }
  465. static void cryptd_hash_final(void *data, int err)
  466. {
  467. struct ahash_request *req = data;
  468. struct shash_desc *desc;
  469. desc = cryptd_hash_prepare(req, err);
  470. if (likely(desc))
  471. err = crypto_shash_final(desc, req->result);
  472. cryptd_hash_complete(req, err, cryptd_hash_final);
  473. }
  474. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  475. {
  476. return cryptd_hash_enqueue(req, cryptd_hash_final);
  477. }
  478. static void cryptd_hash_finup(void *data, int err)
  479. {
  480. struct ahash_request *req = data;
  481. struct shash_desc *desc;
  482. desc = cryptd_hash_prepare(req, err);
  483. if (likely(desc))
  484. err = shash_ahash_finup(req, desc);
  485. cryptd_hash_complete(req, err, cryptd_hash_finup);
  486. }
  487. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  488. {
  489. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  490. }
  491. static void cryptd_hash_digest(void *data, int err)
  492. {
  493. struct ahash_request *req = data;
  494. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  495. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  496. struct crypto_shash *child = ctx->child;
  497. struct shash_desc *desc;
  498. desc = cryptd_hash_prepare(req, err);
  499. if (unlikely(!desc))
  500. goto out;
  501. desc->tfm = child;
  502. err = shash_ahash_digest(req, desc);
  503. out:
  504. cryptd_hash_complete(req, err, cryptd_hash_digest);
  505. }
  506. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  507. {
  508. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  509. }
  510. static int cryptd_hash_export(struct ahash_request *req, void *out)
  511. {
  512. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  513. return crypto_shash_export(&rctx->desc, out);
  514. }
  515. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  516. {
  517. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  518. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  519. struct shash_desc *desc = cryptd_shash_desc(req);
  520. desc->tfm = ctx->child;
  521. return crypto_shash_import(desc, in);
  522. }
  523. static void cryptd_hash_free(struct ahash_instance *inst)
  524. {
  525. struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
  526. crypto_drop_shash(&ctx->spawn);
  527. kfree(inst);
  528. }
  529. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  530. struct crypto_attr_type *algt,
  531. struct cryptd_queue *queue)
  532. {
  533. struct hashd_instance_ctx *ctx;
  534. struct ahash_instance *inst;
  535. struct shash_alg *alg;
  536. u32 type;
  537. u32 mask;
  538. int err;
  539. cryptd_type_and_mask(algt, &type, &mask);
  540. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  541. if (!inst)
  542. return -ENOMEM;
  543. ctx = ahash_instance_ctx(inst);
  544. ctx->queue = queue;
  545. err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
  546. crypto_attr_alg_name(tb[1]), type, mask);
  547. if (err)
  548. goto err_free_inst;
  549. alg = crypto_spawn_shash_alg(&ctx->spawn);
  550. err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
  551. if (err)
  552. goto err_free_inst;
  553. inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC |
  554. (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL|
  555. CRYPTO_ALG_OPTIONAL_KEY));
  556. inst->alg.halg.digestsize = alg->digestsize;
  557. inst->alg.halg.statesize = alg->statesize;
  558. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  559. inst->alg.init_tfm = cryptd_hash_init_tfm;
  560. inst->alg.clone_tfm = cryptd_hash_clone_tfm;
  561. inst->alg.exit_tfm = cryptd_hash_exit_tfm;
  562. inst->alg.init = cryptd_hash_init_enqueue;
  563. inst->alg.update = cryptd_hash_update_enqueue;
  564. inst->alg.final = cryptd_hash_final_enqueue;
  565. inst->alg.finup = cryptd_hash_finup_enqueue;
  566. inst->alg.export = cryptd_hash_export;
  567. inst->alg.import = cryptd_hash_import;
  568. if (crypto_shash_alg_has_setkey(alg))
  569. inst->alg.setkey = cryptd_hash_setkey;
  570. inst->alg.digest = cryptd_hash_digest_enqueue;
  571. inst->free = cryptd_hash_free;
  572. err = ahash_register_instance(tmpl, inst);
  573. if (err) {
  574. err_free_inst:
  575. cryptd_hash_free(inst);
  576. }
  577. return err;
  578. }
  579. static int cryptd_aead_setkey(struct crypto_aead *parent,
  580. const u8 *key, unsigned int keylen)
  581. {
  582. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  583. struct crypto_aead *child = ctx->child;
  584. return crypto_aead_setkey(child, key, keylen);
  585. }
  586. static int cryptd_aead_setauthsize(struct crypto_aead *parent,
  587. unsigned int authsize)
  588. {
  589. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  590. struct crypto_aead *child = ctx->child;
  591. return crypto_aead_setauthsize(child, authsize);
  592. }
  593. static void cryptd_aead_crypt(struct aead_request *req,
  594. struct crypto_aead *child, int err,
  595. int (*crypt)(struct aead_request *req),
  596. crypto_completion_t compl)
  597. {
  598. struct cryptd_aead_request_ctx *rctx;
  599. struct aead_request *subreq;
  600. struct cryptd_aead_ctx *ctx;
  601. struct crypto_aead *tfm;
  602. int refcnt;
  603. rctx = aead_request_ctx(req);
  604. subreq = &rctx->req;
  605. req->base.complete = subreq->base.complete;
  606. req->base.data = subreq->base.data;
  607. tfm = crypto_aead_reqtfm(req);
  608. if (unlikely(err == -EINPROGRESS))
  609. goto out;
  610. aead_request_set_tfm(subreq, child);
  611. aead_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
  612. NULL, NULL);
  613. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  614. req->iv);
  615. aead_request_set_ad(subreq, req->assoclen);
  616. err = crypt(subreq);
  617. out:
  618. ctx = crypto_aead_ctx(tfm);
  619. refcnt = refcount_read(&ctx->refcnt);
  620. local_bh_disable();
  621. aead_request_complete(req, err);
  622. local_bh_enable();
  623. if (err == -EINPROGRESS) {
  624. subreq->base.complete = req->base.complete;
  625. subreq->base.data = req->base.data;
  626. req->base.complete = compl;
  627. req->base.data = req;
  628. } else if (refcnt && refcount_dec_and_test(&ctx->refcnt))
  629. crypto_free_aead(tfm);
  630. }
  631. static void cryptd_aead_encrypt(void *data, int err)
  632. {
  633. struct aead_request *req = data;
  634. struct cryptd_aead_ctx *ctx;
  635. struct crypto_aead *child;
  636. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  637. child = ctx->child;
  638. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt,
  639. cryptd_aead_encrypt);
  640. }
  641. static void cryptd_aead_decrypt(void *data, int err)
  642. {
  643. struct aead_request *req = data;
  644. struct cryptd_aead_ctx *ctx;
  645. struct crypto_aead *child;
  646. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  647. child = ctx->child;
  648. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt,
  649. cryptd_aead_decrypt);
  650. }
  651. static int cryptd_aead_enqueue(struct aead_request *req,
  652. crypto_completion_t compl)
  653. {
  654. struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
  655. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  656. struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
  657. struct aead_request *subreq = &rctx->req;
  658. subreq->base.complete = req->base.complete;
  659. subreq->base.data = req->base.data;
  660. req->base.complete = compl;
  661. req->base.data = req;
  662. return cryptd_enqueue_request(queue, &req->base);
  663. }
  664. static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
  665. {
  666. return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
  667. }
  668. static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
  669. {
  670. return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
  671. }
  672. static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
  673. {
  674. struct aead_instance *inst = aead_alg_instance(tfm);
  675. struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
  676. struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
  677. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  678. struct crypto_aead *cipher;
  679. cipher = crypto_spawn_aead(spawn);
  680. if (IS_ERR(cipher))
  681. return PTR_ERR(cipher);
  682. ctx->child = cipher;
  683. crypto_aead_set_reqsize(
  684. tfm, sizeof(struct cryptd_aead_request_ctx) +
  685. crypto_aead_reqsize(cipher));
  686. return 0;
  687. }
  688. static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
  689. {
  690. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  691. crypto_free_aead(ctx->child);
  692. }
  693. static void cryptd_aead_free(struct aead_instance *inst)
  694. {
  695. struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
  696. crypto_drop_aead(&ctx->aead_spawn);
  697. kfree(inst);
  698. }
  699. static int cryptd_create_aead(struct crypto_template *tmpl,
  700. struct rtattr **tb,
  701. struct crypto_attr_type *algt,
  702. struct cryptd_queue *queue)
  703. {
  704. struct aead_instance_ctx *ctx;
  705. struct aead_instance *inst;
  706. struct aead_alg *alg;
  707. u32 type;
  708. u32 mask;
  709. int err;
  710. cryptd_type_and_mask(algt, &type, &mask);
  711. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  712. if (!inst)
  713. return -ENOMEM;
  714. ctx = aead_instance_ctx(inst);
  715. ctx->queue = queue;
  716. err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
  717. crypto_attr_alg_name(tb[1]), type, mask);
  718. if (err)
  719. goto err_free_inst;
  720. alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
  721. err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
  722. if (err)
  723. goto err_free_inst;
  724. inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
  725. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  726. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
  727. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  728. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  729. inst->alg.init = cryptd_aead_init_tfm;
  730. inst->alg.exit = cryptd_aead_exit_tfm;
  731. inst->alg.setkey = cryptd_aead_setkey;
  732. inst->alg.setauthsize = cryptd_aead_setauthsize;
  733. inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
  734. inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
  735. inst->free = cryptd_aead_free;
  736. err = aead_register_instance(tmpl, inst);
  737. if (err) {
  738. err_free_inst:
  739. cryptd_aead_free(inst);
  740. }
  741. return err;
  742. }
  743. static struct cryptd_queue queue;
  744. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  745. {
  746. struct crypto_attr_type *algt;
  747. algt = crypto_get_attr_type(tb);
  748. if (IS_ERR(algt))
  749. return PTR_ERR(algt);
  750. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  751. case CRYPTO_ALG_TYPE_LSKCIPHER:
  752. return cryptd_create_skcipher(tmpl, tb, algt, &queue);
  753. case CRYPTO_ALG_TYPE_HASH:
  754. return cryptd_create_hash(tmpl, tb, algt, &queue);
  755. case CRYPTO_ALG_TYPE_AEAD:
  756. return cryptd_create_aead(tmpl, tb, algt, &queue);
  757. }
  758. return -EINVAL;
  759. }
  760. static struct crypto_template cryptd_tmpl = {
  761. .name = "cryptd",
  762. .create = cryptd_create,
  763. .module = THIS_MODULE,
  764. };
  765. struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
  766. u32 type, u32 mask)
  767. {
  768. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  769. struct cryptd_skcipher_ctx *ctx;
  770. struct crypto_skcipher *tfm;
  771. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  772. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  773. return ERR_PTR(-EINVAL);
  774. tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
  775. if (IS_ERR(tfm))
  776. return ERR_CAST(tfm);
  777. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  778. crypto_free_skcipher(tfm);
  779. return ERR_PTR(-EINVAL);
  780. }
  781. ctx = crypto_skcipher_ctx(tfm);
  782. refcount_set(&ctx->refcnt, 1);
  783. return container_of(tfm, struct cryptd_skcipher, base);
  784. }
  785. EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
  786. struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
  787. {
  788. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  789. return ctx->child;
  790. }
  791. EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
  792. bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
  793. {
  794. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  795. return refcount_read(&ctx->refcnt) - 1;
  796. }
  797. EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
  798. void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
  799. {
  800. struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
  801. if (refcount_dec_and_test(&ctx->refcnt))
  802. crypto_free_skcipher(&tfm->base);
  803. }
  804. EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
  805. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  806. u32 type, u32 mask)
  807. {
  808. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  809. struct cryptd_hash_ctx *ctx;
  810. struct crypto_ahash *tfm;
  811. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  812. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  813. return ERR_PTR(-EINVAL);
  814. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  815. if (IS_ERR(tfm))
  816. return ERR_CAST(tfm);
  817. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  818. crypto_free_ahash(tfm);
  819. return ERR_PTR(-EINVAL);
  820. }
  821. ctx = crypto_ahash_ctx(tfm);
  822. refcount_set(&ctx->refcnt, 1);
  823. return __cryptd_ahash_cast(tfm);
  824. }
  825. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  826. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  827. {
  828. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  829. return ctx->child;
  830. }
  831. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  832. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  833. {
  834. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  835. return &rctx->desc;
  836. }
  837. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  838. bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
  839. {
  840. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  841. return refcount_read(&ctx->refcnt) - 1;
  842. }
  843. EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
  844. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  845. {
  846. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  847. if (refcount_dec_and_test(&ctx->refcnt))
  848. crypto_free_ahash(&tfm->base);
  849. }
  850. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  851. struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
  852. u32 type, u32 mask)
  853. {
  854. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  855. struct cryptd_aead_ctx *ctx;
  856. struct crypto_aead *tfm;
  857. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  858. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  859. return ERR_PTR(-EINVAL);
  860. tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
  861. if (IS_ERR(tfm))
  862. return ERR_CAST(tfm);
  863. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  864. crypto_free_aead(tfm);
  865. return ERR_PTR(-EINVAL);
  866. }
  867. ctx = crypto_aead_ctx(tfm);
  868. refcount_set(&ctx->refcnt, 1);
  869. return __cryptd_aead_cast(tfm);
  870. }
  871. EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
  872. struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
  873. {
  874. struct cryptd_aead_ctx *ctx;
  875. ctx = crypto_aead_ctx(&tfm->base);
  876. return ctx->child;
  877. }
  878. EXPORT_SYMBOL_GPL(cryptd_aead_child);
  879. bool cryptd_aead_queued(struct cryptd_aead *tfm)
  880. {
  881. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  882. return refcount_read(&ctx->refcnt) - 1;
  883. }
  884. EXPORT_SYMBOL_GPL(cryptd_aead_queued);
  885. void cryptd_free_aead(struct cryptd_aead *tfm)
  886. {
  887. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
  888. if (refcount_dec_and_test(&ctx->refcnt))
  889. crypto_free_aead(&tfm->base);
  890. }
  891. EXPORT_SYMBOL_GPL(cryptd_free_aead);
  892. static int __init cryptd_init(void)
  893. {
  894. int err;
  895. cryptd_wq = alloc_workqueue("cryptd",
  896. WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE | WQ_PERCPU,
  897. 1);
  898. if (!cryptd_wq)
  899. return -ENOMEM;
  900. err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
  901. if (err)
  902. goto err_destroy_wq;
  903. err = crypto_register_template(&cryptd_tmpl);
  904. if (err)
  905. goto err_fini_queue;
  906. return 0;
  907. err_fini_queue:
  908. cryptd_fini_queue(&queue);
  909. err_destroy_wq:
  910. destroy_workqueue(cryptd_wq);
  911. return err;
  912. }
  913. static void __exit cryptd_exit(void)
  914. {
  915. destroy_workqueue(cryptd_wq);
  916. cryptd_fini_queue(&queue);
  917. crypto_unregister_template(&cryptd_tmpl);
  918. }
  919. module_init(cryptd_init);
  920. module_exit(cryptd_exit);
  921. MODULE_LICENSE("GPL");
  922. MODULE_DESCRIPTION("Software async crypto daemon");
  923. MODULE_ALIAS_CRYPTO("cryptd");