Skip to content

Commit fbce6be

Browse files
committed
crypto: shash - Add init_tfm/exit_tfm and verify descsize
The shash interface supports a dynamic descsize field because of the presence of fallbacks (it's just padlock-sha actually, perhaps we can remove it one day). As it is the API does not verify the setting of descsize at all. It is up to the individual algorithms to ensure that descsize does not exceed the specified maximum value of HASH_MAX_DESCSIZE (going above would cause stack corruption). In order to allow the API to impose this limit directly, this patch adds init_tfm/exit_tfm hooks to the shash_alg structure. We can then verify the descsize setting in the API directly. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Reviewed-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 02244ba commit fbce6be

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎crypto/shash.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,41 @@ int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
385385
return 0;
386386
}
387387

388+
static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
389+
{
390+
struct crypto_shash *hash = __crypto_shash_cast(tfm);
391+
struct shash_alg *alg = crypto_shash_alg(hash);
392+
393+
alg->exit_tfm(hash);
394+
}
395+
388396
static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
389397
{
390398
struct crypto_shash *hash = __crypto_shash_cast(tfm);
391399
struct shash_alg *alg = crypto_shash_alg(hash);
400+
int err;
392401

393402
hash->descsize = alg->descsize;
394403

395404
shash_set_needkey(hash, alg);
396405

406+
if (alg->exit_tfm)
407+
tfm->exit = crypto_shash_exit_tfm;
408+
409+
if (!alg->init_tfm)
410+
return 0;
411+
412+
err = alg->init_tfm(hash);
413+
if (err)
414+
return err;
415+
416+
/* ->init_tfm() may have increased the descsize. */
417+
if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) {
418+
if (alg->exit_tfm)
419+
alg->exit_tfm(hash);
420+
return -EINVAL;
421+
}
422+
397423
return 0;
398424
}
399425

‎include/crypto/hash.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ struct shash_desc {
169169
* @export: see struct ahash_alg
170170
* @import: see struct ahash_alg
171171
* @setkey: see struct ahash_alg
172+
* @init_tfm: Initialize the cryptographic transformation object.
173+
* This function is called only once at the instantiation
174+
* time, right after the transformation context was
175+
* allocated. In case the cryptographic hardware has
176+
* some special requirements which need to be handled
177+
* by software, this function shall check for the precise
178+
* requirement of the transformation and put any software
179+
* fallbacks in place.
180+
* @exit_tfm: Deinitialize the cryptographic transformation object.
181+
* This is a counterpart to @init_tfm, used to remove
182+
* various changes set in @init_tfm.
172183
* @digestsize: see struct ahash_alg
173184
* @statesize: see struct ahash_alg
174185
* @descsize: Size of the operational state for the message digest. This state
@@ -189,6 +200,8 @@ struct shash_alg {
189200
int (*import)(struct shash_desc *desc, const void *in);
190201
int (*setkey)(struct crypto_shash *tfm, const u8 *key,
191202
unsigned int keylen);
203+
int (*init_tfm)(struct crypto_shash *tfm);
204+
void (*exit_tfm)(struct crypto_shash *tfm);
192205

193206
unsigned int descsize;
194207

0 commit comments

Comments
 (0)