cryptd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kthread.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #define CRYPTD_MAX_QLEN 100
  26. struct cryptd_state {
  27. spinlock_t lock;
  28. struct mutex mutex;
  29. struct crypto_queue queue;
  30. struct task_struct *task;
  31. };
  32. struct cryptd_instance_ctx {
  33. struct crypto_spawn spawn;
  34. struct cryptd_state *state;
  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 inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
  49. {
  50. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  51. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  52. return ictx->state;
  53. }
  54. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  55. const u8 *key, unsigned int keylen)
  56. {
  57. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  58. struct crypto_blkcipher *child = ctx->child;
  59. int err;
  60. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  61. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  62. CRYPTO_TFM_REQ_MASK);
  63. err = crypto_blkcipher_setkey(child, key, keylen);
  64. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  65. CRYPTO_TFM_RES_MASK);
  66. return err;
  67. }
  68. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  69. struct crypto_blkcipher *child,
  70. int err,
  71. int (*crypt)(struct blkcipher_desc *desc,
  72. struct scatterlist *dst,
  73. struct scatterlist *src,
  74. unsigned int len))
  75. {
  76. struct cryptd_blkcipher_request_ctx *rctx;
  77. struct blkcipher_desc desc;
  78. rctx = ablkcipher_request_ctx(req);
  79. if (unlikely(err == -EINPROGRESS))
  80. goto out;
  81. desc.tfm = child;
  82. desc.info = req->info;
  83. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  84. err = crypt(&desc, req->dst, req->src, req->nbytes);
  85. req->base.complete = rctx->complete;
  86. out:
  87. local_bh_disable();
  88. rctx->complete(&req->base, err);
  89. local_bh_enable();
  90. }
  91. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  92. {
  93. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  94. struct crypto_blkcipher *child = ctx->child;
  95. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  96. crypto_blkcipher_crt(child)->encrypt);
  97. }
  98. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  99. {
  100. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  101. struct crypto_blkcipher *child = ctx->child;
  102. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  103. crypto_blkcipher_crt(child)->decrypt);
  104. }
  105. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  106. crypto_completion_t complete)
  107. {
  108. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  109. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  110. struct cryptd_state *state =
  111. cryptd_get_state(crypto_ablkcipher_tfm(tfm));
  112. int err;
  113. rctx->complete = req->base.complete;
  114. req->base.complete = complete;
  115. spin_lock_bh(&state->lock);
  116. err = ablkcipher_enqueue_request(&state->queue, req);
  117. spin_unlock_bh(&state->lock);
  118. wake_up_process(state->task);
  119. return err;
  120. }
  121. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  122. {
  123. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  124. }
  125. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  126. {
  127. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  128. }
  129. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  130. {
  131. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  132. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  133. struct crypto_spawn *spawn = &ictx->spawn;
  134. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  135. struct crypto_blkcipher *cipher;
  136. cipher = crypto_spawn_blkcipher(spawn);
  137. if (IS_ERR(cipher))
  138. return PTR_ERR(cipher);
  139. ctx->child = cipher;
  140. tfm->crt_ablkcipher.reqsize =
  141. sizeof(struct cryptd_blkcipher_request_ctx);
  142. return 0;
  143. }
  144. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  145. {
  146. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  147. struct cryptd_state *state = cryptd_get_state(tfm);
  148. int active;
  149. mutex_lock(&state->mutex);
  150. active = ablkcipher_tfm_in_queue(&state->queue,
  151. __crypto_ablkcipher_cast(tfm));
  152. mutex_unlock(&state->mutex);
  153. BUG_ON(active);
  154. crypto_free_blkcipher(ctx->child);
  155. }
  156. static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
  157. struct cryptd_state *state)
  158. {
  159. struct crypto_instance *inst;
  160. struct cryptd_instance_ctx *ctx;
  161. int err;
  162. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  163. if (!inst) {
  164. inst = ERR_PTR(-ENOMEM);
  165. goto out;
  166. }
  167. err = -ENAMETOOLONG;
  168. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  169. "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  170. goto out_free_inst;
  171. ctx = crypto_instance_ctx(inst);
  172. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  173. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  174. if (err)
  175. goto out_free_inst;
  176. ctx->state = state;
  177. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  178. inst->alg.cra_priority = alg->cra_priority + 50;
  179. inst->alg.cra_blocksize = alg->cra_blocksize;
  180. inst->alg.cra_alignmask = alg->cra_alignmask;
  181. out:
  182. return inst;
  183. out_free_inst:
  184. kfree(inst);
  185. inst = ERR_PTR(err);
  186. goto out;
  187. }
  188. static struct crypto_instance *cryptd_alloc_blkcipher(
  189. struct rtattr **tb, struct cryptd_state *state)
  190. {
  191. struct crypto_instance *inst;
  192. struct crypto_alg *alg;
  193. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
  194. CRYPTO_ALG_TYPE_MASK);
  195. if (IS_ERR(alg))
  196. return ERR_CAST(alg);
  197. inst = cryptd_alloc_instance(alg, state);
  198. if (IS_ERR(inst))
  199. goto out_put_alg;
  200. inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  201. inst->alg.cra_type = &crypto_ablkcipher_type;
  202. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  203. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  204. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  205. inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
  206. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  207. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  208. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  209. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  210. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  211. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  212. out_put_alg:
  213. crypto_mod_put(alg);
  214. return inst;
  215. }
  216. static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
  217. {
  218. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  219. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  220. struct crypto_spawn *spawn = &ictx->spawn;
  221. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  222. struct crypto_hash *cipher;
  223. cipher = crypto_spawn_hash(spawn);
  224. if (IS_ERR(cipher))
  225. return PTR_ERR(cipher);
  226. ctx->child = cipher;
  227. tfm->crt_ahash.reqsize =
  228. sizeof(struct cryptd_hash_request_ctx);
  229. return 0;
  230. }
  231. static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
  232. {
  233. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  234. struct cryptd_state *state = cryptd_get_state(tfm);
  235. int active;
  236. mutex_lock(&state->mutex);
  237. active = ahash_tfm_in_queue(&state->queue,
  238. __crypto_ahash_cast(tfm));
  239. mutex_unlock(&state->mutex);
  240. BUG_ON(active);
  241. crypto_free_hash(ctx->child);
  242. }
  243. static int cryptd_hash_setkey(struct crypto_ahash *parent,
  244. const u8 *key, unsigned int keylen)
  245. {
  246. struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
  247. struct crypto_hash *child = ctx->child;
  248. int err;
  249. crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  250. crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
  251. CRYPTO_TFM_REQ_MASK);
  252. err = crypto_hash_setkey(child, key, keylen);
  253. crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
  254. CRYPTO_TFM_RES_MASK);
  255. return err;
  256. }
  257. static int cryptd_hash_enqueue(struct ahash_request *req,
  258. crypto_completion_t complete)
  259. {
  260. struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
  261. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  262. struct cryptd_state *state =
  263. cryptd_get_state(crypto_ahash_tfm(tfm));
  264. int err;
  265. rctx->complete = req->base.complete;
  266. req->base.complete = complete;
  267. spin_lock_bh(&state->lock);
  268. err = ahash_enqueue_request(&state->queue, req);
  269. spin_unlock_bh(&state->lock);
  270. wake_up_process(state->task);
  271. return err;
  272. }
  273. static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
  274. {
  275. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  276. struct crypto_hash *child = ctx->child;
  277. struct ahash_request *req = ahash_request_cast(req_async);
  278. struct cryptd_hash_request_ctx *rctx;
  279. struct hash_desc desc;
  280. rctx = ahash_request_ctx(req);
  281. if (unlikely(err == -EINPROGRESS))
  282. goto out;
  283. desc.tfm = child;
  284. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  285. err = crypto_hash_crt(child)->init(&desc);
  286. req->base.complete = rctx->complete;
  287. out:
  288. local_bh_disable();
  289. rctx->complete(&req->base, err);
  290. local_bh_enable();
  291. }
  292. static int cryptd_hash_init_enqueue(struct ahash_request *req)
  293. {
  294. return cryptd_hash_enqueue(req, cryptd_hash_init);
  295. }
  296. static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
  297. {
  298. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  299. struct crypto_hash *child = ctx->child;
  300. struct ahash_request *req = ahash_request_cast(req_async);
  301. struct cryptd_hash_request_ctx *rctx;
  302. struct hash_desc desc;
  303. rctx = ahash_request_ctx(req);
  304. if (unlikely(err == -EINPROGRESS))
  305. goto out;
  306. desc.tfm = child;
  307. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  308. err = crypto_hash_crt(child)->update(&desc,
  309. req->src,
  310. req->nbytes);
  311. req->base.complete = rctx->complete;
  312. out:
  313. local_bh_disable();
  314. rctx->complete(&req->base, err);
  315. local_bh_enable();
  316. }
  317. static int cryptd_hash_update_enqueue(struct ahash_request *req)
  318. {
  319. return cryptd_hash_enqueue(req, cryptd_hash_update);
  320. }
  321. static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
  322. {
  323. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  324. struct crypto_hash *child = ctx->child;
  325. struct ahash_request *req = ahash_request_cast(req_async);
  326. struct cryptd_hash_request_ctx *rctx;
  327. struct hash_desc desc;
  328. rctx = ahash_request_ctx(req);
  329. if (unlikely(err == -EINPROGRESS))
  330. goto out;
  331. desc.tfm = child;
  332. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  333. err = crypto_hash_crt(child)->final(&desc, req->result);
  334. req->base.complete = rctx->complete;
  335. out:
  336. local_bh_disable();
  337. rctx->complete(&req->base, err);
  338. local_bh_enable();
  339. }
  340. static int cryptd_hash_final_enqueue(struct ahash_request *req)
  341. {
  342. return cryptd_hash_enqueue(req, cryptd_hash_final);
  343. }
  344. static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
  345. {
  346. struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
  347. struct crypto_hash *child = ctx->child;
  348. struct ahash_request *req = ahash_request_cast(req_async);
  349. struct cryptd_hash_request_ctx *rctx;
  350. struct hash_desc desc;
  351. rctx = ahash_request_ctx(req);
  352. if (unlikely(err == -EINPROGRESS))
  353. goto out;
  354. desc.tfm = child;
  355. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  356. err = crypto_hash_crt(child)->digest(&desc,
  357. req->src,
  358. req->nbytes,
  359. req->result);
  360. req->base.complete = rctx->complete;
  361. out:
  362. local_bh_disable();
  363. rctx->complete(&req->base, err);
  364. local_bh_enable();
  365. }
  366. static int cryptd_hash_digest_enqueue(struct ahash_request *req)
  367. {
  368. return cryptd_hash_enqueue(req, cryptd_hash_digest);
  369. }
  370. static struct crypto_instance *cryptd_alloc_hash(
  371. struct rtattr **tb, struct cryptd_state *state)
  372. {
  373. struct crypto_instance *inst;
  374. struct crypto_alg *alg;
  375. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
  376. CRYPTO_ALG_TYPE_HASH_MASK);
  377. if (IS_ERR(alg))
  378. return ERR_PTR(PTR_ERR(alg));
  379. inst = cryptd_alloc_instance(alg, state);
  380. if (IS_ERR(inst))
  381. goto out_put_alg;
  382. inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
  383. inst->alg.cra_type = &crypto_ahash_type;
  384. inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
  385. inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
  386. inst->alg.cra_init = cryptd_hash_init_tfm;
  387. inst->alg.cra_exit = cryptd_hash_exit_tfm;
  388. inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
  389. inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
  390. inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
  391. inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
  392. inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
  393. out_put_alg:
  394. crypto_mod_put(alg);
  395. return inst;
  396. }
  397. static struct cryptd_state state;
  398. static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
  399. {
  400. struct crypto_attr_type *algt;
  401. algt = crypto_get_attr_type(tb);
  402. if (IS_ERR(algt))
  403. return ERR_CAST(algt);
  404. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  405. case CRYPTO_ALG_TYPE_BLKCIPHER:
  406. return cryptd_alloc_blkcipher(tb, &state);
  407. case CRYPTO_ALG_TYPE_DIGEST:
  408. return cryptd_alloc_hash(tb, &state);
  409. }
  410. return ERR_PTR(-EINVAL);
  411. }
  412. static void cryptd_free(struct crypto_instance *inst)
  413. {
  414. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  415. crypto_drop_spawn(&ctx->spawn);
  416. kfree(inst);
  417. }
  418. static struct crypto_template cryptd_tmpl = {
  419. .name = "cryptd",
  420. .alloc = cryptd_alloc,
  421. .free = cryptd_free,
  422. .module = THIS_MODULE,
  423. };
  424. static inline int cryptd_create_thread(struct cryptd_state *state,
  425. int (*fn)(void *data), const char *name)
  426. {
  427. spin_lock_init(&state->lock);
  428. mutex_init(&state->mutex);
  429. crypto_init_queue(&state->queue, CRYPTD_MAX_QLEN);
  430. state->task = kthread_run(fn, state, name);
  431. if (IS_ERR(state->task))
  432. return PTR_ERR(state->task);
  433. return 0;
  434. }
  435. static inline void cryptd_stop_thread(struct cryptd_state *state)
  436. {
  437. BUG_ON(state->queue.qlen);
  438. kthread_stop(state->task);
  439. }
  440. static int cryptd_thread(void *data)
  441. {
  442. struct cryptd_state *state = data;
  443. int stop;
  444. current->flags |= PF_NOFREEZE;
  445. do {
  446. struct crypto_async_request *req, *backlog;
  447. mutex_lock(&state->mutex);
  448. __set_current_state(TASK_INTERRUPTIBLE);
  449. spin_lock_bh(&state->lock);
  450. backlog = crypto_get_backlog(&state->queue);
  451. req = crypto_dequeue_request(&state->queue);
  452. spin_unlock_bh(&state->lock);
  453. stop = kthread_should_stop();
  454. if (stop || req) {
  455. __set_current_state(TASK_RUNNING);
  456. if (req) {
  457. if (backlog)
  458. backlog->complete(backlog,
  459. -EINPROGRESS);
  460. req->complete(req, 0);
  461. }
  462. }
  463. mutex_unlock(&state->mutex);
  464. schedule();
  465. } while (!stop);
  466. return 0;
  467. }
  468. static int __init cryptd_init(void)
  469. {
  470. int err;
  471. err = cryptd_create_thread(&state, cryptd_thread, "cryptd");
  472. if (err)
  473. return err;
  474. err = crypto_register_template(&cryptd_tmpl);
  475. if (err)
  476. kthread_stop(state.task);
  477. return err;
  478. }
  479. static void __exit cryptd_exit(void)
  480. {
  481. cryptd_stop_thread(&state);
  482. crypto_unregister_template(&cryptd_tmpl);
  483. }
  484. module_init(cryptd_init);
  485. module_exit(cryptd_exit);
  486. MODULE_LICENSE("GPL");
  487. MODULE_DESCRIPTION("Software async crypto daemon");