Skip to content

Commit b3c16bf

Browse files
committed
crypto: skcipher - Add skcipher_ialg_simple helper
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>
1 parent 93e23eb commit b3c16bf

9 files changed

Lines changed: 45 additions & 43 deletions

File tree

‎crypto/cbc.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
5454
struct crypto_alg *alg;
5555
int err;
5656

57-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
57+
inst = skcipher_alloc_instance_simple(tmpl, tb);
5858
if (IS_ERR(inst))
5959
return PTR_ERR(inst);
6060

61+
alg = skcipher_ialg_simple(inst);
62+
6163
err = -EINVAL;
6264
if (!is_power_of_2(alg->cra_blocksize))
6365
goto out_free_inst;
@@ -66,14 +68,11 @@ static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
6668
inst->alg.decrypt = crypto_cbc_decrypt;
6769

6870
err = skcipher_register_instance(tmpl, inst);
69-
if (err)
70-
goto out_free_inst;
71-
goto out_put_alg;
72-
71+
if (err) {
7372
out_free_inst:
74-
inst->free(inst);
75-
out_put_alg:
76-
crypto_mod_put(alg);
73+
inst->free(inst);
74+
}
75+
7776
return err;
7877
}
7978

‎crypto/cfb.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
203203
struct crypto_alg *alg;
204204
int err;
205205

206-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
206+
inst = skcipher_alloc_instance_simple(tmpl, tb);
207207
if (IS_ERR(inst))
208208
return PTR_ERR(inst);
209209

210+
alg = skcipher_ialg_simple(inst);
211+
210212
/* CFB mode is a stream cipher. */
211213
inst->alg.base.cra_blocksize = 1;
212214

@@ -223,7 +225,6 @@ static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
223225
if (err)
224226
inst->free(inst);
225227

226-
crypto_mod_put(alg);
227228
return err;
228229
}
229230

‎crypto/ctr.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
129129
struct crypto_alg *alg;
130130
int err;
131131

132-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
132+
inst = skcipher_alloc_instance_simple(tmpl, tb);
133133
if (IS_ERR(inst))
134134
return PTR_ERR(inst);
135135

136+
alg = skcipher_ialg_simple(inst);
137+
136138
/* Block size must be >= 4 bytes. */
137139
err = -EINVAL;
138140
if (alg->cra_blocksize < 4)
@@ -155,14 +157,11 @@ static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
155157
inst->alg.decrypt = crypto_ctr_crypt;
156158

157159
err = skcipher_register_instance(tmpl, inst);
158-
if (err)
159-
goto out_free_inst;
160-
goto out_put_alg;
161-
160+
if (err) {
162161
out_free_inst:
163-
inst->free(inst);
164-
out_put_alg:
165-
crypto_mod_put(alg);
162+
inst->free(inst);
163+
}
164+
166165
return err;
167166
}
168167

‎crypto/ecb.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ static int crypto_ecb_decrypt(struct skcipher_request *req)
6161
static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
6262
{
6363
struct skcipher_instance *inst;
64-
struct crypto_alg *alg;
6564
int err;
6665

67-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
66+
inst = skcipher_alloc_instance_simple(tmpl, tb);
6867
if (IS_ERR(inst))
6968
return PTR_ERR(inst);
7069

@@ -76,7 +75,7 @@ static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
7675
err = skcipher_register_instance(tmpl, inst);
7776
if (err)
7877
inst->free(inst);
79-
crypto_mod_put(alg);
78+
8079
return err;
8180
}
8281

‎crypto/keywrap.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,12 @@ static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
266266
struct crypto_alg *alg;
267267
int err;
268268

269-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
269+
inst = skcipher_alloc_instance_simple(tmpl, tb);
270270
if (IS_ERR(inst))
271271
return PTR_ERR(inst);
272272

273+
alg = skcipher_ialg_simple(inst);
274+
273275
err = -EINVAL;
274276
/* Section 5.1 requirement for KW */
275277
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)
283285
inst->alg.decrypt = crypto_kw_decrypt;
284286

285287
err = skcipher_register_instance(tmpl, inst);
286-
if (err)
287-
goto out_free_inst;
288-
goto out_put_alg;
289-
288+
if (err) {
290289
out_free_inst:
291-
inst->free(inst);
292-
out_put_alg:
293-
crypto_mod_put(alg);
290+
inst->free(inst);
291+
}
292+
294293
return err;
295294
}
296295

‎crypto/ofb.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
5555
struct crypto_alg *alg;
5656
int err;
5757

58-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
58+
inst = skcipher_alloc_instance_simple(tmpl, tb);
5959
if (IS_ERR(inst))
6060
return PTR_ERR(inst);
6161

62+
alg = skcipher_ialg_simple(inst);
63+
6264
/* OFB mode is a stream cipher. */
6365
inst->alg.base.cra_blocksize = 1;
6466

@@ -75,7 +77,6 @@ static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
7577
if (err)
7678
inst->free(inst);
7779

78-
crypto_mod_put(alg);
7980
return err;
8081
}
8182

‎crypto/pcbc.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ static int crypto_pcbc_decrypt(struct skcipher_request *req)
153153
static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
154154
{
155155
struct skcipher_instance *inst;
156-
struct crypto_alg *alg;
157156
int err;
158157

159-
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
158+
inst = skcipher_alloc_instance_simple(tmpl, tb);
160159
if (IS_ERR(inst))
161160
return PTR_ERR(inst);
162161

@@ -166,7 +165,7 @@ static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
166165
err = skcipher_register_instance(tmpl, inst);
167166
if (err)
168167
inst->free(inst);
169-
crypto_mod_put(alg);
168+
170169
return err;
171170
}
172171

‎crypto/skcipher.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,12 @@ static void skcipher_free_instance_simple(struct skcipher_instance *inst)
938938
*
939939
* @tmpl: the template being instantiated
940940
* @tb: the template parameters
941-
* @cipher_alg_ret: on success, a pointer to the underlying cipher algorithm is
942-
* returned here. It must be dropped with crypto_mod_put().
943941
*
944942
* Return: a pointer to the new instance, or an ERR_PTR(). The caller still
945943
* needs to register the instance.
946944
*/
947-
struct skcipher_instance *
948-
skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
949-
struct crypto_alg **cipher_alg_ret)
945+
struct skcipher_instance *skcipher_alloc_instance_simple(
946+
struct crypto_template *tmpl, struct rtattr **tb)
950947
{
951948
struct crypto_attr_type *algt;
952949
struct crypto_alg *cipher_alg;
@@ -982,6 +979,7 @@ skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
982979
if (err)
983980
goto err_free_inst;
984981

982+
spawn->dropref = true;
985983
err = crypto_init_spawn(spawn, cipher_alg,
986984
skcipher_crypto_instance(inst),
987985
CRYPTO_ALG_TYPE_MASK);
@@ -1003,7 +1001,6 @@ skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
10031001
inst->alg.init = skcipher_init_tfm_simple;
10041002
inst->alg.exit = skcipher_exit_tfm_simple;
10051003

1006-
*cipher_alg_ret = cipher_alg;
10071004
return inst;
10081005

10091006
err_free_inst:

‎include/crypto/internal/skcipher.h‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,17 @@ skcipher_cipher_simple(struct crypto_skcipher *tfm)
214214

215215
return ctx->cipher;
216216
}
217-
struct skcipher_instance *
218-
skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
219-
struct crypto_alg **cipher_alg_ret);
217+
218+
struct skcipher_instance *skcipher_alloc_instance_simple(
219+
struct crypto_template *tmpl, struct rtattr **tb);
220+
221+
static inline struct crypto_alg *skcipher_ialg_simple(
222+
struct skcipher_instance *inst)
223+
{
224+
struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
225+
226+
return spawn->alg;
227+
}
220228

221229
#endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
222230

0 commit comments

Comments
 (0)