2 * Software async crypto daemon.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #include <crypto/algapi.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/cryptd.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/scatterlist.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
28 #define CRYPTD_MAX_QLEN 100
33 struct crypto_queue queue;
34 struct task_struct *task;
37 struct cryptd_instance_ctx {
38 struct crypto_spawn spawn;
39 struct cryptd_state *state;
42 struct cryptd_blkcipher_ctx {
43 struct crypto_blkcipher *child;
46 struct cryptd_blkcipher_request_ctx {
47 crypto_completion_t complete;
50 struct cryptd_hash_ctx {
51 struct crypto_hash *child;
54 struct cryptd_hash_request_ctx {
55 crypto_completion_t complete;
58 static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
60 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
61 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
65 static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
66 const u8 *key, unsigned int keylen)
68 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
69 struct crypto_blkcipher *child = ctx->child;
72 crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
73 crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
75 err = crypto_blkcipher_setkey(child, key, keylen);
76 crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
81 static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
82 struct crypto_blkcipher *child,
84 int (*crypt)(struct blkcipher_desc *desc,
85 struct scatterlist *dst,
86 struct scatterlist *src,
89 struct cryptd_blkcipher_request_ctx *rctx;
90 struct blkcipher_desc desc;
92 rctx = ablkcipher_request_ctx(req);
94 if (unlikely(err == -EINPROGRESS))
98 desc.info = req->info;
99 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
101 err = crypt(&desc, req->dst, req->src, req->nbytes);
103 req->base.complete = rctx->complete;
107 rctx->complete(&req->base, err);
111 static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
113 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
114 struct crypto_blkcipher *child = ctx->child;
116 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
117 crypto_blkcipher_crt(child)->encrypt);
120 static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
122 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
123 struct crypto_blkcipher *child = ctx->child;
125 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
126 crypto_blkcipher_crt(child)->decrypt);
129 static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
130 crypto_completion_t complete)
132 struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
133 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
134 struct cryptd_state *state =
135 cryptd_get_state(crypto_ablkcipher_tfm(tfm));
138 rctx->complete = req->base.complete;
139 req->base.complete = complete;
141 spin_lock_bh(&state->lock);
142 err = ablkcipher_enqueue_request(&state->queue, req);
143 spin_unlock_bh(&state->lock);
145 wake_up_process(state->task);
149 static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
151 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
154 static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
156 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
159 static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
161 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
162 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
163 struct crypto_spawn *spawn = &ictx->spawn;
164 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
165 struct crypto_blkcipher *cipher;
167 cipher = crypto_spawn_blkcipher(spawn);
169 return PTR_ERR(cipher);
172 tfm->crt_ablkcipher.reqsize =
173 sizeof(struct cryptd_blkcipher_request_ctx);
177 static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
179 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
180 struct cryptd_state *state = cryptd_get_state(tfm);
183 mutex_lock(&state->mutex);
184 active = ablkcipher_tfm_in_queue(&state->queue,
185 __crypto_ablkcipher_cast(tfm));
186 mutex_unlock(&state->mutex);
190 crypto_free_blkcipher(ctx->child);
193 static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
194 struct cryptd_state *state)
196 struct crypto_instance *inst;
197 struct cryptd_instance_ctx *ctx;
200 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
202 inst = ERR_PTR(-ENOMEM);
207 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
208 "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
211 ctx = crypto_instance_ctx(inst);
212 err = crypto_init_spawn(&ctx->spawn, alg, inst,
213 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
219 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
221 inst->alg.cra_priority = alg->cra_priority + 50;
222 inst->alg.cra_blocksize = alg->cra_blocksize;
223 inst->alg.cra_alignmask = alg->cra_alignmask;
234 static struct crypto_instance *cryptd_alloc_blkcipher(
235 struct rtattr **tb, struct cryptd_state *state)
237 struct crypto_instance *inst;
238 struct crypto_alg *alg;
240 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
241 CRYPTO_ALG_TYPE_MASK);
243 return ERR_CAST(alg);
245 inst = cryptd_alloc_instance(alg, state);
249 inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
250 inst->alg.cra_type = &crypto_ablkcipher_type;
252 inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
253 inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
254 inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
256 inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
258 inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
260 inst->alg.cra_init = cryptd_blkcipher_init_tfm;
261 inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
263 inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
264 inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
265 inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
272 static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
274 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
275 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
276 struct crypto_spawn *spawn = &ictx->spawn;
277 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
278 struct crypto_hash *cipher;
280 cipher = crypto_spawn_hash(spawn);
282 return PTR_ERR(cipher);
285 tfm->crt_ahash.reqsize =
286 sizeof(struct cryptd_hash_request_ctx);
290 static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
292 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
293 struct cryptd_state *state = cryptd_get_state(tfm);
296 mutex_lock(&state->mutex);
297 active = ahash_tfm_in_queue(&state->queue,
298 __crypto_ahash_cast(tfm));
299 mutex_unlock(&state->mutex);
303 crypto_free_hash(ctx->child);
306 static int cryptd_hash_setkey(struct crypto_ahash *parent,
307 const u8 *key, unsigned int keylen)
309 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
310 struct crypto_hash *child = ctx->child;
313 crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
314 crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
315 CRYPTO_TFM_REQ_MASK);
316 err = crypto_hash_setkey(child, key, keylen);
317 crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
318 CRYPTO_TFM_RES_MASK);
322 static int cryptd_hash_enqueue(struct ahash_request *req,
323 crypto_completion_t complete)
325 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
326 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
327 struct cryptd_state *state =
328 cryptd_get_state(crypto_ahash_tfm(tfm));
331 rctx->complete = req->base.complete;
332 req->base.complete = complete;
334 spin_lock_bh(&state->lock);
335 err = ahash_enqueue_request(&state->queue, req);
336 spin_unlock_bh(&state->lock);
338 wake_up_process(state->task);
342 static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
344 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
345 struct crypto_hash *child = ctx->child;
346 struct ahash_request *req = ahash_request_cast(req_async);
347 struct cryptd_hash_request_ctx *rctx;
348 struct hash_desc desc;
350 rctx = ahash_request_ctx(req);
352 if (unlikely(err == -EINPROGRESS))
356 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
358 err = crypto_hash_crt(child)->init(&desc);
360 req->base.complete = rctx->complete;
364 rctx->complete(&req->base, err);
368 static int cryptd_hash_init_enqueue(struct ahash_request *req)
370 return cryptd_hash_enqueue(req, cryptd_hash_init);
373 static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
375 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
376 struct crypto_hash *child = ctx->child;
377 struct ahash_request *req = ahash_request_cast(req_async);
378 struct cryptd_hash_request_ctx *rctx;
379 struct hash_desc desc;
381 rctx = ahash_request_ctx(req);
383 if (unlikely(err == -EINPROGRESS))
387 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
389 err = crypto_hash_crt(child)->update(&desc,
393 req->base.complete = rctx->complete;
397 rctx->complete(&req->base, err);
401 static int cryptd_hash_update_enqueue(struct ahash_request *req)
403 return cryptd_hash_enqueue(req, cryptd_hash_update);
406 static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
408 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
409 struct crypto_hash *child = ctx->child;
410 struct ahash_request *req = ahash_request_cast(req_async);
411 struct cryptd_hash_request_ctx *rctx;
412 struct hash_desc desc;
414 rctx = ahash_request_ctx(req);
416 if (unlikely(err == -EINPROGRESS))
420 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
422 err = crypto_hash_crt(child)->final(&desc, req->result);
424 req->base.complete = rctx->complete;
428 rctx->complete(&req->base, err);
432 static int cryptd_hash_final_enqueue(struct ahash_request *req)
434 return cryptd_hash_enqueue(req, cryptd_hash_final);
437 static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
439 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
440 struct crypto_hash *child = ctx->child;
441 struct ahash_request *req = ahash_request_cast(req_async);
442 struct cryptd_hash_request_ctx *rctx;
443 struct hash_desc desc;
445 rctx = ahash_request_ctx(req);
447 if (unlikely(err == -EINPROGRESS))
451 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
453 err = crypto_hash_crt(child)->digest(&desc,
458 req->base.complete = rctx->complete;
462 rctx->complete(&req->base, err);
466 static int cryptd_hash_digest_enqueue(struct ahash_request *req)
468 return cryptd_hash_enqueue(req, cryptd_hash_digest);
471 static struct crypto_instance *cryptd_alloc_hash(
472 struct rtattr **tb, struct cryptd_state *state)
474 struct crypto_instance *inst;
475 struct crypto_alg *alg;
477 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
478 CRYPTO_ALG_TYPE_HASH_MASK);
480 return ERR_PTR(PTR_ERR(alg));
482 inst = cryptd_alloc_instance(alg, state);
486 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
487 inst->alg.cra_type = &crypto_ahash_type;
489 inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
490 inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
492 inst->alg.cra_init = cryptd_hash_init_tfm;
493 inst->alg.cra_exit = cryptd_hash_exit_tfm;
495 inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
496 inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
497 inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
498 inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
499 inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
506 static struct cryptd_state state;
508 static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
510 struct crypto_attr_type *algt;
512 algt = crypto_get_attr_type(tb);
514 return ERR_CAST(algt);
516 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
517 case CRYPTO_ALG_TYPE_BLKCIPHER:
518 return cryptd_alloc_blkcipher(tb, &state);
519 case CRYPTO_ALG_TYPE_DIGEST:
520 return cryptd_alloc_hash(tb, &state);
523 return ERR_PTR(-EINVAL);
526 static void cryptd_free(struct crypto_instance *inst)
528 struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
530 crypto_drop_spawn(&ctx->spawn);
534 static struct crypto_template cryptd_tmpl = {
536 .alloc = cryptd_alloc,
538 .module = THIS_MODULE,
541 struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
544 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
545 struct crypto_ablkcipher *tfm;
547 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
548 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
549 return ERR_PTR(-EINVAL);
550 tfm = crypto_alloc_ablkcipher(cryptd_alg_name, type, mask);
552 return ERR_CAST(tfm);
553 if (crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_module != THIS_MODULE) {
554 crypto_free_ablkcipher(tfm);
555 return ERR_PTR(-EINVAL);
558 return __cryptd_ablkcipher_cast(tfm);
560 EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
562 struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
564 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
567 EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
569 void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
571 crypto_free_ablkcipher(&tfm->base);
573 EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
575 static inline int cryptd_create_thread(struct cryptd_state *state,
576 int (*fn)(void *data), const char *name)
578 spin_lock_init(&state->lock);
579 mutex_init(&state->mutex);
580 crypto_init_queue(&state->queue, CRYPTD_MAX_QLEN);
582 state->task = kthread_run(fn, state, name);
583 if (IS_ERR(state->task))
584 return PTR_ERR(state->task);
589 static inline void cryptd_stop_thread(struct cryptd_state *state)
591 BUG_ON(state->queue.qlen);
592 kthread_stop(state->task);
595 static int cryptd_thread(void *data)
597 struct cryptd_state *state = data;
600 current->flags |= PF_NOFREEZE;
603 struct crypto_async_request *req, *backlog;
605 mutex_lock(&state->mutex);
606 __set_current_state(TASK_INTERRUPTIBLE);
608 spin_lock_bh(&state->lock);
609 backlog = crypto_get_backlog(&state->queue);
610 req = crypto_dequeue_request(&state->queue);
611 spin_unlock_bh(&state->lock);
613 stop = kthread_should_stop();
616 __set_current_state(TASK_RUNNING);
619 backlog->complete(backlog,
621 req->complete(req, 0);
625 mutex_unlock(&state->mutex);
633 static int __init cryptd_init(void)
637 err = cryptd_create_thread(&state, cryptd_thread, "cryptd");
641 err = crypto_register_template(&cryptd_tmpl);
643 kthread_stop(state.task);
648 static void __exit cryptd_exit(void)
650 cryptd_stop_thread(&state);
651 crypto_unregister_template(&cryptd_tmpl);
654 module_init(cryptd_init);
655 module_exit(cryptd_exit);
657 MODULE_LICENSE("GPL");
658 MODULE_DESCRIPTION("Software async crypto daemon");