Commit 50d2b643 authored by Herbert Xu's avatar Herbert Xu

crypto: testmgr - Allow leading zeros in RSA

This patch allows RSA implementations to produce output with
leading zeroes.  testmgr will skip leading zeroes when comparing
the output.

This patch also tries to make the RSA test function generic enough
to potentially handle other akcipher algorithms.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 1503a24f
...@@ -1911,8 +1911,8 @@ static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver, ...@@ -1911,8 +1911,8 @@ static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
return err; return err;
} }
static int do_test_rsa(struct crypto_akcipher *tfm, static int test_akcipher_one(struct crypto_akcipher *tfm,
struct akcipher_testvec *vecs) struct akcipher_testvec *vecs)
{ {
char *xbuf[XBUFSIZE]; char *xbuf[XBUFSIZE];
struct akcipher_request *req; struct akcipher_request *req;
...@@ -1963,17 +1963,18 @@ static int do_test_rsa(struct crypto_akcipher *tfm, ...@@ -1963,17 +1963,18 @@ static int do_test_rsa(struct crypto_akcipher *tfm,
/* Run RSA encrypt - c = m^e mod n;*/ /* Run RSA encrypt - c = m^e mod n;*/
err = wait_async_op(&result, crypto_akcipher_encrypt(req)); err = wait_async_op(&result, crypto_akcipher_encrypt(req));
if (err) { if (err) {
pr_err("alg: rsa: encrypt test failed. err %d\n", err); pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
goto free_all; goto free_all;
} }
if (req->dst_len != vecs->c_size) { if (req->dst_len != vecs->c_size) {
pr_err("alg: rsa: encrypt test failed. Invalid output len\n"); pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
err = -EINVAL; err = -EINVAL;
goto free_all; goto free_all;
} }
/* verify that encrypted message is equal to expected */ /* verify that encrypted message is equal to expected */
if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) { if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
pr_err("alg: rsa: encrypt test failed. Invalid output\n"); pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
hexdump(outbuf_enc, vecs->c_size);
err = -EINVAL; err = -EINVAL;
goto free_all; goto free_all;
} }
...@@ -2001,18 +2002,22 @@ static int do_test_rsa(struct crypto_akcipher *tfm, ...@@ -2001,18 +2002,22 @@ static int do_test_rsa(struct crypto_akcipher *tfm,
/* Run RSA decrypt - m = c^d mod n;*/ /* Run RSA decrypt - m = c^d mod n;*/
err = wait_async_op(&result, crypto_akcipher_decrypt(req)); err = wait_async_op(&result, crypto_akcipher_decrypt(req));
if (err) { if (err) {
pr_err("alg: rsa: decrypt test failed. err %d\n", err); pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
goto free_all; goto free_all;
} }
out_len = req->dst_len; out_len = req->dst_len;
if (out_len != vecs->m_size) { if (out_len < vecs->m_size) {
pr_err("alg: rsa: decrypt test failed. Invalid output len\n"); pr_err("alg: akcipher: decrypt test failed. "
"Invalid output len %u\n", out_len);
err = -EINVAL; err = -EINVAL;
goto free_all; goto free_all;
} }
/* verify that decrypted message is equal to the original msg */ /* verify that decrypted message is equal to the original msg */
if (memcmp(vecs->m, outbuf_dec, vecs->m_size)) { if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
pr_err("alg: rsa: decrypt test failed. Invalid output\n"); memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
vecs->m_size)) {
pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
hexdump(outbuf_dec, out_len);
err = -EINVAL; err = -EINVAL;
} }
free_all: free_all:
...@@ -2025,28 +2030,20 @@ static int do_test_rsa(struct crypto_akcipher *tfm, ...@@ -2025,28 +2030,20 @@ static int do_test_rsa(struct crypto_akcipher *tfm,
return err; return err;
} }
static int test_rsa(struct crypto_akcipher *tfm, struct akcipher_testvec *vecs, static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
unsigned int tcount) struct akcipher_testvec *vecs, unsigned int tcount)
{ {
int ret, i; int ret, i;
for (i = 0; i < tcount; i++) { for (i = 0; i < tcount; i++) {
ret = do_test_rsa(tfm, vecs++); ret = test_akcipher_one(tfm, vecs++);
if (ret) { if (!ret)
pr_err("alg: rsa: test failed on vector %d, err=%d\n", continue;
i + 1, ret);
return ret;
}
}
return 0;
}
static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
struct akcipher_testvec *vecs, unsigned int tcount)
{
if (strncmp(alg, "rsa", 3) == 0)
return test_rsa(tfm, vecs, tcount);
pr_err("alg: akcipher: test failed on vector %d, err=%d\n",
i + 1, ret);
return ret;
}
return 0; return 0;
} }
......
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