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