2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Low level APIs are deprecated for public use, but still ok for internal use.
13 #include "internal/deprecated.h"
19 #include <openssl/bio.h>
20 #include <openssl/evp.h>
21 #include <openssl/bn.h>
22 #include <openssl/crypto.h>
23 #include <openssl/err.h>
24 #include <openssl/rand.h>
27 #ifndef OPENSSL_NO_SM2
29 # include "crypto/sm2.h"
31 static fake_random_generate_cb get_faked_bytes
;
33 static OSSL_PROVIDER
*fake_rand
= NULL
;
34 static uint8_t *fake_rand_bytes
= NULL
;
35 static size_t fake_rand_bytes_offset
= 0;
36 static size_t fake_rand_size
= 0;
38 static int get_faked_bytes(unsigned char *buf
, size_t num
,
39 ossl_unused
const char *name
,
40 ossl_unused EVP_RAND_CTX
*ctx
)
42 if (!TEST_ptr(fake_rand_bytes
) || !TEST_size_t_gt(fake_rand_size
, 0))
46 if (fake_rand_bytes_offset
>= fake_rand_size
)
47 fake_rand_bytes_offset
= 0;
48 *buf
++ = fake_rand_bytes
[fake_rand_bytes_offset
++];
54 static int start_fake_rand(const char *hex_bytes
)
56 OPENSSL_free(fake_rand_bytes
);
57 fake_rand_bytes_offset
= 0;
58 fake_rand_size
= strlen(hex_bytes
) / 2;
59 if (!TEST_ptr(fake_rand_bytes
= OPENSSL_hexstr2buf(hex_bytes
, NULL
)))
62 /* use own random function */
63 fake_rand_set_public_private_callbacks(NULL
, get_faked_bytes
);
68 static void restore_rand(void)
70 fake_rand_set_public_private_callbacks(NULL
, NULL
);
71 OPENSSL_free(fake_rand_bytes
);
72 fake_rand_bytes
= NULL
;
73 fake_rand_bytes_offset
= 0;
76 static EC_GROUP
*create_EC_group(const char *p_hex
, const char *a_hex
,
77 const char *b_hex
, const char *x_hex
,
78 const char *y_hex
, const char *order_hex
,
88 EC_POINT
*generator
= NULL
;
89 EC_GROUP
*group
= NULL
;
92 if (!TEST_true(BN_hex2bn(&p
, p_hex
))
93 || !TEST_true(BN_hex2bn(&a
, a_hex
))
94 || !TEST_true(BN_hex2bn(&b
, b_hex
)))
97 group
= EC_GROUP_new_curve_GFp(p
, a
, b
, NULL
);
101 generator
= EC_POINT_new(group
);
102 if (!TEST_ptr(generator
))
105 if (!TEST_true(BN_hex2bn(&g_x
, x_hex
))
106 || !TEST_true(BN_hex2bn(&g_y
, y_hex
))
107 || !TEST_true(EC_POINT_set_affine_coordinates(group
, generator
, g_x
,
111 if (!TEST_true(BN_hex2bn(&order
, order_hex
))
112 || !TEST_true(BN_hex2bn(&cof
, cof_hex
))
113 || !TEST_true(EC_GROUP_set_generator(group
, generator
, order
, cof
)))
123 EC_POINT_free(generator
);
127 EC_GROUP_free(group
);
134 static int test_sm2_crypt(const EC_GROUP
*group
,
135 const EVP_MD
*digest
,
136 const char *privkey_hex
,
138 const char *k_hex
, const char *ctext_hex
)
140 const size_t msg_len
= strlen(message
);
144 unsigned char *expected
= OPENSSL_hexstr2buf(ctext_hex
, NULL
);
145 size_t ctext_len
= 0;
146 size_t ptext_len
= 0;
147 uint8_t *ctext
= NULL
;
148 uint8_t *recovered
= NULL
;
149 size_t recovered_len
= msg_len
;
152 if (!TEST_ptr(expected
)
153 || !TEST_true(BN_hex2bn(&priv
, privkey_hex
)))
158 || !TEST_true(EC_KEY_set_group(key
, group
))
159 || !TEST_true(EC_KEY_set_private_key(key
, priv
)))
162 pt
= EC_POINT_new(group
);
164 || !TEST_true(EC_POINT_mul(group
, pt
, priv
, NULL
, NULL
, NULL
))
165 || !TEST_true(EC_KEY_set_public_key(key
, pt
))
166 || !TEST_true(ossl_sm2_ciphertext_size(key
, digest
, msg_len
,
170 ctext
= OPENSSL_zalloc(ctext_len
);
171 if (!TEST_ptr(ctext
))
174 start_fake_rand(k_hex
);
175 if (!TEST_true(ossl_sm2_encrypt(key
, digest
,
176 (const uint8_t *)message
, msg_len
,
177 ctext
, &ctext_len
))) {
183 if (!TEST_mem_eq(ctext
, ctext_len
, expected
, ctext_len
))
186 if (!TEST_true(ossl_sm2_plaintext_size(ctext
, ctext_len
, &ptext_len
))
187 || !TEST_int_eq(ptext_len
, msg_len
))
190 recovered
= OPENSSL_zalloc(ptext_len
);
191 if (!TEST_ptr(recovered
)
192 || !TEST_true(ossl_sm2_decrypt(key
, digest
, ctext
, ctext_len
,
193 recovered
, &recovered_len
))
194 || !TEST_int_eq(recovered_len
, msg_len
)
195 || !TEST_mem_eq(recovered
, recovered_len
, message
, msg_len
))
203 OPENSSL_free(recovered
);
204 OPENSSL_free(expected
);
209 static int sm2_crypt_test(void)
212 EC_GROUP
*test_group
=
214 ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
215 "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
216 "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
217 "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
218 "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
219 "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
222 if (!TEST_ptr(test_group
))
228 "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
229 "encryption standard",
230 "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
231 "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820"
232 "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7",
233 "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
234 "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
235 "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
236 "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467"))
239 /* Same test as above except using SHA-256 instead of SM3 */
243 "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
244 "encryption standard",
245 "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
246 "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98"
247 "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588",
248 "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F"
249 "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84"
250 "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9"
251 "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33"))
256 EC_GROUP_free(test_group
);
261 static int test_sm2_sign(const EC_GROUP
*group
,
263 const char *privkey_hex
,
269 const size_t msg_len
= strlen(message
);
274 ECDSA_SIG
*sig
= NULL
;
275 const BIGNUM
*sig_r
= NULL
;
276 const BIGNUM
*sig_s
= NULL
;
280 if (!TEST_true(BN_hex2bn(&priv
, privkey_hex
)))
285 || !TEST_true(EC_KEY_set_group(key
, group
))
286 || !TEST_true(EC_KEY_set_private_key(key
, priv
)))
289 pt
= EC_POINT_new(group
);
291 || !TEST_true(EC_POINT_mul(group
, pt
, priv
, NULL
, NULL
, NULL
))
292 || !TEST_true(EC_KEY_set_public_key(key
, pt
)))
295 start_fake_rand(k_hex
);
296 sig
= ossl_sm2_do_sign(key
, EVP_sm3(), (const uint8_t *)userid
,
297 strlen(userid
), (const uint8_t *)message
, msg_len
);
298 if (!TEST_ptr(sig
)) {
304 ECDSA_SIG_get0(sig
, &sig_r
, &sig_s
);
306 if (!TEST_true(BN_hex2bn(&r
, r_hex
))
307 || !TEST_true(BN_hex2bn(&s
, s_hex
))
308 || !TEST_BN_eq(r
, sig_r
)
309 || !TEST_BN_eq(s
, sig_s
))
312 ok
= ossl_sm2_do_verify(key
, EVP_sm3(), sig
, (const uint8_t *)userid
,
313 strlen(userid
), (const uint8_t *)message
, msg_len
);
315 /* We goto done whether this passes or fails */
329 static int sm2_sig_test(void)
332 /* From draft-shen-sm2-ecdsa-02 */
333 EC_GROUP
*test_group
=
335 ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
336 "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
337 "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
338 "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
339 "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
340 "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
343 if (!TEST_ptr(test_group
))
346 if (!TEST_true(test_sm2_sign(
348 "ALICE123@YAHOO.COM",
349 "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
351 "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
352 "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a",
353 "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
354 "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7")))
360 EC_GROUP_free(test_group
);
367 int setup_tests(void)
369 #ifdef OPENSSL_NO_SM2
370 TEST_note("SM2 is disabled.");
372 fake_rand
= fake_rand_start(NULL
);
373 if (fake_rand
== NULL
)
376 ADD_TEST(sm2_crypt_test
);
377 ADD_TEST(sm2_sig_test
);
382 void cleanup_tests(void)
384 #ifndef OPENSSL_NO_SM2
385 fake_rand_finish(fake_rand
);