cryptd.c 26 KB

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