cryptd.c 26 KB

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