aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2008-07-07 08:23:56 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2008-07-10 08:35:17 -0400
commitca786dc738f4f583b57b1bba7a335b5e8233f4b0 (patch)
treee2e6178fac1d9b3ac2b557cac76977e15f7d5d2c
parentcaee16883a235b1b042282276859e7d5901fad21 (diff)
crypto: hash - Fixed digest size check
The digest size check on hash algorithms is incorrect. It's perfectly valid for hash algorithms to have a digest length longer than their block size. For example crc32c has a block size of 1 and a digest size of 4. Rather than having it lie about its block size, this patch fixes the checks to do what they really should which is to bound the digest size so that code placing the digest on the stack continue to work. HMAC however still needs to check this as it's only defined for such algorithms. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/ahash.c2
-rw-r--r--crypto/digest.c2
-rw-r--r--crypto/hash.c2
-rw-r--r--crypto/hmac.c16
4 files changed, 13 insertions, 9 deletions
diff --git a/crypto/ahash.c b/crypto/ahash.c
index a83e035d9a3f..8c1f918a6878 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -68,7 +68,7 @@ static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
68 struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash; 68 struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
69 struct ahash_tfm *crt = &tfm->crt_ahash; 69 struct ahash_tfm *crt = &tfm->crt_ahash;
70 70
71 if (alg->digestsize > crypto_tfm_alg_blocksize(tfm)) 71 if (alg->digestsize > PAGE_SIZE / 8)
72 return -EINVAL; 72 return -EINVAL;
73 73
74 crt->init = alg->init; 74 crt->init = alg->init;
diff --git a/crypto/digest.c b/crypto/digest.c
index 025c9aea24ed..d63d5d96feec 100644
--- a/crypto/digest.c
+++ b/crypto/digest.c
@@ -141,7 +141,7 @@ int crypto_init_digest_ops(struct crypto_tfm *tfm)
141 struct hash_tfm *ops = &tfm->crt_hash; 141 struct hash_tfm *ops = &tfm->crt_hash;
142 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest; 142 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
143 143
144 if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm)) 144 if (dalg->dia_digestsize > PAGE_SIZE / 8)
145 return -EINVAL; 145 return -EINVAL;
146 146
147 ops->init = init; 147 ops->init = init;
diff --git a/crypto/hash.c b/crypto/hash.c
index f9400a014e74..0d7caa9ab748 100644
--- a/crypto/hash.c
+++ b/crypto/hash.c
@@ -152,7 +152,7 @@ static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
152{ 152{
153 struct hash_alg *alg = &tfm->__crt_alg->cra_hash; 153 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
154 154
155 if (alg->digestsize > crypto_tfm_alg_blocksize(tfm)) 155 if (alg->digestsize > PAGE_SIZE / 8)
156 return -EINVAL; 156 return -EINVAL;
157 157
158 if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK) 158 if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
diff --git a/crypto/hmac.c b/crypto/hmac.c
index 14c6351e639d..7ff2d6a8c7d0 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -226,6 +226,7 @@ static struct crypto_instance *hmac_alloc(struct rtattr **tb)
226 struct crypto_instance *inst; 226 struct crypto_instance *inst;
227 struct crypto_alg *alg; 227 struct crypto_alg *alg;
228 int err; 228 int err;
229 int ds;
229 230
230 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH); 231 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
231 if (err) 232 if (err)
@@ -236,6 +237,13 @@ static struct crypto_instance *hmac_alloc(struct rtattr **tb)
236 if (IS_ERR(alg)) 237 if (IS_ERR(alg))
237 return ERR_CAST(alg); 238 return ERR_CAST(alg);
238 239
240 inst = ERR_PTR(-EINVAL);
241 ds = (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
242 CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
243 alg->cra_digest.dia_digestsize;
244 if (ds > alg->cra_blocksize)
245 goto out_put_alg;
246
239 inst = crypto_alloc_instance("hmac", alg); 247 inst = crypto_alloc_instance("hmac", alg);
240 if (IS_ERR(inst)) 248 if (IS_ERR(inst))
241 goto out_put_alg; 249 goto out_put_alg;
@@ -246,14 +254,10 @@ static struct crypto_instance *hmac_alloc(struct rtattr **tb)
246 inst->alg.cra_alignmask = alg->cra_alignmask; 254 inst->alg.cra_alignmask = alg->cra_alignmask;
247 inst->alg.cra_type = &crypto_hash_type; 255 inst->alg.cra_type = &crypto_hash_type;
248 256
249 inst->alg.cra_hash.digestsize = 257 inst->alg.cra_hash.digestsize = ds;
250 (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
251 CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
252 alg->cra_digest.dia_digestsize;
253 258
254 inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) + 259 inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
255 ALIGN(inst->alg.cra_blocksize * 2 + 260 ALIGN(inst->alg.cra_blocksize * 2 + ds,
256 inst->alg.cra_hash.digestsize,
257 sizeof(void *)); 261 sizeof(void *));
258 262
259 inst->alg.cra_init = hmac_init_tfm; 263 inst->alg.cra_init = hmac_init_tfm;