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