Skip to content

Commit 30e4c01

Browse files
committed
crypto: aead - Add crypto_aead_alg_ivsize/maxauthsize
AEAD algorithm implementors need to figure out a given algorithm's IV size and maximum authentication size. During the transition this is difficult to do as an algorithm could be new style or old style. This patch creates two helpers to make this easier. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 445a8e0 commit 30e4c01

3 files changed

Lines changed: 28 additions & 27 deletions

File tree

‎crypto/aead.c‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
6969
{
7070
int err;
7171

72-
if (authsize > tfm->maxauthsize)
72+
if (authsize > crypto_aead_maxauthsize(tfm))
7373
return -EINVAL;
7474

7575
if (tfm->setauthsize) {
@@ -162,8 +162,6 @@ static int crypto_old_aead_init_tfm(struct crypto_tfm *tfm)
162162
crt->givdecrypt = aead_null_givdecrypt;
163163
}
164164
crt->child = __crypto_aead_cast(tfm);
165-
crt->ivsize = alg->ivsize;
166-
crt->maxauthsize = alg->maxauthsize;
167165
crt->authsize = alg->maxauthsize;
168166

169167
return 0;
@@ -182,8 +180,6 @@ static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
182180
aead->encrypt = alg->encrypt;
183181
aead->decrypt = alg->decrypt;
184182
aead->child = __crypto_aead_cast(tfm);
185-
aead->ivsize = alg->ivsize;
186-
aead->maxauthsize = alg->maxauthsize;
187183
aead->authsize = alg->maxauthsize;
188184

189185
return 0;
@@ -418,13 +414,8 @@ struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
418414

419415
alg = crypto_spawn_aead_alg(spawn);
420416

421-
if (alg->base.cra_aead.encrypt) {
422-
ivsize = alg->base.cra_aead.ivsize;
423-
maxauthsize = alg->base.cra_aead.maxauthsize;
424-
} else {
425-
ivsize = alg->ivsize;
426-
maxauthsize = alg->maxauthsize;
427-
}
417+
ivsize = crypto_aead_alg_ivsize(alg);
418+
maxauthsize = crypto_aead_alg_maxauthsize(alg);
428419

429420
err = -EINVAL;
430421
if (!ivsize)

‎include/crypto/aead.h‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ struct crypto_aead {
139139

140140
struct crypto_aead *child;
141141

142-
unsigned int ivsize;
143142
unsigned int authsize;
144-
unsigned int maxauthsize;
145143
unsigned int reqsize;
146144

147145
struct crypto_tfm base;
@@ -187,6 +185,23 @@ static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
187185
return tfm;
188186
}
189187

188+
static inline struct old_aead_alg *crypto_old_aead_alg(struct crypto_aead *tfm)
189+
{
190+
return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
191+
}
192+
193+
static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
194+
{
195+
return container_of(crypto_aead_tfm(tfm)->__crt_alg,
196+
struct aead_alg, base);
197+
}
198+
199+
static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)
200+
{
201+
return alg->base.cra_aead.encrypt ? alg->base.cra_aead.ivsize :
202+
alg->ivsize;
203+
}
204+
190205
/**
191206
* crypto_aead_ivsize() - obtain IV size
192207
* @tfm: cipher handle
@@ -198,7 +213,7 @@ static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
198213
*/
199214
static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
200215
{
201-
return tfm->ivsize;
216+
return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));
202217
}
203218

204219
/**

‎include/crypto/internal/aead.h‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ struct crypto_aead_spawn {
3030
extern const struct crypto_type crypto_aead_type;
3131
extern const struct crypto_type crypto_nivaead_type;
3232

33-
static inline struct old_aead_alg *crypto_old_aead_alg(struct crypto_aead *tfm)
34-
{
35-
return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
36-
}
37-
38-
static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
39-
{
40-
return container_of(crypto_aead_tfm(tfm)->__crt_alg,
41-
struct aead_alg, base);
42-
}
43-
4433
static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
4534
{
4635
return crypto_tfm_ctx(&tfm->base);
@@ -145,9 +134,15 @@ static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
145134
crypto_aead_crt(aead)->reqsize = reqsize;
146135
}
147136

137+
static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
138+
{
139+
return alg->base.cra_aead.encrypt ? alg->base.cra_aead.maxauthsize :
140+
alg->maxauthsize;
141+
}
142+
148143
static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
149144
{
150-
return aead->maxauthsize;
145+
return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
151146
}
152147

153148
int crypto_register_aead(struct aead_alg *alg);

0 commit comments

Comments
 (0)