Skip to content

Commit aacd5b4

Browse files
ebiggersherbertx
authored andcommitted
crypto: skcipher - use crypto_grab_cipher() and simplify error paths
Make skcipher_alloc_instance_simple() use the new function crypto_grab_cipher() to initialize its cipher spawn. This is needed to make all spawns be initialized in a consistent way. 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 c282586 commit aacd5b4

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

‎crypto/skcipher.c‎

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
908908

909909
static void skcipher_free_instance_simple(struct skcipher_instance *inst)
910910
{
911-
crypto_drop_spawn(skcipher_instance_ctx(inst));
911+
crypto_drop_cipher(skcipher_instance_ctx(inst));
912912
kfree(inst);
913913
}
914914

@@ -932,10 +932,10 @@ struct skcipher_instance *skcipher_alloc_instance_simple(
932932
struct crypto_template *tmpl, struct rtattr **tb)
933933
{
934934
struct crypto_attr_type *algt;
935-
struct crypto_alg *cipher_alg;
936-
struct skcipher_instance *inst;
937-
struct crypto_spawn *spawn;
938935
u32 mask;
936+
struct skcipher_instance *inst;
937+
struct crypto_cipher_spawn *spawn;
938+
struct crypto_alg *cipher_alg;
939939
int err;
940940

941941
algt = crypto_get_attr_type(tb);
@@ -945,32 +945,25 @@ struct skcipher_instance *skcipher_alloc_instance_simple(
945945
if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
946946
return ERR_PTR(-EINVAL);
947947

948-
mask = CRYPTO_ALG_TYPE_MASK |
949-
crypto_requires_off(algt->type, algt->mask,
950-
CRYPTO_ALG_NEED_FALLBACK);
951-
952-
cipher_alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
953-
if (IS_ERR(cipher_alg))
954-
return ERR_CAST(cipher_alg);
948+
mask = crypto_requires_off(algt->type, algt->mask,
949+
CRYPTO_ALG_NEED_FALLBACK);
955950

956951
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
957-
if (!inst) {
958-
err = -ENOMEM;
959-
goto err_put_cipher_alg;
960-
}
952+
if (!inst)
953+
return ERR_PTR(-ENOMEM);
961954
spawn = skcipher_instance_ctx(inst);
962955

963-
err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
964-
cipher_alg);
956+
err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
957+
crypto_attr_alg_name(tb[1]), 0, mask);
965958
if (err)
966959
goto err_free_inst;
960+
cipher_alg = crypto_spawn_cipher_alg(spawn);
967961

968-
spawn->dropref = true;
969-
err = crypto_init_spawn(spawn, cipher_alg,
970-
skcipher_crypto_instance(inst),
971-
CRYPTO_ALG_TYPE_MASK);
962+
err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
963+
cipher_alg);
972964
if (err)
973965
goto err_free_inst;
966+
974967
inst->free = skcipher_free_instance_simple;
975968

976969
/* Default algorithm properties, can be overridden */
@@ -990,9 +983,7 @@ struct skcipher_instance *skcipher_alloc_instance_simple(
990983
return inst;
991984

992985
err_free_inst:
993-
kfree(inst);
994-
err_put_cipher_alg:
995-
crypto_mod_put(cipher_alg);
986+
skcipher_free_instance_simple(inst);
996987
return ERR_PTR(err);
997988
}
998989
EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);

‎include/crypto/internal/skcipher.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ struct skcipher_instance *skcipher_alloc_instance_simple(
214214
static inline struct crypto_alg *skcipher_ialg_simple(
215215
struct skcipher_instance *inst)
216216
{
217-
struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
217+
struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
218218

219-
return spawn->alg;
219+
return crypto_spawn_cipher_alg(spawn);
220220
}
221221

222222
#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */

0 commit comments

Comments
 (0)