Commit 76715095 authored by Gilad Ben-Yossef's avatar Gilad Ben-Yossef Committed by Herbert Xu

crypto: testmgr - add hash finup tests

The testmgr hash tests were testing init, digest, update and final
methods but not the finup method. Add a test for this one too.

While doing this, make sure we only run the partial tests once with
the digest tests and skip them with the final and finup tests since
they are the same.
Signed-off-by: default avatarGilad Ben-Yossef <gilad@benyossef.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 26497e72
...@@ -259,9 +259,15 @@ static int ahash_partial_update(struct ahash_request **preq, ...@@ -259,9 +259,15 @@ static int ahash_partial_update(struct ahash_request **preq,
return ret; return ret;
} }
enum hash_test {
HASH_TEST_DIGEST,
HASH_TEST_FINAL,
HASH_TEST_FINUP
};
static int __test_hash(struct crypto_ahash *tfm, static int __test_hash(struct crypto_ahash *tfm,
const struct hash_testvec *template, unsigned int tcount, const struct hash_testvec *template, unsigned int tcount,
bool use_digest, const int align_offset) enum hash_test test_type, const int align_offset)
{ {
const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm)); const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
size_t digest_size = crypto_ahash_digestsize(tfm); size_t digest_size = crypto_ahash_digestsize(tfm);
...@@ -332,14 +338,17 @@ static int __test_hash(struct crypto_ahash *tfm, ...@@ -332,14 +338,17 @@ static int __test_hash(struct crypto_ahash *tfm,
} }
ahash_request_set_crypt(req, sg, result, template[i].psize); ahash_request_set_crypt(req, sg, result, template[i].psize);
if (use_digest) { switch (test_type) {
case HASH_TEST_DIGEST:
ret = crypto_wait_req(crypto_ahash_digest(req), &wait); ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
if (ret) { if (ret) {
pr_err("alg: hash: digest failed on test %d " pr_err("alg: hash: digest failed on test %d "
"for %s: ret=%d\n", j, algo, -ret); "for %s: ret=%d\n", j, algo, -ret);
goto out; goto out;
} }
} else { break;
case HASH_TEST_FINAL:
memset(result, 1, digest_size); memset(result, 1, digest_size);
ret = crypto_wait_req(crypto_ahash_init(req), &wait); ret = crypto_wait_req(crypto_ahash_init(req), &wait);
if (ret) { if (ret) {
...@@ -371,6 +380,29 @@ static int __test_hash(struct crypto_ahash *tfm, ...@@ -371,6 +380,29 @@ static int __test_hash(struct crypto_ahash *tfm,
"for %s: ret=%d\n", j, algo, -ret); "for %s: ret=%d\n", j, algo, -ret);
goto out; goto out;
} }
break;
case HASH_TEST_FINUP:
memset(result, 1, digest_size);
ret = crypto_wait_req(crypto_ahash_init(req), &wait);
if (ret) {
pr_err("alg: hash: init failed on test %d "
"for %s: ret=%d\n", j, algo, -ret);
goto out;
}
ret = ahash_guard_result(result, 1, digest_size);
if (ret) {
pr_err("alg: hash: init failed on test %d "
"for %s: used req->result\n", j, algo);
goto out;
}
ret = crypto_wait_req(crypto_ahash_finup(req), &wait);
if (ret) {
pr_err("alg: hash: final failed on test %d "
"for %s: ret=%d\n", j, algo, -ret);
goto out;
}
break;
} }
if (memcmp(result, template[i].digest, if (memcmp(result, template[i].digest,
...@@ -383,6 +415,9 @@ static int __test_hash(struct crypto_ahash *tfm, ...@@ -383,6 +415,9 @@ static int __test_hash(struct crypto_ahash *tfm,
} }
} }
if (test_type)
goto out;
j = 0; j = 0;
for (i = 0; i < tcount; i++) { for (i = 0; i < tcount; i++) {
/* alignment tests are only done with continuous buffers */ /* alignment tests are only done with continuous buffers */
...@@ -540,24 +575,24 @@ static int __test_hash(struct crypto_ahash *tfm, ...@@ -540,24 +575,24 @@ static int __test_hash(struct crypto_ahash *tfm,
static int test_hash(struct crypto_ahash *tfm, static int test_hash(struct crypto_ahash *tfm,
const struct hash_testvec *template, const struct hash_testvec *template,
unsigned int tcount, bool use_digest) unsigned int tcount, enum hash_test test_type)
{ {
unsigned int alignmask; unsigned int alignmask;
int ret; int ret;
ret = __test_hash(tfm, template, tcount, use_digest, 0); ret = __test_hash(tfm, template, tcount, test_type, 0);
if (ret) if (ret)
return ret; return ret;
/* test unaligned buffers, check with one byte offset */ /* test unaligned buffers, check with one byte offset */
ret = __test_hash(tfm, template, tcount, use_digest, 1); ret = __test_hash(tfm, template, tcount, test_type, 1);
if (ret) if (ret)
return ret; return ret;
alignmask = crypto_tfm_alg_alignmask(&tfm->base); alignmask = crypto_tfm_alg_alignmask(&tfm->base);
if (alignmask) { if (alignmask) {
/* Check if alignment mask for tfm is correctly set. */ /* Check if alignment mask for tfm is correctly set. */
ret = __test_hash(tfm, template, tcount, use_digest, ret = __test_hash(tfm, template, tcount, test_type,
alignmask + 1); alignmask + 1);
if (ret) if (ret)
return ret; return ret;
...@@ -1803,9 +1838,11 @@ static int __alg_test_hash(const struct hash_testvec *template, ...@@ -1803,9 +1838,11 @@ static int __alg_test_hash(const struct hash_testvec *template,
return PTR_ERR(tfm); return PTR_ERR(tfm);
} }
err = test_hash(tfm, template, tcount, true); err = test_hash(tfm, template, tcount, HASH_TEST_DIGEST);
if (!err)
err = test_hash(tfm, template, tcount, HASH_TEST_FINAL);
if (!err) if (!err)
err = test_hash(tfm, template, tcount, false); err = test_hash(tfm, template, tcount, HASH_TEST_FINUP);
crypto_free_ahash(tfm); crypto_free_ahash(tfm);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment