mcryptd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Software multibuffer async crypto daemon.
  3. *
  4. * Copyright (c) 2014 Tim Chen <tim.c.chen@linux.intel.com>
  5. *
  6. * Adapted from crypto daemon.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <crypto/algapi.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/mcryptd.h>
  18. #include <crypto/crypto_wq.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/hardirq.h>
  28. #define MCRYPTD_MAX_CPU_QLEN 100
  29. #define MCRYPTD_BATCH 9
  30. static void *mcryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  31. unsigned int tail);
  32. struct mcryptd_flush_list {
  33. struct list_head list;
  34. struct mutex lock;
  35. };
  36. static struct mcryptd_flush_list __percpu *mcryptd_flist;
  37. struct hashd_instance_ctx {
  38. struct crypto_shash_spawn spawn;
  39. struct mcryptd_queue *queue;
  40. };
  41. static void mcryptd_queue_worker(struct work_struct *work);
  42. void mcryptd_arm_flusher(struct mcryptd_alg_cstate *cstate, unsigned long delay)
  43. {
  44. struct mcryptd_flush_list *flist;
  45. if (!cstate->flusher_engaged) {
  46. /* put the flusher on the flush list */
  47. flist = per_cpu_ptr(mcryptd_flist, smp_processor_id());
  48. mutex_lock(&flist->lock);
  49. list_add_tail(&cstate->flush_list, &flist->list);
  50. cstate->flusher_engaged = true;
  51. cstate->next_flush = jiffies + delay;
  52. queue_delayed_work_on(smp_processor_id(), kcrypto_wq,
  53. &cstate->flush, delay);
  54. mutex_unlock(&flist->lock);
  55. }
  56. }
  57. EXPORT_SYMBOL(mcryptd_arm_flusher);
  58. static int mcryptd_init_queue(struct mcryptd_queue *queue,
  59. unsigned int max_cpu_qlen)
  60. {
  61. int cpu;
  62. struct mcryptd_cpu_queue *cpu_queue;
  63. queue->cpu_queue = alloc_percpu(struct mcryptd_cpu_queue);
  64. pr_debug("mqueue:%p mcryptd_cpu_queue %p\n", queue, queue->cpu_queue);
  65. if (!queue->cpu_queue)
  66. return -ENOMEM;
  67. for_each_possible_cpu(cpu) {
  68. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  69. pr_debug("cpu_queue #%d %p\n", cpu, queue->cpu_queue);
  70. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  71. INIT_WORK(&cpu_queue->work, mcryptd_queue_worker);
  72. }
  73. return 0;
  74. }
  75. static void mcryptd_fini_queue(struct mcryptd_queue *queue)
  76. {
  77. int cpu;
  78. struct mcryptd_cpu_queue *cpu_queue;
  79. for_each_possible_cpu(cpu) {
  80. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  81. BUG_ON(cpu_queue->queue.qlen);
  82. }
  83. free_percpu(queue->cpu_queue);
  84. }
  85. static int mcryptd_enqueue_request(struct mcryptd_queue *queue,
  86. struct crypto_async_request *request,
  87. struct mcryptd_hash_request_ctx *rctx)
  88. {
  89. int cpu, err;
  90. struct mcryptd_cpu_queue *cpu_queue;
  91. cpu = get_cpu();
  92. cpu_queue = this_cpu_ptr(queue->cpu_queue);
  93. rctx->tag.cpu = cpu;
  94. err = crypto_enqueue_request(&cpu_queue->queue, request);
  95. pr_debug("enqueue request: cpu %d cpu_queue %p request %p\n",
  96. cpu, cpu_queue, request);
  97. queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
  98. put_cpu();
  99. return err;
  100. }
  101. /*
  102. * Try to opportunisticlly flush the partially completed jobs if
  103. * crypto daemon is the only task running.
  104. */
  105. static void mcryptd_opportunistic_flush(void)
  106. {
  107. struct mcryptd_flush_list *flist;
  108. struct mcryptd_alg_cstate *cstate;
  109. flist = per_cpu_ptr(mcryptd_flist, smp_processor_id());
  110. while (single_task_running()) {
  111. mutex_lock(&flist->lock);
  112. cstate = list_first_entry_or_null(&flist->list,
  113. struct mcryptd_alg_cstate, flush_list);
  114. if (!cstate || !cstate->flusher_engaged) {
  115. mutex_unlock(&flist->lock);
  116. return;
  117. }
  118. list_del(&cstate->flush_list);
  119. cstate->flusher_engaged = false;
  120. mutex_unlock(&flist->lock);
  121. cstate->alg_state->flusher(cstate);
  122. }
  123. }
  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. */
  129. static void mcryptd_queue_worker(struct work_struct *work)
  130. {
  131. struct mcryptd_cpu_queue *cpu_queue;
  132. struct crypto_async_request *req, *backlog;
  133. int i;
  134. /*
  135. * Need to loop through more than once for multi-buffer to
  136. * be effective.
  137. */
  138. cpu_queue = container_of(work, struct mcryptd_cpu_queue, work);
  139. i = 0;
  140. while (i < MCRYPTD_BATCH || single_task_running()) {
  141. /*
  142. * preempt_disable/enable is used to prevent
  143. * being preempted by mcryptd_enqueue_request()
  144. */
  145. local_bh_disable();
  146. preempt_disable();
  147. backlog = crypto_get_backlog(&cpu_queue->queue);
  148. req = crypto_dequeue_request(&cpu_queue->queue);
  149. preempt_enable();
  150. local_bh_enable();
  151. if (!req) {
  152. mcryptd_opportunistic_flush();
  153. return;
  154. }
  155. if (backlog)
  156. backlog->complete(backlog, -EINPROGRESS);
  157. req->complete(req, 0);
  158. if (!cpu_queue->queue.qlen)
  159. return;
  160. ++i;
  161. }
  162. if (cpu_queue->queue.qlen)
  163. queue_work(kcrypto_wq, &cpu_queue->work);
  164. }
  165. void mcryptd_flusher(struct work_struct *__work)
  166. {
  167. struct mcryptd_alg_cstate *alg_cpu_state;
  168. struct mcryptd_alg_state *alg_state;
  169. struct mcryptd_flush_list *flist;
  170. int cpu;
  171. cpu = smp_processor_id();
  172. alg_cpu_state = container_of(to_delayed_work(__work),
  173. struct mcryptd_alg_cstate, flush);
  174. alg_state = alg_cpu_state->alg_state;
  175. if (alg_cpu_state->cpu != cpu)
  176. pr_debug("mcryptd error: work on cpu %d, should be cpu %d\n",
  177. cpu, alg_cpu_state->cpu);
  178. if (alg_cpu_state->flusher_engaged) {
  179. flist = per_cpu_ptr(mcryptd_flist, cpu);
  180. mutex_lock(&flist->lock);
  181. list_del(&alg_cpu_state->flush_list);
  182. alg_cpu_state->flusher_engaged = false;
  183. mutex_unlock(&flist->lock);
  184. alg_state->flusher(alg_cpu_state);
  185. }
  186. }
  187. EXPORT_SYMBOL_GPL(mcryptd_flusher);
  188. static inline struct mcryptd_queue *mcryptd_get_queue(struct crypto_tfm *tfm)
  189. {
  190. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  191. struct mcryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  192. return ictx->queue;
  193. }
  194. static void *mcryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  195. unsigned int tail)
  196. {
  197. char *p;
  198. struct crypto_instance *inst;
  199. int err;
  200. p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
  201. if (!p)
  202. return ERR_PTR(-ENOMEM);
  203. inst = (void *)(p + head);
  204. err = -ENAMETOOLONG;
  205. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  206. "mcryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  207. goto out_free_inst;
  208. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  209. inst->alg.cra_priority = alg->cra_priority + 50;
  210. inst->alg.cra_blocksize = alg->cra_blocksize;
  211. inst->alg.cra_alignmask = alg->cra_alignmask;
  212. out:
  213. return p;
  214. out_free_inst:
  215. kfree(p);
  216. p = ERR_PTR(err);
  217. goto out;
  218. }
  219. static inline void mcryptd_check_internal(struct rtattr **tb, u32 *type,
  220. u32 *mask)
  221. {
  222. struct crypto_attr_type *algt;
  223. algt = crypto_get_attr_type(tb);
  224. if (IS_ERR(algt))
  225. return;
  226. if ((algt->type & CRYPTO_ALG_INTERNAL))
  227. *type |= CRYPTO_ALG_INTERNAL;
  228. if ((algt->mask & CRYPTO_ALG_INTERNAL))
  229. *mask |= CRYPTO_ALG_INTERNAL;
  230. }
  231. static int mcryptd_hash_init_tfm(struct crypto_tfm *tfm)
  232. {
  233. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  234. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  235. struct crypto_shash_spawn *spawn = &ictx->spawn;
  236. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  237. struct crypto_shash *hash;
  238. hash = crypto_spawn_shash(spawn);
  239. if (IS_ERR(hash))
  240. return PTR_ERR(hash);
  241. ctx->child = hash;
  242. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  243. sizeof(struct mcryptd_hash_request_ctx) +
  244. crypto_shash_descsize(hash));
  245. return 0;
  246. }
  247. static void mcryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  248. {
  249. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  250. crypto_free_shash(ctx->child);
  251. }
  252. static int mcryptd_hash_setkey(struct crypto_ahash *parent,
  253. const u8 *key, unsigned int keylen)
  254. {
  255. struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  256. struct crypto_shash *child = ctx->child;
  257. int err;
  258. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  259. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  260. CRYPTO_TFM_REQ_MASK);
  261. err = crypto_shash_setkey(child, key, keylen);
  262. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  263. CRYPTO_TFM_RES_MASK);
  264. return err;
  265. }
  266. static int mcryptd_hash_enqueue(struct ahash_request *req,
  267. crypto_completion_t complete)
  268. {
  269. int ret;
  270. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  271. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  272. struct mcryptd_queue *queue =
  273. mcryptd_get_queue(crypto_ahash_tfm(tfm));
  274. rctx->complete = req->base.complete;
  275. req->base.complete = complete;
  276. ret = mcryptd_enqueue_request(queue, &req->base, rctx);
  277. return ret;
  278. }
  279. static void mcryptd_hash_init(struct crypto_async_request *req_async, int err)
  280. {
  281. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  282. struct crypto_shash *child = ctx->child;
  283. struct ahash_request *req = ahash_request_cast(req_async);
  284. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  285. struct shash_desc *desc = &rctx->desc;
  286. if (unlikely(err == -EINPROGRESS))
  287. goto out;
  288. desc->tfm = child;
  289. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  290. err = crypto_shash_init(desc);
  291. req->base.complete = rctx->complete;
  292. out:
  293. local_bh_disable();
  294. rctx->complete(&req->base, err);
  295. local_bh_enable();
  296. }
  297. static int mcryptd_hash_init_enqueue(struct ahash_request *req)
  298. {
  299. return mcryptd_hash_enqueue(req, mcryptd_hash_init);
  300. }
  301. static void mcryptd_hash_update(struct crypto_async_request *req_async, int err)
  302. {
  303. struct ahash_request *req = ahash_request_cast(req_async);
  304. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  305. if (unlikely(err == -EINPROGRESS))
  306. goto out;
  307. err = shash_ahash_mcryptd_update(req, &rctx->desc);
  308. if (err) {
  309. req->base.complete = rctx->complete;
  310. goto out;
  311. }
  312. return;
  313. out:
  314. local_bh_disable();
  315. rctx->complete(&req->base, err);
  316. local_bh_enable();
  317. }
  318. static int mcryptd_hash_update_enqueue(struct ahash_request *req)
  319. {
  320. return mcryptd_hash_enqueue(req, mcryptd_hash_update);
  321. }
  322. static void mcryptd_hash_final(struct crypto_async_request *req_async, int err)
  323. {
  324. struct ahash_request *req = ahash_request_cast(req_async);
  325. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  326. if (unlikely(err == -EINPROGRESS))
  327. goto out;
  328. err = shash_ahash_mcryptd_final(req, &rctx->desc);
  329. if (err) {
  330. req->base.complete = rctx->complete;
  331. goto out;
  332. }
  333. return;
  334. out:
  335. local_bh_disable();
  336. rctx->complete(&req->base, err);
  337. local_bh_enable();
  338. }
  339. static int mcryptd_hash_final_enqueue(struct ahash_request *req)
  340. {
  341. return mcryptd_hash_enqueue(req, mcryptd_hash_final);
  342. }
  343. static void mcryptd_hash_finup(struct crypto_async_request *req_async, int err)
  344. {
  345. struct ahash_request *req = ahash_request_cast(req_async);
  346. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  347. if (unlikely(err == -EINPROGRESS))
  348. goto out;
  349. err = shash_ahash_mcryptd_finup(req, &rctx->desc);
  350. if (err) {
  351. req->base.complete = rctx->complete;
  352. goto out;
  353. }
  354. return;
  355. out:
  356. local_bh_disable();
  357. rctx->complete(&req->base, err);
  358. local_bh_enable();
  359. }
  360. static int mcryptd_hash_finup_enqueue(struct ahash_request *req)
  361. {
  362. return mcryptd_hash_enqueue(req, mcryptd_hash_finup);
  363. }
  364. static void mcryptd_hash_digest(struct crypto_async_request *req_async, int err)
  365. {
  366. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  367. struct crypto_shash *child = ctx->child;
  368. struct ahash_request *req = ahash_request_cast(req_async);
  369. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  370. struct shash_desc *desc = &rctx->desc;
  371. if (unlikely(err == -EINPROGRESS))
  372. goto out;
  373. desc->tfm = child;
  374. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; /* check this again */
  375. err = shash_ahash_mcryptd_digest(req, desc);
  376. if (err) {
  377. req->base.complete = rctx->complete;
  378. goto out;
  379. }
  380. return;
  381. out:
  382. local_bh_disable();
  383. rctx->complete(&req->base, err);
  384. local_bh_enable();
  385. }
  386. static int mcryptd_hash_digest_enqueue(struct ahash_request *req)
  387. {
  388. return mcryptd_hash_enqueue(req, mcryptd_hash_digest);
  389. }
  390. static int mcryptd_hash_export(struct ahash_request *req, void *out)
  391. {
  392. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  393. return crypto_shash_export(&rctx->desc, out);
  394. }
  395. static int mcryptd_hash_import(struct ahash_request *req, const void *in)
  396. {
  397. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  398. return crypto_shash_import(&rctx->desc, in);
  399. }
  400. static int mcryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  401. struct mcryptd_queue *queue)
  402. {
  403. struct hashd_instance_ctx *ctx;
  404. struct ahash_instance *inst;
  405. struct shash_alg *salg;
  406. struct crypto_alg *alg;
  407. u32 type = 0;
  408. u32 mask = 0;
  409. int err;
  410. mcryptd_check_internal(tb, &type, &mask);
  411. salg = shash_attr_alg(tb[1], type, mask);
  412. if (IS_ERR(salg))
  413. return PTR_ERR(salg);
  414. alg = &salg->base;
  415. pr_debug("crypto: mcryptd hash alg: %s\n", alg->cra_name);
  416. inst = mcryptd_alloc_instance(alg, ahash_instance_headroom(),
  417. sizeof(*ctx));
  418. err = PTR_ERR(inst);
  419. if (IS_ERR(inst))
  420. goto out_put_alg;
  421. ctx = ahash_instance_ctx(inst);
  422. ctx->queue = queue;
  423. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  424. ahash_crypto_instance(inst));
  425. if (err)
  426. goto out_free_inst;
  427. type = CRYPTO_ALG_ASYNC;
  428. if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
  429. type |= CRYPTO_ALG_INTERNAL;
  430. inst->alg.halg.base.cra_flags = type;
  431. inst->alg.halg.digestsize = salg->digestsize;
  432. inst->alg.halg.base.cra_ctxsize = sizeof(struct mcryptd_hash_ctx);
  433. inst->alg.halg.base.cra_init = mcryptd_hash_init_tfm;
  434. inst->alg.halg.base.cra_exit = mcryptd_hash_exit_tfm;
  435. inst->alg.init = mcryptd_hash_init_enqueue;
  436. inst->alg.update = mcryptd_hash_update_enqueue;
  437. inst->alg.final = mcryptd_hash_final_enqueue;
  438. inst->alg.finup = mcryptd_hash_finup_enqueue;
  439. inst->alg.export = mcryptd_hash_export;
  440. inst->alg.import = mcryptd_hash_import;
  441. inst->alg.setkey = mcryptd_hash_setkey;
  442. inst->alg.digest = mcryptd_hash_digest_enqueue;
  443. err = ahash_register_instance(tmpl, inst);
  444. if (err) {
  445. crypto_drop_shash(&ctx->spawn);
  446. out_free_inst:
  447. kfree(inst);
  448. }
  449. out_put_alg:
  450. crypto_mod_put(alg);
  451. return err;
  452. }
  453. static struct mcryptd_queue mqueue;
  454. static int mcryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  455. {
  456. struct crypto_attr_type *algt;
  457. algt = crypto_get_attr_type(tb);
  458. if (IS_ERR(algt))
  459. return PTR_ERR(algt);
  460. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  461. case CRYPTO_ALG_TYPE_DIGEST:
  462. return mcryptd_create_hash(tmpl, tb, &mqueue);
  463. break;
  464. }
  465. return -EINVAL;
  466. }
  467. static void mcryptd_free(struct crypto_instance *inst)
  468. {
  469. struct mcryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  470. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  471. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  472. case CRYPTO_ALG_TYPE_AHASH:
  473. crypto_drop_shash(&hctx->spawn);
  474. kfree(ahash_instance(inst));
  475. return;
  476. default:
  477. crypto_drop_spawn(&ctx->spawn);
  478. kfree(inst);
  479. }
  480. }
  481. static struct crypto_template mcryptd_tmpl = {
  482. .name = "mcryptd",
  483. .create = mcryptd_create,
  484. .free = mcryptd_free,
  485. .module = THIS_MODULE,
  486. };
  487. struct mcryptd_ahash *mcryptd_alloc_ahash(const char *alg_name,
  488. u32 type, u32 mask)
  489. {
  490. char mcryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  491. struct crypto_ahash *tfm;
  492. if (snprintf(mcryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  493. "mcryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  494. return ERR_PTR(-EINVAL);
  495. tfm = crypto_alloc_ahash(mcryptd_alg_name, type, mask);
  496. if (IS_ERR(tfm))
  497. return ERR_CAST(tfm);
  498. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  499. crypto_free_ahash(tfm);
  500. return ERR_PTR(-EINVAL);
  501. }
  502. return __mcryptd_ahash_cast(tfm);
  503. }
  504. EXPORT_SYMBOL_GPL(mcryptd_alloc_ahash);
  505. int shash_ahash_mcryptd_digest(struct ahash_request *req,
  506. struct shash_desc *desc)
  507. {
  508. int err;
  509. err = crypto_shash_init(desc) ?:
  510. shash_ahash_mcryptd_finup(req, desc);
  511. return err;
  512. }
  513. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_digest);
  514. int shash_ahash_mcryptd_update(struct ahash_request *req,
  515. struct shash_desc *desc)
  516. {
  517. struct crypto_shash *tfm = desc->tfm;
  518. struct shash_alg *shash = crypto_shash_alg(tfm);
  519. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  520. return shash->update(desc, NULL, 0);
  521. }
  522. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_update);
  523. int shash_ahash_mcryptd_finup(struct ahash_request *req,
  524. struct shash_desc *desc)
  525. {
  526. struct crypto_shash *tfm = desc->tfm;
  527. struct shash_alg *shash = crypto_shash_alg(tfm);
  528. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  529. return shash->finup(desc, NULL, 0, req->result);
  530. }
  531. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_finup);
  532. int shash_ahash_mcryptd_final(struct ahash_request *req,
  533. struct shash_desc *desc)
  534. {
  535. struct crypto_shash *tfm = desc->tfm;
  536. struct shash_alg *shash = crypto_shash_alg(tfm);
  537. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  538. return shash->final(desc, req->result);
  539. }
  540. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_final);
  541. struct crypto_shash *mcryptd_ahash_child(struct mcryptd_ahash *tfm)
  542. {
  543. struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  544. return ctx->child;
  545. }
  546. EXPORT_SYMBOL_GPL(mcryptd_ahash_child);
  547. struct shash_desc *mcryptd_shash_desc(struct ahash_request *req)
  548. {
  549. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  550. return &rctx->desc;
  551. }
  552. EXPORT_SYMBOL_GPL(mcryptd_shash_desc);
  553. void mcryptd_free_ahash(struct mcryptd_ahash *tfm)
  554. {
  555. crypto_free_ahash(&tfm->base);
  556. }
  557. EXPORT_SYMBOL_GPL(mcryptd_free_ahash);
  558. static int __init mcryptd_init(void)
  559. {
  560. int err, cpu;
  561. struct mcryptd_flush_list *flist;
  562. mcryptd_flist = alloc_percpu(struct mcryptd_flush_list);
  563. for_each_possible_cpu(cpu) {
  564. flist = per_cpu_ptr(mcryptd_flist, cpu);
  565. INIT_LIST_HEAD(&flist->list);
  566. mutex_init(&flist->lock);
  567. }
  568. err = mcryptd_init_queue(&mqueue, MCRYPTD_MAX_CPU_QLEN);
  569. if (err) {
  570. free_percpu(mcryptd_flist);
  571. return err;
  572. }
  573. err = crypto_register_template(&mcryptd_tmpl);
  574. if (err) {
  575. mcryptd_fini_queue(&mqueue);
  576. free_percpu(mcryptd_flist);
  577. }
  578. return err;
  579. }
  580. static void __exit mcryptd_exit(void)
  581. {
  582. mcryptd_fini_queue(&mqueue);
  583. crypto_unregister_template(&mcryptd_tmpl);
  584. free_percpu(mcryptd_flist);
  585. }
  586. subsys_initcall(mcryptd_init);
  587. module_exit(mcryptd_exit);
  588. MODULE_LICENSE("GPL");
  589. MODULE_DESCRIPTION("Software async multibuffer crypto daemon");
  590. MODULE_ALIAS_CRYPTO("mcryptd");