cryptd.c 29 KB

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