cryptd.c 26 KB

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