neighbor free after disconnect
[cor_2_6_31.git] / crypto / cryptd.c
blobae5fa99d5d3666b4f483469f1cd1464bb36cb172
1 /*
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)
9 * any later version.
13 #include <crypto/algapi.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/cryptd.h>
16 #include <crypto/crypto_wq.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/scatterlist.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
26 #define CRYPTD_MAX_CPU_QLEN 100
28 struct cryptd_cpu_queue {
29 struct crypto_queue queue;
30 struct work_struct work;
33 struct cryptd_queue {
34 struct cryptd_cpu_queue *cpu_queue;
37 struct cryptd_instance_ctx {
38 struct crypto_spawn spawn;
39 struct cryptd_queue *queue;
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 void cryptd_queue_worker(struct work_struct *work);
60 static int cryptd_init_queue(struct cryptd_queue *queue,
61 unsigned int max_cpu_qlen)
63 int cpu;
64 struct cryptd_cpu_queue *cpu_queue;
66 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
67 if (!queue->cpu_queue)
68 return -ENOMEM;
69 for_each_possible_cpu(cpu) {
70 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
71 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
72 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
74 return 0;
77 static void cryptd_fini_queue(struct cryptd_queue *queue)
79 int cpu;
80 struct cryptd_cpu_queue *cpu_queue;
82 for_each_possible_cpu(cpu) {
83 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
84 BUG_ON(cpu_queue->queue.qlen);
86 free_percpu(queue->cpu_queue);
89 static int cryptd_enqueue_request(struct cryptd_queue *queue,
90 struct crypto_async_request *request)
92 int cpu, err;
93 struct cryptd_cpu_queue *cpu_queue;
95 cpu = get_cpu();
96 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
97 err = crypto_enqueue_request(&cpu_queue->queue, request);
98 queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
99 put_cpu();
101 return err;
104 /* Called in workqueue context, do one real cryption work (via
105 * req->complete) and reschedule itself if there are more work to
106 * do. */
107 static void cryptd_queue_worker(struct work_struct *work)
109 struct cryptd_cpu_queue *cpu_queue;
110 struct crypto_async_request *req, *backlog;
112 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
113 /* Only handle one request at a time to avoid hogging crypto
114 * workqueue. preempt_disable/enable is used to prevent
115 * being preempted by cryptd_enqueue_request() */
116 preempt_disable();
117 backlog = crypto_get_backlog(&cpu_queue->queue);
118 req = crypto_dequeue_request(&cpu_queue->queue);
119 preempt_enable();
121 if (!req)
122 return;
124 if (backlog)
125 backlog->complete(backlog, -EINPROGRESS);
126 req->complete(req, 0);
128 if (cpu_queue->queue.qlen)
129 queue_work(kcrypto_wq, &cpu_queue->work);
132 static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
134 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
135 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
136 return ictx->queue;
139 static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
140 const u8 *key, unsigned int keylen)
142 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
143 struct crypto_blkcipher *child = ctx->child;
144 int err;
146 crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
147 crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
148 CRYPTO_TFM_REQ_MASK);
149 err = crypto_blkcipher_setkey(child, key, keylen);
150 crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
151 CRYPTO_TFM_RES_MASK);
152 return err;
155 static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
156 struct crypto_blkcipher *child,
157 int err,
158 int (*crypt)(struct blkcipher_desc *desc,
159 struct scatterlist *dst,
160 struct scatterlist *src,
161 unsigned int len))
163 struct cryptd_blkcipher_request_ctx *rctx;
164 struct blkcipher_desc desc;
166 rctx = ablkcipher_request_ctx(req);
168 if (unlikely(err == -EINPROGRESS))
169 goto out;
171 desc.tfm = child;
172 desc.info = req->info;
173 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
175 err = crypt(&desc, req->dst, req->src, req->nbytes);
177 req->base.complete = rctx->complete;
179 out:
180 local_bh_disable();
181 rctx->complete(&req->base, err);
182 local_bh_enable();
185 static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
187 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
188 struct crypto_blkcipher *child = ctx->child;
190 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
191 crypto_blkcipher_crt(child)->encrypt);
194 static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
196 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
197 struct crypto_blkcipher *child = ctx->child;
199 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
200 crypto_blkcipher_crt(child)->decrypt);
203 static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
204 crypto_completion_t complete)
206 struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
207 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
208 struct cryptd_queue *queue;
210 queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
211 rctx->complete = req->base.complete;
212 req->base.complete = complete;
214 return cryptd_enqueue_request(queue, &req->base);
217 static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
219 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
222 static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
224 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
227 static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
229 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
230 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
231 struct crypto_spawn *spawn = &ictx->spawn;
232 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
233 struct crypto_blkcipher *cipher;
235 cipher = crypto_spawn_blkcipher(spawn);
236 if (IS_ERR(cipher))
237 return PTR_ERR(cipher);
239 ctx->child = cipher;
240 tfm->crt_ablkcipher.reqsize =
241 sizeof(struct cryptd_blkcipher_request_ctx);
242 return 0;
245 static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
247 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
249 crypto_free_blkcipher(ctx->child);
252 static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg,
253 struct cryptd_queue *queue)
255 struct crypto_instance *inst;
256 struct cryptd_instance_ctx *ctx;
257 int err;
259 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
260 if (!inst) {
261 inst = ERR_PTR(-ENOMEM);
262 goto out;
265 err = -ENAMETOOLONG;
266 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
267 "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
268 goto out_free_inst;
270 ctx = crypto_instance_ctx(inst);
271 err = crypto_init_spawn(&ctx->spawn, alg, inst,
272 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
273 if (err)
274 goto out_free_inst;
276 ctx->queue = queue;
278 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
280 inst->alg.cra_priority = alg->cra_priority + 50;
281 inst->alg.cra_blocksize = alg->cra_blocksize;
282 inst->alg.cra_alignmask = alg->cra_alignmask;
284 out:
285 return inst;
287 out_free_inst:
288 kfree(inst);
289 inst = ERR_PTR(err);
290 goto out;
293 static struct crypto_instance *cryptd_alloc_blkcipher(
294 struct rtattr **tb, struct cryptd_queue *queue)
296 struct crypto_instance *inst;
297 struct crypto_alg *alg;
299 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER,
300 CRYPTO_ALG_TYPE_MASK);
301 if (IS_ERR(alg))
302 return ERR_CAST(alg);
304 inst = cryptd_alloc_instance(alg, queue);
305 if (IS_ERR(inst))
306 goto out_put_alg;
308 inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
309 inst->alg.cra_type = &crypto_ablkcipher_type;
311 inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
312 inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
313 inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
315 inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
317 inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
319 inst->alg.cra_init = cryptd_blkcipher_init_tfm;
320 inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
322 inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
323 inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
324 inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
326 out_put_alg:
327 crypto_mod_put(alg);
328 return inst;
331 static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
333 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
334 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
335 struct crypto_spawn *spawn = &ictx->spawn;
336 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
337 struct crypto_hash *cipher;
339 cipher = crypto_spawn_hash(spawn);
340 if (IS_ERR(cipher))
341 return PTR_ERR(cipher);
343 ctx->child = cipher;
344 tfm->crt_ahash.reqsize =
345 sizeof(struct cryptd_hash_request_ctx);
346 return 0;
349 static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
351 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
353 crypto_free_hash(ctx->child);
356 static int cryptd_hash_setkey(struct crypto_ahash *parent,
357 const u8 *key, unsigned int keylen)
359 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
360 struct crypto_hash *child = ctx->child;
361 int err;
363 crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
364 crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
365 CRYPTO_TFM_REQ_MASK);
366 err = crypto_hash_setkey(child, key, keylen);
367 crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
368 CRYPTO_TFM_RES_MASK);
369 return err;
372 static int cryptd_hash_enqueue(struct ahash_request *req,
373 crypto_completion_t complete)
375 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
376 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
377 struct cryptd_queue *queue =
378 cryptd_get_queue(crypto_ahash_tfm(tfm));
380 rctx->complete = req->base.complete;
381 req->base.complete = complete;
383 return cryptd_enqueue_request(queue, &req->base);
386 static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
388 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
389 struct crypto_hash *child = ctx->child;
390 struct ahash_request *req = ahash_request_cast(req_async);
391 struct cryptd_hash_request_ctx *rctx;
392 struct hash_desc desc;
394 rctx = ahash_request_ctx(req);
396 if (unlikely(err == -EINPROGRESS))
397 goto out;
399 desc.tfm = child;
400 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
402 err = crypto_hash_crt(child)->init(&desc);
404 req->base.complete = rctx->complete;
406 out:
407 local_bh_disable();
408 rctx->complete(&req->base, err);
409 local_bh_enable();
412 static int cryptd_hash_init_enqueue(struct ahash_request *req)
414 return cryptd_hash_enqueue(req, cryptd_hash_init);
417 static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
419 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
420 struct crypto_hash *child = ctx->child;
421 struct ahash_request *req = ahash_request_cast(req_async);
422 struct cryptd_hash_request_ctx *rctx;
423 struct hash_desc desc;
425 rctx = ahash_request_ctx(req);
427 if (unlikely(err == -EINPROGRESS))
428 goto out;
430 desc.tfm = child;
431 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
433 err = crypto_hash_crt(child)->update(&desc,
434 req->src,
435 req->nbytes);
437 req->base.complete = rctx->complete;
439 out:
440 local_bh_disable();
441 rctx->complete(&req->base, err);
442 local_bh_enable();
445 static int cryptd_hash_update_enqueue(struct ahash_request *req)
447 return cryptd_hash_enqueue(req, cryptd_hash_update);
450 static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
452 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
453 struct crypto_hash *child = ctx->child;
454 struct ahash_request *req = ahash_request_cast(req_async);
455 struct cryptd_hash_request_ctx *rctx;
456 struct hash_desc desc;
458 rctx = ahash_request_ctx(req);
460 if (unlikely(err == -EINPROGRESS))
461 goto out;
463 desc.tfm = child;
464 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
466 err = crypto_hash_crt(child)->final(&desc, req->result);
468 req->base.complete = rctx->complete;
470 out:
471 local_bh_disable();
472 rctx->complete(&req->base, err);
473 local_bh_enable();
476 static int cryptd_hash_final_enqueue(struct ahash_request *req)
478 return cryptd_hash_enqueue(req, cryptd_hash_final);
481 static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
483 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
484 struct crypto_hash *child = ctx->child;
485 struct ahash_request *req = ahash_request_cast(req_async);
486 struct cryptd_hash_request_ctx *rctx;
487 struct hash_desc desc;
489 rctx = ahash_request_ctx(req);
491 if (unlikely(err == -EINPROGRESS))
492 goto out;
494 desc.tfm = child;
495 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
497 err = crypto_hash_crt(child)->digest(&desc,
498 req->src,
499 req->nbytes,
500 req->result);
502 req->base.complete = rctx->complete;
504 out:
505 local_bh_disable();
506 rctx->complete(&req->base, err);
507 local_bh_enable();
510 static int cryptd_hash_digest_enqueue(struct ahash_request *req)
512 return cryptd_hash_enqueue(req, cryptd_hash_digest);
515 static struct crypto_instance *cryptd_alloc_hash(
516 struct rtattr **tb, struct cryptd_queue *queue)
518 struct crypto_instance *inst;
519 struct crypto_alg *alg;
521 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
522 CRYPTO_ALG_TYPE_HASH_MASK);
523 if (IS_ERR(alg))
524 return ERR_PTR(PTR_ERR(alg));
526 inst = cryptd_alloc_instance(alg, queue);
527 if (IS_ERR(inst))
528 goto out_put_alg;
530 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
531 inst->alg.cra_type = &crypto_ahash_type;
533 inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
534 inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
536 inst->alg.cra_init = cryptd_hash_init_tfm;
537 inst->alg.cra_exit = cryptd_hash_exit_tfm;
539 inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
540 inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
541 inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
542 inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
543 inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
545 out_put_alg:
546 crypto_mod_put(alg);
547 return inst;
550 static struct cryptd_queue queue;
552 static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
554 struct crypto_attr_type *algt;
556 algt = crypto_get_attr_type(tb);
557 if (IS_ERR(algt))
558 return ERR_CAST(algt);
560 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
561 case CRYPTO_ALG_TYPE_BLKCIPHER:
562 return cryptd_alloc_blkcipher(tb, &queue);
563 case CRYPTO_ALG_TYPE_DIGEST:
564 return cryptd_alloc_hash(tb, &queue);
567 return ERR_PTR(-EINVAL);
570 static void cryptd_free(struct crypto_instance *inst)
572 struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
574 crypto_drop_spawn(&ctx->spawn);
575 kfree(inst);
578 static struct crypto_template cryptd_tmpl = {
579 .name = "cryptd",
580 .alloc = cryptd_alloc,
581 .free = cryptd_free,
582 .module = THIS_MODULE,
585 struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
586 u32 type, u32 mask)
588 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
589 struct crypto_tfm *tfm;
591 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
592 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
593 return ERR_PTR(-EINVAL);
594 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
595 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
596 mask &= ~CRYPTO_ALG_TYPE_MASK;
597 mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
598 tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
599 if (IS_ERR(tfm))
600 return ERR_CAST(tfm);
601 if (tfm->__crt_alg->cra_module != THIS_MODULE) {
602 crypto_free_tfm(tfm);
603 return ERR_PTR(-EINVAL);
606 return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
608 EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
610 struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
612 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
613 return ctx->child;
615 EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
617 void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
619 crypto_free_ablkcipher(&tfm->base);
621 EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
623 static int __init cryptd_init(void)
625 int err;
627 err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
628 if (err)
629 return err;
631 err = crypto_register_template(&cryptd_tmpl);
632 if (err)
633 cryptd_fini_queue(&queue);
635 return err;
638 static void __exit cryptd_exit(void)
640 cryptd_fini_queue(&queue);
641 crypto_unregister_template(&cryptd_tmpl);
644 module_init(cryptd_init);
645 module_exit(cryptd_exit);
647 MODULE_LICENSE("GPL");
648 MODULE_DESCRIPTION("Software async crypto daemon");