summaryrefslogtreecommitdiffstats
path: root/test/evp_extra_test.c
diff options
context:
space:
mode:
authorJack Lloyd <jack.lloyd@ribose.com>2018-06-18 15:51:56 -0400
committerMatt Caswell <matt@openssl.org>2018-06-19 11:29:44 +0100
commitddb634fe6f9aeea34fe036cf804903b4240d38ac (patch)
tree60501110c68634a55fdc888fcb5598e8ba9c4887 /test/evp_extra_test.c
parent2f2e6b6278bc4cbf670e42ae9f4ff818529df37c (diff)
Move SM2 algos to SM2 specific PKEY method
Use EVP_PKEY_set_alias_type to access Reviewed-by: Andy Polyakov <appro@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/6443)
Diffstat (limited to 'test/evp_extra_test.c')
-rw-r--r--test/evp_extra_test.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 640da7c765..deb8cfd95d 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -477,6 +477,127 @@ static int test_EVP_PKCS82PKEY(void)
}
#endif
+#ifndef OPENSSL_NO_SM2
+
+static int test_EVP_SM2(void)
+{
+ int ret = 0;
+ EVP_PKEY *pkey = NULL;
+ EVP_PKEY *params = NULL;
+ EVP_PKEY_CTX *pctx = NULL;
+ EVP_PKEY_CTX *kctx = NULL;
+ size_t sig_len = 0;
+ unsigned char *sig = NULL;
+ EVP_MD_CTX *md_ctx = NULL;
+ EVP_MD_CTX *md_ctx_verify = NULL;
+ EVP_PKEY_CTX *cctx = NULL;
+
+ uint8_t ciphertext[128];
+ size_t ctext_len = sizeof(ciphertext);
+
+ uint8_t plaintext[8];
+ size_t ptext_len = sizeof(plaintext);
+
+ pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
+ if (!TEST_ptr(pctx))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_paramgen(pctx, &params)))
+ goto done;
+
+ kctx = EVP_PKEY_CTX_new(params, NULL);
+ if (!TEST_ptr(kctx))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
+ goto done;
+
+ if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
+ goto done;
+
+ if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
+ goto done;
+
+ if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey)))
+ goto done;
+
+ if(!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
+ goto done;
+
+ /* Determine the size of the signature. */
+ if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
+ goto done;
+
+ if (!TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
+ goto done;
+
+ if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
+ goto done;
+
+ if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
+ goto done;
+
+ /* Ensure that the signature round-trips. */
+
+ if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sm3(), NULL, pkey)))
+ goto done;
+
+ if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
+ goto done;
+
+ if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
+ goto done;
+
+ /* now check encryption/decryption */
+
+ if (!TEST_ptr(cctx = EVP_PKEY_CTX_new(pkey, NULL)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg, sizeof(kMsg))))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
+ goto done;
+
+ if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext, ctext_len)))
+ goto done;
+
+ if (!TEST_true(ptext_len == sizeof(kMsg)))
+ goto done;
+
+ if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
+ goto done;
+
+ ret = 1;
+done:
+ EVP_PKEY_CTX_free(pctx);
+ EVP_PKEY_CTX_free(kctx);
+ EVP_PKEY_CTX_free(cctx);
+ EVP_PKEY_free(pkey);
+ EVP_PKEY_free(params);
+ EVP_MD_CTX_free(md_ctx);
+ EVP_MD_CTX_free(md_ctx_verify);
+ OPENSSL_free(sig);
+ return ret;
+}
+
+#endif
+
static struct keys_st {
int type;
char *priv;
@@ -664,6 +785,9 @@ int setup_tests(void)
#ifndef OPENSSL_NO_EC
ADD_TEST(test_EVP_PKCS82PKEY);
#endif
+#ifndef OPENSSL_NO_SM2
+ ADD_TEST(test_EVP_SM2);
+#endif
ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
if (!TEST_ptr(custom_pmeth))