cryptd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Software async crypto daemon.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/algapi.h>
  13. #include <crypto/internal/hash.h>
  14. #include <crypto/cryptd.h>
  15. #include <crypto/crypto_wq.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #define CRYPTD_MAX_CPU_QLEN 100
  25. struct cryptd_cpu_queue {
  26. struct crypto_queue queue;
  27. struct work_struct work;
  28. };
  29. struct cryptd_queue {
  30. struct cryptd_cpu_queue __percpu *cpu_queue;
  31. };
  32. struct cryptd_instance_ctx {
  33. struct crypto_spawn spawn;
  34. struct cryptd_queue *queue;
  35. };
  36. struct hashd_instance_ctx {
  37. struct crypto_shash_spawn spawn;
  38. struct cryptd_queue *queue;
  39. };
  40. struct cryptd_blkcipher_ctx {
  41. struct crypto_blkcipher *child;
  42. };
  43. struct cryptd_blkcipher_request_ctx {
  44. crypto_completion_t complete;
  45. };
  46. struct cryptd_hash_ctx {
  47. struct crypto_shash *child;
  48. };
  49. struct cryptd_hash_request_ctx {
  50. crypto_completion_t complete;
  51. struct shash_desc desc;
  52. };
  53. static void cryptd_queue_worker(struct work_struct *work);
  54. static int cryptd_init_queue(struct cryptd_queue *queue,
  55. unsigned int max_cpu_qlen)
  56. {
  57. int cpu;
  58. struct cryptd_cpu_queue *cpu_queue;
  59. queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
  60. if (!queue->cpu_queue)
  61. return -ENOMEM;
  62. for_each_possible_cpu(cpu) {
  63. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  64. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  65. INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
  66. }
  67. return 0;
  68. }
  69. static void cryptd_fini_queue(struct cryptd_queue *queue)
  70. {
  71. int cpu;
  72. struct cryptd_cpu_queue *cpu_queue;
  73. for_each_possible_cpu(cpu) {
  74. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  75. BUG_ON(cpu_queue->queue.qlen);
  76. }
  77. free_percpu(queue->cpu_queue);
  78. }
  79. static int cryptd_enqueue_request(struct cryptd_queue *queue,
  80. struct crypto_async_request *request)
  81. {
  82. int cpu, err;
  83. struct cryptd_cpu_queue *cpu_queue;
  84. cpu = get_cpu();
  85. cpu_queue = this_cpu_ptr(queue->cpu_queue);
  86. err = crypto_enqueue_request(&cpu_queue->queue, request);
  87. queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
  88. put_cpu();
  89. return err;
  90. }
  91. /* Called in workqueue context, do one real cryption work (via
  92. * req->complete) and reschedule itself if there are more work to
  93. * do. */
  94. static void cryptd_queue_worker(struct work_struct *work)
  95. {
  96. struct cryptd_cpu_queue *cpu_queue;
  97. struct crypto_async_request *req, *backlog;
  98. cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
  99. /* Only handle one request at a time to avoid hogging crypto
  100. * workqueue. preempt_disable/enable is used to prevent
  101. * being preempted by cryptd_enqueue_request() */
  102. preempt_disable();
  103. backlog = crypto_get_backlog(&cpu_queue->queue);
  104. req = crypto_dequeue_request(&cpu_queue->queue);
  105. preempt_enable();
  106. if (!req)
  107. return;
  108. if (backlog)
  109. backlog->complete(backlog, -EINPROGRESS);
  110. req->complete(req, 0);
  111. if (cpu_queue->queue.qlen)
  112. queue_work(kcrypto_wq, &cpu_queue->work);
  113. }
  114. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  115. {
  116. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  117. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  118. return ictx->queue;
  119. }
  120. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  121. const u8 *key, unsigned int keylen)
  122. {
  123. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  124. struct crypto_blkcipher *child = ctx->child;
  125. int err;
  126. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  127. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  128. CRYPTO_TFM_REQ_MASK);
  129. err = crypto_blkcipher_setkey(child, key, keylen);
  130. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  131. CRYPTO_TFM_RES_MASK);
  132. return err;
  133. }
  134. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  135. struct crypto_blkcipher *child,
  136. int err,
  137. int (*crypt)(struct blkcipher_desc *desc,
  138. struct scatterlist *dst,
  139. struct scatterlist *src,
  140. unsigned int len))
  141. {
  142. struct cryptd_blkcipher_request_ctx *rctx;
  143. struct blkcipher_desc desc;
  144. rctx = ablkcipher_request_ctx(req);
  145. if (unlikely(err == -EINPROGRESS))
  146. goto out;
  147. desc.tfm = child;
  148. desc.info = req->info;
  149. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  150. err = crypt(&desc, req->dst, req->src, req->nbytes);
  151. req->base.complete = rctx->complete;
  152. out:
  153. local_bh_disable();
  154. rctx->complete(&req->base, err);
  155. local_bh_enable();
  156. }
  157. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  158. {
  159. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  160. struct crypto_blkcipher *child = ctx->child;
  161. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  162. crypto_blkcipher_crt(child)->encrypt);
  163. }
  164. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  165. {
  166. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  167. struct crypto_blkcipher *child = ctx->child;
  168. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  169. crypto_blkcipher_crt(child)->decrypt);
  170. }
  171. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  172. crypto_completion_t complete)
  173. {
  174. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  175. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  176. struct cryptd_queue *queue;
  177. queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
  178. rctx->complete = req->base.complete;
  179. req->base.complete = complete;
  180. return cryptd_enqueue_request(queue, &req->base);
  181. }
  182. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  183. {
  184. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  185. }
  186. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  187. {
  188. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  189. }
  190. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  191. {
  192. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  193. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  194. struct crypto_spawn *spawn = &ictx->spawn;
  195. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  196. struct crypto_blkcipher *cipher;
  197. cipher = crypto_spawn_blkcipher(spawn);
  198. if (IS_ERR(cipher))
  199. return PTR_ERR(cipher);
  200. ctx->child = cipher;
  201. tfm->crt_ablkcipher.reqsize =
  202. sizeof(struct cryptd_blkcipher_request_ctx);
  203. return 0;
  204. }
  205. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  206. {
  207. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  208. crypto_free_blkcipher(ctx->child);
  209. }
  210. static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
  211. unsigned int tail)
  212. {
  213. char *p;
  214. struct crypto_instance *inst;
  215. int err;
  216. p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
  217. if (!p)
  218. return ERR_PTR(-ENOMEM);
  219. inst = (void *)(p + head);
  220. err = -ENAMETOOLONG;
  221. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  222. "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  223. goto out_free_inst;
  224. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  225. inst->alg.cra_priority = alg->cra_priority + 50;
  226. inst->alg.cra_blocksize = alg->cra_blocksize;
  227. inst->alg.cra_alignmask = alg->cra_alignmask;
  228. out:
  229. return p;
  230. out_free_inst:
  231. kfree(p);
  232. p = ERR_PTR(err);
  233. goto out;
  234. }
  235. static int cryptd_create_blkcipher(struct crypto_template *tmpl,
  236. struct rtattr **tb,
  237. struct cryptd_queue *queue)
  238. {
  239. struct cryptd_instance_ctx *ctx;
  240. struct crypto_instance *inst;
  241. struct crypto_alg *alg;
  242. int err;
  243. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
  244. CRYPTO_ALG_TYPE_MASK);
  245. if (IS_ERR(alg))
  246. return PTR_ERR(alg);
  247. inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
  248. err = PTR_ERR(inst);
  249. if (IS_ERR(inst))
  250. goto out_put_alg;
  251. ctx = crypto_instance_ctx(inst);
  252. ctx->queue = queue;
  253. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  254. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  255. if (err)
  256. goto out_free_inst;
  257. inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  258. inst->alg.cra_type = &crypto_ablkcipher_type;
  259. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  260. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  261. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  262. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  263. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  264. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  265. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  266. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  267. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  268. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  269. err = crypto_register_instance(tmpl, inst);
  270. if (err) {
  271. crypto_drop_spawn(&ctx->spawn);
  272. out_free_inst:
  273. kfree(inst);
  274. }
  275. out_put_alg:
  276. crypto_mod_put(alg);
  277. return err;
  278. }
  279. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  280. {
  281. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  282. struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
  283. struct crypto_shash_spawn *spawn = &ictx->spawn;
  284. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  285. struct crypto_shash *hash;
  286. hash = crypto_spawn_shash(spawn);
  287. if (IS_ERR(hash))
  288. return PTR_ERR(hash);
  289. ctx->child = hash;
  290. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  291. sizeof(struct cryptd_hash_request_ctx) +
  292. crypto_shash_descsize(hash));
  293. return 0;
  294. }
  295. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  296. {
  297. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  298. crypto_free_shash(ctx->child);
  299. }
  300. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  301. const u8 *key, unsigned int keylen)
  302. {
  303. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  304. struct crypto_shash *child = ctx->child;
  305. int err;
  306. crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  307. crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
  308. CRYPTO_TFM_REQ_MASK);
  309. err = crypto_shash_setkey(child, key, keylen);
  310. crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
  311. CRYPTO_TFM_RES_MASK);
  312. return err;
  313. }
  314. static int cryptd_hash_enqueue(struct ahash_request *req,
  315. crypto_completion_t complete)
  316. {
  317. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  318. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  319. struct cryptd_queue *queue =
  320. cryptd_get_queue(crypto_ahash_tfm(tfm));
  321. rctx->complete = req->base.complete;
  322. req->base.complete = complete;
  323. return cryptd_enqueue_request(queue, &req->base);
  324. }
  325. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  326. {
  327. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  328. struct crypto_shash *child = ctx->child;
  329. struct ahash_request *req = ahash_request_cast(req_async);
  330. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  331. struct shash_desc *desc = &rctx->desc;
  332. if (unlikely(err == -EINPROGRESS))
  333. goto out;
  334. desc->tfm = child;
  335. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  336. err = crypto_shash_init(desc);
  337. req->base.complete = rctx->complete;
  338. out:
  339. local_bh_disable();
  340. rctx->complete(&req->base, err);
  341. local_bh_enable();
  342. }
  343. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  344. {
  345. return cryptd_hash_enqueue(req, cryptd_hash_init);
  346. }
  347. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  348. {
  349. struct ahash_request *req = ahash_request_cast(req_async);
  350. struct cryptd_hash_request_ctx *rctx;
  351. rctx = ahash_request_ctx(req);
  352. if (unlikely(err == -EINPROGRESS))
  353. goto out;
  354. err = shash_ahash_update(req, &rctx->desc);
  355. req->base.complete = rctx->complete;
  356. out:
  357. local_bh_disable();
  358. rctx->complete(&req->base, err);
  359. local_bh_enable();
  360. }
  361. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  362. {
  363. return cryptd_hash_enqueue(req, cryptd_hash_update);
  364. }
  365. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  366. {
  367. struct ahash_request *req = ahash_request_cast(req_async);
  368. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  369. if (unlikely(err == -EINPROGRESS))
  370. goto out;
  371. err = crypto_shash_final(&rctx->desc, req->result);
  372. req->base.complete = rctx->complete;
  373. out:
  374. local_bh_disable();
  375. rctx->complete(&req->base, err);
  376. local_bh_enable();
  377. }
  378. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  379. {
  380. return cryptd_hash_enqueue(req, cryptd_hash_final);
  381. }
  382. static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
  383. {
  384. struct ahash_request *req = ahash_request_cast(req_async);
  385. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  386. if (unlikely(err == -EINPROGRESS))
  387. goto out;
  388. err = shash_ahash_finup(req, &rctx->desc);
  389. req->base.complete = rctx->complete;
  390. out:
  391. local_bh_disable();
  392. rctx->complete(&req->base, err);
  393. local_bh_enable();
  394. }
  395. static int cryptd_hash_finup_enqueue(struct ahash_request *req)
  396. {
  397. return cryptd_hash_enqueue(req, cryptd_hash_finup);
  398. }
  399. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  400. {
  401. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  402. struct crypto_shash *child = ctx->child;
  403. struct ahash_request *req = ahash_request_cast(req_async);
  404. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  405. struct shash_desc *desc = &rctx->desc;
  406. if (unlikely(err == -EINPROGRESS))
  407. goto out;
  408. desc->tfm = child;
  409. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  410. err = shash_ahash_digest(req, desc);
  411. req->base.complete = rctx->complete;
  412. out:
  413. local_bh_disable();
  414. rctx->complete(&req->base, err);
  415. local_bh_enable();
  416. }
  417. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  418. {
  419. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  420. }
  421. static int cryptd_hash_export(struct ahash_request *req, void *out)
  422. {
  423. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  424. return crypto_shash_export(&rctx->desc, out);
  425. }
  426. static int cryptd_hash_import(struct ahash_request *req, const void *in)
  427. {
  428. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  429. return crypto_shash_import(&rctx->desc, in);
  430. }
  431. static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
  432. struct cryptd_queue *queue)
  433. {
  434. struct hashd_instance_ctx *ctx;
  435. struct ahash_instance *inst;
  436. struct shash_alg *salg;
  437. struct crypto_alg *alg;
  438. int err;
  439. salg = shash_attr_alg(tb[1], 0, 0);
  440. if (IS_ERR(salg))
  441. return PTR_ERR(salg);
  442. alg = &salg->base;
  443. inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
  444. sizeof(*ctx));
  445. err = PTR_ERR(inst);
  446. if (IS_ERR(inst))
  447. goto out_put_alg;
  448. ctx = ahash_instance_ctx(inst);
  449. ctx->queue = queue;
  450. err = crypto_init_shash_spawn(&ctx->spawn, salg,
  451. ahash_crypto_instance(inst));
  452. if (err)
  453. goto out_free_inst;
  454. inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  455. inst->alg.halg.digestsize = salg->digestsize;
  456. inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  457. inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
  458. inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
  459. inst->alg.init = cryptd_hash_init_enqueue;
  460. inst->alg.update = cryptd_hash_update_enqueue;
  461. inst->alg.final = cryptd_hash_final_enqueue;
  462. inst->alg.finup = cryptd_hash_finup_enqueue;
  463. inst->alg.export = cryptd_hash_export;
  464. inst->alg.import = cryptd_hash_import;
  465. inst->alg.setkey = cryptd_hash_setkey;
  466. inst->alg.digest = cryptd_hash_digest_enqueue;
  467. err = ahash_register_instance(tmpl, inst);
  468. if (err) {
  469. crypto_drop_shash(&ctx->spawn);
  470. out_free_inst:
  471. kfree(inst);
  472. }
  473. out_put_alg:
  474. crypto_mod_put(alg);
  475. return err;
  476. }
  477. static struct cryptd_queue queue;
  478. static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
  479. {
  480. struct crypto_attr_type *algt;
  481. algt = crypto_get_attr_type(tb);
  482. if (IS_ERR(algt))
  483. return PTR_ERR(algt);
  484. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  485. case CRYPTO_ALG_TYPE_BLKCIPHER:
  486. return cryptd_create_blkcipher(tmpl, tb, &queue);
  487. case CRYPTO_ALG_TYPE_DIGEST:
  488. return cryptd_create_hash(tmpl, tb, &queue);
  489. }
  490. return -EINVAL;
  491. }
  492. static void cryptd_free(struct crypto_instance *inst)
  493. {
  494. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  495. struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
  496. switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
  497. case CRYPTO_ALG_TYPE_AHASH:
  498. crypto_drop_shash(&hctx->spawn);
  499. kfree(ahash_instance(inst));
  500. return;
  501. }
  502. crypto_drop_spawn(&ctx->spawn);
  503. kfree(inst);
  504. }
  505. static struct crypto_template cryptd_tmpl = {
  506. .name = "cryptd",
  507. .create = cryptd_create,
  508. .free = cryptd_free,
  509. .module = THIS_MODULE,
  510. };
  511. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  512. u32 type, u32 mask)
  513. {
  514. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  515. struct crypto_tfm *tfm;
  516. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  517. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  518. return ERR_PTR(-EINVAL);
  519. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  520. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  521. mask &= ~CRYPTO_ALG_TYPE_MASK;
  522. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  523. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  524. if (IS_ERR(tfm))
  525. return ERR_CAST(tfm);
  526. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  527. crypto_free_tfm(tfm);
  528. return ERR_PTR(-EINVAL);
  529. }
  530. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  531. }
  532. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  533. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  534. {
  535. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  536. return ctx->child;
  537. }
  538. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  539. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  540. {
  541. crypto_free_ablkcipher(&tfm->base);
  542. }
  543. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  544. struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
  545. u32 type, u32 mask)
  546. {
  547. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  548. struct crypto_ahash *tfm;
  549. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  550. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  551. return ERR_PTR(-EINVAL);
  552. tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
  553. if (IS_ERR(tfm))
  554. return ERR_CAST(tfm);
  555. if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
  556. crypto_free_ahash(tfm);
  557. return ERR_PTR(-EINVAL);
  558. }
  559. return __cryptd_ahash_cast(tfm);
  560. }
  561. EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
  562. struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
  563. {
  564. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
  565. return ctx->child;
  566. }
  567. EXPORT_SYMBOL_GPL(cryptd_ahash_child);
  568. struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
  569. {
  570. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  571. return &rctx->desc;
  572. }
  573. EXPORT_SYMBOL_GPL(cryptd_shash_desc);
  574. void cryptd_free_ahash(struct cryptd_ahash *tfm)
  575. {
  576. crypto_free_ahash(&tfm->base);
  577. }
  578. EXPORT_SYMBOL_GPL(cryptd_free_ahash);
  579. static int __init cryptd_init(void)
  580. {
  581. int err;
  582. err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
  583. if (err)
  584. return err;
  585. err = crypto_register_template(&cryptd_tmpl);
  586. if (err)
  587. cryptd_fini_queue(&queue);
  588. return err;
  589. }
  590. static void __exit cryptd_exit(void)
  591. {
  592. cryptd_fini_queue(&queue);
  593. crypto_unregister_template(&cryptd_tmpl);
  594. }
  595. module_init(cryptd_init);
  596. module_exit(cryptd_exit);
  597. MODULE_LICENSE("GPL");
  598. MODULE_DESCRIPTION("Software async crypto daemon");