Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: cryptd - Add support for skcipher

This patch adds skcipher support to cryptd alongside ablkcipher.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+294 -3
+282 -2
crypto/cryptd.c
··· 17 17 * 18 18 */ 19 19 20 - #include <crypto/algapi.h> 21 20 #include <crypto/internal/hash.h> 22 21 #include <crypto/internal/aead.h> 22 + #include <crypto/internal/skcipher.h> 23 23 #include <crypto/cryptd.h> 24 24 #include <crypto/crypto_wq.h> 25 25 #include <linux/atomic.h> ··· 48 48 struct cryptd_queue *queue; 49 49 }; 50 50 51 + struct skcipherd_instance_ctx { 52 + struct crypto_skcipher_spawn spawn; 53 + struct cryptd_queue *queue; 54 + }; 55 + 51 56 struct hashd_instance_ctx { 52 57 struct crypto_shash_spawn spawn; 53 58 struct cryptd_queue *queue; ··· 69 64 }; 70 65 71 66 struct cryptd_blkcipher_request_ctx { 67 + crypto_completion_t complete; 68 + }; 69 + 70 + struct cryptd_skcipher_ctx { 71 + atomic_t refcnt; 72 + struct crypto_skcipher *child; 73 + }; 74 + 75 + struct cryptd_skcipher_request_ctx { 72 76 crypto_completion_t complete; 73 77 }; 74 78 ··· 441 427 442 428 out_put_alg: 443 429 crypto_mod_put(alg); 430 + return err; 431 + } 432 + 433 + static int cryptd_skcipher_setkey(struct crypto_skcipher *parent, 434 + const u8 *key, unsigned int keylen) 435 + { 436 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent); 437 + struct crypto_skcipher *child = ctx->child; 438 + int err; 439 + 440 + crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 441 + crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) & 442 + CRYPTO_TFM_REQ_MASK); 443 + err = crypto_skcipher_setkey(child, key, keylen); 444 + crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) & 445 + CRYPTO_TFM_RES_MASK); 446 + return err; 447 + } 448 + 449 + static void cryptd_skcipher_complete(struct skcipher_request *req, int err) 450 + { 451 + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 452 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 453 + struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 454 + int refcnt = atomic_read(&ctx->refcnt); 455 + 456 + local_bh_disable(); 457 + rctx->complete(&req->base, err); 458 + local_bh_enable(); 459 + 460 + if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt)) 461 + crypto_free_skcipher(tfm); 462 + } 463 + 464 + static void cryptd_skcipher_encrypt(struct crypto_async_request *base, 465 + int err) 466 + { 467 + struct skcipher_request *req = skcipher_request_cast(base); 468 + struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 469 + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 470 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 471 + struct crypto_skcipher *child = ctx->child; 472 + SKCIPHER_REQUEST_ON_STACK(subreq, child); 473 + 474 + if (unlikely(err == -EINPROGRESS)) 475 + goto out; 476 + 477 + skcipher_request_set_tfm(subreq, child); 478 + skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 479 + NULL, NULL); 480 + skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 481 + req->iv); 482 + 483 + err = crypto_skcipher_encrypt(subreq); 484 + skcipher_request_zero(subreq); 485 + 486 + req->base.complete = rctx->complete; 487 + 488 + out: 489 + cryptd_skcipher_complete(req, err); 490 + } 491 + 492 + static void cryptd_skcipher_decrypt(struct crypto_async_request *base, 493 + int err) 494 + { 495 + struct skcipher_request *req = skcipher_request_cast(base); 496 + struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 497 + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 498 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 499 + struct crypto_skcipher *child = ctx->child; 500 + SKCIPHER_REQUEST_ON_STACK(subreq, child); 501 + 502 + if (unlikely(err == -EINPROGRESS)) 503 + goto out; 504 + 505 + skcipher_request_set_tfm(subreq, child); 506 + skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 507 + NULL, NULL); 508 + skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 509 + req->iv); 510 + 511 + err = crypto_skcipher_decrypt(subreq); 512 + skcipher_request_zero(subreq); 513 + 514 + req->base.complete = rctx->complete; 515 + 516 + out: 517 + cryptd_skcipher_complete(req, err); 518 + } 519 + 520 + static int cryptd_skcipher_enqueue(struct skcipher_request *req, 521 + crypto_completion_t compl) 522 + { 523 + struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 524 + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 525 + struct cryptd_queue *queue; 526 + 527 + queue = cryptd_get_queue(crypto_skcipher_tfm(tfm)); 528 + rctx->complete = req->base.complete; 529 + req->base.complete = compl; 530 + 531 + return cryptd_enqueue_request(queue, &req->base); 532 + } 533 + 534 + static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req) 535 + { 536 + return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt); 537 + } 538 + 539 + static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req) 540 + { 541 + return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt); 542 + } 543 + 544 + static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm) 545 + { 546 + struct skcipher_instance *inst = skcipher_alg_instance(tfm); 547 + struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst); 548 + struct crypto_skcipher_spawn *spawn = &ictx->spawn; 549 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 550 + struct crypto_skcipher *cipher; 551 + 552 + cipher = crypto_spawn_skcipher(spawn); 553 + if (IS_ERR(cipher)) 554 + return PTR_ERR(cipher); 555 + 556 + ctx->child = cipher; 557 + crypto_skcipher_set_reqsize( 558 + tfm, sizeof(struct cryptd_skcipher_request_ctx)); 559 + return 0; 560 + } 561 + 562 + static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm) 563 + { 564 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 565 + 566 + crypto_free_skcipher(ctx->child); 567 + } 568 + 569 + static void cryptd_skcipher_free(struct skcipher_instance *inst) 570 + { 571 + struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst); 572 + 573 + crypto_drop_skcipher(&ctx->spawn); 574 + } 575 + 576 + static int cryptd_create_skcipher(struct crypto_template *tmpl, 577 + struct rtattr **tb, 578 + struct cryptd_queue *queue) 579 + { 580 + struct skcipherd_instance_ctx *ctx; 581 + struct skcipher_instance *inst; 582 + struct skcipher_alg *alg; 583 + const char *name; 584 + u32 type; 585 + u32 mask; 586 + int err; 587 + 588 + type = 0; 589 + mask = CRYPTO_ALG_ASYNC; 590 + 591 + cryptd_check_internal(tb, &type, &mask); 592 + 593 + name = crypto_attr_alg_name(tb[1]); 594 + if (IS_ERR(name)) 595 + return PTR_ERR(name); 596 + 597 + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 598 + if (!inst) 599 + return -ENOMEM; 600 + 601 + ctx = skcipher_instance_ctx(inst); 602 + ctx->queue = queue; 603 + 604 + crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst)); 605 + err = crypto_grab_skcipher(&ctx->spawn, name, type, mask); 606 + if (err) 607 + goto out_free_inst; 608 + 609 + alg = crypto_spawn_skcipher_alg(&ctx->spawn); 610 + err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base); 611 + if (err) 612 + goto out_drop_skcipher; 613 + 614 + inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC | 615 + (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); 616 + 617 + inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg); 618 + inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg); 619 + inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg); 620 + inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg); 621 + 622 + inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx); 623 + 624 + inst->alg.init = cryptd_skcipher_init_tfm; 625 + inst->alg.exit = cryptd_skcipher_exit_tfm; 626 + 627 + inst->alg.setkey = cryptd_skcipher_setkey; 628 + inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue; 629 + inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue; 630 + 631 + inst->free = cryptd_skcipher_free; 632 + 633 + err = skcipher_register_instance(tmpl, inst); 634 + if (err) { 635 + out_drop_skcipher: 636 + crypto_drop_skcipher(&ctx->spawn); 637 + out_free_inst: 638 + kfree(inst); 639 + } 444 640 return err; 445 641 } 446 642 ··· 1117 893 1118 894 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { 1119 895 case CRYPTO_ALG_TYPE_BLKCIPHER: 1120 - return cryptd_create_blkcipher(tmpl, tb, &queue); 896 + if ((algt->type & CRYPTO_ALG_TYPE_MASK) == 897 + CRYPTO_ALG_TYPE_BLKCIPHER) 898 + return cryptd_create_blkcipher(tmpl, tb, &queue); 899 + 900 + return cryptd_create_skcipher(tmpl, tb, &queue); 1121 901 case CRYPTO_ALG_TYPE_DIGEST: 1122 902 return cryptd_create_hash(tmpl, tb, &queue); 1123 903 case CRYPTO_ALG_TYPE_AEAD: ··· 1210 982 crypto_free_ablkcipher(&tfm->base); 1211 983 } 1212 984 EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher); 985 + 986 + struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name, 987 + u32 type, u32 mask) 988 + { 989 + char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; 990 + struct cryptd_skcipher_ctx *ctx; 991 + struct crypto_skcipher *tfm; 992 + 993 + if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, 994 + "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) 995 + return ERR_PTR(-EINVAL); 996 + 997 + tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask); 998 + if (IS_ERR(tfm)) 999 + return ERR_CAST(tfm); 1000 + 1001 + if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { 1002 + crypto_free_skcipher(tfm); 1003 + return ERR_PTR(-EINVAL); 1004 + } 1005 + 1006 + ctx = crypto_skcipher_ctx(tfm); 1007 + atomic_set(&ctx->refcnt, 1); 1008 + 1009 + return container_of(tfm, struct cryptd_skcipher, base); 1010 + } 1011 + EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher); 1012 + 1013 + struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm) 1014 + { 1015 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 1016 + 1017 + return ctx->child; 1018 + } 1019 + EXPORT_SYMBOL_GPL(cryptd_skcipher_child); 1020 + 1021 + bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm) 1022 + { 1023 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 1024 + 1025 + return atomic_read(&ctx->refcnt) - 1; 1026 + } 1027 + EXPORT_SYMBOL_GPL(cryptd_skcipher_queued); 1028 + 1029 + void cryptd_free_skcipher(struct cryptd_skcipher *tfm) 1030 + { 1031 + struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 1032 + 1033 + if (atomic_dec_and_test(&ctx->refcnt)) 1034 + crypto_free_skcipher(&tfm->base); 1035 + } 1036 + EXPORT_SYMBOL_GPL(cryptd_free_skcipher); 1213 1037 1214 1038 struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name, 1215 1039 u32 type, u32 mask)
+12 -1
include/crypto/cryptd.h
··· 12 12 #ifndef _CRYPTO_CRYPT_H 13 13 #define _CRYPTO_CRYPT_H 14 14 15 - #include <linux/crypto.h> 16 15 #include <linux/kernel.h> 17 16 #include <crypto/aead.h> 18 17 #include <crypto/hash.h> 18 + #include <crypto/skcipher.h> 19 19 20 20 struct cryptd_ablkcipher { 21 21 struct crypto_ablkcipher base; ··· 33 33 struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm); 34 34 bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm); 35 35 void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm); 36 + 37 + struct cryptd_skcipher { 38 + struct crypto_skcipher base; 39 + }; 40 + 41 + struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name, 42 + u32 type, u32 mask); 43 + struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm); 44 + /* Must be called without moving CPUs. */ 45 + bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm); 46 + void cryptd_free_skcipher(struct cryptd_skcipher *tfm); 36 47 37 48 struct cryptd_ahash { 38 49 struct crypto_ahash base;