1e0c4386eSCy Schubert /* 2*44096ebdSEnji Cooper * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved. 3e0c4386eSCy Schubert * 4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 8e0c4386eSCy Schubert */ 9e0c4386eSCy Schubert 10e0c4386eSCy Schubert /* 11e0c4386eSCy Schubert * Low level APIs are deprecated for public use, but still ok for internal use. 12e0c4386eSCy Schubert */ 13e0c4386eSCy Schubert #include "internal/deprecated.h" 14e0c4386eSCy Schubert 15e0c4386eSCy Schubert #include <stdio.h> 16e0c4386eSCy Schubert #include <stdlib.h> 17e0c4386eSCy Schubert #include <string.h> 18e0c4386eSCy Schubert 19e0c4386eSCy Schubert #include <openssl/bio.h> 20e0c4386eSCy Schubert #include <openssl/evp.h> 21e0c4386eSCy Schubert #include <openssl/bn.h> 22e0c4386eSCy Schubert #include <openssl/crypto.h> 23e0c4386eSCy Schubert #include <openssl/err.h> 24e0c4386eSCy Schubert #include <openssl/rand.h> 25e0c4386eSCy Schubert #include "testutil.h" 26e0c4386eSCy Schubert 27e0c4386eSCy Schubert #ifndef OPENSSL_NO_SM2 28e0c4386eSCy Schubert 29e0c4386eSCy Schubert # include "crypto/sm2.h" 30e0c4386eSCy Schubert 31e0c4386eSCy Schubert static fake_random_generate_cb get_faked_bytes; 32e0c4386eSCy Schubert 33e0c4386eSCy Schubert static OSSL_PROVIDER *fake_rand = NULL; 34e0c4386eSCy Schubert static uint8_t *fake_rand_bytes = NULL; 35e0c4386eSCy Schubert static size_t fake_rand_bytes_offset = 0; 36e0c4386eSCy Schubert static size_t fake_rand_size = 0; 37e0c4386eSCy Schubert 38e0c4386eSCy Schubert static int get_faked_bytes(unsigned char *buf, size_t num, 39e0c4386eSCy Schubert ossl_unused const char *name, 40e0c4386eSCy Schubert ossl_unused EVP_RAND_CTX *ctx) 41e0c4386eSCy Schubert { 42e0c4386eSCy Schubert if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0)) 43e0c4386eSCy Schubert return 0; 44e0c4386eSCy Schubert 45e0c4386eSCy Schubert while (num-- > 0) { 46e0c4386eSCy Schubert if (fake_rand_bytes_offset >= fake_rand_size) 47e0c4386eSCy Schubert fake_rand_bytes_offset = 0; 48e0c4386eSCy Schubert *buf++ = fake_rand_bytes[fake_rand_bytes_offset++]; 49e0c4386eSCy Schubert } 50e0c4386eSCy Schubert 51e0c4386eSCy Schubert return 1; 52e0c4386eSCy Schubert } 53e0c4386eSCy Schubert 54e0c4386eSCy Schubert static int start_fake_rand(const char *hex_bytes) 55e0c4386eSCy Schubert { 56e0c4386eSCy Schubert OPENSSL_free(fake_rand_bytes); 57e0c4386eSCy Schubert fake_rand_bytes_offset = 0; 58e0c4386eSCy Schubert fake_rand_size = strlen(hex_bytes) / 2; 59e0c4386eSCy Schubert if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL))) 60e0c4386eSCy Schubert return 0; 61e0c4386eSCy Schubert 62e0c4386eSCy Schubert /* use own random function */ 63e0c4386eSCy Schubert fake_rand_set_public_private_callbacks(NULL, get_faked_bytes); 64e0c4386eSCy Schubert return 1; 65e0c4386eSCy Schubert 66e0c4386eSCy Schubert } 67e0c4386eSCy Schubert 68e0c4386eSCy Schubert static void restore_rand(void) 69e0c4386eSCy Schubert { 70e0c4386eSCy Schubert fake_rand_set_public_private_callbacks(NULL, NULL); 71e0c4386eSCy Schubert OPENSSL_free(fake_rand_bytes); 72e0c4386eSCy Schubert fake_rand_bytes = NULL; 73e0c4386eSCy Schubert fake_rand_bytes_offset = 0; 74e0c4386eSCy Schubert } 75e0c4386eSCy Schubert 76e0c4386eSCy Schubert static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex, 77e0c4386eSCy Schubert const char *b_hex, const char *x_hex, 78e0c4386eSCy Schubert const char *y_hex, const char *order_hex, 79e0c4386eSCy Schubert const char *cof_hex) 80e0c4386eSCy Schubert { 81e0c4386eSCy Schubert BIGNUM *p = NULL; 82e0c4386eSCy Schubert BIGNUM *a = NULL; 83e0c4386eSCy Schubert BIGNUM *b = NULL; 84e0c4386eSCy Schubert BIGNUM *g_x = NULL; 85e0c4386eSCy Schubert BIGNUM *g_y = NULL; 86e0c4386eSCy Schubert BIGNUM *order = NULL; 87e0c4386eSCy Schubert BIGNUM *cof = NULL; 88e0c4386eSCy Schubert EC_POINT *generator = NULL; 89e0c4386eSCy Schubert EC_GROUP *group = NULL; 90e0c4386eSCy Schubert int ok = 0; 91e0c4386eSCy Schubert 92e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&p, p_hex)) 93e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, a_hex)) 94e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, b_hex))) 95e0c4386eSCy Schubert goto done; 96e0c4386eSCy Schubert 97e0c4386eSCy Schubert group = EC_GROUP_new_curve_GFp(p, a, b, NULL); 98e0c4386eSCy Schubert if (!TEST_ptr(group)) 99e0c4386eSCy Schubert goto done; 100e0c4386eSCy Schubert 101e0c4386eSCy Schubert generator = EC_POINT_new(group); 102e0c4386eSCy Schubert if (!TEST_ptr(generator)) 103e0c4386eSCy Schubert goto done; 104e0c4386eSCy Schubert 105e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&g_x, x_hex)) 106e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&g_y, y_hex)) 107e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x, 108e0c4386eSCy Schubert g_y, NULL))) 109e0c4386eSCy Schubert goto done; 110e0c4386eSCy Schubert 111e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&order, order_hex)) 112e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&cof, cof_hex)) 113e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof))) 114e0c4386eSCy Schubert goto done; 115e0c4386eSCy Schubert 116e0c4386eSCy Schubert ok = 1; 117e0c4386eSCy Schubert done: 118e0c4386eSCy Schubert BN_free(p); 119e0c4386eSCy Schubert BN_free(a); 120e0c4386eSCy Schubert BN_free(b); 121e0c4386eSCy Schubert BN_free(g_x); 122e0c4386eSCy Schubert BN_free(g_y); 123e0c4386eSCy Schubert EC_POINT_free(generator); 124e0c4386eSCy Schubert BN_free(order); 125e0c4386eSCy Schubert BN_free(cof); 126e0c4386eSCy Schubert if (!ok) { 127e0c4386eSCy Schubert EC_GROUP_free(group); 128e0c4386eSCy Schubert group = NULL; 129e0c4386eSCy Schubert } 130e0c4386eSCy Schubert 131e0c4386eSCy Schubert return group; 132e0c4386eSCy Schubert } 133e0c4386eSCy Schubert 134e0c4386eSCy Schubert static int test_sm2_crypt(const EC_GROUP *group, 135e0c4386eSCy Schubert const EVP_MD *digest, 136e0c4386eSCy Schubert const char *privkey_hex, 137e0c4386eSCy Schubert const char *message, 138e0c4386eSCy Schubert const char *k_hex, const char *ctext_hex) 139e0c4386eSCy Schubert { 140e0c4386eSCy Schubert const size_t msg_len = strlen(message); 141e0c4386eSCy Schubert BIGNUM *priv = NULL; 142e0c4386eSCy Schubert EC_KEY *key = NULL; 143e0c4386eSCy Schubert EC_POINT *pt = NULL; 144e0c4386eSCy Schubert unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL); 145e0c4386eSCy Schubert size_t ctext_len = 0; 146e0c4386eSCy Schubert size_t ptext_len = 0; 147e0c4386eSCy Schubert uint8_t *ctext = NULL; 148e0c4386eSCy Schubert uint8_t *recovered = NULL; 149e0c4386eSCy Schubert size_t recovered_len = msg_len; 150e0c4386eSCy Schubert int rc = 0; 151e0c4386eSCy Schubert 152e0c4386eSCy Schubert if (!TEST_ptr(expected) 153e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&priv, privkey_hex))) 154e0c4386eSCy Schubert goto done; 155e0c4386eSCy Schubert 156e0c4386eSCy Schubert key = EC_KEY_new(); 157e0c4386eSCy Schubert if (!TEST_ptr(key) 158e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_group(key, group)) 159e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_private_key(key, priv))) 160e0c4386eSCy Schubert goto done; 161e0c4386eSCy Schubert 162e0c4386eSCy Schubert pt = EC_POINT_new(group); 163e0c4386eSCy Schubert if (!TEST_ptr(pt) 164e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL)) 165e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_public_key(key, pt)) 166e0c4386eSCy Schubert || !TEST_true(ossl_sm2_ciphertext_size(key, digest, msg_len, 167e0c4386eSCy Schubert &ctext_len))) 168e0c4386eSCy Schubert goto done; 169e0c4386eSCy Schubert 170e0c4386eSCy Schubert ctext = OPENSSL_zalloc(ctext_len); 171e0c4386eSCy Schubert if (!TEST_ptr(ctext)) 172e0c4386eSCy Schubert goto done; 173e0c4386eSCy Schubert 174e0c4386eSCy Schubert start_fake_rand(k_hex); 175e0c4386eSCy Schubert if (!TEST_true(ossl_sm2_encrypt(key, digest, 176e0c4386eSCy Schubert (const uint8_t *)message, msg_len, 177e0c4386eSCy Schubert ctext, &ctext_len))) { 178e0c4386eSCy Schubert restore_rand(); 179e0c4386eSCy Schubert goto done; 180e0c4386eSCy Schubert } 181e0c4386eSCy Schubert restore_rand(); 182e0c4386eSCy Schubert 183e0c4386eSCy Schubert if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len)) 184e0c4386eSCy Schubert goto done; 185e0c4386eSCy Schubert 186e0c4386eSCy Schubert if (!TEST_true(ossl_sm2_plaintext_size(ctext, ctext_len, &ptext_len)) 187e0c4386eSCy Schubert || !TEST_int_eq(ptext_len, msg_len)) 188e0c4386eSCy Schubert goto done; 189e0c4386eSCy Schubert 190e0c4386eSCy Schubert recovered = OPENSSL_zalloc(ptext_len); 191e0c4386eSCy Schubert if (!TEST_ptr(recovered) 192e0c4386eSCy Schubert || !TEST_true(ossl_sm2_decrypt(key, digest, ctext, ctext_len, 193e0c4386eSCy Schubert recovered, &recovered_len)) 194e0c4386eSCy Schubert || !TEST_int_eq(recovered_len, msg_len) 195e0c4386eSCy Schubert || !TEST_mem_eq(recovered, recovered_len, message, msg_len)) 196e0c4386eSCy Schubert goto done; 197e0c4386eSCy Schubert 198e0c4386eSCy Schubert rc = 1; 199e0c4386eSCy Schubert done: 200e0c4386eSCy Schubert BN_free(priv); 201e0c4386eSCy Schubert EC_POINT_free(pt); 202e0c4386eSCy Schubert OPENSSL_free(ctext); 203e0c4386eSCy Schubert OPENSSL_free(recovered); 204e0c4386eSCy Schubert OPENSSL_free(expected); 205e0c4386eSCy Schubert EC_KEY_free(key); 206e0c4386eSCy Schubert return rc; 207e0c4386eSCy Schubert } 208e0c4386eSCy Schubert 209e0c4386eSCy Schubert static int sm2_crypt_test(void) 210e0c4386eSCy Schubert { 211e0c4386eSCy Schubert int testresult = 0; 212e0c4386eSCy Schubert EC_GROUP *gm_group = NULL; 213e0c4386eSCy Schubert EC_GROUP *test_group = 214e0c4386eSCy Schubert create_EC_group 215e0c4386eSCy Schubert ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 216e0c4386eSCy Schubert "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 217e0c4386eSCy Schubert "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 218e0c4386eSCy Schubert "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 219e0c4386eSCy Schubert "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 220e0c4386eSCy Schubert "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 221e0c4386eSCy Schubert "1"); 222e0c4386eSCy Schubert 223e0c4386eSCy Schubert if (!TEST_ptr(test_group)) 224e0c4386eSCy Schubert goto done; 225e0c4386eSCy Schubert 226e0c4386eSCy Schubert if (!test_sm2_crypt( 227e0c4386eSCy Schubert test_group, 228e0c4386eSCy Schubert EVP_sm3(), 229e0c4386eSCy Schubert "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", 230e0c4386eSCy Schubert "encryption standard", 231e0c4386eSCy Schubert "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F" 232e0c4386eSCy Schubert "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820" 233e0c4386eSCy Schubert "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7", 234e0c4386eSCy Schubert "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1" 235e0c4386eSCy Schubert "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2" 236e0c4386eSCy Schubert "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A" 237e0c4386eSCy Schubert "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467")) 238e0c4386eSCy Schubert goto done; 239e0c4386eSCy Schubert 240e0c4386eSCy Schubert /* Same test as above except using SHA-256 instead of SM3 */ 241e0c4386eSCy Schubert if (!test_sm2_crypt( 242e0c4386eSCy Schubert test_group, 243e0c4386eSCy Schubert EVP_sha256(), 244e0c4386eSCy Schubert "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", 245e0c4386eSCy Schubert "encryption standard", 246e0c4386eSCy Schubert "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F" 247e0c4386eSCy Schubert "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98" 248e0c4386eSCy Schubert "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588", 249e0c4386eSCy Schubert "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F" 250e0c4386eSCy Schubert "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84" 251e0c4386eSCy Schubert "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9" 252e0c4386eSCy Schubert "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33")) 253e0c4386eSCy Schubert goto done; 254e0c4386eSCy Schubert 255e0c4386eSCy Schubert /* From Annex C in both GM/T0003.5-2012 and GB/T 32918.5-2016.*/ 256e0c4386eSCy Schubert gm_group = create_EC_group( 257e0c4386eSCy Schubert "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff", 258e0c4386eSCy Schubert "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc", 259e0c4386eSCy Schubert "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93", 260e0c4386eSCy Schubert "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7", 261e0c4386eSCy Schubert "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0", 262e0c4386eSCy Schubert "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123", 263e0c4386eSCy Schubert "1"); 264e0c4386eSCy Schubert 265e0c4386eSCy Schubert if (!TEST_ptr(gm_group)) 266e0c4386eSCy Schubert goto done; 267e0c4386eSCy Schubert 268e0c4386eSCy Schubert if (!test_sm2_crypt( 269e0c4386eSCy Schubert gm_group, 270e0c4386eSCy Schubert EVP_sm3(), 271e0c4386eSCy Schubert /* privkey (from which the encrypting public key is derived) */ 272e0c4386eSCy Schubert "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8", 273e0c4386eSCy Schubert /* plaintext message */ 274e0c4386eSCy Schubert "encryption standard", 275e0c4386eSCy Schubert /* ephemeral nonce k */ 276e0c4386eSCy Schubert "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21", 277e0c4386eSCy Schubert /* 278e0c4386eSCy Schubert * expected ciphertext, the field values are from GM/T 0003.5-2012 279e0c4386eSCy Schubert * (Annex C), but serialized following the ASN.1 format specified 280e0c4386eSCy Schubert * in GM/T 0009-2012 (Sec. 7.2). 281e0c4386eSCy Schubert */ 282e0c4386eSCy Schubert "307C" /* SEQUENCE, 0x7c bytes */ 283e0c4386eSCy Schubert "0220" /* INTEGER, 0x20 bytes */ 284e0c4386eSCy Schubert "04EBFC718E8D1798620432268E77FEB6415E2EDE0E073C0F4F640ECD2E149A73" 285e0c4386eSCy Schubert "0221" /* INTEGER, 0x21 bytes */ 286e0c4386eSCy Schubert "00" /* leading 00 due to DER for pos. int with topmost bit set */ 287e0c4386eSCy Schubert "E858F9D81E5430A57B36DAAB8F950A3C64E6EE6A63094D99283AFF767E124DF0" 288e0c4386eSCy Schubert "0420" /* OCTET STRING, 0x20 bytes */ 289e0c4386eSCy Schubert "59983C18F809E262923C53AEC295D30383B54E39D609D160AFCB1908D0BD8766" 290e0c4386eSCy Schubert "0413" /* OCTET STRING, 0x13 bytes */ 291e0c4386eSCy Schubert "21886CA989CA9C7D58087307CA93092D651EFA")) 292e0c4386eSCy Schubert goto done; 293e0c4386eSCy Schubert 294e0c4386eSCy Schubert testresult = 1; 295e0c4386eSCy Schubert done: 296e0c4386eSCy Schubert EC_GROUP_free(test_group); 297e0c4386eSCy Schubert EC_GROUP_free(gm_group); 298e0c4386eSCy Schubert 299e0c4386eSCy Schubert return testresult; 300e0c4386eSCy Schubert } 301e0c4386eSCy Schubert 302e0c4386eSCy Schubert static int test_sm2_sign(const EC_GROUP *group, 303e0c4386eSCy Schubert const char *userid, 304e0c4386eSCy Schubert const char *privkey_hex, 305e0c4386eSCy Schubert const char *message, 306e0c4386eSCy Schubert const char *k_hex, 307e0c4386eSCy Schubert const char *r_hex, 308*44096ebdSEnji Cooper const char *s_hex, 309*44096ebdSEnji Cooper int omit_pubkey) 310e0c4386eSCy Schubert { 311e0c4386eSCy Schubert const size_t msg_len = strlen(message); 312e0c4386eSCy Schubert int ok = 0; 313e0c4386eSCy Schubert BIGNUM *priv = NULL; 314e0c4386eSCy Schubert EC_POINT *pt = NULL; 315e0c4386eSCy Schubert EC_KEY *key = NULL; 316e0c4386eSCy Schubert ECDSA_SIG *sig = NULL; 317e0c4386eSCy Schubert const BIGNUM *sig_r = NULL; 318e0c4386eSCy Schubert const BIGNUM *sig_s = NULL; 319e0c4386eSCy Schubert BIGNUM *r = NULL; 320e0c4386eSCy Schubert BIGNUM *s = NULL; 321e0c4386eSCy Schubert 322e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&priv, privkey_hex))) 323e0c4386eSCy Schubert goto done; 324e0c4386eSCy Schubert 325e0c4386eSCy Schubert key = EC_KEY_new(); 326e0c4386eSCy Schubert if (!TEST_ptr(key) 327e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_group(key, group)) 328e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_private_key(key, priv))) 329e0c4386eSCy Schubert goto done; 330e0c4386eSCy Schubert 331*44096ebdSEnji Cooper if (omit_pubkey == 0) { 332e0c4386eSCy Schubert pt = EC_POINT_new(group); 333e0c4386eSCy Schubert if (!TEST_ptr(pt) 334e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL)) 335e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_public_key(key, pt))) 336e0c4386eSCy Schubert goto done; 337*44096ebdSEnji Cooper } 338e0c4386eSCy Schubert 339e0c4386eSCy Schubert start_fake_rand(k_hex); 340e0c4386eSCy Schubert sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid, 341e0c4386eSCy Schubert strlen(userid), (const uint8_t *)message, msg_len); 342e0c4386eSCy Schubert if (!TEST_ptr(sig)) { 343e0c4386eSCy Schubert restore_rand(); 344e0c4386eSCy Schubert goto done; 345e0c4386eSCy Schubert } 346e0c4386eSCy Schubert restore_rand(); 347e0c4386eSCy Schubert 348e0c4386eSCy Schubert ECDSA_SIG_get0(sig, &sig_r, &sig_s); 349e0c4386eSCy Schubert 350e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&r, r_hex)) 351e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&s, s_hex)) 352e0c4386eSCy Schubert || !TEST_BN_eq(r, sig_r) 353e0c4386eSCy Schubert || !TEST_BN_eq(s, sig_s)) 354e0c4386eSCy Schubert goto done; 355e0c4386eSCy Schubert 356e0c4386eSCy Schubert ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid, 357e0c4386eSCy Schubert strlen(userid), (const uint8_t *)message, msg_len); 358e0c4386eSCy Schubert 359e0c4386eSCy Schubert /* We goto done whether this passes or fails */ 360e0c4386eSCy Schubert TEST_true(ok); 361e0c4386eSCy Schubert 362e0c4386eSCy Schubert done: 363e0c4386eSCy Schubert ECDSA_SIG_free(sig); 364e0c4386eSCy Schubert EC_POINT_free(pt); 365e0c4386eSCy Schubert EC_KEY_free(key); 366e0c4386eSCy Schubert BN_free(priv); 367e0c4386eSCy Schubert BN_free(r); 368e0c4386eSCy Schubert BN_free(s); 369e0c4386eSCy Schubert 370e0c4386eSCy Schubert return ok; 371e0c4386eSCy Schubert } 372e0c4386eSCy Schubert 373e0c4386eSCy Schubert static int sm2_sig_test(void) 374e0c4386eSCy Schubert { 375e0c4386eSCy Schubert int testresult = 0; 376e0c4386eSCy Schubert /* From draft-shen-sm2-ecdsa-02 */ 377e0c4386eSCy Schubert EC_GROUP *test_group = 378e0c4386eSCy Schubert create_EC_group 379e0c4386eSCy Schubert ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 380e0c4386eSCy Schubert "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 381e0c4386eSCy Schubert "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 382e0c4386eSCy Schubert "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 383e0c4386eSCy Schubert "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 384e0c4386eSCy Schubert "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 385e0c4386eSCy Schubert "1"); 386e0c4386eSCy Schubert 387e0c4386eSCy Schubert if (!TEST_ptr(test_group)) 388e0c4386eSCy Schubert goto done; 389e0c4386eSCy Schubert 390e0c4386eSCy Schubert if (!TEST_true(test_sm2_sign( 391e0c4386eSCy Schubert test_group, 392e0c4386eSCy Schubert "ALICE123@YAHOO.COM", 393e0c4386eSCy Schubert "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263", 394e0c4386eSCy Schubert "message digest", 395e0c4386eSCy Schubert "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F" 396e0c4386eSCy Schubert "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a", 397e0c4386eSCy Schubert "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1", 398*44096ebdSEnji Cooper "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7", 0))) 399*44096ebdSEnji Cooper goto done; 400*44096ebdSEnji Cooper 401*44096ebdSEnji Cooper /* Make sure we fail if we omit the public portion of the key */ 402*44096ebdSEnji Cooper if (!TEST_false(test_sm2_sign( 403*44096ebdSEnji Cooper test_group, 404*44096ebdSEnji Cooper /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/ 405*44096ebdSEnji Cooper SM2_DEFAULT_USERID, 406*44096ebdSEnji Cooper /* privkey */ 407*44096ebdSEnji Cooper "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8", 408*44096ebdSEnji Cooper /* plaintext message */ 409*44096ebdSEnji Cooper "message digest", 410*44096ebdSEnji Cooper /* ephemeral nonce k */ 411*44096ebdSEnji Cooper "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21", 412*44096ebdSEnji Cooper /* expected signature, */ 413*44096ebdSEnji Cooper /* signature R, 0x20 bytes */ 414*44096ebdSEnji Cooper "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3", 415*44096ebdSEnji Cooper /* signature S, 0x20 bytes */ 416*44096ebdSEnji Cooper "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA", 1))) 417e0c4386eSCy Schubert goto done; 418e0c4386eSCy Schubert 419e0c4386eSCy Schubert testresult = 1; 420e0c4386eSCy Schubert 421e0c4386eSCy Schubert done: 422e0c4386eSCy Schubert EC_GROUP_free(test_group); 423e0c4386eSCy Schubert 424e0c4386eSCy Schubert return testresult; 425e0c4386eSCy Schubert } 426e0c4386eSCy Schubert 427e0c4386eSCy Schubert #endif 428e0c4386eSCy Schubert 429e0c4386eSCy Schubert int setup_tests(void) 430e0c4386eSCy Schubert { 431e0c4386eSCy Schubert #ifdef OPENSSL_NO_SM2 432e0c4386eSCy Schubert TEST_note("SM2 is disabled."); 433e0c4386eSCy Schubert #else 434e0c4386eSCy Schubert fake_rand = fake_rand_start(NULL); 435e0c4386eSCy Schubert if (fake_rand == NULL) 436e0c4386eSCy Schubert return 0; 437e0c4386eSCy Schubert 438e0c4386eSCy Schubert ADD_TEST(sm2_crypt_test); 439e0c4386eSCy Schubert ADD_TEST(sm2_sig_test); 440e0c4386eSCy Schubert #endif 441e0c4386eSCy Schubert return 1; 442e0c4386eSCy Schubert } 443e0c4386eSCy Schubert 444e0c4386eSCy Schubert void cleanup_tests(void) 445e0c4386eSCy Schubert { 446e0c4386eSCy Schubert #ifndef OPENSSL_NO_SM2 447e0c4386eSCy Schubert fake_rand_finish(fake_rand); 448e0c4386eSCy Schubert #endif 449e0c4386eSCy Schubert } 450