Fix external symbols related to ec & sm2 keys
[openssl.git] / test / sm2_internal_test.c
1 /*
2  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * Low level APIs are deprecated for public use, but still ok for internal use.
12  */
13 #include "internal/deprecated.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
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>
25 #include "testutil.h"
26
27 #ifndef OPENSSL_NO_SM2
28
29 # include "crypto/sm2.h"
30
31 static OSSL_PROVIDER *fake_rand = NULL;
32 static uint8_t *fake_rand_bytes = NULL;
33 static size_t fake_rand_bytes_offset = 0;
34 static size_t fake_rand_size = 0;
35
36 static int get_faked_bytes(unsigned char *buf, size_t num)
37 {
38     if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0))
39         return 0;
40
41     while (num-- > 0) {
42         if (fake_rand_bytes_offset >= fake_rand_size)
43             fake_rand_bytes_offset = 0;
44         *buf++ = fake_rand_bytes[fake_rand_bytes_offset++];
45     }
46
47     return 1;
48 }
49
50 static int start_fake_rand(const char *hex_bytes)
51 {
52     OPENSSL_free(fake_rand_bytes);
53     fake_rand_bytes_offset = 0;
54     fake_rand_size = strlen(hex_bytes) / 2;
55     if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL)))
56         return 0;
57
58     /* use own random function */
59     fake_rand_set_callback(get_faked_bytes);
60     return 1;
61
62 }
63
64 static void restore_rand(void)
65 {
66     fake_rand_set_callback(NULL);
67     OPENSSL_free(fake_rand_bytes);
68     fake_rand_bytes = NULL;
69     fake_rand_bytes_offset = 0;
70 }
71
72 static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
73                                  const char *b_hex, const char *x_hex,
74                                  const char *y_hex, const char *order_hex,
75                                  const char *cof_hex)
76 {
77     BIGNUM *p = NULL;
78     BIGNUM *a = NULL;
79     BIGNUM *b = NULL;
80     BIGNUM *g_x = NULL;
81     BIGNUM *g_y = NULL;
82     BIGNUM *order = NULL;
83     BIGNUM *cof = NULL;
84     EC_POINT *generator = NULL;
85     EC_GROUP *group = NULL;
86     int ok = 0;
87
88     if (!TEST_true(BN_hex2bn(&p, p_hex))
89             || !TEST_true(BN_hex2bn(&a, a_hex))
90             || !TEST_true(BN_hex2bn(&b, b_hex)))
91         goto done;
92
93     group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
94     if (!TEST_ptr(group))
95         goto done;
96
97     generator = EC_POINT_new(group);
98     if (!TEST_ptr(generator))
99         goto done;
100
101     if (!TEST_true(BN_hex2bn(&g_x, x_hex))
102             || !TEST_true(BN_hex2bn(&g_y, y_hex))
103             || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x,
104                                                           g_y, NULL)))
105         goto done;
106
107     if (!TEST_true(BN_hex2bn(&order, order_hex))
108             || !TEST_true(BN_hex2bn(&cof, cof_hex))
109             || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof)))
110         goto done;
111
112     ok = 1;
113 done:
114     BN_free(p);
115     BN_free(a);
116     BN_free(b);
117     BN_free(g_x);
118     BN_free(g_y);
119     EC_POINT_free(generator);
120     BN_free(order);
121     BN_free(cof);
122     if (!ok) {
123         EC_GROUP_free(group);
124         group = NULL;
125     }
126
127     return group;
128 }
129
130 static int test_sm2_crypt(const EC_GROUP *group,
131                           const EVP_MD *digest,
132                           const char *privkey_hex,
133                           const char *message,
134                           const char *k_hex, const char *ctext_hex)
135 {
136     const size_t msg_len = strlen(message);
137     BIGNUM *priv = NULL;
138     EC_KEY *key = NULL;
139     EC_POINT *pt = NULL;
140     unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
141     size_t ctext_len = 0;
142     size_t ptext_len = 0;
143     uint8_t *ctext = NULL;
144     uint8_t *recovered = NULL;
145     size_t recovered_len = msg_len;
146     int rc = 0;
147
148     if (!TEST_ptr(expected)
149             || !TEST_true(BN_hex2bn(&priv, privkey_hex)))
150         goto done;
151
152     key = EC_KEY_new();
153     if (!TEST_ptr(key)
154             || !TEST_true(EC_KEY_set_group(key, group))
155             || !TEST_true(EC_KEY_set_private_key(key, priv)))
156         goto done;
157
158     pt = EC_POINT_new(group);
159     if (!TEST_ptr(pt)
160             || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
161             || !TEST_true(EC_KEY_set_public_key(key, pt))
162             || !TEST_true(ossl_sm2_ciphertext_size(key, digest, msg_len,
163                                                    &ctext_len)))
164         goto done;
165
166     ctext = OPENSSL_zalloc(ctext_len);
167     if (!TEST_ptr(ctext))
168         goto done;
169
170     start_fake_rand(k_hex);
171     if (!TEST_true(ossl_sm2_encrypt(key, digest,
172                                     (const uint8_t *)message, msg_len,
173                                     ctext, &ctext_len))) {
174         restore_rand();
175         goto done;
176     }
177     restore_rand();
178
179     if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len))
180         goto done;
181
182     if (!TEST_true(ossl_sm2_plaintext_size(key, digest, ctext_len, &ptext_len))
183             || !TEST_int_eq(ptext_len, msg_len))
184         goto done;
185
186     recovered = OPENSSL_zalloc(ptext_len);
187     if (!TEST_ptr(recovered)
188             || !TEST_true(ossl_sm2_decrypt(key, digest, ctext, ctext_len,
189                                            recovered, &recovered_len))
190             || !TEST_int_eq(recovered_len, msg_len)
191             || !TEST_mem_eq(recovered, recovered_len, message, msg_len))
192         goto done;
193
194     rc = 1;
195  done:
196     BN_free(priv);
197     EC_POINT_free(pt);
198     OPENSSL_free(ctext);
199     OPENSSL_free(recovered);
200     OPENSSL_free(expected);
201     EC_KEY_free(key);
202     return rc;
203 }
204
205 static int sm2_crypt_test(void)
206 {
207     int testresult = 0;
208     EC_GROUP *test_group =
209         create_EC_group
210         ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
211          "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
212          "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
213          "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
214          "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
215          "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
216          "1");
217
218     if (!TEST_ptr(test_group))
219         goto done;
220
221     if (!test_sm2_crypt(
222             test_group,
223             EVP_sm3(),
224             "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
225             "encryption standard",
226             "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
227             "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820"
228             "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7",
229             "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
230             "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
231             "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
232             "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467"))
233         goto done;
234
235     /* Same test as above except using SHA-256 instead of SM3 */
236     if (!test_sm2_crypt(
237             test_group,
238             EVP_sha256(),
239             "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
240             "encryption standard",
241             "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
242             "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98"
243             "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588",
244             "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F"
245             "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84"
246             "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9"
247             "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33"))
248         goto done;
249
250     testresult = 1;
251  done:
252     EC_GROUP_free(test_group);
253
254     return testresult;
255 }
256
257 static int test_sm2_sign(const EC_GROUP *group,
258                          const char *userid,
259                          const char *privkey_hex,
260                          const char *message,
261                          const char *k_hex,
262                          const char *r_hex,
263                          const char *s_hex)
264 {
265     const size_t msg_len = strlen(message);
266     int ok = 0;
267     BIGNUM *priv = NULL;
268     EC_POINT *pt = NULL;
269     EC_KEY *key = NULL;
270     ECDSA_SIG *sig = NULL;
271     const BIGNUM *sig_r = NULL;
272     const BIGNUM *sig_s = NULL;
273     BIGNUM *r = NULL;
274     BIGNUM *s = NULL;
275
276     if (!TEST_true(BN_hex2bn(&priv, privkey_hex)))
277         goto done;
278
279     key = EC_KEY_new();
280     if (!TEST_ptr(key)
281             || !TEST_true(EC_KEY_set_group(key, group))
282             || !TEST_true(EC_KEY_set_private_key(key, priv)))
283         goto done;
284
285     pt = EC_POINT_new(group);
286     if (!TEST_ptr(pt)
287             || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
288             || !TEST_true(EC_KEY_set_public_key(key, pt)))
289         goto done;
290
291     start_fake_rand(k_hex);
292     sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid,
293                            strlen(userid), (const uint8_t *)message, msg_len);
294     if (!TEST_ptr(sig)) {
295         restore_rand();
296         goto done;
297     }
298     restore_rand();
299
300     ECDSA_SIG_get0(sig, &sig_r, &sig_s);
301
302     if (!TEST_true(BN_hex2bn(&r, r_hex))
303             || !TEST_true(BN_hex2bn(&s, s_hex))
304             || !TEST_BN_eq(r, sig_r)
305             || !TEST_BN_eq(s, sig_s))
306         goto done;
307
308     ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid,
309                             strlen(userid), (const uint8_t *)message, msg_len);
310
311     /* We goto done whether this passes or fails */
312     TEST_true(ok);
313
314  done:
315     ECDSA_SIG_free(sig);
316     EC_POINT_free(pt);
317     EC_KEY_free(key);
318     BN_free(priv);
319     BN_free(r);
320     BN_free(s);
321
322     return ok;
323 }
324
325 static int sm2_sig_test(void)
326 {
327     int testresult = 0;
328     /* From draft-shen-sm2-ecdsa-02 */
329     EC_GROUP *test_group =
330         create_EC_group
331         ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
332          "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
333          "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
334          "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
335          "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
336          "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
337          "1");
338
339     if (!TEST_ptr(test_group))
340         goto done;
341
342     if (!TEST_true(test_sm2_sign(
343                         test_group,
344                         "ALICE123@YAHOO.COM",
345                         "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
346                         "message digest",
347                         "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
348                         "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a",
349                         "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
350                         "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7")))
351         goto done;
352
353     testresult = 1;
354
355  done:
356     EC_GROUP_free(test_group);
357
358     return testresult;
359 }
360
361 #endif
362
363 int setup_tests(void)
364 {
365 #ifdef OPENSSL_NO_SM2
366     TEST_note("SM2 is disabled.");
367 #else
368     fake_rand = fake_rand_start(NULL);
369     if (fake_rand == NULL)
370         return 0;
371
372     ADD_TEST(sm2_crypt_test);
373     ADD_TEST(sm2_sig_test);
374 #endif
375     return 1;
376 }
377
378 void cleanup_tests(void)
379 {
380 #ifdef OPENSSL_NO_SM2
381     fake_rand_finish(fake_rand);
382 #endif
383 }