cryptd.c 25 KB

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