OSDN Git Service

crypto: skcipher - Add skcipher_ialg_simple helper
authorHerbert Xu <herbert@gondor.apana.org.au>
Fri, 20 Dec 2019 05:29:40 +0000 (13:29 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 27 Dec 2019 10:18:04 +0000 (18:18 +0800)
This patch introduces the skcipher_ialg_simple helper which fetches
the crypto_alg structure from a simple skcipher instance's spawn.

This allows us to remove the third argument from the function
skcipher_alloc_instance_simple.

In doing so the reference count to the algorithm is now maintained
by the Crypto API and the caller no longer needs to drop the alg
refcount.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/cbc.c
crypto/cfb.c
crypto/ctr.c
crypto/ecb.c
crypto/keywrap.c
crypto/ofb.c
crypto/pcbc.c
crypto/skcipher.c
include/crypto/internal/skcipher.h

index dd96bcf..e6f6273 100644 (file)
@@ -54,10 +54,12 @@ static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
        struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
+       alg = skcipher_ialg_simple(inst);
+
        err = -EINVAL;
        if (!is_power_of_2(alg->cra_blocksize))
                goto out_free_inst;
@@ -66,14 +68,11 @@ static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
        inst->alg.decrypt = crypto_cbc_decrypt;
 
        err = skcipher_register_instance(tmpl, inst);
-       if (err)
-               goto out_free_inst;
-       goto out_put_alg;
-
+       if (err) {
 out_free_inst:
-       inst->free(inst);
-out_put_alg:
-       crypto_mod_put(alg);
+               inst->free(inst);
+       }
+
        return err;
 }
 
index 7b68fbb..4e5219b 100644 (file)
@@ -203,10 +203,12 @@ static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
        struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
+       alg = skcipher_ialg_simple(inst);
+
        /* CFB mode is a stream cipher. */
        inst->alg.base.cra_blocksize = 1;
 
@@ -223,7 +225,6 @@ static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
        if (err)
                inst->free(inst);
 
-       crypto_mod_put(alg);
        return err;
 }
 
index 70a3fcc..1e9d6b8 100644 (file)
@@ -129,10 +129,12 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
        struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
+       alg = skcipher_ialg_simple(inst);
+
        /* Block size must be >= 4 bytes. */
        err = -EINVAL;
        if (alg->cra_blocksize < 4)
@@ -155,14 +157,11 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
        inst->alg.decrypt = crypto_ctr_crypt;
 
        err = skcipher_register_instance(tmpl, inst);
-       if (err)
-               goto out_free_inst;
-       goto out_put_alg;
-
+       if (err) {
 out_free_inst:
-       inst->free(inst);
-out_put_alg:
-       crypto_mod_put(alg);
+               inst->free(inst);
+       }
+
        return err;
 }
 
index 9d6981c..69a687c 100644 (file)
@@ -61,10 +61,9 @@ static int crypto_ecb_decrypt(struct skcipher_request *req)
 static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
 {
        struct skcipher_instance *inst;
-       struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
@@ -76,7 +75,7 @@ static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
        err = skcipher_register_instance(tmpl, inst);
        if (err)
                inst->free(inst);
-       crypto_mod_put(alg);
+
        return err;
 }
 
index a155c88..0355cce 100644 (file)
@@ -266,10 +266,12 @@ static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
        struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
+       alg = skcipher_ialg_simple(inst);
+
        err = -EINVAL;
        /* Section 5.1 requirement for KW */
        if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
@@ -283,14 +285,11 @@ static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
        inst->alg.decrypt = crypto_kw_decrypt;
 
        err = skcipher_register_instance(tmpl, inst);
-       if (err)
-               goto out_free_inst;
-       goto out_put_alg;
-
+       if (err) {
 out_free_inst:
-       inst->free(inst);
-out_put_alg:
-       crypto_mod_put(alg);
+               inst->free(inst);
+       }
+
        return err;
 }
 
index 133ff4c..2ec68e3 100644 (file)
@@ -55,10 +55,12 @@ static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
        struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
+       alg = skcipher_ialg_simple(inst);
+
        /* OFB mode is a stream cipher. */
        inst->alg.base.cra_blocksize = 1;
 
@@ -75,7 +77,6 @@ static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
        if (err)
                inst->free(inst);
 
-       crypto_mod_put(alg);
        return err;
 }
 
index 862cdb8..ae921fb 100644 (file)
@@ -153,10 +153,9 @@ static int crypto_pcbc_decrypt(struct skcipher_request *req)
 static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
 {
        struct skcipher_instance *inst;
-       struct crypto_alg *alg;
        int err;
 
-       inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
+       inst = skcipher_alloc_instance_simple(tmpl, tb);
        if (IS_ERR(inst))
                return PTR_ERR(inst);
 
@@ -166,7 +165,7 @@ static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
        err = skcipher_register_instance(tmpl, inst);
        if (err)
                inst->free(inst);
-       crypto_mod_put(alg);
+
        return err;
 }
 
index 39a718d..37adb71 100644 (file)
@@ -938,15 +938,12 @@ static void skcipher_free_instance_simple(struct skcipher_instance *inst)
  *
  * @tmpl: the template being instantiated
  * @tb: the template parameters
- * @cipher_alg_ret: on success, a pointer to the underlying cipher algorithm is
- *                 returned here.  It must be dropped with crypto_mod_put().
  *
  * Return: a pointer to the new instance, or an ERR_PTR().  The caller still
  *        needs to register the instance.
  */
-struct skcipher_instance *
-skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
-                              struct crypto_alg **cipher_alg_ret)
+struct skcipher_instance *skcipher_alloc_instance_simple(
+       struct crypto_template *tmpl, struct rtattr **tb)
 {
        struct crypto_attr_type *algt;
        struct crypto_alg *cipher_alg;
@@ -982,6 +979,7 @@ skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
        if (err)
                goto err_free_inst;
 
+       spawn->dropref = true;
        err = crypto_init_spawn(spawn, cipher_alg,
                                skcipher_crypto_instance(inst),
                                CRYPTO_ALG_TYPE_MASK);
@@ -1003,7 +1001,6 @@ skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
        inst->alg.init = skcipher_init_tfm_simple;
        inst->alg.exit = skcipher_exit_tfm_simple;
 
-       *cipher_alg_ret = cipher_alg;
        return inst;
 
 err_free_inst:
index 921c409..ad4a633 100644 (file)
@@ -214,9 +214,17 @@ skcipher_cipher_simple(struct crypto_skcipher *tfm)
 
        return ctx->cipher;
 }
-struct skcipher_instance *
-skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
-                              struct crypto_alg **cipher_alg_ret);
+
+struct skcipher_instance *skcipher_alloc_instance_simple(
+       struct crypto_template *tmpl, struct rtattr **tb);
+
+static inline struct crypto_alg *skcipher_ialg_simple(
+       struct skcipher_instance *inst)
+{
+       struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
+
+       return spawn->alg;
+}
 
 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */