27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
34 #define CRYPTD_MAX_CPU_QLEN 100
88 unsigned int max_cpu_qlen)
104 static void cryptd_fini_queue(
struct cryptd_queue *queue)
116 static int cryptd_enqueue_request(
struct cryptd_queue *queue,
148 backlog = crypto_get_backlog(&cpu_queue->
queue);
160 if (cpu_queue->
queue.qlen)
172 const u8 *
key,
unsigned int keylen)
179 crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
181 err = crypto_blkcipher_setkey(child, key, keylen);
182 crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
198 rctx = ablkcipher_request_ctx(req);
209 req->
base.complete = rctx->complete;
213 rctx->complete(&req->
base, err);
222 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
223 crypto_blkcipher_crt(child)->
encrypt);
231 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
232 crypto_blkcipher_crt(child)->decrypt);
242 queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
246 return cryptd_enqueue_request(queue, &req->
base);
251 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
256 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
259 static int cryptd_blkcipher_init_tfm(
struct crypto_tfm *tfm)
267 cipher = crypto_spawn_blkcipher(spawn);
269 return PTR_ERR(cipher);
272 tfm->crt_ablkcipher.reqsize =
277 static void cryptd_blkcipher_exit_tfm(
struct crypto_tfm *tfm)
281 crypto_free_blkcipher(ctx->
child);
284 static void *cryptd_alloc_instance(
struct crypto_alg *alg,
unsigned int head,
291 p = kzalloc(head +
sizeof(*inst) + tail,
GFP_KERNEL);
295 inst = (
void *)(p + head);
331 inst = cryptd_alloc_instance(alg, 0,
sizeof(*ctx));
336 ctx = crypto_instance_ctx(inst);
347 inst->
alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
348 inst->
alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
349 inst->
alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
351 inst->
alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
355 inst->
alg.cra_init = cryptd_blkcipher_init_tfm;
356 inst->
alg.cra_exit = cryptd_blkcipher_exit_tfm;
358 inst->
alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
359 inst->
alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
360 inst->
alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
374 static int cryptd_hash_init_tfm(
struct crypto_tfm *tfm)
382 hash = crypto_spawn_shash(spawn);
384 return PTR_ERR(hash);
387 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
389 crypto_shash_descsize(hash));
393 static void cryptd_hash_exit_tfm(
struct crypto_tfm *tfm)
397 crypto_free_shash(ctx->
child);
400 static int cryptd_hash_setkey(
struct crypto_ahash *parent,
401 const u8 *key,
unsigned int keylen)
408 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
411 crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
422 cryptd_get_queue(crypto_ahash_tfm(tfm));
427 return cryptd_enqueue_request(queue, &req->
base);
444 err = crypto_shash_init(desc);
454 static int cryptd_hash_init_enqueue(
struct ahash_request *req)
456 return cryptd_hash_enqueue(req, cryptd_hash_init);
464 rctx = ahash_request_ctx(req);
479 static int cryptd_hash_update_enqueue(
struct ahash_request *req)
481 return cryptd_hash_enqueue(req, cryptd_hash_update);
502 static int cryptd_hash_final_enqueue(
struct ahash_request *req)
504 return cryptd_hash_enqueue(req, cryptd_hash_final);
525 static int cryptd_hash_finup_enqueue(
struct ahash_request *req)
527 return cryptd_hash_enqueue(req, cryptd_hash_finup);
554 static int cryptd_hash_digest_enqueue(
struct ahash_request *req)
556 return cryptd_hash_enqueue(req, cryptd_hash_digest);
563 return crypto_shash_export(&rctx->
desc, out);
566 static int cryptd_hash_import(
struct ahash_request *req,
const void *
in)
570 return crypto_shash_import(&rctx->
desc, in);
584 return PTR_ERR(salg);
587 inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
593 ctx = ahash_instance_ctx(inst);
597 ahash_crypto_instance(inst));
603 inst->
alg.halg.digestsize = salg->digestsize;
606 inst->
alg.halg.base.cra_init = cryptd_hash_init_tfm;
607 inst->
alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
609 inst->
alg.init = cryptd_hash_init_enqueue;
610 inst->
alg.update = cryptd_hash_update_enqueue;
611 inst->
alg.final = cryptd_hash_final_enqueue;
612 inst->
alg.finup = cryptd_hash_finup_enqueue;
613 inst->
alg.export = cryptd_hash_export;
614 inst->
alg.import = cryptd_hash_import;
615 inst->
alg.setkey = cryptd_hash_setkey;
616 inst->
alg.digest = cryptd_hash_digest_enqueue;
620 crypto_drop_shash(&ctx->
spawn);
636 rctx = aead_request_ctx(req);
640 aead_request_set_tfm(req, child);
656 cryptd_aead_crypt(req, child, err, crypto_aead_crt(child)->
encrypt);
666 cryptd_aead_crypt(req, child, err, crypto_aead_crt(child)->decrypt);
669 static int cryptd_aead_enqueue(
struct aead_request *req,
674 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
678 return cryptd_enqueue_request(queue, &req->
base);
681 static int cryptd_aead_encrypt_enqueue(
struct aead_request *req)
683 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
686 static int cryptd_aead_decrypt_enqueue(
struct aead_request *req)
688 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
691 static int cryptd_aead_init_tfm(
struct crypto_tfm *tfm)
699 cipher = crypto_spawn_aead(spawn);
701 return PTR_ERR(cipher);
709 static void cryptd_aead_exit_tfm(
struct crypto_tfm *tfm)
712 crypto_free_aead(ctx->
child);
729 inst = cryptd_alloc_instance(alg, 0,
sizeof(*ctx));
734 ctx = crypto_instance_ctx(inst);
745 inst->
alg.cra_init = cryptd_aead_init_tfm;
746 inst->
alg.cra_exit = cryptd_aead_exit_tfm;
747 inst->
alg.cra_aead.setkey = alg->cra_aead.setkey;
748 inst->
alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
749 inst->
alg.cra_aead.geniv = alg->cra_aead.geniv;
750 inst->
alg.cra_aead.ivsize = alg->cra_aead.ivsize;
751 inst->
alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
752 inst->
alg.cra_aead.encrypt = cryptd_aead_encrypt_enqueue;
753 inst->
alg.cra_aead.decrypt = cryptd_aead_decrypt_enqueue;
754 inst->
alg.cra_aead.givencrypt = alg->cra_aead.givencrypt;
755 inst->
alg.cra_aead.givdecrypt = alg->cra_aead.givdecrypt;
776 return PTR_ERR(algt);
780 return cryptd_create_blkcipher(tmpl, tb, &queue);
782 return cryptd_create_hash(tmpl, tb, &queue);
784 return cryptd_create_aead(tmpl, tb, &queue);
798 crypto_drop_shash(&hctx->
spawn);
813 .create = cryptd_create,
833 return ERR_CAST(tfm);
839 return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
852 crypto_free_ablkcipher(&tfm->
base);
867 return ERR_CAST(tfm);
869 crypto_free_ahash(tfm);
873 return __cryptd_ahash_cast(tfm);
894 crypto_free_ahash(&tfm->
base);
909 return ERR_CAST(tfm);
911 crypto_free_aead(tfm);
914 return __cryptd_aead_cast(tfm);
921 ctx = crypto_aead_ctx(&tfm->
base);
928 crypto_free_aead(&tfm->
base);
932 static int __init cryptd_init(
void)
942 cryptd_fini_queue(&queue);
947 static void __exit cryptd_exit(
void)
949 cryptd_fini_queue(&queue);