summaryrefslogtreecommitdiffstats
path: root/crypto/testmgr.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2016-06-29 07:32:20 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-07-01 11:45:12 -0400
commit50d2b643ea6675927435743633a57c2a9cfd8d83 (patch)
tree46238fecd4c3440709cb36da5f8775ec79fd5bee /crypto/testmgr.c
parent1503a24f53f153f8c88be9810bc73efaf6853e1c (diff)
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: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/testmgr.c')
-rw-r--r--crypto/testmgr.c51
1 files changed, 24 insertions, 27 deletions
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index 537fdc380a7b..38e23be315a0 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -1911,8 +1911,8 @@ static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,
1911 return err; 1911 return err;
1912} 1912}
1913 1913
1914static int do_test_rsa(struct crypto_akcipher *tfm, 1914static int test_akcipher_one(struct crypto_akcipher *tfm,
1915 struct akcipher_testvec *vecs) 1915 struct akcipher_testvec *vecs)
1916{ 1916{
1917 char *xbuf[XBUFSIZE]; 1917 char *xbuf[XBUFSIZE];
1918 struct akcipher_request *req; 1918 struct akcipher_request *req;
@@ -1963,17 +1963,18 @@ static int do_test_rsa(struct crypto_akcipher *tfm,
1963 /* Run RSA encrypt - c = m^e mod n;*/ 1963 /* Run RSA encrypt - c = m^e mod n;*/
1964 err = wait_async_op(&result, crypto_akcipher_encrypt(req)); 1964 err = wait_async_op(&result, crypto_akcipher_encrypt(req));
1965 if (err) { 1965 if (err) {
1966 pr_err("alg: rsa: encrypt test failed. err %d\n", err); 1966 pr_err("alg: akcipher: encrypt test failed. err %d\n", err);
1967 goto free_all; 1967 goto free_all;
1968 } 1968 }
1969 if (req->dst_len != vecs->c_size) { 1969 if (req->dst_len != vecs->c_size) {
1970 pr_err("alg: rsa: encrypt test failed. Invalid output len\n"); 1970 pr_err("alg: akcipher: encrypt test failed. Invalid output len\n");
1971 err = -EINVAL; 1971 err = -EINVAL;
1972 goto free_all; 1972 goto free_all;
1973 } 1973 }
1974 /* verify that encrypted message is equal to expected */ 1974 /* verify that encrypted message is equal to expected */
1975 if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) { 1975 if (memcmp(vecs->c, outbuf_enc, vecs->c_size)) {
1976 pr_err("alg: rsa: encrypt test failed. Invalid output\n"); 1976 pr_err("alg: akcipher: encrypt test failed. Invalid output\n");
1977 hexdump(outbuf_enc, vecs->c_size);
1977 err = -EINVAL; 1978 err = -EINVAL;
1978 goto free_all; 1979 goto free_all;
1979 } 1980 }
@@ -2001,18 +2002,22 @@ static int do_test_rsa(struct crypto_akcipher *tfm,
2001 /* Run RSA decrypt - m = c^d mod n;*/ 2002 /* Run RSA decrypt - m = c^d mod n;*/
2002 err = wait_async_op(&result, crypto_akcipher_decrypt(req)); 2003 err = wait_async_op(&result, crypto_akcipher_decrypt(req));
2003 if (err) { 2004 if (err) {
2004 pr_err("alg: rsa: decrypt test failed. err %d\n", err); 2005 pr_err("alg: akcipher: decrypt test failed. err %d\n", err);
2005 goto free_all; 2006 goto free_all;
2006 } 2007 }
2007 out_len = req->dst_len; 2008 out_len = req->dst_len;
2008 if (out_len != vecs->m_size) { 2009 if (out_len < vecs->m_size) {
2009 pr_err("alg: rsa: decrypt test failed. Invalid output len\n"); 2010 pr_err("alg: akcipher: decrypt test failed. "
2011 "Invalid output len %u\n", out_len);
2010 err = -EINVAL; 2012 err = -EINVAL;
2011 goto free_all; 2013 goto free_all;
2012 } 2014 }
2013 /* verify that decrypted message is equal to the original msg */ 2015 /* verify that decrypted message is equal to the original msg */
2014 if (memcmp(vecs->m, outbuf_dec, vecs->m_size)) { 2016 if (memchr_inv(outbuf_dec, 0, out_len - vecs->m_size) ||
2015 pr_err("alg: rsa: decrypt test failed. Invalid output\n"); 2017 memcmp(vecs->m, outbuf_dec + out_len - vecs->m_size,
2018 vecs->m_size)) {
2019 pr_err("alg: akcipher: decrypt test failed. Invalid output\n");
2020 hexdump(outbuf_dec, out_len);
2016 err = -EINVAL; 2021 err = -EINVAL;
2017 } 2022 }
2018free_all: 2023free_all:
@@ -2025,28 +2030,20 @@ free_xbuf:
2025 return err; 2030 return err;
2026} 2031}
2027 2032
2028static int test_rsa(struct crypto_akcipher *tfm, struct akcipher_testvec *vecs, 2033static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2029 unsigned int tcount) 2034 struct akcipher_testvec *vecs, unsigned int tcount)
2030{ 2035{
2031 int ret, i; 2036 int ret, i;
2032 2037
2033 for (i = 0; i < tcount; i++) { 2038 for (i = 0; i < tcount; i++) {
2034 ret = do_test_rsa(tfm, vecs++); 2039 ret = test_akcipher_one(tfm, vecs++);
2035 if (ret) { 2040 if (!ret)
2036 pr_err("alg: rsa: test failed on vector %d, err=%d\n", 2041 continue;
2037 i + 1, ret);
2038 return ret;
2039 }
2040 }
2041 return 0;
2042}
2043
2044static int test_akcipher(struct crypto_akcipher *tfm, const char *alg,
2045 struct akcipher_testvec *vecs, unsigned int tcount)
2046{
2047 if (strncmp(alg, "rsa", 3) == 0)
2048 return test_rsa(tfm, vecs, tcount);
2049 2042
2043 pr_err("alg: akcipher: test failed on vector %d, err=%d\n",
2044 i + 1, ret);
2045 return ret;
2046 }
2050 return 0; 2047 return 0;
2051} 2048}
2052 2049