cryptd.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Software async crypto daemon.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * Added AEAD support to cryptd.
  7. * Authors: Tadeusz Struk (tadeusz.struk@intel.com)
  8. * Adrian Hoban <adrian.hoban@intel.com>
  9. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  10. * Aidan O'Mahony (aidan.o.mahony@intel.com)
  11. * Copyright (c) 2010, Intel Corporation.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. */
  19. #include <crypto/algapi.h>
  20. #include <crypto/internal/hash.h>
  21. #include <crypto/internal/aead.h>
  22. #include <crypto/cryptd.h>
  23. #include <crypto/crypto_wq.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #define CRYPTD_MAX_CPU_QLEN 100
  33. struct cryptd_cpu_queue {
  34. struct crypto_queue queue;
  35. struct work_struct work;
  36. };
  37. struct cryptd_queue {
  38. struct cryptd_cpu_queue __percpu *cpu_queue;
  39. };
  40. struct cryptd_instance_ctx {
  41. struct crypto_spawn spawn;
  42. struct cryptd_queue *queue;
  43. };
  44. struct hashd_instance_ctx {
  45. struct crypto_shash_spawn spawn;
  46. struct cryptd_queue *queue;
  47. };
  48. struct aead_instance_ctx {
  49. struct crypto_aead_spawn aead_spawn;
  50. struct cryptd_queue *queue;
  51. };
  52. struct cryptd_blkcipher_ctx {
  53. struct crypto_blkcipher *child;
  54. };
  55. struct cryptd_blkcipher_request_ctx {
  56. crypto_completion_t complete;
  57. };
  58. struct cryptd_hash_ctx {
  59. struct crypto_shash *child;
  60. };
  61. struct cryptd_hash_request_ctx {
  62. crypto_completion_t complete;
  63. struct shash_desc desc;
  64. };
  65. struct cryptd_aead_ctx {
  66. struct crypto_aead *child;
  67. };
  68. struct cryptd_aead_request_ctx {
  69. crypto_completion_t complete;
  70. };
  71. static void cryptd_queue_worker(struct work_struct *work);
  72. static int cryptd_init_queue(struct cryptd_queue *queue,
  73. unsigned int max_cpu_qlen)
  74. {
  75. int cpu;
  76. struct cryptd_cpu_queue *cpu_queue;
  77. queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
  78. if (!queue->cpu_queue)
  79. return -ENOMEM;
  80. for_each_possible_cpu(cpu) {
  81. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  82. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  83. INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
  84. }
  85. return 0;
  86. }
  87. static void cryptd_fini_queue(struct cryptd_queue *queue)
  88. {
  89. int cpu;
  90. struct cryptd_cpu_queue *cpu_queue;
  91. for_each_possible_cpu(cpu) {
  92. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  93. BUG_ON(cpu_queue->queue.qlen);
  94. }
  95. free_percpu(queue->cpu_queue);
  96. }
  97. static int cryptd_enqueue_request(struct cryptd_queue *queue,
  98. struct crypto_async_request *request)
  99. {
  100. int cpu, err;
  101. struct cryptd_cpu_queue *cpu_queue;
  102. cpu = get_cpu();
  103. cpu_queue = this_cpu_ptr(queue->cpu_queue);
  104. err = crypto_enqueue_request(&cpu_queue->queue, request);
  105. queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
  106. put_cpu();
  107. return err;
  108. }
  109. /* Called in workqueue context, do one real cryption work (via
  110. * req->complete) and reschedule itself if there are more work to
  111. * do. */
  112. static void cryptd_queue_worker(struct work_struct *work)
  113. {
  114. struct cryptd_cpu_queue *cpu_queue;
  115. struct crypto_async_request *req, *backlog;
  116. cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
  117. /*
  118. * Only handle one request at a time to avoid hogging crypto workqueue.
  119. * preempt_disable/enable is used to prevent being preempted by
  120. * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
  121. * cryptd_enqueue_request() being accessed from software interrupts.
  122. */
  123. local_bh_disable();
  124. preempt_disable();
  125. backlog = crypto_get_backlog(&cpu_queue->queue);
  126. req = crypto_dequeue_request(&cpu_queue->queue);
  127. preempt_enable();
  128. local_bh_enable();
  129. if (!req)
  130. return;
  131. if (backlog)
  132. backlog->complete(backlog, -EINPROGRESS);
  133. req->complete(req, 0);
  134. if (cpu_queue->queue.qlen)
  135. queue_work(kcrypto_wq, &cpu_queue->work);
  136. }
  137. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  138. {
  139. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  140. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  141. return ictx->queue;
  142. }
  143. static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
  144. u32 *mask)
  145. {
  146. struct crypto_attr_type *algt;
  147. algt = crypto_get_attr_type(tb);
  148. if (IS_ERR(algt))
  149. return;
  150. *type |= algt->type & CRYPTO_ALG_INTERNAL;
  151. *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
  152. }
  153. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  154. const u8 *key, unsigned int keylen)
  155. {
  156. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  157. struct crypto_blkcipher *child = ctx->child;
  158. int err;
  159. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  160. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  161. CRYPTO_TFM_REQ_MASK);
  162. err = crypto_blkcipher_setkey(child, key, keylen);
  163. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  164. CRYPTO_TFM_RES_MASK);
  165. return err;
  166. }
  167. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  168. struct crypto_blkcipher *child,
  169. int err,
  170. int (*crypt)(struct blkcipher_desc *desc,
  171. struct scatterlist *dst,
  172. struct scatterlist *src,
  173. unsigned int len))
  174. {
  175. struct cryptd_blkcipher_request_ctx *rctx;
  176. struct blkcipher_desc desc;
  177. rctx = ablkcipher_request_ctx(req);
  178. if (unlikely(err == -EINPROGRESS))
  179. goto out;
  180. desc.tfm = child;
  181. desc.info = req->info;
  182. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  183. err = crypt(&desc, req->dst, req->src, req->nbytes);
  184. req->base.complete = rctx->complete;
  185. out:
  186. local_bh_disable();
  187. rctx->complete(&req->base, err);
  188. local_bh_enable();
  189. }
  190. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  191. {
  192. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  193. struct crypto_blkcipher *child = ctx->child;
  194. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  195. crypto_blkcipher_crt(child)->encrypt);
  196. }
  197. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  198. {
  199. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  200. struct crypto_blkcipher *child = ctx->child;
  201. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  202. crypto_blkcipher_crt(child)->decrypt);
  203. }
  204. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  205. crypto_completion_t compl)
  206. {
  207. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  208. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  209. struct cryptd_queue *queue;
  210. queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
  211. rctx->complete = req->base.complete;
  212. req->base.complete = compl;
  213. return cryptd_enqueue_request(queue, &req->base);
  214. }
  215. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  216. {
  217. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  218. }
  219. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  220. {
  221. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  222. }
  223. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  224. {
  225. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  226. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  227. struct crypto_spawn *spawn = &ictx->spawn;
  228. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  229. struct crypto_blkcipher *cipher;
  230. cipher = crypto_spawn_blkcipher(spawn);
  231. if (IS_ERR(cipher))
  232. return PTR_ERR(cipher);
  233. ctx->child = cipher;
  234. tfm->crt_ablkcipher.reqsize =
  235. sizeof(struct cryptd_blkcipher_request_ctx);
  236. return 0;
  237. }
  238. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  239. {
  240. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  241. crypto_free_blkcipher(ctx->child);
  242. }
  243. static int cryptd_init_instance(struct crypto_instance *inst,
  244. struct crypto_alg *alg)
  245. {
  246. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  247. "cryptd(%s)",
  248. alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  249. return -ENAMETOOLONG;
  250. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  251. inst->alg.cra_priority = alg->cra_priority + 50;
  252. inst->alg.cra_blocksize = alg->cra_blocksize;
  253. inst->alg.cra_alignmask = alg->cra_alignmask;
  254. return 0;
  255. }
  256. static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  257. unsigned int tail)
  258. {
  259. char *p;
  260. struct crypto_instance *inst;
  261. int err;
  262. p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
  263. if (!p)
  264. return ERR_PTR(-ENOMEM);
  265. inst = (void *)(p + head);
  266. err = cryptd_init_instance(inst, alg);
  267. if (err)
  268. goto out_free_inst;
  269. out:
  270. return p;
  271. out_free_inst:
  272. kfree(p);
  273. p = ERR_PTR(err);
  274. goto out;
  275. }
  276. static int cryptd_create_blkcipher(struct crypto_template *tmpl,
  277. struct rtattr **tb,
  278. struct cryptd_queue *queue)
  279. {
  280. struct cryptd_instance_ctx *ctx;
  281. struct crypto_instance *inst;
  282. struct crypto_alg *alg;
  283. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  284. u32 mask = CRYPTO_ALG_TYPE_MASK;
  285. int err;
  286. cryptd_check_internal(tb, &type, &mask);
  287. alg = crypto_get_attr_alg(tb, type, mask);
  288. if (IS_ERR(alg))
  289. return PTR_ERR(alg);
  290. inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
  291. err = PTR_ERR(inst);
  292. if (IS_ERR(inst))
  293. goto out_put_alg;
  294. ctx = crypto_instance_ctx(inst);
  295. ctx->queue = queue;
  296. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  297. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  298. if (err)
  299. goto out_free_inst;
  300. type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  301. if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
  302. type |= CRYPTO_ALG_INTERNAL;
  303. inst->alg.cra_flags = type;
  304. inst->alg.cra_type = &crypto_ablkcipher_type;
  305. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  306. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  307. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  308. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  309. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  310. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  311. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  312. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  313. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  314. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  315. err = crypto_register_instance(tmpl, inst);
  316. if (err) {
  317. crypto_drop_spawn(&ctx->spawn);
  318. out_free_inst:
  319. kfree(inst);
  320. }
  321. out_put_alg:
  322. crypto_mod_put(alg);
  323. return err;
  324. }
  325. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  326. {
  327. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  328. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  329. struct crypto_shash_spawn *spawn = &ictx->spawn;
  330. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  331. struct crypto_shash *hash;
  332. hash = crypto_spawn_shash(spawn);
  333. if (IS_ERR(hash))
  334. return PTR_ERR(hash);
  335. ctx->child = hash;
  336. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  337. sizeof(struct cryptd_hash_request_ctx) +
  338. crypto_shash_descsize(hash));
  339. return 0;
  340. }
  341. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  342. {
  343. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  344. crypto_free_shash(ctx->child);
  345. }
  346. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  347. const u8 *key, unsigned int keylen)
  348. {
  349. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  350. struct crypto_shash *child = ctx->child;
  351. int err;
  352. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  353. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  354. CRYPTO_TFM_REQ_MASK);
  355. err = crypto_shash_setkey(child, key, keylen);
  356. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  357. CRYPTO_TFM_RES_MASK);
  358. return err;
  359. }
  360. static int cryptd_hash_enqueue(struct ahash_request *req,
  361. crypto_completion_t compl)
  362. {
  363. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  364. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  365. struct cryptd_queue *queue =
  366. cryptd_get_queue(crypto_ahash_tfm(tfm));
  367. rctx->complete = req->base.complete;
  368. req->base.complete = compl;
  369. return cryptd_enqueue_request(queue, &req->base);
  370. }
  371. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  372. {
  373. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  374. struct crypto_shash *child = ctx->child;
  375. struct ahash_request *req = ahash_request_cast(req_async);
  376. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  377. struct shash_desc *desc = &rctx->desc;
  378. if (unlikely(err == -EINPROGRESS))
  379. goto out;
  380. desc->tfm = child;
  381. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  382. err = crypto_shash_init(desc);
  383. req->base.complete = rctx->complete;
  384. out:
  385. local_bh_disable();
  386. rctx->complete(&req->base, err);
  387. local_bh_enable();
  388. }
  389. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  390. {
  391. return cryptd_hash_enqueue(req, cryptd_hash_init);
  392. }
  393. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  394. {
  395. struct ahash_request *req = ahash_request_cast(req_async);
  396. struct cryptd_hash_request_ctx *rctx;
  397. rctx = ahash_request_ctx(req);
  398. if (unlikely(err == -EINPROGRESS))
  399. goto out;
  400. err = shash_ahash_update(req, &rctx->desc);
  401. req->base.complete = rctx->complete;
  402. out:
  403. local_bh_disable();
  404. rctx->complete(&req->base, err);
  405. local_bh_enable();
  406. }
  407. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  408. {
  409. return cryptd_hash_enqueue(req, cryptd_hash_update);
  410. }
  411. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  412. {
  413. struct ahash_request *req = ahash_request_cast(req_async);
  414. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  415. if (unlikely(err == -EINPROGRESS))
  416. goto out;
  417. err = crypto_shash_final(&rctx->desc, req->result);
  418. req->base.complete = rctx->complete;
  419. out:
  420. local_bh_disable();
  421. rctx->complete(&req->base, err);
  422. local_bh_enable();
  423. }
  424. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  425. {
  426. return cryptd_hash_enqueue(req, cryptd_hash_final);
  427. }
  428. static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
  429. {
  430. struct ahash_request *req = ahash_request_cast(req_async);
  431. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  432. if (unlikely(err == -EINPROGRESS))
  433. goto out;
  434. err = shash_ahash_finup(req, &rctx->desc);
  435. req->base.complete = rctx->complete;
  436. out:
  437. local_bh_disable();
  438. rctx->complete(&req->base, err);
  439. local_bh_enable();
  440. }
  441. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  442. {
  443. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  444. }
  445. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  446. {
  447. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  448. struct crypto_shash *child = ctx->child;
  449. struct ahash_request *req = ahash_request_cast(req_async);
  450. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  451. struct shash_desc *desc = &rctx->desc;
  452. if (unlikely(err == -EINPROGRESS))
  453. goto out;
  454. desc->tfm = child;
  455. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  456. err = shash_ahash_digest(req, desc);
  457. req->base.complete = rctx->complete;
  458. out:
  459. local_bh_disable();
  460. rctx->complete(&req->base, err);
  461. local_bh_enable();
  462. }
  463. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  464. {
  465. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  466. }
  467. static int cryptd_hash_export(struct ahash_request *req, void *out)
  468. {
  469. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  470. return crypto_shash_export(&rctx->desc, out);
  471. }
  472. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  473. {
  474. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  475. return crypto_shash_import(&rctx->desc, in);
  476. }
  477. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  478. struct cryptd_queue *queue)
  479. {
  480. struct hashd_instance_ctx *ctx;
  481. struct ahash_instance *inst;
  482. struct shash_alg *salg;
  483. struct crypto_alg *alg;
  484. u32 type = 0;
  485. u32 mask = 0;
  486. int err;
  487. cryptd_check_internal(tb, &type, &mask);
  488. salg = shash_attr_alg(tb[1], type, mask);
  489. if (IS_ERR(salg))
  490. return PTR_ERR(salg);
  491. alg = &salg->base;
  492. inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
  493. sizeof(*ctx));
  494. err = PTR_ERR(inst);
  495. if (IS_ERR(inst))
  496. goto out_put_alg;
  497. ctx = ahash_instance_ctx(inst);
  498. ctx->queue = queue;
  499. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  500. ahash_crypto_instance(inst));
  501. if (err)
  502. goto out_free_inst;
  503. type = CRYPTO_ALG_ASYNC;
  504. if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
  505. type |= CRYPTO_ALG_INTERNAL;
  506. inst->alg.halg.base.cra_flags = type;
  507. inst->alg.halg.digestsize = salg->digestsize;
  508. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  509. inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
  510. inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
  511. inst->alg.init = cryptd_hash_init_enqueue;
  512. inst->alg.update = cryptd_hash_update_enqueue;
  513. inst->alg.final = cryptd_hash_final_enqueue;
  514. inst->alg.finup = cryptd_hash_finup_enqueue;
  515. inst->alg.export = cryptd_hash_export;
  516. inst->alg.import = cryptd_hash_import;
  517. inst->alg.setkey = cryptd_hash_setkey;
  518. inst->alg.digest = cryptd_hash_digest_enqueue;
  519. err = ahash_register_instance(tmpl, inst);
  520. if (err) {
  521. crypto_drop_shash(&ctx->spawn);
  522. out_free_inst:
  523. kfree(inst);
  524. }
  525. out_put_alg:
  526. crypto_mod_put(alg);
  527. return err;
  528. }
  529. static int cryptd_aead_setkey(struct crypto_aead *parent,
  530. const u8 *key, unsigned int keylen)
  531. {
  532. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  533. struct crypto_aead *child = ctx->child;
  534. return crypto_aead_setkey(child, key, keylen);
  535. }
  536. static int cryptd_aead_setauthsize(struct crypto_aead *parent,
  537. unsigned int authsize)
  538. {
  539. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
  540. struct crypto_aead *child = ctx->child;
  541. return crypto_aead_setauthsize(child, authsize);
  542. }
  543. static void cryptd_aead_crypt(struct aead_request *req,
  544. struct crypto_aead *child,
  545. int err,
  546. int (*crypt)(struct aead_request *req))
  547. {
  548. struct cryptd_aead_request_ctx *rctx;
  549. crypto_completion_t compl;
  550. rctx = aead_request_ctx(req);
  551. compl = rctx->complete;
  552. if (unlikely(err == -EINPROGRESS))
  553. goto out;
  554. aead_request_set_tfm(req, child);
  555. err = crypt( req );
  556. out:
  557. local_bh_disable();
  558. compl(&req->base, err);
  559. local_bh_enable();
  560. }
  561. static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
  562. {
  563. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  564. struct crypto_aead *child = ctx->child;
  565. struct aead_request *req;
  566. req = container_of(areq, struct aead_request, base);
  567. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
  568. }
  569. static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
  570. {
  571. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  572. struct crypto_aead *child = ctx->child;
  573. struct aead_request *req;
  574. req = container_of(areq, struct aead_request, base);
  575. cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
  576. }
  577. static int cryptd_aead_enqueue(struct aead_request *req,
  578. crypto_completion_t compl)
  579. {
  580. struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
  581. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  582. struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
  583. rctx->complete = req->base.complete;
  584. req->base.complete = compl;
  585. return cryptd_enqueue_request(queue, &req->base);
  586. }
  587. static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
  588. {
  589. return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
  590. }
  591. static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
  592. {
  593. return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
  594. }
  595. static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
  596. {
  597. struct aead_instance *inst = aead_alg_instance(tfm);
  598. struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
  599. struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
  600. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  601. struct crypto_aead *cipher;
  602. cipher = crypto_spawn_aead(spawn);
  603. if (IS_ERR(cipher))
  604. return PTR_ERR(cipher);
  605. ctx->child = cipher;
  606. crypto_aead_set_reqsize(
  607. tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
  608. crypto_aead_reqsize(cipher)));
  609. return 0;
  610. }
  611. static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
  612. {
  613. struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
  614. crypto_free_aead(ctx->child);
  615. }
  616. static int cryptd_create_aead(struct crypto_template *tmpl,
  617. struct rtattr **tb,
  618. struct cryptd_queue *queue)
  619. {
  620. struct aead_instance_ctx *ctx;
  621. struct aead_instance *inst;
  622. struct aead_alg *alg;
  623. const char *name;
  624. u32 type = 0;
  625. u32 mask = CRYPTO_ALG_ASYNC;
  626. int err;
  627. cryptd_check_internal(tb, &type, &mask);
  628. name = crypto_attr_alg_name(tb[1]);
  629. if (IS_ERR(name))
  630. return PTR_ERR(name);
  631. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  632. if (!inst)
  633. return -ENOMEM;
  634. ctx = aead_instance_ctx(inst);
  635. ctx->queue = queue;
  636. crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
  637. err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
  638. if (err)
  639. goto out_free_inst;
  640. alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
  641. err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
  642. if (err)
  643. goto out_drop_aead;
  644. inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
  645. (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
  646. inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
  647. inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
  648. inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
  649. inst->alg.init = cryptd_aead_init_tfm;
  650. inst->alg.exit = cryptd_aead_exit_tfm;
  651. inst->alg.setkey = cryptd_aead_setkey;
  652. inst->alg.setauthsize = cryptd_aead_setauthsize;
  653. inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
  654. inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
  655. err = aead_register_instance(tmpl, inst);
  656. if (err) {
  657. out_drop_aead:
  658. crypto_drop_aead(&ctx->aead_spawn);
  659. out_free_inst:
  660. kfree(inst);
  661. }
  662. return err;
  663. }
  664. static struct cryptd_queue queue;
  665. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  666. {
  667. struct crypto_attr_type *algt;
  668. algt = crypto_get_attr_type(tb);
  669. if (IS_ERR(algt))
  670. return PTR_ERR(algt);
  671. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  672. case CRYPTO_ALG_TYPE_BLKCIPHER:
  673. return cryptd_create_blkcipher(tmpl, tb, &queue);
  674. case CRYPTO_ALG_TYPE_DIGEST:
  675. return cryptd_create_hash(tmpl, tb, &queue);
  676. case CRYPTO_ALG_TYPE_AEAD:
  677. return cryptd_create_aead(tmpl, tb, &queue);
  678. }
  679. return -EINVAL;
  680. }
  681. static void cryptd_free(struct crypto_instance *inst)
  682. {
  683. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  684. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  685. struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
  686. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  687. case CRYPTO_ALG_TYPE_AHASH:
  688. crypto_drop_shash(&hctx->spawn);
  689. kfree(ahash_instance(inst));
  690. return;
  691. case CRYPTO_ALG_TYPE_AEAD:
  692. crypto_drop_aead(&aead_ctx->aead_spawn);
  693. kfree(aead_instance(inst));
  694. return;
  695. default:
  696. crypto_drop_spawn(&ctx->spawn);
  697. kfree(inst);
  698. }
  699. }
  700. static struct crypto_template cryptd_tmpl = {
  701. .name = "cryptd",
  702. .create = cryptd_create,
  703. .free = cryptd_free,
  704. .module = THIS_MODULE,
  705. };
  706. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  707. u32 type, u32 mask)
  708. {
  709. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  710. struct crypto_tfm *tfm;
  711. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  712. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  713. return ERR_PTR(-EINVAL);
  714. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  715. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  716. mask &= ~CRYPTO_ALG_TYPE_MASK;
  717. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  718. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  719. if (IS_ERR(tfm))
  720. return ERR_CAST(tfm);
  721. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  722. crypto_free_tfm(tfm);
  723. return ERR_PTR(-EINVAL);
  724. }
  725. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  726. }
  727. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  728. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  729. {
  730. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  731. return ctx->child;
  732. }
  733. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  734. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  735. {
  736. crypto_free_ablkcipher(&tfm->base);
  737. }
  738. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  739. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  740. u32 type, u32 mask)
  741. {
  742. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  743. struct crypto_ahash *tfm;
  744. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  745. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  746. return ERR_PTR(-EINVAL);
  747. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  748. if (IS_ERR(tfm))
  749. return ERR_CAST(tfm);
  750. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  751. crypto_free_ahash(tfm);
  752. return ERR_PTR(-EINVAL);
  753. }
  754. return __cryptd_ahash_cast(tfm);
  755. }
  756. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  757. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  758. {
  759. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  760. return ctx->child;
  761. }
  762. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  763. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  764. {
  765. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  766. return &rctx->desc;
  767. }
  768. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  769. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  770. {
  771. crypto_free_ahash(&tfm->base);
  772. }
  773. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  774. struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
  775. u32 type, u32 mask)
  776. {
  777. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  778. struct crypto_aead *tfm;
  779. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  780. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  781. return ERR_PTR(-EINVAL);
  782. tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
  783. if (IS_ERR(tfm))
  784. return ERR_CAST(tfm);
  785. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  786. crypto_free_aead(tfm);
  787. return ERR_PTR(-EINVAL);
  788. }
  789. return __cryptd_aead_cast(tfm);
  790. }
  791. EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
  792. struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
  793. {
  794. struct cryptd_aead_ctx *ctx;
  795. ctx = crypto_aead_ctx(&tfm->base);
  796. return ctx->child;
  797. }
  798. EXPORT_SYMBOL_GPL(cryptd_aead_child);
  799. void cryptd_free_aead(struct cryptd_aead *tfm)
  800. {
  801. crypto_free_aead(&tfm->base);
  802. }
  803. EXPORT_SYMBOL_GPL(cryptd_free_aead);
  804. static int __init cryptd_init(void)
  805. {
  806. int err;
  807. err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
  808. if (err)
  809. return err;
  810. err = crypto_register_template(&cryptd_tmpl);
  811. if (err)
  812. cryptd_fini_queue(&queue);
  813. return err;
  814. }
  815. static void __exit cryptd_exit(void)
  816. {
  817. cryptd_fini_queue(&queue);
  818. crypto_unregister_template(&cryptd_tmpl);
  819. }
  820. subsys_initcall(cryptd_init);
  821. module_exit(cryptd_exit);
  822. MODULE_LICENSE("GPL");
  823. MODULE_DESCRIPTION("Software async crypto daemon");
  824. MODULE_ALIAS_CRYPTO("cryptd");