Skip to content

Commit 113adef

Browse files
committed
crypto: shash - Make descsize a run-time attribute
This patch changes descsize to a run-time attribute so that implementations can change it in their init functions. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent aef73cf commit 113adef

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎crypto/shash.c‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,16 @@ static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
300300
static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
301301
unsigned int keylen)
302302
{
303-
struct shash_desc *desc = crypto_hash_ctx(tfm);
303+
struct shash_desc **descp = crypto_hash_ctx(tfm);
304+
struct shash_desc *desc = *descp;
304305

305306
return crypto_shash_setkey(desc->tfm, key, keylen);
306307
}
307308

308309
static int shash_compat_init(struct hash_desc *hdesc)
309310
{
310-
struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
311+
struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
312+
struct shash_desc *desc = *descp;
311313

312314
desc->flags = hdesc->flags;
313315

@@ -317,7 +319,8 @@ static int shash_compat_init(struct hash_desc *hdesc)
317319
static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
318320
unsigned int len)
319321
{
320-
struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
322+
struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
323+
struct shash_desc *desc = *descp;
321324
struct crypto_hash_walk walk;
322325
int nbytes;
323326

@@ -330,7 +333,9 @@ static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
330333

331334
static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
332335
{
333-
return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out);
336+
struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
337+
338+
return crypto_shash_final(*descp, out);
334339
}
335340

336341
static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
@@ -340,7 +345,8 @@ static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
340345
int err;
341346

342347
if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
343-
struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
348+
struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
349+
struct shash_desc *desc = *descp;
344350
void *data;
345351

346352
desc->flags = hdesc->flags;
@@ -368,18 +374,21 @@ static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
368374

369375
static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
370376
{
371-
struct shash_desc *desc= crypto_tfm_ctx(tfm);
377+
struct shash_desc **descp = crypto_tfm_ctx(tfm);
378+
struct shash_desc *desc = *descp;
372379

373380
crypto_free_shash(desc->tfm);
381+
kzfree(desc);
374382
}
375383

376384
static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
377385
{
378386
struct hash_tfm *crt = &tfm->crt_hash;
379387
struct crypto_alg *calg = tfm->__crt_alg;
380388
struct shash_alg *alg = __crypto_shash_alg(calg);
381-
struct shash_desc *desc = crypto_tfm_ctx(tfm);
389+
struct shash_desc **descp = crypto_tfm_ctx(tfm);
382390
struct crypto_shash *shash;
391+
struct shash_desc *desc;
383392

384393
if (!crypto_mod_get(calg))
385394
return -EAGAIN;
@@ -390,6 +399,14 @@ static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
390399
return PTR_ERR(shash);
391400
}
392401

402+
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
403+
GFP_KERNEL);
404+
if (!desc) {
405+
crypto_free_shash(shash);
406+
return -ENOMEM;
407+
}
408+
409+
*descp = desc;
393410
desc->tfm = shash;
394411
tfm->exit = crypto_exit_shash_ops_compat;
395412

@@ -419,11 +436,9 @@ static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
419436
static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
420437
u32 mask)
421438
{
422-
struct shash_alg *salg = __crypto_shash_alg(alg);
423-
424439
switch (mask & CRYPTO_ALG_TYPE_MASK) {
425440
case CRYPTO_ALG_TYPE_HASH_MASK:
426-
return sizeof(struct shash_desc) + salg->descsize;
441+
return sizeof(struct shash_desc *);
427442
case CRYPTO_ALG_TYPE_AHASH_MASK:
428443
return sizeof(struct crypto_shash *);
429444
}
@@ -434,6 +449,9 @@ static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
434449
static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
435450
const struct crypto_type *frontend)
436451
{
452+
struct crypto_shash *hash = __crypto_shash_cast(tfm);
453+
454+
hash->descsize = crypto_shash_alg(hash)->descsize;
437455
return 0;
438456
}
439457

@@ -452,7 +470,6 @@ static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
452470
seq_printf(m, "type : shash\n");
453471
seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
454472
seq_printf(m, "digestsize : %u\n", salg->digestsize);
455-
seq_printf(m, "descsize : %u\n", salg->descsize);
456473
}
457474

458475
static const struct crypto_type crypto_shash_type = {

‎include/crypto/hash.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct crypto_ahash {
4848
};
4949

5050
struct crypto_shash {
51+
unsigned int descsize;
5152
struct crypto_tfm base;
5253
};
5354

@@ -275,7 +276,7 @@ static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
275276

276277
static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
277278
{
278-
return crypto_shash_alg(tfm)->descsize;
279+
return tfm->descsize;
279280
}
280281

281282
static inline void *shash_desc_ctx(struct shash_desc *desc)

‎include/crypto/internal/hash.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static inline void *crypto_shash_ctx_aligned(struct crypto_shash *tfm)
135135

136136
static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
137137
{
138-
return (struct crypto_shash *)tfm;
138+
return container_of(tfm, struct crypto_shash, base);
139139
}
140140

141141
#endif /* _CRYPTO_INTERNAL_HASH_H */

0 commit comments

Comments
 (0)