summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <jack.lloyd@ribose.com>2018-09-04 23:25:29 +0800
committerPaul Yang <yang.yang@baishancloud.com>2018-09-07 18:12:26 +0800
commita6c4cb845a031f09c6303a2c3452e253d0d01518 (patch)
tree4d45bdb443d0f460aac4063d91fbf6a173d46d6c
parent4803717f5e3bcfba3e3442e1611f421bf1090a47 (diff)
Add test case for SM2 evp verification
This test case is originally submitted in #6757, by Jack Lloyd. The test case has been modified to use the a different method to set the ID when computing the Z hash of SM2 signature. Reviewed-by: Tim Hudson <tjh@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Yang <yang.yang@baishancloud.com> (Merged from https://github.com/openssl/openssl/pull/7113)
-rw-r--r--test/evp_extra_test.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 2fb3914ee8..f0b0040eae 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -524,6 +524,84 @@ static int test_EVP_PKCS82PKEY(void)
#ifndef OPENSSL_NO_SM2
+static int test_EVP_SM2_verify(void)
+{
+ /* From https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02#appendix-A */
+ const char *pubkey =
+ "-----BEGIN PUBLIC KEY-----\n"
+ "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEAhULWnkwETxjouSQ1\n"
+ "v2/33kVyg5FcRVF9ci7biwjx38MwRAQgeHlotPoyw/0kF4Quc7v+/y88hItoMdfg\n"
+ "7GUiizk35JgEIGPkxtOyOwyEnPhCQUhL/kj2HVmlsWugbm4S0donxSSaBEEEQh3r\n"
+ "1hti6rZ0ZDTrw8wxXjIiCzut1QvcTE5sFH/t1D0GgFEry7QsB9RzSdIVO3DE5df9\n"
+ "/L+jbqGoWEG55G4JogIhAIVC1p5MBE8Y6LkkNb9v990pdyBjBIVijVrnTufDLnm3\n"
+ "AgEBA0IABArkx3mKoPEZRxvuEYJb5GICu3nipYRElel8BP9N8lSKfAJA+I8c1OFj\n"
+ "Uqc8F7fxbwc1PlOhdtaEqf4Ma7eY6Fc=\n"
+ "-----END PUBLIC KEY-----\n";
+
+ const char *msg = "message digest";
+ const char *id = "ALICE123@YAHOO.COM";
+
+ const uint8_t signature[] = {
+ 0x30, 0x44, 0x02, 0x20,
+
+ 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4, 0x9E, 0x09, 0xDC,
+ 0xEF, 0x49, 0x13, 0x0D, 0x41, 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2,
+ 0xCA, 0xA5, 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1,
+
+ 0x02, 0x20,
+
+ 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1, 0x0C, 0x77, 0xDF,
+ 0xB2, 0x0F, 0x7C, 0x2E, 0xB6, 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0,
+ 0x9E, 0xC5, 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7
+ };
+
+ int rc = 0;
+ BIO *bio = NULL;
+ EVP_PKEY *pkey = NULL;
+ EVP_MD_CTX *mctx = NULL;
+ EVP_PKEY_CTX *pctx = NULL;
+
+ bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
+ if (!TEST_true(bio != NULL))
+ goto done;
+
+ pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
+ if (!TEST_true(pkey != NULL))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
+ goto done;
+
+ if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
+ goto done;
+
+ if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL)))
+ goto done;
+
+ if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, (const uint8_t *)id,
+ strlen(id)), 0))
+ goto done;
+
+ EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
+
+ if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey)))
+ goto done;
+
+ if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
+ goto done;
+
+ if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
+ goto done;
+ rc = 1;
+
+ done:
+ BIO_free(bio);
+ EVP_PKEY_free(pkey);
+ EVP_PKEY_CTX_free(pctx);
+ EVP_MD_CTX_free(mctx);
+ return rc;
+}
+
static int test_EVP_SM2(void)
{
int ret = 0;
@@ -846,6 +924,7 @@ int setup_tests(void)
#endif
#ifndef OPENSSL_NO_SM2
ADD_TEST(test_EVP_SM2);
+ ADD_TEST(test_EVP_SM2_verify);
#endif
ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);