cryptd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. /* Only handle one request at a time to avoid hogging crypto
  118. * workqueue. preempt_disable/enable is used to prevent
  119. * being preempted by cryptd_enqueue_request() */
  120. preempt_disable();
  121. backlog = crypto_get_backlog(&cpu_queue->queue);
  122. req = crypto_dequeue_request(&cpu_queue->queue);
  123. preempt_enable();
  124. if (!req)
  125. return;
  126. if (backlog)
  127. backlog->complete(backlog, -EINPROGRESS);
  128. req->complete(req, 0);
  129. if (cpu_queue->queue.qlen)
  130. queue_work(kcrypto_wq, &cpu_queue->work);
  131. }
  132. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  133. {
  134. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  135. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  136. return ictx->queue;
  137. }
  138. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  139. const u8 *key, unsigned int keylen)
  140. {
  141. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  142. struct crypto_blkcipher *child = ctx->child;
  143. int err;
  144. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  145. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  146. CRYPTO_TFM_REQ_MASK);
  147. err = crypto_blkcipher_setkey(child, key, keylen);
  148. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  149. CRYPTO_TFM_RES_MASK);
  150. return err;
  151. }
  152. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  153. struct crypto_blkcipher *child,
  154. int err,
  155. int (*crypt)(struct blkcipher_desc *desc,
  156. struct scatterlist *dst,
  157. struct scatterlist *src,
  158. unsigned int len))
  159. {
  160. struct cryptd_blkcipher_request_ctx *rctx;
  161. struct blkcipher_desc desc;
  162. rctx = ablkcipher_request_ctx(req);
  163. if (unlikely(err == -EINPROGRESS))
  164. goto out;
  165. desc.tfm = child;
  166. desc.info = req->info;
  167. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  168. err = crypt(&desc, req->dst, req->src, req->nbytes);
  169. req->base.complete = rctx->complete;
  170. out:
  171. local_bh_disable();
  172. rctx->complete(&req->base, err);
  173. local_bh_enable();
  174. }
  175. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  176. {
  177. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  178. struct crypto_blkcipher *child = ctx->child;
  179. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  180. crypto_blkcipher_crt(child)->encrypt);
  181. }
  182. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  183. {
  184. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  185. struct crypto_blkcipher *child = ctx->child;
  186. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  187. crypto_blkcipher_crt(child)->decrypt);
  188. }
  189. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  190. crypto_completion_t complete)
  191. {
  192. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  193. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  194. struct cryptd_queue *queue;
  195. queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
  196. rctx->complete = req->base.complete;
  197. req->base.complete = complete;
  198. return cryptd_enqueue_request(queue, &req->base);
  199. }
  200. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  201. {
  202. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  203. }
  204. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  205. {
  206. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  207. }
  208. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  209. {
  210. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  211. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  212. struct crypto_spawn *spawn = &ictx->spawn;
  213. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  214. struct crypto_blkcipher *cipher;
  215. cipher = crypto_spawn_blkcipher(spawn);
  216. if (IS_ERR(cipher))
  217. return PTR_ERR(cipher);
  218. ctx->child = cipher;
  219. tfm->crt_ablkcipher.reqsize =
  220. sizeof(struct cryptd_blkcipher_request_ctx);
  221. return 0;
  222. }
  223. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  224. {
  225. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  226. crypto_free_blkcipher(ctx->child);
  227. }
  228. static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  229. unsigned int tail)
  230. {
  231. char *p;
  232. struct crypto_instance *inst;
  233. int err;
  234. p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
  235. if (!p)
  236. return ERR_PTR(-ENOMEM);
  237. inst = (void *)(p + head);
  238. err = -ENAMETOOLONG;
  239. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  240. "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  241. goto out_free_inst;
  242. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  243. inst->alg.cra_priority = alg->cra_priority + 50;
  244. inst->alg.cra_blocksize = alg->cra_blocksize;
  245. inst->alg.cra_alignmask = alg->cra_alignmask;
  246. out:
  247. return p;
  248. out_free_inst:
  249. kfree(p);
  250. p = ERR_PTR(err);
  251. goto out;
  252. }
  253. static int cryptd_create_blkcipher(struct crypto_template *tmpl,
  254. struct rtattr **tb,
  255. struct cryptd_queue *queue)
  256. {
  257. struct cryptd_instance_ctx *ctx;
  258. struct crypto_instance *inst;
  259. struct crypto_alg *alg;
  260. int err;
  261. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
  262. CRYPTO_ALG_TYPE_MASK);
  263. if (IS_ERR(alg))
  264. return PTR_ERR(alg);
  265. inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
  266. err = PTR_ERR(inst);
  267. if (IS_ERR(inst))
  268. goto out_put_alg;
  269. ctx = crypto_instance_ctx(inst);
  270. ctx->queue = queue;
  271. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  272. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  273. if (err)
  274. goto out_free_inst;
  275. inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  276. inst->alg.cra_type = &crypto_ablkcipher_type;
  277. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  278. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  279. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  280. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  281. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  282. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  283. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  284. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  285. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  286. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  287. err = crypto_register_instance(tmpl, inst);
  288. if (err) {
  289. crypto_drop_spawn(&ctx->spawn);
  290. out_free_inst:
  291. kfree(inst);
  292. }
  293. out_put_alg:
  294. crypto_mod_put(alg);
  295. return err;
  296. }
  297. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  298. {
  299. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  300. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  301. struct crypto_shash_spawn *spawn = &ictx->spawn;
  302. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  303. struct crypto_shash *hash;
  304. hash = crypto_spawn_shash(spawn);
  305. if (IS_ERR(hash))
  306. return PTR_ERR(hash);
  307. ctx->child = hash;
  308. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  309. sizeof(struct cryptd_hash_request_ctx) +
  310. crypto_shash_descsize(hash));
  311. return 0;
  312. }
  313. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  314. {
  315. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  316. crypto_free_shash(ctx->child);
  317. }
  318. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  319. const u8 *key, unsigned int keylen)
  320. {
  321. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  322. struct crypto_shash *child = ctx->child;
  323. int err;
  324. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  325. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  326. CRYPTO_TFM_REQ_MASK);
  327. err = crypto_shash_setkey(child, key, keylen);
  328. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  329. CRYPTO_TFM_RES_MASK);
  330. return err;
  331. }
  332. static int cryptd_hash_enqueue(struct ahash_request *req,
  333. crypto_completion_t complete)
  334. {
  335. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  336. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  337. struct cryptd_queue *queue =
  338. cryptd_get_queue(crypto_ahash_tfm(tfm));
  339. rctx->complete = req->base.complete;
  340. req->base.complete = complete;
  341. return cryptd_enqueue_request(queue, &req->base);
  342. }
  343. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  344. {
  345. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  346. struct crypto_shash *child = ctx->child;
  347. struct ahash_request *req = ahash_request_cast(req_async);
  348. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  349. struct shash_desc *desc = &rctx->desc;
  350. if (unlikely(err == -EINPROGRESS))
  351. goto out;
  352. desc->tfm = child;
  353. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  354. err = crypto_shash_init(desc);
  355. req->base.complete = rctx->complete;
  356. out:
  357. local_bh_disable();
  358. rctx->complete(&req->base, err);
  359. local_bh_enable();
  360. }
  361. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  362. {
  363. return cryptd_hash_enqueue(req, cryptd_hash_init);
  364. }
  365. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  366. {
  367. struct ahash_request *req = ahash_request_cast(req_async);
  368. struct cryptd_hash_request_ctx *rctx;
  369. rctx = ahash_request_ctx(req);
  370. if (unlikely(err == -EINPROGRESS))
  371. goto out;
  372. err = shash_ahash_update(req, &rctx->desc);
  373. req->base.complete = rctx->complete;
  374. out:
  375. local_bh_disable();
  376. rctx->complete(&req->base, err);
  377. local_bh_enable();
  378. }
  379. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  380. {
  381. return cryptd_hash_enqueue(req, cryptd_hash_update);
  382. }
  383. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  384. {
  385. struct ahash_request *req = ahash_request_cast(req_async);
  386. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  387. if (unlikely(err == -EINPROGRESS))
  388. goto out;
  389. err = crypto_shash_final(&rctx->desc, req->result);
  390. req->base.complete = rctx->complete;
  391. out:
  392. local_bh_disable();
  393. rctx->complete(&req->base, err);
  394. local_bh_enable();
  395. }
  396. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  397. {
  398. return cryptd_hash_enqueue(req, cryptd_hash_final);
  399. }
  400. static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
  401. {
  402. struct ahash_request *req = ahash_request_cast(req_async);
  403. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  404. if (unlikely(err == -EINPROGRESS))
  405. goto out;
  406. err = shash_ahash_finup(req, &rctx->desc);
  407. req->base.complete = rctx->complete;
  408. out:
  409. local_bh_disable();
  410. rctx->complete(&req->base, err);
  411. local_bh_enable();
  412. }
  413. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  414. {
  415. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  416. }
  417. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  418. {
  419. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  420. struct crypto_shash *child = ctx->child;
  421. struct ahash_request *req = ahash_request_cast(req_async);
  422. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  423. struct shash_desc *desc = &rctx->desc;
  424. if (unlikely(err == -EINPROGRESS))
  425. goto out;
  426. desc->tfm = child;
  427. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  428. err = shash_ahash_digest(req, desc);
  429. req->base.complete = rctx->complete;
  430. out:
  431. local_bh_disable();
  432. rctx->complete(&req->base, err);
  433. local_bh_enable();
  434. }
  435. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  436. {
  437. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  438. }
  439. static int cryptd_hash_export(struct ahash_request *req, void *out)
  440. {
  441. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  442. return crypto_shash_export(&rctx->desc, out);
  443. }
  444. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  445. {
  446. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  447. return crypto_shash_import(&rctx->desc, in);
  448. }
  449. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  450. struct cryptd_queue *queue)
  451. {
  452. struct hashd_instance_ctx *ctx;
  453. struct ahash_instance *inst;
  454. struct shash_alg *salg;
  455. struct crypto_alg *alg;
  456. int err;
  457. salg = shash_attr_alg(tb[1], 0, 0);
  458. if (IS_ERR(salg))
  459. return PTR_ERR(salg);
  460. alg = &salg->base;
  461. inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
  462. sizeof(*ctx));
  463. err = PTR_ERR(inst);
  464. if (IS_ERR(inst))
  465. goto out_put_alg;
  466. ctx = ahash_instance_ctx(inst);
  467. ctx->queue = queue;
  468. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  469. ahash_crypto_instance(inst));
  470. if (err)
  471. goto out_free_inst;
  472. inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  473. inst->alg.halg.digestsize = salg->digestsize;
  474. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  475. inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
  476. inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
  477. inst->alg.init = cryptd_hash_init_enqueue;
  478. inst->alg.update = cryptd_hash_update_enqueue;
  479. inst->alg.final = cryptd_hash_final_enqueue;
  480. inst->alg.finup = cryptd_hash_finup_enqueue;
  481. inst->alg.export = cryptd_hash_export;
  482. inst->alg.import = cryptd_hash_import;
  483. inst->alg.setkey = cryptd_hash_setkey;
  484. inst->alg.digest = cryptd_hash_digest_enqueue;
  485. err = ahash_register_instance(tmpl, inst);
  486. if (err) {
  487. crypto_drop_shash(&ctx->spawn);
  488. out_free_inst:
  489. kfree(inst);
  490. }
  491. out_put_alg:
  492. crypto_mod_put(alg);
  493. return err;
  494. }
  495. static void cryptd_aead_crypt(struct aead_request *req,
  496. struct crypto_aead *child,
  497. int err,
  498. int (*crypt)(struct aead_request *req))
  499. {
  500. struct cryptd_aead_request_ctx *rctx;
  501. rctx = aead_request_ctx(req);
  502. if (unlikely(err == -EINPROGRESS))
  503. goto out;
  504. aead_request_set_tfm(req, child);
  505. err = crypt( req );
  506. req->base.complete = rctx->complete;
  507. out:
  508. local_bh_disable();
  509. rctx->complete(&req->base, err);
  510. local_bh_enable();
  511. }
  512. static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
  513. {
  514. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  515. struct crypto_aead *child = ctx->child;
  516. struct aead_request *req;
  517. req = container_of(areq, struct aead_request, base);
  518. cryptd_aead_crypt(req, child, err, crypto_aead_crt(child)->encrypt);
  519. }
  520. static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
  521. {
  522. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
  523. struct crypto_aead *child = ctx->child;
  524. struct aead_request *req;
  525. req = container_of(areq, struct aead_request, base);
  526. cryptd_aead_crypt(req, child, err, crypto_aead_crt(child)->decrypt);
  527. }
  528. static int cryptd_aead_enqueue(struct aead_request *req,
  529. crypto_completion_t complete)
  530. {
  531. struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
  532. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  533. struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
  534. rctx->complete = req->base.complete;
  535. req->base.complete = complete;
  536. return cryptd_enqueue_request(queue, &req->base);
  537. }
  538. static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
  539. {
  540. return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
  541. }
  542. static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
  543. {
  544. return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
  545. }
  546. static int cryptd_aead_init_tfm(struct crypto_tfm *tfm)
  547. {
  548. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  549. struct aead_instance_ctx *ictx = crypto_instance_ctx(inst);
  550. struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
  551. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  552. struct crypto_aead *cipher;
  553. cipher = crypto_spawn_aead(spawn);
  554. if (IS_ERR(cipher))
  555. return PTR_ERR(cipher);
  556. crypto_aead_set_flags(cipher, CRYPTO_TFM_REQ_MAY_SLEEP);
  557. ctx->child = cipher;
  558. tfm->crt_aead.reqsize = sizeof(struct cryptd_aead_request_ctx);
  559. return 0;
  560. }
  561. static void cryptd_aead_exit_tfm(struct crypto_tfm *tfm)
  562. {
  563. struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  564. crypto_free_aead(ctx->child);
  565. }
  566. static int cryptd_create_aead(struct crypto_template *tmpl,
  567. struct rtattr **tb,
  568. struct cryptd_queue *queue)
  569. {
  570. struct aead_instance_ctx *ctx;
  571. struct crypto_instance *inst;
  572. struct crypto_alg *alg;
  573. int err;
  574. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_AEAD,
  575. CRYPTO_ALG_TYPE_MASK);
  576. if (IS_ERR(alg))
  577. return PTR_ERR(alg);
  578. inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
  579. err = PTR_ERR(inst);
  580. if (IS_ERR(inst))
  581. goto out_put_alg;
  582. ctx = crypto_instance_ctx(inst);
  583. ctx->queue = queue;
  584. err = crypto_init_spawn(&ctx->aead_spawn.base, alg, inst,
  585. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  586. if (err)
  587. goto out_free_inst;
  588. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
  589. inst->alg.cra_type = alg->cra_type;
  590. inst->alg.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
  591. inst->alg.cra_init = cryptd_aead_init_tfm;
  592. inst->alg.cra_exit = cryptd_aead_exit_tfm;
  593. inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
  594. inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
  595. inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
  596. inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
  597. inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
  598. inst->alg.cra_aead.encrypt = cryptd_aead_encrypt_enqueue;
  599. inst->alg.cra_aead.decrypt = cryptd_aead_decrypt_enqueue;
  600. inst->alg.cra_aead.givencrypt = alg->cra_aead.givencrypt;
  601. inst->alg.cra_aead.givdecrypt = alg->cra_aead.givdecrypt;
  602. err = crypto_register_instance(tmpl, inst);
  603. if (err) {
  604. crypto_drop_spawn(&ctx->aead_spawn.base);
  605. out_free_inst:
  606. kfree(inst);
  607. }
  608. out_put_alg:
  609. crypto_mod_put(alg);
  610. return err;
  611. }
  612. static struct cryptd_queue queue;
  613. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  614. {
  615. struct crypto_attr_type *algt;
  616. algt = crypto_get_attr_type(tb);
  617. if (IS_ERR(algt))
  618. return PTR_ERR(algt);
  619. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  620. case CRYPTO_ALG_TYPE_BLKCIPHER:
  621. return cryptd_create_blkcipher(tmpl, tb, &queue);
  622. case CRYPTO_ALG_TYPE_DIGEST:
  623. return cryptd_create_hash(tmpl, tb, &queue);
  624. case CRYPTO_ALG_TYPE_AEAD:
  625. return cryptd_create_aead(tmpl, tb, &queue);
  626. }
  627. return -EINVAL;
  628. }
  629. static void cryptd_free(struct crypto_instance *inst)
  630. {
  631. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  632. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  633. struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
  634. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  635. case CRYPTO_ALG_TYPE_AHASH:
  636. crypto_drop_shash(&hctx->spawn);
  637. kfree(ahash_instance(inst));
  638. return;
  639. case CRYPTO_ALG_TYPE_AEAD:
  640. crypto_drop_spawn(&aead_ctx->aead_spawn.base);
  641. kfree(inst);
  642. return;
  643. default:
  644. crypto_drop_spawn(&ctx->spawn);
  645. kfree(inst);
  646. }
  647. }
  648. static struct crypto_template cryptd_tmpl = {
  649. .name = "cryptd",
  650. .create = cryptd_create,
  651. .free = cryptd_free,
  652. .module = THIS_MODULE,
  653. };
  654. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  655. u32 type, u32 mask)
  656. {
  657. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  658. struct crypto_tfm *tfm;
  659. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  660. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  661. return ERR_PTR(-EINVAL);
  662. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  663. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  664. mask &= ~CRYPTO_ALG_TYPE_MASK;
  665. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  666. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  667. if (IS_ERR(tfm))
  668. return ERR_CAST(tfm);
  669. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  670. crypto_free_tfm(tfm);
  671. return ERR_PTR(-EINVAL);
  672. }
  673. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  674. }
  675. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  676. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  677. {
  678. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  679. return ctx->child;
  680. }
  681. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  682. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  683. {
  684. crypto_free_ablkcipher(&tfm->base);
  685. }
  686. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  687. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  688. u32 type, u32 mask)
  689. {
  690. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  691. struct crypto_ahash *tfm;
  692. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  693. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  694. return ERR_PTR(-EINVAL);
  695. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  696. if (IS_ERR(tfm))
  697. return ERR_CAST(tfm);
  698. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  699. crypto_free_ahash(tfm);
  700. return ERR_PTR(-EINVAL);
  701. }
  702. return __cryptd_ahash_cast(tfm);
  703. }
  704. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  705. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  706. {
  707. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  708. return ctx->child;
  709. }
  710. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  711. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  712. {
  713. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  714. return &rctx->desc;
  715. }
  716. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  717. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  718. {
  719. crypto_free_ahash(&tfm->base);
  720. }
  721. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  722. struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
  723. u32 type, u32 mask)
  724. {
  725. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  726. struct crypto_aead *tfm;
  727. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  728. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  729. return ERR_PTR(-EINVAL);
  730. tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
  731. if (IS_ERR(tfm))
  732. return ERR_CAST(tfm);
  733. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  734. crypto_free_aead(tfm);
  735. return ERR_PTR(-EINVAL);
  736. }
  737. return __cryptd_aead_cast(tfm);
  738. }
  739. EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
  740. struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
  741. {
  742. struct cryptd_aead_ctx *ctx;
  743. ctx = crypto_aead_ctx(&tfm->base);
  744. return ctx->child;
  745. }
  746. EXPORT_SYMBOL_GPL(cryptd_aead_child);
  747. void cryptd_free_aead(struct cryptd_aead *tfm)
  748. {
  749. crypto_free_aead(&tfm->base);
  750. }
  751. EXPORT_SYMBOL_GPL(cryptd_free_aead);
  752. static int __init cryptd_init(void)
  753. {
  754. int err;
  755. err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
  756. if (err)
  757. return err;
  758. err = crypto_register_template(&cryptd_tmpl);
  759. if (err)
  760. cryptd_fini_queue(&queue);
  761. return err;
  762. }
  763. static void __exit cryptd_exit(void)
  764. {
  765. cryptd_fini_queue(&queue);
  766. crypto_unregister_template(&cryptd_tmpl);
  767. }
  768. subsys_initcall(cryptd_init);
  769. module_exit(cryptd_exit);
  770. MODULE_LICENSE("GPL");
  771. MODULE_DESCRIPTION("Software async crypto daemon");