mcryptd.c 18 KB

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