2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
15 #include <openssl/bio.h>
16 #include <openssl/evp.h>
17 #include <openssl/bn.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
23 #ifndef OPENSSL_NO_SM2
25 # include <openssl/sm2.h>
27 static RAND_METHOD fake_rand;
28 static const RAND_METHOD *saved_rand;
30 static uint8_t *fake_rand_bytes = NULL;
31 static size_t fake_rand_bytes_offset = 0;
33 static int get_faked_bytes(unsigned char *buf, int num)
37 if (fake_rand_bytes == NULL)
38 return saved_rand->bytes(buf, num);
40 for (i = 0; i != num; ++i)
41 buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
42 fake_rand_bytes_offset += num;
46 static int start_fake_rand(const char *hex_bytes)
48 /* save old rand method */
49 if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
52 fake_rand = *saved_rand;
53 /* use own random function */
54 fake_rand.bytes = get_faked_bytes;
56 fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
57 fake_rand_bytes_offset = 0;
59 /* set new RAND_METHOD */
60 if (!TEST_true(RAND_set_rand_method(&fake_rand)))
65 static int restore_rand(void)
67 OPENSSL_free(fake_rand_bytes);
68 fake_rand_bytes = NULL;
69 fake_rand_bytes_offset = 0;
70 if (!TEST_true(RAND_set_rand_method(saved_rand)))
75 static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
76 const char *b_hex, const char *x_hex,
77 const char *y_hex, const char *order_hex,
87 EC_POINT *generator = NULL;
88 EC_GROUP *group = NULL;
94 group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
102 generator = EC_POINT_new(group);
103 if (generator == NULL)
106 BN_hex2bn(&g_x, x_hex);
107 BN_hex2bn(&g_y, y_hex);
109 if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
116 BN_hex2bn(&order, order_hex);
117 BN_hex2bn(&cof, cof_hex);
119 if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
122 EC_POINT_free(generator);
130 static int test_sm2(const EC_GROUP *group,
132 const char *privkey_hex,
134 const char *k_hex, const char *r_hex, const char *s_hex)
136 const size_t msg_len = strlen(message);
141 ECDSA_SIG *sig = NULL;
142 const BIGNUM *sig_r = NULL;
143 const BIGNUM *sig_s = NULL;
147 BN_hex2bn(&priv, privkey_hex);
150 EC_KEY_set_group(key, group);
151 EC_KEY_set_private_key(key, priv);
153 pt = EC_POINT_new(group);
154 EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
155 EC_KEY_set_public_key(key, pt);
157 start_fake_rand(k_hex);
158 sig = SM2_do_sign(key, EVP_sm3(), userid, (const uint8_t *)message, msg_len);
164 ECDSA_SIG_get0(sig, &sig_r, &sig_s);
166 BN_hex2bn(&r, r_hex);
167 BN_hex2bn(&s, s_hex);
169 if (BN_cmp(r, sig_r) != 0) {
170 printf("Signature R mismatch: ");
171 BN_print_fp(stdout, r);
173 BN_print_fp(stdout, sig_r);
177 if (BN_cmp(s, sig_s) != 0) {
178 printf("Signature S mismatch: ");
179 BN_print_fp(stdout, s);
181 BN_print_fp(stdout, sig_s);
186 ok = SM2_do_verify(key, EVP_sm3(), sig, userid, (const uint8_t *)message, msg_len);
198 static int sm2_sig_test(void)
201 /* From draft-shen-sm2-ecdsa-02 */
202 EC_GROUP *test_group =
204 ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
205 "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
206 "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
207 "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
208 "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
209 "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
212 if (test_group == NULL)
215 rc = test_sm2(test_group,
216 "ALICE123@YAHOO.COM",
217 "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
219 "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
220 "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
221 "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
223 EC_GROUP_free(test_group);
230 int setup_tests(void)
232 #ifdef OPENSSL_NO_SM2
233 TEST_note("SM2 is disabled.");
235 ADD_TEST(sm2_sig_test);