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