mcryptd.c 18 KB

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