cryptd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 *cpu_queue;
  31. };
  32. struct cryptd_instance_ctx {
  33. struct crypto_spawn spawn;
  34. struct cryptd_queue *queue;
  35. };
  36. struct cryptd_blkcipher_ctx {
  37. struct crypto_blkcipher *child;
  38. };
  39. struct cryptd_blkcipher_request_ctx {
  40. crypto_completion_t complete;
  41. };
  42. struct cryptd_hash_ctx {
  43. struct crypto_hash *child;
  44. };
  45. struct cryptd_hash_request_ctx {
  46. crypto_completion_t complete;
  47. };
  48. static void cryptd_queue_worker(struct work_struct *work);
  49. static int cryptd_init_queue(struct cryptd_queue *queue,
  50. unsigned int max_cpu_qlen)
  51. {
  52. int cpu;
  53. struct cryptd_cpu_queue *cpu_queue;
  54. queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
  55. if (!queue->cpu_queue)
  56. return -ENOMEM;
  57. for_each_possible_cpu(cpu) {
  58. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  59. crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
  60. INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
  61. }
  62. return 0;
  63. }
  64. static void cryptd_fini_queue(struct cryptd_queue *queue)
  65. {
  66. int cpu;
  67. struct cryptd_cpu_queue *cpu_queue;
  68. for_each_possible_cpu(cpu) {
  69. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  70. BUG_ON(cpu_queue->queue.qlen);
  71. }
  72. free_percpu(queue->cpu_queue);
  73. }
  74. static int cryptd_enqueue_request(struct cryptd_queue *queue,
  75. struct crypto_async_request *request)
  76. {
  77. int cpu, err;
  78. struct cryptd_cpu_queue *cpu_queue;
  79. cpu = get_cpu();
  80. cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
  81. err = crypto_enqueue_request(&cpu_queue->queue, request);
  82. queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
  83. put_cpu();
  84. return err;
  85. }
  86. /* Called in workqueue context, do one real cryption work (via
  87. * req->complete) and reschedule itself if there are more work to
  88. * do. */
  89. static void cryptd_queue_worker(struct work_struct *work)
  90. {
  91. struct cryptd_cpu_queue *cpu_queue;
  92. struct crypto_async_request *req, *backlog;
  93. cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
  94. /* Only handle one request at a time to avoid hogging crypto
  95. * workqueue. preempt_disable/enable is used to prevent
  96. * being preempted by cryptd_enqueue_request() */
  97. preempt_disable();
  98. backlog = crypto_get_backlog(&cpu_queue->queue);
  99. req = crypto_dequeue_request(&cpu_queue->queue);
  100. preempt_enable();
  101. if (!req)
  102. return;
  103. if (backlog)
  104. backlog->complete(backlog, -EINPROGRESS);
  105. req->complete(req, 0);
  106. if (cpu_queue->queue.qlen)
  107. queue_work(kcrypto_wq, &cpu_queue->work);
  108. }
  109. static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
  110. {
  111. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  112. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  113. return ictx->queue;
  114. }
  115. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  116. const u8 *key, unsigned int keylen)
  117. {
  118. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  119. struct crypto_blkcipher *child = ctx->child;
  120. int err;
  121. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  122. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  123. CRYPTO_TFM_REQ_MASK);
  124. err = crypto_blkcipher_setkey(child, key, keylen);
  125. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  126. CRYPTO_TFM_RES_MASK);
  127. return err;
  128. }
  129. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  130. struct crypto_blkcipher *child,
  131. int err,
  132. int (*crypt)(struct blkcipher_desc *desc,
  133. struct scatterlist *dst,
  134. struct scatterlist *src,
  135. unsigned int len))
  136. {
  137. struct cryptd_blkcipher_request_ctx *rctx;
  138. struct blkcipher_desc desc;
  139. rctx = ablkcipher_request_ctx(req);
  140. if (unlikely(err == -EINPROGRESS))
  141. goto out;
  142. desc.tfm = child;
  143. desc.info = req->info;
  144. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  145. err = crypt(&desc, req->dst, req->src, req->nbytes);
  146. req->base.complete = rctx->complete;
  147. out:
  148. local_bh_disable();
  149. rctx->complete(&req->base, err);
  150. local_bh_enable();
  151. }
  152. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  153. {
  154. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  155. struct crypto_blkcipher *child = ctx->child;
  156. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  157. crypto_blkcipher_crt(child)->encrypt);
  158. }
  159. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  160. {
  161. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  162. struct crypto_blkcipher *child = ctx->child;
  163. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  164. crypto_blkcipher_crt(child)->decrypt);
  165. }
  166. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  167. crypto_completion_t complete)
  168. {
  169. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  170. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  171. struct cryptd_queue *queue;
  172. queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
  173. rctx->complete = req->base.complete;
  174. req->base.complete = complete;
  175. return cryptd_enqueue_request(queue, &req->base);
  176. }
  177. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  178. {
  179. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  180. }
  181. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  182. {
  183. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  184. }
  185. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  186. {
  187. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  188. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  189. struct crypto_spawn *spawn = &ictx->spawn;
  190. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  191. struct crypto_blkcipher *cipher;
  192. cipher = crypto_spawn_blkcipher(spawn);
  193. if (IS_ERR(cipher))
  194. return PTR_ERR(cipher);
  195. ctx->child = cipher;
  196. tfm->crt_ablkcipher.reqsize =
  197. sizeof(struct cryptd_blkcipher_request_ctx);
  198. return 0;
  199. }
  200. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  201. {
  202. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  203. crypto_free_blkcipher(ctx->child);
  204. }
  205. static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
  206. struct cryptd_queue *queue)
  207. {
  208. struct crypto_instance *inst;
  209. struct cryptd_instance_ctx *ctx;
  210. int err;
  211. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  212. if (!inst) {
  213. inst = ERR_PTR(-ENOMEM);
  214. goto out;
  215. }
  216. err = -ENAMETOOLONG;
  217. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  218. "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  219. goto out_free_inst;
  220. ctx = crypto_instance_ctx(inst);
  221. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  222. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  223. if (err)
  224. goto out_free_inst;
  225. ctx->queue = queue;
  226. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  227. inst->alg.cra_priority = alg->cra_priority + 50;
  228. inst->alg.cra_blocksize = alg->cra_blocksize;
  229. inst->alg.cra_alignmask = alg->cra_alignmask;
  230. out:
  231. return inst;
  232. out_free_inst:
  233. kfree(inst);
  234. inst = ERR_PTR(err);
  235. goto out;
  236. }
  237. static struct crypto_instance *cryptd_alloc_blkcipher(
  238. struct rtattr **tb, struct cryptd_queue *queue)
  239. {
  240. struct crypto_instance *inst;
  241. struct crypto_alg *alg;
  242. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
  243. CRYPTO_ALG_TYPE_MASK);
  244. if (IS_ERR(alg))
  245. return ERR_CAST(alg);
  246. inst = cryptd_alloc_instance(alg, queue);
  247. if (IS_ERR(inst))
  248. goto out_put_alg;
  249. inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  250. inst->alg.cra_type = &crypto_ablkcipher_type;
  251. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  252. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  253. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  254. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  255. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  256. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  257. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  258. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  259. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  260. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  261. out_put_alg:
  262. crypto_mod_put(alg);
  263. return inst;
  264. }
  265. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  266. {
  267. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  268. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  269. struct crypto_spawn *spawn = &ictx->spawn;
  270. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  271. struct crypto_hash *cipher;
  272. cipher = crypto_spawn_hash(spawn);
  273. if (IS_ERR(cipher))
  274. return PTR_ERR(cipher);
  275. ctx->child = cipher;
  276. tfm->crt_ahash.reqsize =
  277. sizeof(struct cryptd_hash_request_ctx);
  278. return 0;
  279. }
  280. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  281. {
  282. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  283. crypto_free_hash(ctx->child);
  284. }
  285. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  286. const u8 *key, unsigned int keylen)
  287. {
  288. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  289. struct crypto_hash *child = ctx->child;
  290. int err;
  291. crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  292. crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
  293. CRYPTO_TFM_REQ_MASK);
  294. err = crypto_hash_setkey(child, key, keylen);
  295. crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
  296. CRYPTO_TFM_RES_MASK);
  297. return err;
  298. }
  299. static int cryptd_hash_enqueue(struct ahash_request *req,
  300. crypto_completion_t complete)
  301. {
  302. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  303. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  304. struct cryptd_queue *queue =
  305. cryptd_get_queue(crypto_ahash_tfm(tfm));
  306. rctx->complete = req->base.complete;
  307. req->base.complete = complete;
  308. return cryptd_enqueue_request(queue, &req->base);
  309. }
  310. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  311. {
  312. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  313. struct crypto_hash *child = ctx->child;
  314. struct ahash_request *req = ahash_request_cast(req_async);
  315. struct cryptd_hash_request_ctx *rctx;
  316. struct hash_desc desc;
  317. rctx = ahash_request_ctx(req);
  318. if (unlikely(err == -EINPROGRESS))
  319. goto out;
  320. desc.tfm = child;
  321. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  322. err = crypto_hash_crt(child)->init(&desc);
  323. req->base.complete = rctx->complete;
  324. out:
  325. local_bh_disable();
  326. rctx->complete(&req->base, err);
  327. local_bh_enable();
  328. }
  329. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  330. {
  331. return cryptd_hash_enqueue(req, cryptd_hash_init);
  332. }
  333. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  334. {
  335. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  336. struct crypto_hash *child = ctx->child;
  337. struct ahash_request *req = ahash_request_cast(req_async);
  338. struct cryptd_hash_request_ctx *rctx;
  339. struct hash_desc desc;
  340. rctx = ahash_request_ctx(req);
  341. if (unlikely(err == -EINPROGRESS))
  342. goto out;
  343. desc.tfm = child;
  344. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  345. err = crypto_hash_crt(child)->update(&desc,
  346. req->src,
  347. req->nbytes);
  348. req->base.complete = rctx->complete;
  349. out:
  350. local_bh_disable();
  351. rctx->complete(&req->base, err);
  352. local_bh_enable();
  353. }
  354. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  355. {
  356. return cryptd_hash_enqueue(req, cryptd_hash_update);
  357. }
  358. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  359. {
  360. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  361. struct crypto_hash *child = ctx->child;
  362. struct ahash_request *req = ahash_request_cast(req_async);
  363. struct cryptd_hash_request_ctx *rctx;
  364. struct hash_desc desc;
  365. rctx = ahash_request_ctx(req);
  366. if (unlikely(err == -EINPROGRESS))
  367. goto out;
  368. desc.tfm = child;
  369. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  370. err = crypto_hash_crt(child)->final(&desc, req->result);
  371. req->base.complete = rctx->complete;
  372. out:
  373. local_bh_disable();
  374. rctx->complete(&req->base, err);
  375. local_bh_enable();
  376. }
  377. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  378. {
  379. return cryptd_hash_enqueue(req, cryptd_hash_final);
  380. }
  381. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  382. {
  383. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  384. struct crypto_hash *child = ctx->child;
  385. struct ahash_request *req = ahash_request_cast(req_async);
  386. struct cryptd_hash_request_ctx *rctx;
  387. struct hash_desc desc;
  388. rctx = ahash_request_ctx(req);
  389. if (unlikely(err == -EINPROGRESS))
  390. goto out;
  391. desc.tfm = child;
  392. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  393. err = crypto_hash_crt(child)->digest(&desc,
  394. req->src,
  395. req->nbytes,
  396. req->result);
  397. req->base.complete = rctx->complete;
  398. out:
  399. local_bh_disable();
  400. rctx->complete(&req->base, err);
  401. local_bh_enable();
  402. }
  403. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  404. {
  405. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  406. }
  407. static struct crypto_instance *cryptd_alloc_hash(
  408. struct rtattr **tb, struct cryptd_queue *queue)
  409. {
  410. struct crypto_instance *inst;
  411. struct crypto_alg *alg;
  412. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
  413. CRYPTO_ALG_TYPE_HASH_MASK);
  414. if (IS_ERR(alg))
  415. return ERR_PTR(PTR_ERR(alg));
  416. inst = cryptd_alloc_instance(alg, queue);
  417. if (IS_ERR(inst))
  418. goto out_put_alg;
  419. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
  420. inst->alg.cra_type = &crypto_ahash_type;
  421. inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
  422. inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  423. inst->alg.cra_init = cryptd_hash_init_tfm;
  424. inst->alg.cra_exit = cryptd_hash_exit_tfm;
  425. inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
  426. inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
  427. inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
  428. inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
  429. inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
  430. out_put_alg:
  431. crypto_mod_put(alg);
  432. return inst;
  433. }
  434. static struct cryptd_queue queue;
  435. static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
  436. {
  437. struct crypto_attr_type *algt;
  438. algt = crypto_get_attr_type(tb);
  439. if (IS_ERR(algt))
  440. return ERR_CAST(algt);
  441. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  442. case CRYPTO_ALG_TYPE_BLKCIPHER:
  443. return cryptd_alloc_blkcipher(tb, &queue);
  444. case CRYPTO_ALG_TYPE_DIGEST:
  445. return cryptd_alloc_hash(tb, &queue);
  446. }
  447. return ERR_PTR(-EINVAL);
  448. }
  449. static void cryptd_free(struct crypto_instance *inst)
  450. {
  451. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  452. crypto_drop_spawn(&ctx->spawn);
  453. kfree(inst);
  454. }
  455. static struct crypto_template cryptd_tmpl = {
  456. .name = "cryptd",
  457. .alloc = cryptd_alloc,
  458. .free = cryptd_free,
  459. .module = THIS_MODULE,
  460. };
  461. struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
  462. u32 type, u32 mask)
  463. {
  464. char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
  465. struct crypto_tfm *tfm;
  466. if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
  467. "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
  468. return ERR_PTR(-EINVAL);
  469. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  470. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  471. mask &= ~CRYPTO_ALG_TYPE_MASK;
  472. mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
  473. tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
  474. if (IS_ERR(tfm))
  475. return ERR_CAST(tfm);
  476. if (tfm->__crt_alg->cra_module != THIS_MODULE) {
  477. crypto_free_tfm(tfm);
  478. return ERR_PTR(-EINVAL);
  479. }
  480. return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
  481. }
  482. EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
  483. struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
  484. {
  485. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
  486. return ctx->child;
  487. }
  488. EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
  489. void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
  490. {
  491. crypto_free_ablkcipher(&tfm->base);
  492. }
  493. EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
  494. static int __init cryptd_init(void)
  495. {
  496. int err;
  497. err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
  498. if (err)
  499. return err;
  500. err = crypto_register_template(&cryptd_tmpl);
  501. if (err)
  502. cryptd_fini_queue(&queue);
  503. return err;
  504. }
  505. static void __exit cryptd_exit(void)
  506. {
  507. cryptd_fini_queue(&queue);
  508. crypto_unregister_template(&cryptd_tmpl);
  509. }
  510. module_init(cryptd_init);
  511. module_exit(cryptd_exit);
  512. MODULE_LICENSE("GPL");
  513. MODULE_DESCRIPTION("Software async crypto daemon");