Fix dsaparam -genkey with DER outform
[openssl.git] / test / sm2crypttest.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 <openssl/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
89     BN_hex2bn(&p, p_hex);
90     BN_hex2bn(&a, a_hex);
91     BN_hex2bn(&b, b_hex);
92
93     group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
94     BN_free(p);
95     BN_free(a);
96     BN_free(b);
97
98     if (group == NULL)
99         return NULL;
100
101     generator = EC_POINT_new(group);
102     if (generator == NULL)
103         return NULL;
104
105     BN_hex2bn(&g_x, x_hex);
106     BN_hex2bn(&g_y, y_hex);
107
108     if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
109         0)
110         return NULL;
111
112     BN_free(g_x);
113     BN_free(g_y);
114
115     BN_hex2bn(&order, order_hex);
116     BN_hex2bn(&cof, cof_hex);
117
118     if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
119         return NULL;
120
121     EC_POINT_free(generator);
122     BN_free(order);
123     BN_free(cof);
124
125     return group;
126 }
127
128 static int test_sm2(const EC_GROUP *group,
129                     const EVP_MD *digest,
130                     const char *privkey_hex,
131                     const char *message,
132                     const char *k_hex, const char *ctext_hex)
133 {
134     const size_t msg_len = strlen(message);
135
136     BIGNUM *priv = NULL;
137     EC_KEY *key = NULL;
138     EC_POINT *pt = NULL;
139     unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
140
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
147     int rc = 0;
148
149     BN_hex2bn(&priv, privkey_hex);
150
151     key = EC_KEY_new();
152     EC_KEY_set_group(key, group);
153     EC_KEY_set_private_key(key, priv);
154
155     pt = EC_POINT_new(group);
156     EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
157
158     EC_KEY_set_public_key(key, pt);
159     BN_free(priv);
160     EC_POINT_free(pt);
161
162     ctext_len = SM2_ciphertext_size(key, digest, msg_len);
163     ctext = OPENSSL_zalloc(ctext_len);
164     if (ctext == NULL)
165         goto done;
166
167     start_fake_rand(k_hex);
168     rc = SM2_encrypt(key, digest,
169                      (const uint8_t *)message, msg_len, ctext, &ctext_len);
170     restore_rand();
171
172     TEST_mem_eq(ctext, ctext_len, expected, ctext_len);
173     if (rc == 0)
174         goto done;
175
176     ptext_len = SM2_plaintext_size(key, digest, ctext_len);
177
178     TEST_int_eq(ptext_len, msg_len);
179
180     recovered = OPENSSL_zalloc(ptext_len);
181     if (recovered == NULL)
182         goto done;
183     rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);
184
185     TEST_int_eq(recovered_len, msg_len);
186     TEST_mem_eq(recovered, recovered_len, message, msg_len);
187     if (rc == 0)
188         return 0;
189
190     rc = 1;
191  done:
192
193     OPENSSL_free(ctext);
194     OPENSSL_free(recovered);
195     OPENSSL_free(expected);
196     EC_KEY_free(key);
197     return rc;
198 }
199
200 static int sm2_crypt_test(void)
201 {
202     int rc;
203     EC_GROUP *test_group =
204         create_EC_group
205         ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
206          "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
207          "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
208          "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
209          "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
210          "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
211          "1");
212
213     if (test_group == NULL)
214         return 0;
215
216     rc = test_sm2(test_group,
217                   EVP_sm3(),
218                   "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
219                   "encryption standard",
220                   "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
221                   "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
222                   "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
223                   "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
224                   "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467");
225
226     if (rc == 0)
227         return 0;
228
229     /* Same test as above except using SHA-256 instead of SM3 */
230     rc = test_sm2(test_group,
231                   EVP_sha256(),
232                   "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
233                   "encryption standard",
234                   "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
235                   "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E988E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33");
236     if (rc == 0)
237         return 0;
238
239     EC_GROUP_free(test_group);
240
241     return 1;
242 }
243
244 #endif
245
246 int setup_tests(void)
247 {
248 #ifdef OPENSSL_NO_SM2
249     TEST_note("SM2 is disabled.");
250 #else
251     ADD_TEST(sm2_crypt_test);
252 #endif
253     return 1;
254 }