mcryptd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. 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. if (list_empty(&flist->list)) {
  115. mutex_unlock(&flist->lock);
  116. return;
  117. }
  118. cstate = list_entry(flist->list.next,
  119. struct mcryptd_alg_cstate, flush_list);
  120. if (!cstate->flusher_engaged) {
  121. mutex_unlock(&flist->lock);
  122. return;
  123. }
  124. list_del(&cstate->flush_list);
  125. cstate->flusher_engaged = false;
  126. mutex_unlock(&flist->lock);
  127. cstate->alg_state->flusher(cstate);
  128. }
  129. }
  130. /*
  131. * Called in workqueue context, do one real cryption work (via
  132. * req->complete) and reschedule itself if there are more work to
  133. * do.
  134. */
  135. static void mcryptd_queue_worker(struct work_struct *work)
  136. {
  137. struct mcryptd_cpu_queue *cpu_queue;
  138. struct crypto_async_request *req, *backlog;
  139. int i;
  140. /*
  141. * Need to loop through more than once for multi-buffer to
  142. * be effective.
  143. */
  144. cpu_queue = container_of(work, struct mcryptd_cpu_queue, work);
  145. i = 0;
  146. while (i < MCRYPTD_BATCH || single_task_running()) {
  147. spin_lock_bh(&cpu_queue->q_lock);
  148. backlog = crypto_get_backlog(&cpu_queue->queue);
  149. req = crypto_dequeue_request(&cpu_queue->queue);
  150. spin_unlock_bh(&cpu_queue->q_lock);
  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_on(smp_processor_id(), 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 bool 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 false;
  226. *type |= algt->type & CRYPTO_ALG_INTERNAL;
  227. *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
  228. if (*type & *mask & CRYPTO_ALG_INTERNAL)
  229. return true;
  230. else
  231. return false;
  232. }
  233. static int mcryptd_hash_init_tfm(struct crypto_tfm *tfm)
  234. {
  235. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  236. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  237. struct crypto_shash_spawn *spawn = &ictx->spawn;
  238. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  239. struct crypto_shash *hash;
  240. hash = crypto_spawn_shash(spawn);
  241. if (IS_ERR(hash))
  242. return PTR_ERR(hash);
  243. ctx->child = hash;
  244. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  245. sizeof(struct mcryptd_hash_request_ctx) +
  246. crypto_shash_descsize(hash));
  247. return 0;
  248. }
  249. static void mcryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  250. {
  251. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  252. crypto_free_shash(ctx->child);
  253. }
  254. static int mcryptd_hash_setkey(struct crypto_ahash *parent,
  255. const u8 *key, unsigned int keylen)
  256. {
  257. struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  258. struct crypto_shash *child = ctx->child;
  259. int err;
  260. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  261. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  262. CRYPTO_TFM_REQ_MASK);
  263. err = crypto_shash_setkey(child, key, keylen);
  264. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  265. CRYPTO_TFM_RES_MASK);
  266. return err;
  267. }
  268. static int mcryptd_hash_enqueue(struct ahash_request *req,
  269. crypto_completion_t complete)
  270. {
  271. int ret;
  272. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  273. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  274. struct mcryptd_queue *queue =
  275. mcryptd_get_queue(crypto_ahash_tfm(tfm));
  276. rctx->complete = req->base.complete;
  277. req->base.complete = complete;
  278. ret = mcryptd_enqueue_request(queue, &req->base, rctx);
  279. return ret;
  280. }
  281. static void mcryptd_hash_init(struct crypto_async_request *req_async, int err)
  282. {
  283. struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  284. struct crypto_shash *child = ctx->child;
  285. struct ahash_request *req = ahash_request_cast(req_async);
  286. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  287. struct shash_desc *desc = &rctx->desc;
  288. if (unlikely(err == -EINPROGRESS))
  289. goto out;
  290. desc->tfm = child;
  291. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  292. err = crypto_shash_init(desc);
  293. req->base.complete = rctx->complete;
  294. out:
  295. local_bh_disable();
  296. rctx->complete(&req->base, err);
  297. local_bh_enable();
  298. }
  299. static int mcryptd_hash_init_enqueue(struct ahash_request *req)
  300. {
  301. return mcryptd_hash_enqueue(req, mcryptd_hash_init);
  302. }
  303. static void mcryptd_hash_update(struct crypto_async_request *req_async, int err)
  304. {
  305. struct ahash_request *req = ahash_request_cast(req_async);
  306. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  307. if (unlikely(err == -EINPROGRESS))
  308. goto out;
  309. err = shash_ahash_mcryptd_update(req, &rctx->desc);
  310. if (err) {
  311. req->base.complete = rctx->complete;
  312. goto out;
  313. }
  314. return;
  315. out:
  316. local_bh_disable();
  317. rctx->complete(&req->base, err);
  318. local_bh_enable();
  319. }
  320. static int mcryptd_hash_update_enqueue(struct ahash_request *req)
  321. {
  322. return mcryptd_hash_enqueue(req, mcryptd_hash_update);
  323. }
  324. static void mcryptd_hash_final(struct crypto_async_request *req_async, int err)
  325. {
  326. struct ahash_request *req = ahash_request_cast(req_async);
  327. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  328. if (unlikely(err == -EINPROGRESS))
  329. goto out;
  330. err = shash_ahash_mcryptd_final(req, &rctx->desc);
  331. if (err) {
  332. req->base.complete = rctx->complete;
  333. goto out;
  334. }
  335. return;
  336. out:
  337. local_bh_disable();
  338. rctx->complete(&req->base, err);
  339. local_bh_enable();
  340. }
  341. static int mcryptd_hash_final_enqueue(struct ahash_request *req)
  342. {
  343. return mcryptd_hash_enqueue(req, mcryptd_hash_final);
  344. }
  345. static void mcryptd_hash_finup(struct crypto_async_request *req_async, int err)
  346. {
  347. struct ahash_request *req = ahash_request_cast(req_async);
  348. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  349. if (unlikely(err == -EINPROGRESS))
  350. goto out;
  351. err = shash_ahash_mcryptd_finup(req, &rctx->desc);
  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_shash *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 shash_desc *desc = &rctx->desc;
  373. if (unlikely(err == -EINPROGRESS))
  374. goto out;
  375. desc->tfm = child;
  376. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; /* check this again */
  377. err = shash_ahash_mcryptd_digest(req, desc);
  378. if (err) {
  379. req->base.complete = rctx->complete;
  380. goto out;
  381. }
  382. return;
  383. out:
  384. local_bh_disable();
  385. rctx->complete(&req->base, err);
  386. local_bh_enable();
  387. }
  388. static int mcryptd_hash_digest_enqueue(struct ahash_request *req)
  389. {
  390. return mcryptd_hash_enqueue(req, mcryptd_hash_digest);
  391. }
  392. static int mcryptd_hash_export(struct ahash_request *req, void *out)
  393. {
  394. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  395. return crypto_shash_export(&rctx->desc, out);
  396. }
  397. static int mcryptd_hash_import(struct ahash_request *req, const void *in)
  398. {
  399. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  400. return crypto_shash_import(&rctx->desc, in);
  401. }
  402. static int mcryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  403. struct mcryptd_queue *queue)
  404. {
  405. struct hashd_instance_ctx *ctx;
  406. struct ahash_instance *inst;
  407. struct shash_alg *salg;
  408. struct crypto_alg *alg;
  409. u32 type = 0;
  410. u32 mask = 0;
  411. int err;
  412. if (!mcryptd_check_internal(tb, &type, &mask))
  413. return -EINVAL;
  414. salg = shash_attr_alg(tb[1], type, mask);
  415. if (IS_ERR(salg))
  416. return PTR_ERR(salg);
  417. alg = &salg->base;
  418. pr_debug("crypto: mcryptd hash alg: %s\n", alg->cra_name);
  419. inst = mcryptd_alloc_instance(alg, ahash_instance_headroom(),
  420. sizeof(*ctx));
  421. err = PTR_ERR(inst);
  422. if (IS_ERR(inst))
  423. goto out_put_alg;
  424. ctx = ahash_instance_ctx(inst);
  425. ctx->queue = queue;
  426. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  427. ahash_crypto_instance(inst));
  428. if (err)
  429. goto out_free_inst;
  430. type = CRYPTO_ALG_ASYNC;
  431. if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
  432. type |= CRYPTO_ALG_INTERNAL;
  433. inst->alg.halg.base.cra_flags = type;
  434. inst->alg.halg.digestsize = salg->digestsize;
  435. inst->alg.halg.statesize = salg->statesize;
  436. inst->alg.halg.base.cra_ctxsize = sizeof(struct mcryptd_hash_ctx);
  437. inst->alg.halg.base.cra_init = mcryptd_hash_init_tfm;
  438. inst->alg.halg.base.cra_exit = mcryptd_hash_exit_tfm;
  439. inst->alg.init = mcryptd_hash_init_enqueue;
  440. inst->alg.update = mcryptd_hash_update_enqueue;
  441. inst->alg.final = mcryptd_hash_final_enqueue;
  442. inst->alg.finup = mcryptd_hash_finup_enqueue;
  443. inst->alg.export = mcryptd_hash_export;
  444. inst->alg.import = mcryptd_hash_import;
  445. inst->alg.setkey = mcryptd_hash_setkey;
  446. inst->alg.digest = mcryptd_hash_digest_enqueue;
  447. err = ahash_register_instance(tmpl, inst);
  448. if (err) {
  449. crypto_drop_shash(&ctx->spawn);
  450. out_free_inst:
  451. kfree(inst);
  452. }
  453. out_put_alg:
  454. crypto_mod_put(alg);
  455. return err;
  456. }
  457. static struct mcryptd_queue mqueue;
  458. static int mcryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  459. {
  460. struct crypto_attr_type *algt;
  461. algt = crypto_get_attr_type(tb);
  462. if (IS_ERR(algt))
  463. return PTR_ERR(algt);
  464. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  465. case CRYPTO_ALG_TYPE_DIGEST:
  466. return mcryptd_create_hash(tmpl, tb, &mqueue);
  467. break;
  468. }
  469. return -EINVAL;
  470. }
  471. static void mcryptd_free(struct crypto_instance *inst)
  472. {
  473. struct mcryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  474. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  475. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  476. case CRYPTO_ALG_TYPE_AHASH:
  477. crypto_drop_shash(&hctx->spawn);
  478. kfree(ahash_instance(inst));
  479. return;
  480. default:
  481. crypto_drop_spawn(&ctx->spawn);
  482. kfree(inst);
  483. }
  484. }
  485. static struct crypto_template mcryptd_tmpl = {
  486. .name = "mcryptd",
  487. .create = mcryptd_create,
  488. .free = mcryptd_free,
  489. .module = THIS_MODULE,
  490. };
  491. struct mcryptd_ahash *mcryptd_alloc_ahash(const char *alg_name,
  492. u32 type, u32 mask)
  493. {
  494. char mcryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  495. struct crypto_ahash *tfm;
  496. if (snprintf(mcryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  497. "mcryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  498. return ERR_PTR(-EINVAL);
  499. tfm = crypto_alloc_ahash(mcryptd_alg_name, type, mask);
  500. if (IS_ERR(tfm))
  501. return ERR_CAST(tfm);
  502. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  503. crypto_free_ahash(tfm);
  504. return ERR_PTR(-EINVAL);
  505. }
  506. return __mcryptd_ahash_cast(tfm);
  507. }
  508. EXPORT_SYMBOL_GPL(mcryptd_alloc_ahash);
  509. int shash_ahash_mcryptd_digest(struct ahash_request *req,
  510. struct shash_desc *desc)
  511. {
  512. int err;
  513. err = crypto_shash_init(desc) ?:
  514. shash_ahash_mcryptd_finup(req, desc);
  515. return err;
  516. }
  517. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_digest);
  518. int shash_ahash_mcryptd_update(struct ahash_request *req,
  519. struct shash_desc *desc)
  520. {
  521. struct crypto_shash *tfm = desc->tfm;
  522. struct shash_alg *shash = crypto_shash_alg(tfm);
  523. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  524. return shash->update(desc, NULL, 0);
  525. }
  526. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_update);
  527. int shash_ahash_mcryptd_finup(struct ahash_request *req,
  528. struct shash_desc *desc)
  529. {
  530. struct crypto_shash *tfm = desc->tfm;
  531. struct shash_alg *shash = crypto_shash_alg(tfm);
  532. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  533. return shash->finup(desc, NULL, 0, req->result);
  534. }
  535. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_finup);
  536. int shash_ahash_mcryptd_final(struct ahash_request *req,
  537. struct shash_desc *desc)
  538. {
  539. struct crypto_shash *tfm = desc->tfm;
  540. struct shash_alg *shash = crypto_shash_alg(tfm);
  541. /* alignment is to be done by multi-buffer crypto algorithm if needed */
  542. return shash->final(desc, req->result);
  543. }
  544. EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_final);
  545. struct crypto_shash *mcryptd_ahash_child(struct mcryptd_ahash *tfm)
  546. {
  547. struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  548. return ctx->child;
  549. }
  550. EXPORT_SYMBOL_GPL(mcryptd_ahash_child);
  551. struct shash_desc *mcryptd_shash_desc(struct ahash_request *req)
  552. {
  553. struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  554. return &rctx->desc;
  555. }
  556. EXPORT_SYMBOL_GPL(mcryptd_shash_desc);
  557. void mcryptd_free_ahash(struct mcryptd_ahash *tfm)
  558. {
  559. crypto_free_ahash(&tfm->base);
  560. }
  561. EXPORT_SYMBOL_GPL(mcryptd_free_ahash);
  562. static int __init mcryptd_init(void)
  563. {
  564. int err, cpu;
  565. struct mcryptd_flush_list *flist;
  566. mcryptd_flist = alloc_percpu(struct mcryptd_flush_list);
  567. for_each_possible_cpu(cpu) {
  568. flist = per_cpu_ptr(mcryptd_flist, cpu);
  569. INIT_LIST_HEAD(&flist->list);
  570. mutex_init(&flist->lock);
  571. }
  572. err = mcryptd_init_queue(&mqueue, MCRYPTD_MAX_CPU_QLEN);
  573. if (err) {
  574. free_percpu(mcryptd_flist);
  575. return err;
  576. }
  577. err = crypto_register_template(&mcryptd_tmpl);
  578. if (err) {
  579. mcryptd_fini_queue(&mqueue);
  580. free_percpu(mcryptd_flist);
  581. }
  582. return err;
  583. }
  584. static void __exit mcryptd_exit(void)
  585. {
  586. mcryptd_fini_queue(&mqueue);
  587. crypto_unregister_template(&mcryptd_tmpl);
  588. free_percpu(mcryptd_flist);
  589. }
  590. subsys_initcall(mcryptd_init);
  591. module_exit(mcryptd_exit);
  592. MODULE_LICENSE("GPL");
  593. MODULE_DESCRIPTION("Software async multibuffer crypto daemon");
  594. MODULE_ALIAS_CRYPTO("mcryptd");