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