Skip to content

Commit 218c503

Browse files
ebiggersherbertx
authored andcommitted
crypto: cryptd - use crypto_grab_shash() and simplify error paths
Make the cryptd template (in the hash case) use the new function crypto_grab_shash() to initialize its shash spawn. This is needed to make all spawns be initialized in a consistent way. This required making cryptd_create_hash() allocate the instance directly rather than use cryptd_alloc_instance(). Also simplify the error handling by taking advantage of crypto_drop_*() now accepting (as a no-op) spawns that haven't been initialized yet, and by taking advantage of crypto_grab_*() now handling ERR_PTR() names. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ba44840 commit 218c503

1 file changed

Lines changed: 18 additions & 50 deletions

File tree

‎crypto/cryptd.c‎

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -221,32 +221,6 @@ static int cryptd_init_instance(struct crypto_instance *inst,
221221
return 0;
222222
}
223223

224-
static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
225-
unsigned int tail)
226-
{
227-
char *p;
228-
struct crypto_instance *inst;
229-
int err;
230-
231-
p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
232-
if (!p)
233-
return ERR_PTR(-ENOMEM);
234-
235-
inst = (void *)(p + head);
236-
237-
err = cryptd_init_instance(inst, alg);
238-
if (err)
239-
goto out_free_inst;
240-
241-
out:
242-
return p;
243-
244-
out_free_inst:
245-
kfree(p);
246-
p = ERR_PTR(err);
247-
goto out;
248-
}
249-
250224
static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
251225
const u8 *key, unsigned int keylen)
252226
{
@@ -662,39 +636,36 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
662636
{
663637
struct hashd_instance_ctx *ctx;
664638
struct ahash_instance *inst;
665-
struct shash_alg *salg;
666-
struct crypto_alg *alg;
639+
struct shash_alg *alg;
667640
u32 type = 0;
668641
u32 mask = 0;
669642
int err;
670643

671644
cryptd_check_internal(tb, &type, &mask);
672645

673-
salg = shash_attr_alg(tb[1], type, mask);
674-
if (IS_ERR(salg))
675-
return PTR_ERR(salg);
676-
677-
alg = &salg->base;
678-
inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
679-
sizeof(*ctx));
680-
err = PTR_ERR(inst);
681-
if (IS_ERR(inst))
682-
goto out_put_alg;
646+
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
647+
if (!inst)
648+
return -ENOMEM;
683649

684650
ctx = ahash_instance_ctx(inst);
685651
ctx->queue = queue;
686652

687-
err = crypto_init_shash_spawn(&ctx->spawn, salg,
688-
ahash_crypto_instance(inst));
653+
err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
654+
crypto_attr_alg_name(tb[1]), type, mask);
689655
if (err)
690-
goto out_free_inst;
656+
goto err_free_inst;
657+
alg = crypto_spawn_shash_alg(&ctx->spawn);
658+
659+
err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
660+
if (err)
661+
goto err_free_inst;
691662

692663
inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
693-
(alg->cra_flags & (CRYPTO_ALG_INTERNAL |
694-
CRYPTO_ALG_OPTIONAL_KEY));
664+
(alg->base.cra_flags & (CRYPTO_ALG_INTERNAL |
665+
CRYPTO_ALG_OPTIONAL_KEY));
695666

696-
inst->alg.halg.digestsize = salg->digestsize;
697-
inst->alg.halg.statesize = salg->statesize;
667+
inst->alg.halg.digestsize = alg->digestsize;
668+
inst->alg.halg.statesize = alg->statesize;
698669
inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
699670

700671
inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
@@ -706,19 +677,16 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
706677
inst->alg.finup = cryptd_hash_finup_enqueue;
707678
inst->alg.export = cryptd_hash_export;
708679
inst->alg.import = cryptd_hash_import;
709-
if (crypto_shash_alg_has_setkey(salg))
680+
if (crypto_shash_alg_has_setkey(alg))
710681
inst->alg.setkey = cryptd_hash_setkey;
711682
inst->alg.digest = cryptd_hash_digest_enqueue;
712683

713684
err = ahash_register_instance(tmpl, inst);
714685
if (err) {
686+
err_free_inst:
715687
crypto_drop_shash(&ctx->spawn);
716-
out_free_inst:
717688
kfree(inst);
718689
}
719-
720-
out_put_alg:
721-
crypto_mod_put(alg);
722690
return err;
723691
}
724692

0 commit comments

Comments
 (0)