crypto: cmac - use crypto_grab_cipher() and simplify error paths
authorEric Biggers <ebiggers@google.com>
Fri, 3 Jan 2020 03:59:02 +0000 (19:59 -0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 9 Jan 2020 03:30:56 +0000 (11:30 +0800)
Make the cmac template 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.

This required making cmac_create() allocate the instance directly rather
than use shash_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>
crypto/cmac.c

index 0928aeb..c6bf78b 100644 (file)
@@ -222,6 +222,7 @@ static void cmac_exit_tfm(struct crypto_tfm *tfm)
 static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
 {
        struct shash_instance *inst;
+       struct crypto_cipher_spawn *spawn;
        struct crypto_alg *alg;
        unsigned long alignmask;
        int err;
@@ -230,10 +231,16 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
        if (err)
                return err;
 
-       alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
-                                 CRYPTO_ALG_TYPE_MASK);
-       if (IS_ERR(alg))
-               return PTR_ERR(alg);
+       inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
+       if (!inst)
+               return -ENOMEM;
+       spawn = shash_instance_ctx(inst);
+
+       err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
+                                crypto_attr_alg_name(tb[1]), 0, 0);
+       if (err)
+               goto err_free_inst;
+       alg = crypto_spawn_cipher_alg(spawn);
 
        switch (alg->cra_blocksize) {
        case 16:
@@ -241,19 +248,12 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
                break;
        default:
                err = -EINVAL;
-               goto out_put_alg;
+               goto err_free_inst;
        }
 
-       inst = shash_alloc_instance("cmac", alg);
-       err = PTR_ERR(inst);
-       if (IS_ERR(inst))
-               goto out_put_alg;
-
-       err = crypto_init_spawn(shash_instance_ctx(inst), alg,
-                               shash_crypto_instance(inst),
-                               CRYPTO_ALG_TYPE_MASK);
+       err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
        if (err)
-               goto out_free_inst;
+               goto err_free_inst;
 
        alignmask = alg->cra_alignmask;
        inst->alg.base.cra_alignmask = alignmask;
@@ -282,12 +282,9 @@ static int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
 
        err = shash_register_instance(tmpl, inst);
        if (err) {
-out_free_inst:
+err_free_inst:
                shash_free_instance(shash_crypto_instance(inst));
        }
-
-out_put_alg:
-       crypto_mod_put(alg);
        return err;
 }