cryptd.c 29 KB

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