Codebase list openssl / a6c4cb8
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) Jack Lloyd authored 5 years ago Paul Yang committed 5 years ago
1 changed file(s) with 79 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
523523
524524 #ifndef OPENSSL_NO_SM2
525525
526 static int test_EVP_SM2_verify(void)
527 {
528 /* From https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02#appendix-A */
529 const char *pubkey =
530 "-----BEGIN PUBLIC KEY-----\n"
531 "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEAhULWnkwETxjouSQ1\n"
532 "v2/33kVyg5FcRVF9ci7biwjx38MwRAQgeHlotPoyw/0kF4Quc7v+/y88hItoMdfg\n"
533 "7GUiizk35JgEIGPkxtOyOwyEnPhCQUhL/kj2HVmlsWugbm4S0donxSSaBEEEQh3r\n"
534 "1hti6rZ0ZDTrw8wxXjIiCzut1QvcTE5sFH/t1D0GgFEry7QsB9RzSdIVO3DE5df9\n"
535 "/L+jbqGoWEG55G4JogIhAIVC1p5MBE8Y6LkkNb9v990pdyBjBIVijVrnTufDLnm3\n"
536 "AgEBA0IABArkx3mKoPEZRxvuEYJb5GICu3nipYRElel8BP9N8lSKfAJA+I8c1OFj\n"
537 "Uqc8F7fxbwc1PlOhdtaEqf4Ma7eY6Fc=\n"
538 "-----END PUBLIC KEY-----\n";
539
540 const char *msg = "message digest";
541 const char *id = "ALICE123@YAHOO.COM";
542
543 const uint8_t signature[] = {
544 0x30, 0x44, 0x02, 0x20,
545
546 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4, 0x9E, 0x09, 0xDC,
547 0xEF, 0x49, 0x13, 0x0D, 0x41, 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2,
548 0xCA, 0xA5, 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1,
549
550 0x02, 0x20,
551
552 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1, 0x0C, 0x77, 0xDF,
553 0xB2, 0x0F, 0x7C, 0x2E, 0xB6, 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0,
554 0x9E, 0xC5, 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7
555 };
556
557 int rc = 0;
558 BIO *bio = NULL;
559 EVP_PKEY *pkey = NULL;
560 EVP_MD_CTX *mctx = NULL;
561 EVP_PKEY_CTX *pctx = NULL;
562
563 bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
564 if (!TEST_true(bio != NULL))
565 goto done;
566
567 pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
568 if (!TEST_true(pkey != NULL))
569 goto done;
570
571 if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
572 goto done;
573
574 if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
575 goto done;
576
577 if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL)))
578 goto done;
579
580 if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, (const uint8_t *)id,
581 strlen(id)), 0))
582 goto done;
583
584 EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
585
586 if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey)))
587 goto done;
588
589 if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
590 goto done;
591
592 if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
593 goto done;
594 rc = 1;
595
596 done:
597 BIO_free(bio);
598 EVP_PKEY_free(pkey);
599 EVP_PKEY_CTX_free(pctx);
600 EVP_MD_CTX_free(mctx);
601 return rc;
602 }
603
526604 static int test_EVP_SM2(void)
527605 {
528606 int ret = 0;
845923 #endif
846924 #ifndef OPENSSL_NO_SM2
847925 ADD_TEST(test_EVP_SM2);
926 ADD_TEST(test_EVP_SM2_verify);
848927 #endif
849928 ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
850929 custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);