cryptd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kthread.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #define CRYPTD_MAX_QLEN 100
  25. struct cryptd_state {
  26. spinlock_t lock;
  27. struct mutex mutex;
  28. struct crypto_queue queue;
  29. struct task_struct *task;
  30. };
  31. struct cryptd_instance_ctx {
  32. struct crypto_spawn spawn;
  33. struct cryptd_state *state;
  34. };
  35. struct cryptd_blkcipher_ctx {
  36. struct crypto_blkcipher *child;
  37. };
  38. struct cryptd_blkcipher_request_ctx {
  39. crypto_completion_t complete;
  40. };
  41. static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
  42. {
  43. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  44. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  45. return ictx->state;
  46. }
  47. static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
  48. const u8 *key, unsigned int keylen)
  49. {
  50. struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
  51. struct crypto_blkcipher *child = ctx->child;
  52. int err;
  53. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  54. crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  55. CRYPTO_TFM_REQ_MASK);
  56. err = crypto_blkcipher_setkey(child, key, keylen);
  57. crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
  58. CRYPTO_TFM_RES_MASK);
  59. return err;
  60. }
  61. static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
  62. struct crypto_blkcipher *child,
  63. int err,
  64. int (*crypt)(struct blkcipher_desc *desc,
  65. struct scatterlist *dst,
  66. struct scatterlist *src,
  67. unsigned int len))
  68. {
  69. struct cryptd_blkcipher_request_ctx *rctx;
  70. struct blkcipher_desc desc;
  71. rctx = ablkcipher_request_ctx(req);
  72. if (unlikely(err == -EINPROGRESS)) {
  73. rctx->complete(&req->base, err);
  74. return;
  75. }
  76. desc.tfm = child;
  77. desc.info = req->info;
  78. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  79. err = crypt(&desc, req->dst, req->src, req->nbytes);
  80. req->base.complete = rctx->complete;
  81. local_bh_disable();
  82. req->base.complete(&req->base, err);
  83. local_bh_enable();
  84. }
  85. static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
  86. {
  87. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  88. struct crypto_blkcipher *child = ctx->child;
  89. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  90. crypto_blkcipher_crt(child)->encrypt);
  91. }
  92. static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
  93. {
  94. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
  95. struct crypto_blkcipher *child = ctx->child;
  96. cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
  97. crypto_blkcipher_crt(child)->decrypt);
  98. }
  99. static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
  100. crypto_completion_t complete)
  101. {
  102. struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
  103. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  104. struct cryptd_state *state =
  105. cryptd_get_state(crypto_ablkcipher_tfm(tfm));
  106. int err;
  107. rctx->complete = req->base.complete;
  108. req->base.complete = complete;
  109. spin_lock_bh(&state->lock);
  110. err = ablkcipher_enqueue_request(&state->queue, req);
  111. spin_unlock_bh(&state->lock);
  112. wake_up_process(state->task);
  113. return err;
  114. }
  115. static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
  116. {
  117. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
  118. }
  119. static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
  120. {
  121. return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
  122. }
  123. static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
  124. {
  125. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  126. struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
  127. struct crypto_spawn *spawn = &ictx->spawn;
  128. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  129. struct crypto_blkcipher *cipher;
  130. cipher = crypto_spawn_blkcipher(spawn);
  131. if (IS_ERR(cipher))
  132. return PTR_ERR(cipher);
  133. ctx->child = cipher;
  134. tfm->crt_ablkcipher.reqsize =
  135. sizeof(struct cryptd_blkcipher_request_ctx);
  136. return 0;
  137. }
  138. static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
  139. {
  140. struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  141. struct cryptd_state *state = cryptd_get_state(tfm);
  142. int active;
  143. mutex_lock(&state->mutex);
  144. active = ablkcipher_tfm_in_queue(&state->queue,
  145. __crypto_ablkcipher_cast(tfm));
  146. mutex_unlock(&state->mutex);
  147. BUG_ON(active);
  148. crypto_free_blkcipher(ctx->child);
  149. }
  150. static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
  151. struct cryptd_state *state)
  152. {
  153. struct crypto_instance *inst;
  154. struct cryptd_instance_ctx *ctx;
  155. int err;
  156. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  157. if (IS_ERR(inst))
  158. goto out;
  159. err = -ENAMETOOLONG;
  160. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  161. "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  162. goto out_free_inst;
  163. ctx = crypto_instance_ctx(inst);
  164. err = crypto_init_spawn(&ctx->spawn, alg, inst,
  165. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  166. if (err)
  167. goto out_free_inst;
  168. ctx->state = state;
  169. memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  170. inst->alg.cra_priority = alg->cra_priority + 50;
  171. inst->alg.cra_blocksize = alg->cra_blocksize;
  172. inst->alg.cra_alignmask = alg->cra_alignmask;
  173. out:
  174. return inst;
  175. out_free_inst:
  176. kfree(inst);
  177. inst = ERR_PTR(err);
  178. goto out;
  179. }
  180. static struct crypto_instance *cryptd_alloc_blkcipher(
  181. struct rtattr **tb, struct cryptd_state *state)
  182. {
  183. struct crypto_instance *inst;
  184. struct crypto_alg *alg;
  185. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
  186. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  187. if (IS_ERR(alg))
  188. return ERR_PTR(PTR_ERR(alg));
  189. inst = cryptd_alloc_instance(alg, state);
  190. if (IS_ERR(inst))
  191. goto out_put_alg;
  192. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_ASYNC;
  193. inst->alg.cra_type = &crypto_ablkcipher_type;
  194. inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
  195. inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  196. inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  197. inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
  198. inst->alg.cra_init = cryptd_blkcipher_init_tfm;
  199. inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
  200. inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
  201. inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
  202. inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
  203. out_put_alg:
  204. crypto_mod_put(alg);
  205. return inst;
  206. }
  207. static struct cryptd_state state;
  208. static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
  209. {
  210. struct crypto_attr_type *algt;
  211. algt = crypto_get_attr_type(tb);
  212. if (IS_ERR(algt))
  213. return ERR_PTR(PTR_ERR(algt));
  214. switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
  215. case CRYPTO_ALG_TYPE_BLKCIPHER:
  216. return cryptd_alloc_blkcipher(tb, &state);
  217. }
  218. return ERR_PTR(-EINVAL);
  219. }
  220. static void cryptd_free(struct crypto_instance *inst)
  221. {
  222. struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
  223. crypto_drop_spawn(&ctx->spawn);
  224. kfree(inst);
  225. }
  226. static struct crypto_template cryptd_tmpl = {
  227. .name = "cryptd",
  228. .alloc = cryptd_alloc,
  229. .free = cryptd_free,
  230. .module = THIS_MODULE,
  231. };
  232. static inline int cryptd_create_thread(struct cryptd_state *state,
  233. int (*fn)(void *data), const char *name)
  234. {
  235. spin_lock_init(&state->lock);
  236. mutex_init(&state->mutex);
  237. crypto_init_queue(&state->queue, CRYPTD_MAX_QLEN);
  238. state->task = kthread_run(fn, state, name);
  239. if (IS_ERR(state->task))
  240. return PTR_ERR(state->task);
  241. return 0;
  242. }
  243. static inline void cryptd_stop_thread(struct cryptd_state *state)
  244. {
  245. BUG_ON(state->queue.qlen);
  246. kthread_stop(state->task);
  247. }
  248. static int cryptd_thread(void *data)
  249. {
  250. struct cryptd_state *state = data;
  251. int stop;
  252. current->flags |= PF_NOFREEZE;
  253. do {
  254. struct crypto_async_request *req, *backlog;
  255. mutex_lock(&state->mutex);
  256. __set_current_state(TASK_INTERRUPTIBLE);
  257. spin_lock_bh(&state->lock);
  258. backlog = crypto_get_backlog(&state->queue);
  259. req = crypto_dequeue_request(&state->queue);
  260. spin_unlock_bh(&state->lock);
  261. stop = kthread_should_stop();
  262. if (stop || req) {
  263. __set_current_state(TASK_RUNNING);
  264. if (req) {
  265. if (backlog)
  266. backlog->complete(backlog,
  267. -EINPROGRESS);
  268. req->complete(req, 0);
  269. }
  270. }
  271. mutex_unlock(&state->mutex);
  272. schedule();
  273. } while (!stop);
  274. return 0;
  275. }
  276. static int __init cryptd_init(void)
  277. {
  278. int err;
  279. err = cryptd_create_thread(&state, cryptd_thread, "cryptd");
  280. if (err)
  281. return err;
  282. err = crypto_register_template(&cryptd_tmpl);
  283. if (err)
  284. kthread_stop(state.task);
  285. return err;
  286. }
  287. static void __exit cryptd_exit(void)
  288. {
  289. cryptd_stop_thread(&state);
  290. crypto_unregister_template(&cryptd_tmpl);
  291. }
  292. module_init(cryptd_init);
  293. module_exit(cryptd_exit);
  294. MODULE_LICENSE("GPL");
  295. MODULE_DESCRIPTION("Software async crypto daemon");