• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <gmssl/asn1.h>
15 #include <gmssl/error.h>
16 #include <gmssl/sm2.h>
17 #include <gmssl/pkcs8.h>
18 
19 #define sm2_print_bn(label,a) sm2_bn_print(stderr,0,0,label,a) // 这个不应该放在这里,应该放在测试文件中
20 
21 
22 #define hex_fp_add_x_y "eefbe4cf140ff8b5b956d329d5a2eae8608c933cb89053217439786e54866567"
23 #define hex_fp_sub_x_y "768d77882a23097d05db3562fed0a840bf3984422c3bc4a26e7b12a412128426"
24 #define hex_fp_sub_y_x "89728876d5dcf682fa24ca9d012f57bf40c67bbcd3c43b5e9184ed5beded7bd9"
25 #define hex_fp_neg_x   "cd3b51d2e0e67ee6a066fbb995c6366b701cf43f0d99f41f8ea5ba76ccb38b38"
26 #define hex_fp_mul_x_y "edd7e745bdc4630ccfa1da1057033a525346dbf202f082f3c431349991ace76a"
27 #define hex_fp_squ_x   "f4e2cca0bcfd67fba8531eebff519e4cb3d47f9fe8c5eff5151f4c497ec99fbf"
28 #define hex_fp_exp_x_y "8cafd11b1a0d2072b82911ba87e0d376103a1be5986fce91d8d297b758f68146"
29 #define hex_fp_inv_x   "053b878fb82e213c17e554b9a574b7bd31775222704b7fd9c7d6f8441026cd80"
30 
31 #define hex_fn_add_x_y "eefbe4cf140ff8b5b956d329d5a2eae8608c933cb89053217439786e54866567"
32 #define hex_fn_sub_x_y "768d77882a23097d05db3562fed0a840313d63ae4e01c9ccc23706ad4be7c54a"
33 #define hex_fn_sub_y_x "89728876d5dcf682fa24ca9d012f57bf40c67bbcd3c43b5e9184ed5beded7bd9"
34 #define hex_fn_neg_x   "cd3b51d2e0e67ee6a066fbb995c6366ae220d3ab2f5ff949e261ae800688cc5c"
35 #define hex_fn_mul_x_y "cf7296d5cbf0b64bb5e9a11b294962e9c779b41c038e9c8d815234a0df9d6623"
36 #define hex_fn_sqr_x   "82d3d1b296d3a3803888b7ffc78f23eca824e7ec8d7ddaf231ffb0d256a19da2"
37 #define hex_fn_exp_x_y "0cf4df7e76d7d49ff23b94853a98aba1e36e9ca0358acbf23a3bbda406f46df3"
38 #define hex_fn_inv_x   "96340ec8b80f44e9b345a706bdb5c9e3ab8a6474a5cb4e0d4645dbaecf1cf03d"
39 #define hex_v          "d3da0ef661be97360e1b32f834e6ca5673b1984b22bb420133da05e56ccd59fb"
40 #define hex_fn_mul_x_v "0375c61e1ed13e460f4b5d462dc5b2c846f36c7b481cd4bed8f7dd55908a6afd"
41 
42 #define hex_t		"2fbadf57b52dc19e8470bf201cb182e0a4f7fa5e28d356b15da173132b94b325"
43 
44 
test_sm2_bn(void)45 int test_sm2_bn(void)
46 {
47 	SM2_BN r;
48 	SM2_BN x;
49 	SM2_BN y;
50 	int ok, i = 1;
51 
52 	char hex[65];
53 
54 	SM2_BN v = {
55 		0x6ccd59fb, 0x33da05e5, 0x22bb4201, 0x73b1984b,
56 		0x34e6ca56, 0x0e1b32f8, 0x61be9736, 0xd3da0ef6,
57 	};
58 
59 	SM2_BN t;
60 
61 	sm2_bn_copy(x, SM2_G->X);
62 	sm2_bn_copy(y, SM2_G->Y);
63 
64 	sm2_bn_from_hex(r, hex_v);
65 	ok = (sm2_bn_cmp(r, v) == 0);
66 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
67 	if (!ok) return -1;
68 
69 	// fp tests
70 	sm2_fp_add(r, x, y);
71 	ok = sm2_bn_equ_hex(r, hex_fp_add_x_y);
72 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
73 	if (!ok) return -1;
74 
75 	sm2_fp_sub(r, x, y);
76 	ok = sm2_bn_equ_hex(r, hex_fp_sub_x_y);
77 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
78 	if (!ok) return -1;
79 
80 	sm2_fp_mul(r, x, y);
81 	ok = sm2_bn_equ_hex(r, hex_fp_mul_x_y);
82 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
83 	if (!ok) return -1;
84 
85 	sm2_fp_exp(r, x, y);
86 	ok = sm2_bn_equ_hex(r, hex_fp_exp_x_y);
87 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
88 	if (!ok) return -1;
89 
90 	sm2_fp_inv(r, x);
91 	ok = sm2_bn_equ_hex(r, hex_fp_inv_x);
92 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
93 	if (!ok) return -1;
94 
95 	sm2_fp_neg(r, x);
96 	ok = sm2_bn_equ_hex(r, hex_fp_neg_x);
97 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
98 	if (!ok) return -1;
99 
100 	// fn tests
101 	sm2_fn_add(r, x, y);
102 	ok = sm2_bn_equ_hex(r, hex_fn_add_x_y);
103 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
104 	if (!ok) return -1;
105 
106 	sm2_fn_sub(r, x, y);
107 	ok = sm2_bn_equ_hex(r, hex_fn_sub_x_y);
108 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
109 	if (!ok) return -1;
110 
111 	sm2_fn_sub(r, y, x);
112 	ok = sm2_bn_equ_hex(r, hex_fn_sub_y_x);
113 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
114 	if (!ok) return -1;
115 
116 	sm2_fn_neg(r, x);
117 	ok = sm2_bn_equ_hex(r, hex_fn_neg_x);
118 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
119 	if (!ok) return -1;
120 
121 	sm2_fn_mul(r, x, y);
122 	ok = sm2_bn_equ_hex(r, hex_fn_mul_x_y);
123 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
124 	if (!ok) return -1;
125 
126 	sm2_fn_mul(r, x, v);
127 	ok = sm2_bn_equ_hex(r, hex_fn_mul_x_v);
128 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
129 	if (!ok) return -1;
130 
131 	sm2_fn_sqr(r, x);
132 	ok = sm2_bn_equ_hex(r, hex_fn_sqr_x);
133 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
134 	if (!ok) return -1;
135 
136 	sm2_fn_exp(r, x, y);
137 	ok = sm2_bn_equ_hex(r, hex_fn_exp_x_y);
138 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
139 	if (!ok) return -1;
140 
141 	sm2_fn_inv(r, x);
142 	ok = sm2_bn_equ_hex(r, hex_fn_inv_x);
143 	printf("sm2 bn test %d %s\n", i++, ok ? "ok" : "failed");
144 	if (!ok) return -1;
145 
146 	SM2_BN tv = {
147 		0x2b94b325, 0x5da17313, 0x28d356b1, 0xa4f7fa5e,
148 		0x1cb182e0, 0x8470bf20, 0xb52dc19e, 0x2fbadf57,
149 	};
150 	sm2_bn_from_hex(t, hex_t);
151 	ok = (sm2_bn_cmp(t, tv) == 0);
152 	if (!ok) return -1;
153 
154 	sm2_bn_to_hex(t, hex);
155 
156 	return 1;
157 }
158 
159 
160 #define hex_G \
161 	"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7" \
162 	"bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0"
163 #define hex_2G \
164 	"56cefd60d7c87c000d58ef57fa73ba4d9c0dfa08c08a7331495c2e1da3f2bd52" \
165 	"31b7e7e6cc8189f668535ce0f8eaf1bd6de84c182f6c8e716f780d3a970a23c3"
166 #define hex_3G \
167 	"a97f7cd4b3c993b4be2daa8cdb41e24ca13f6bd945302244e26918f1d0509ebf" \
168 	"530b5dd88c688ef5ccc5cec08a72150f7c400ee5cd045292aaacdd037458f6e6"
169 #define hex_negG \
170 	"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7" \
171 	"43c8c95c0b098863a642311c9496deac2f56788239d5b8c0fd20cd1adec60f5f"
172 #define hex_10G \
173 	"d3f94862519621c121666061f65c3e32b2d0d065cd219e3284a04814db522756" \
174 	"4b9030cf676f6a742ebd57d146dca428f6b743f64d1482d147d46fb2bab82a14"
175 #define hex_bG \
176 	"528470bc74a6ebc663c06fc4cfa1b630d1e9d4a80c0a127b47f73c324c46c0ba" \
177 	"832cf9c5a15b997e60962b4cf6e2c9cee488faaec98d20599d323d4cabfc1bf4"
178 
179 #define hex_P \
180 	"504cfe2fae749d645e99fbb5b25995cc6fed70196007b039bdc44706bdabc0d9" \
181 	"b80a8018eda5f55ddc4b870d7784b7b84e53af02f575ab53ed8a99a3bbe2abc2"
182 #define hex_2P \
183 	"a53d20e89312b5243f66aec12ef6471f5911941d86302d5d8337cb70937d65ae" \
184 	"96953c46815e4259363256ddd6c77fcc33787aeafc6a57beec5833f476dd69e0"
185 
186 #define hex_tP \
187 	"02deff2c5b3656ca3f7c7ca9d710ca1d69860c75a9c7ec284b96b8adc50b2936" \
188 	"b74bcba937e9267fce4ccc069a6681f5b04dcedd9e2794c6a25ddc7856df7145"
189 
190 
test_sm2_jacobian_point(void)191 int test_sm2_jacobian_point(void)
192 {
193 	SM2_JACOBIAN_POINT _P, *P = &_P;
194 	SM2_JACOBIAN_POINT _G, *G = &_G;
195 	SM2_BN k;
196 	int i = 1, ok;
197 
198 	uint8_t buf[64];
199 
200 	printf("sm2_jacobian_point_test\n");
201 
202 	sm2_jacobian_point_copy(G, SM2_G);
203 	ok = sm2_jacobian_point_equ_hex(G, hex_G);
204 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
205 	if (!ok) return -1;
206 
207 	ok = sm2_jacobian_point_is_on_curve(G);
208 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
209 	if (!ok) return -1;
210 
211 	sm2_jacobian_point_dbl(P, G);
212 	ok = sm2_jacobian_point_equ_hex(P, hex_2G);
213 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
214 	if (!ok) return -1;
215 
216 	sm2_jacobian_point_add(P, P, G);
217 	ok = sm2_jacobian_point_equ_hex(P, hex_3G);
218 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
219 	if (!ok) return -1;
220 
221 	sm2_jacobian_point_sub(P, P, G);
222 	ok = sm2_jacobian_point_equ_hex(P, hex_2G);
223 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
224 	if (!ok) return -1;
225 
226 	sm2_jacobian_point_neg(P, G);
227 	ok = sm2_jacobian_point_equ_hex(P, hex_negG);
228 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
229 	if (!ok) return -1;
230 
231 	sm2_bn_set_word(k, 10);
232 	sm2_jacobian_point_mul(P, k, G);
233 	ok = sm2_jacobian_point_equ_hex(P, hex_10G);
234 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
235 	if (!ok) return -1;
236 
237 	sm2_jacobian_point_mul_generator(P, SM2_B);
238 	ok = sm2_jacobian_point_equ_hex(P, hex_bG);
239 	printf("sm2 point test %d %s\n", i++, ok ? "ok" : "failed");
240 	if (!ok) return -1;
241 
242 	sm2_jacobian_point_to_bytes(P, buf);
243 	sm2_jacobian_point_from_hex(P, hex_P);
244 
245 	return 1;
246 }
247 
248 #define hex_d   "5aebdfd947543b713bc0df2c65baaecc5dadd2cab39c6971402daf92c263fad2"
249 #define hex_e   "c0881c19beec741b9af27cc26493dcc33b05d481bfeab2f3ce9cc056e6ff8400"
250 #define hex_k   "981325ee1ab171e9d2cffb317181a02957b18a34bca610a6d2f8afcdeb53f6b8"
251 #define hex_x1  "17d2dfe83f23cce8499bca983950d59f0fd56c4c671dd63c04b27e4e94cfd767"
252 #define hex_r   "d85afc01fe104103e48e475a9de4b2624adb40ce2708892fd34f3ea57bcf5b67"
253 #define hex_rd  "a70ba64f9c30e05095f39fe26675114e3f157b2c35191bf6ff06246452f82eb3"
254 #define hex_di  "3ecfdb51c24b0eecb2d4238d1da8c013b8b575cef14ef43e2ddb7bce740ce9cf"
255 #define hex_krd "f1077f9d7e8091993cdc5b4f0b0c8eda8a9fee73a952f9db27ae7f72d2310928"
256 #define hex_s   "006bac5b8057ca829534dfde72a0d7883444a3b9bfe9bcdfb383fb90ed7d9486"
257 
258 
test_sm2_point(void)259 static int test_sm2_point(void)
260 {
261 	SM2_POINT P, Q;
262 	uint8_t k[32] = {0};
263 	uint8_t buf[65] = {0};
264 	int i;
265 
266 	for (i = 1; i < 8; i++) {
267 		k[31] = (uint8_t)i;
268 
269 		if (sm2_point_mul_generator(&P, k) != 1
270 			|| sm2_point_is_on_curve(&P) != 1) {
271 			error_print();
272 			return -1;
273 		}
274 		format_print(stderr, 0, 0, "k = %d, ", i);
275 		sm2_point_print(stderr, 0, 0, "k * G", &P);
276 
277 		memset(buf, 0, sizeof(buf));
278 		sm2_point_to_compressed_octets(&P, buf);
279 		format_bytes(stderr, 0, 4, "compressedPoint", buf, 33);
280 		memset(&Q, 0, sizeof(Q));
281 		if (sm2_point_from_x(&Q, buf + 1, buf[0]) != 1
282 			|| memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
283 
284 			sm2_point_print(stderr, 0, 4, "P", &P);
285 			sm2_point_print(stderr, 0, 4, "Q", &Q);
286 
287 			error_print();
288 			return -1;
289 		}
290 
291 		memset(buf, 0, sizeof(buf));
292 		sm2_point_to_uncompressed_octets(&P, buf);
293 		format_bytes(stderr, 0, 4, "compressedPoint", buf, 65);
294 		memset(&Q, 0, sizeof(Q));
295 		if (sm2_point_from_octets(&Q, buf, 65) != 1
296 			|| memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
297 			error_print();
298 			return -1;
299 		}
300 	}
301 
302 	printf("%s() ok\n", __FUNCTION__);
303 	return 1;
304 }
305 
test_sm2_point_der(void)306 static int test_sm2_point_der(void)
307 {
308 	SM2_POINT P, Q;
309 	uint8_t k[32] = {0};
310 	uint8_t buf[512];
311 	int i;
312 
313 	for (i = 1; i < 8; i++) {
314 		uint8_t *p = buf;
315 		const uint8_t *cp = buf;
316 		size_t len = 0;
317 
318 		k[31] = i;
319 		memset(&P, 0, sizeof(P));
320 		memset(&Q, 0, sizeof(Q));
321 
322 		if (sm2_point_mul_generator(&P, k) != 1
323 			|| sm2_point_to_der(&P, &p, &len) != 1
324 			|| format_bytes(stderr, 0, 4, "ECPoint", buf, len) != 1
325 			|| sm2_point_from_der(&Q, &cp, &len) != 1
326 			|| asn1_length_is_zero(len) != 1) {
327 			error_print();
328 			return -1;
329 		}
330 		if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
331 			error_print();
332 			sm2_point_print(stderr, 0, 4, "P", &P);
333 			sm2_point_print(stderr, 0, 4, "Q", &Q);
334 			return -1;
335 		}
336 	}
337 
338 	printf("%s() ok\n", __FUNCTION__);
339 	return 1;
340 }
341 
test_sm2_point_octets(void)342 static int test_sm2_point_octets(void)
343 {
344 	SM2_POINT P, Q;
345 	uint8_t k[32] = {0};
346 	uint8_t buf[33];
347 	int i;
348 
349 	for (i = 1; i < 8; i++) {
350 		uint8_t *p = buf;
351 		const uint8_t *cp = buf;
352 		size_t len = 0;
353 
354 		k[31] = i;
355 		memset(&P, 0, sizeof(P));
356 		memset(&Q, 0, sizeof(Q));
357 
358 		if (sm2_point_mul_generator(&P, k) != 1) {
359 			error_print();
360 			return -1;
361 		}
362 		sm2_point_to_compressed_octets(&P, buf);
363 		format_bytes(stderr, 0, 4, "compressedPoint", buf, sizeof(buf));
364 		if (sm2_point_from_octets(&Q, buf, sizeof(buf)) != 1) {
365 			error_print();
366 			return -1;
367 		}
368 		if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
369 			error_print();
370 			sm2_point_print(stderr, 0, 4, "P", &P);
371 			sm2_point_print(stderr, 0, 4, "Q", &Q);
372 			return -1;
373 		}
374 	}
375 
376 	printf("%s() ok\n", __FUNCTION__);
377 	return 1;
378 }
379 
test_sm2_point_from_x(void)380 static int test_sm2_point_from_x(void)
381 {
382 	SM2_POINT P, Q;
383 	uint8_t k[32] = {0};
384 	uint8_t buf[33];
385 	int i;
386 
387 	for (i = 1; i < 8; i++) {
388 		uint8_t *p = buf;
389 		const uint8_t *cp = buf;
390 		size_t len = 0;
391 
392 		k[31] = i;
393 		memset(&P, 0, sizeof(P));
394 		memset(&Q, 0, sizeof(Q));
395 
396 		if (sm2_point_mul_generator(&P, k) != 1) {
397 			error_print();
398 			return -1;
399 		}
400 		sm2_point_to_compressed_octets(&P, buf);
401 		if (sm2_point_from_x(&Q, buf + 1, buf[0]) != 1) {
402 			error_print();
403 			return -1;
404 		}
405 		if (memcmp(&P, &Q, sizeof(SM2_POINT)) != 0) {
406 			error_print();
407 			sm2_point_print(stderr, 0, 4, "P", &P);
408 			sm2_point_print(stderr, 0, 4, "Q", &Q);
409 			return -1;
410 		}
411 	}
412 
413 	printf("%s() ok\n", __FUNCTION__);
414 	return 1;
415 }
416 
test_sm2_signature(void)417 static int test_sm2_signature(void)
418 {
419 	SM2_SIGNATURE sig;
420 	uint8_t buf[512];
421 	uint8_t *p = buf;
422 	const uint8_t *cp = buf;
423 	size_t len = 0;
424 
425 	// MinLen
426 	memset(&sig, 0x00, sizeof(sig));
427 	cp = p = buf; len = 0;
428 	if (sm2_signature_to_der(&sig, &p, &len) != 1) {
429 		error_print();
430 		return -1;
431 	}
432 	format_print(stderr, 0, 4, "SM2_MIN_SIGNATURE_SIZE: %zu\n", len);
433 	format_bytes(stderr, 0, 4, "", buf, len);
434 	sm2_signature_print(stderr, 0, 4, "signature", buf, len);
435 	if (len != SM2_MIN_SIGNATURE_SIZE) {
436 		error_print();
437 		return -1;
438 	}
439 	if (sm2_signature_from_der(&sig, &cp, &len) != 1
440 		|| asn1_length_is_zero(len) != 1) {
441 		error_print();
442 		return -1;
443 	}
444 
445 
446 	// MaxLen
447 	memset(&sig, 0x80, sizeof(sig));
448 	cp = p = buf; len = 0;
449 	if (sm2_signature_to_der(&sig, &p, &len) != 1) {
450 		error_print();
451 		return -1;
452 	}
453 	format_print(stderr, 0, 4, "SM2_MAX_SIGNATURE_SIZE: %zu\n", len);
454 	format_bytes(stderr, 0, 4, "", buf, len);
455 	sm2_signature_print(stderr, 0, 4, "signature", buf, len);
456 	if (len != SM2_MAX_SIGNATURE_SIZE) {
457 		error_print();
458 		return -1;
459 	}
460 	if (sm2_signature_from_der(&sig, &cp, &len) != 1
461 		|| asn1_length_is_zero(len) != 1) {
462 		error_print();
463 		return -1;
464 	}
465 
466 
467 	printf("%s() ok\n", __FUNCTION__);
468 	return 1;
469 }
470 
test_sm2_sign(void)471 static int test_sm2_sign(void)
472 {
473 	int ret;
474 	SM2_KEY sm2_key;
475 	SM2_SIGN_CTX sign_ctx;
476 	uint8_t msg[] = "Hello World!";
477 	uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0};
478 	size_t siglen;
479 
480 	if (sm2_key_generate(&sm2_key) != 1) {
481 		error_print();
482 		return -1;
483 	}
484 	sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
485 
486 	if (sm2_sign_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
487 		|| sm2_sign_update(&sign_ctx, msg, sizeof(msg)) != 1
488 		|| sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
489 		error_print();
490 		return -1;
491 	}
492 	format_bytes(stderr, 0, 4, "signature", sig, siglen);
493 	sm2_signature_print(stderr, 0, 4, "signature", sig, siglen);
494 
495 	if (sm2_verify_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
496 		|| sm2_verify_update(&sign_ctx, msg, sizeof(msg)) != 1
497 		|| (ret = sm2_verify_finish(&sign_ctx, sig, siglen)) != 1) {
498 		error_print();
499 		return -1;
500 	}
501 	format_print(stderr, 0, 4, "verification: %s\n", ret ? "success" : "failed");
502 
503 
504 	// FIXME: 还应该增加验证不通过的测试
505 	// 还应该增加底层的参数
506 	printf("%s() ok\n", __FUNCTION__);
507 	return 1;
508 }
509 
510 // 由于当前Ciphertext中椭圆曲线点数据不正确,因此无法通过测试
test_sm2_ciphertext(void)511 static int test_sm2_ciphertext(void)
512 {
513 	SM2_CIPHERTEXT C;
514 	uint8_t buf[1024];
515 	uint8_t *p = buf;
516 	const uint8_t *cp = buf;
517 	size_t len = 0;
518 
519 	memset(&C, 0, sizeof(SM2_CIPHERTEXT));
520 
521 	cp = p = buf; len = 0;
522 	if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
523 		error_print();
524 		return -1;
525 	}
526 	format_print(stderr, 0, 4, "SM2_NULL_CIPHERTEXT_SIZE: %zu\n", len);
527 	format_bytes(stderr, 0, 4, "", buf, len);
528 
529 
530 	if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
531 		|| asn1_length_is_zero(len) != 1) {
532 		error_print();
533 		return -1;
534 	}
535 
536 
537 	// {0, 0, Hash, MinLen}
538 	C.ciphertext_size = SM2_MIN_PLAINTEXT_SIZE;
539 	cp = p = buf; len = 0;
540 	if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
541 		error_print();
542 		return -1;
543 	}
544 	format_print(stderr, 0, 4, "SM2_MIN_PLAINTEXT_SIZE: %zu\n", SM2_MIN_PLAINTEXT_SIZE);
545 	format_print(stderr, 0, 4, "SM2_MIN_CIPHERTEXT_SIZE: %zu\n", len);
546 	format_bytes(stderr, 0, 4, "", buf, len);
547 	if (len != SM2_MIN_CIPHERTEXT_SIZE) {
548 		error_print();
549 		return -1;
550 	}
551 	if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
552 		|| asn1_length_is_zero(len) != 1) {
553 		error_print();
554 		return -1;
555 	}
556 
557 	// { 33, 33, Hash, NULL }
558 	memset(&C, 0x80, sizeof(SM2_POINT));
559 	cp = p = buf; len = 0;
560 	if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
561 		error_print();
562 		return -1;
563 	}
564 	format_print(stderr, 0, 4, "ciphertext len: %zu\n", len);
565 	format_bytes(stderr, 0, 4, "", buf, len);
566 	if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
567 		|| asn1_length_is_zero(len) != 1) {
568 		error_print();
569 		return -1;
570 	}
571 
572 	// { 33, 33, Hash, MaxLen }
573 	C.ciphertext_size = SM2_MAX_PLAINTEXT_SIZE;//SM2_MAX_PLAINTEXT_SIZE;
574 	cp = p = buf; len = 0;
575 	if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
576 		error_print();
577 		return -1;
578 	}
579 	format_print(stderr, 0, 4, "SM2_MAX_PLAINTEXT_SIZE: %zu\n", SM2_MAX_PLAINTEXT_SIZE);
580 	format_print(stderr, 0, 4, "SM2_MAX_CIPHERTEXT_SIZE: %zu\n", len);
581 	format_bytes(stderr, 0, 4, "", buf, len);
582 	if (len != SM2_MAX_CIPHERTEXT_SIZE) {
583 		error_print();
584 		return -1;
585 	}
586 	if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
587 		|| asn1_length_is_zero(len) != 1) {
588 		error_print();
589 		return -1;
590 	}
591 
592 	printf("%s() ok\n", __FUNCTION__);
593 	return 1;
594 }
595 
596 
test_sm2_do_encrypt(void)597 static int test_sm2_do_encrypt(void)
598 {
599 	SM2_KEY sm2_key;
600 	uint8_t plaintext[] = "Hello World!";
601 	SM2_CIPHERTEXT ciphertext;
602 
603 	uint8_t plainbuf[SM2_MAX_PLAINTEXT_SIZE] = {0};
604 	size_t plainlen = 0;
605 	int r = 0;
606 
607 	if (sm2_key_generate(&sm2_key) != 1) {
608 		error_print();
609 		return -1;
610 	}
611 
612 	if (sm2_do_encrypt(&sm2_key, plaintext, sizeof(plaintext), &ciphertext) != 1
613 		|| sm2_do_decrypt(&sm2_key, &ciphertext, plainbuf, &plainlen) != 1) {
614 		error_print();
615 		return -1;
616 	}
617 
618 	if (plainlen != sizeof(plaintext)
619 		|| memcmp(plainbuf, plaintext, sizeof(plaintext)) != 0) {
620 		error_print();
621 		return -1;
622 	}
623 
624 	printf("%s() ok\n", __FUNCTION__);
625 	return 1;
626 }
627 
628 
test_sm2_encrypt(void)629 static int test_sm2_encrypt(void)
630 {
631 	SM2_KEY sm2_key;
632 	uint8_t msg[SM2_MAX_PLAINTEXT_SIZE];
633 	uint8_t cbuf[SM2_MAX_CIPHERTEXT_SIZE+100];
634 	uint8_t mbuf[SM2_MAX_CIPHERTEXT_SIZE];
635 	size_t lens[] = {
636 //		0,
637 		1,
638 		16,
639 		SM2_MAX_PLAINTEXT_SIZE,
640 	};
641 	size_t clen, mlen;
642 	int i;
643 
644 	if (sm2_key_generate(&sm2_key) != 1) {
645 		error_print();
646 		return -1;
647 	}
648 
649 	for (i = 0; i < sizeof(msg); i++) {
650 		msg[i] = (uint8_t)i;
651 	}
652 
653 	for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
654 		format_print(stderr, 0, 0, "test %d\n", i + 1);
655 		format_bytes(stderr, 0, 4, "plaintext", msg, lens[i]);
656 		if (sm2_encrypt(&sm2_key, msg, lens[i], cbuf, &clen) != 1) {
657 			error_print();
658 			return -1;
659 		}
660 		format_bytes(stderr, 0, 4, "ciphertext", cbuf, clen);
661 		sm2_ciphertext_print(stderr, 0, 4, "Ciphertext", cbuf, clen);
662 		format_print(stderr, 0, 0, "\n");
663 
664 		if (sm2_decrypt(&sm2_key, cbuf, clen, mbuf, &mlen) != 1) {
665 			error_print();
666 			return -1;
667 		}
668 		if (mlen != lens[i]
669 			|| memcmp(mbuf, msg, lens[i]) != 0) {
670 			error_print();
671 			return -1;
672 		}
673 	}
674 
675 	printf("%s() ok\n", __FUNCTION__);
676 	return 1;
677 }
678 
679 
680 
test_sm2_private_key(void)681 static int test_sm2_private_key(void)
682 {
683 	SM2_KEY sm2_key;
684 	SM2_KEY tmp_key;
685 	uint8_t buf[SM2_PRIVATE_KEY_BUF_SIZE];
686 	uint8_t *p = buf;
687 	const uint8_t *cp = buf;
688 	size_t len = 0;
689 	const uint8_t *d;
690 	size_t dlen;
691 
692 	if (sm2_key_generate(&sm2_key) != 1) {
693 		error_print();
694 		return -1;
695 	}
696 	sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
697 
698 	if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
699 		error_print();
700 		return -1;
701 	}
702 	format_bytes(stderr, 0, 4, "ECPrivateKey", buf, len);
703 	format_print(stderr, 0, 4, "#define SM2_PRIVATE_KEY_DEFAULT_SIZE %zu\n", len);
704 	if (sm2_private_key_from_der(&tmp_key, &cp, &len) != 1
705 		|| asn1_length_is_zero(len) != 1) {
706 		error_print();
707 		return -1;
708 	}
709 
710 	if (memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
711 
712 		sm2_key_print(stderr, 0, 0, "sm2_key", &sm2_key);
713 		sm2_key_print(stderr, 0, 0, "tmp_key", &tmp_key);
714 
715 
716 		error_print();
717 		return -1;
718 	}
719 
720 	cp = p = buf; len = 0;
721 	memset(&tmp_key, 0, sizeof(tmp_key));
722 	if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
723 		|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
724 		|| asn1_length_is_zero(len) != 1) {
725 		error_print();
726 		return -1;
727 	}
728 	sm2_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
729 
730 	printf("%s() ok\n", __FUNCTION__);
731 	return 1;
732 }
733 
test_sm2_private_key_info(void)734 static int test_sm2_private_key_info(void)
735 {
736 	uint8_t buf[512];
737 	uint8_t *p = buf;
738 	const uint8_t *cp = buf;
739 	size_t len = 0;
740 	const uint8_t *d;
741 	size_t dlen;
742 
743 	SM2_KEY sm2_key;
744 	SM2_KEY tmp_key;
745 	const uint8_t *attrs;
746 	size_t attrs_len;
747 
748 	if (sm2_key_generate(&sm2_key) != 1) {
749 		error_print();
750 		return -1;
751 	}
752 	sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
753 
754 	if (sm2_private_key_info_to_der(&sm2_key, &p, &len) != 1) {
755 		error_print();
756 		return -1;
757 	}
758 	format_bytes(stderr, 0, 4, "PrivateKeyInfo", buf, len);
759 	format_print(stderr, 0, 4, "sizeof(PrivateKeyInfo): %zu\n", len);
760 	if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
761 		|| asn1_length_is_zero(len) != 1) {
762 		error_print();
763 		return -1;
764 	}
765 	sm2_private_key_info_print(stderr, 0, 4, "PrivateKeyInfo", d, dlen);
766 
767 	cp = p = buf; len = 0;
768 	if (sm2_private_key_info_to_der(&sm2_key, &p, &len) != 1) {
769 		error_print();
770 		return -1;
771 	}
772 	if (sm2_private_key_info_from_der(&tmp_key, &attrs, &attrs_len, &cp, &len) != 1
773 		|| asn1_length_is_zero(len) != 1
774 		|| memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
775 		error_print();
776 		return -1;
777 	}
778 
779 	printf("%s() ok\n", __FUNCTION__);
780 	return 1;
781 }
782 
test_sm2_enced_private_key_info(void)783 static int test_sm2_enced_private_key_info(void)
784 {
785 	uint8_t buf[512];
786 	uint8_t *p = buf;
787 	const uint8_t *cp = buf;
788 	size_t len = 0;
789 	const uint8_t *d;
790 	size_t dlen;
791 
792 	SM2_KEY sm2_key;
793 	SM2_KEY tmp_key;
794 	const uint8_t *attrs;
795 	size_t attrs_len;
796 	const char *pass = "Password";
797 
798 	if (sm2_key_generate(&sm2_key) != 1) {
799 		error_print();
800 		return -1;
801 	}
802 	sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
803 
804 	if (sm2_private_key_info_encrypt_to_der(&sm2_key, pass, &p, &len) != 1) {
805 		error_print();
806 		return -1;
807 	}
808 	format_bytes(stderr, 0, 4, "EncryptedPrivateKeyInfo", buf, len);
809 	format_print(stderr, 0, 4, "sizeof(EncryptedPrivateKeyInfo): %zu\n", len);
810 	if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
811 		|| asn1_length_is_zero(len) != 1) {
812 		error_print();
813 		return -1;
814 	}
815 	pkcs8_enced_private_key_info_print(stderr, 0, 4, "EncryptedPrivateKeyInfo", d, dlen);
816 
817 
818 	cp = p = buf; len = 0;
819 	if (sm2_private_key_info_encrypt_to_der(&sm2_key, pass, &p, &len) != 1) {
820 		error_print();
821 		return -1;
822 	}
823 	if (sm2_private_key_info_decrypt_from_der(&tmp_key, &attrs, &attrs_len, pass, &cp, &len) != 1
824 		|| asn1_length_is_zero(len) != 1
825 		|| memcmp(&tmp_key, &sm2_key, sizeof(SM2_KEY)) != 0) {
826 		error_print();
827 		return -1;
828 	}
829 
830 	printf("%s() ok\n", __FUNCTION__);
831 	return 1;
832 }
833 
834 
main(void)835 int main(void)
836 {
837 	if (test_sm2_bn()  != 1) goto err;
838 	if (test_sm2_jacobian_point() != 1) goto err;
839 	if (test_sm2_point() != 1) goto err;
840 	if (test_sm2_point_octets() != 1) goto err;
841 	if (test_sm2_point_from_x() != 1) goto err;
842 	if (test_sm2_point_der() != 1) goto err;
843 	if (test_sm2_private_key() != 1) goto err;
844 	if (test_sm2_private_key_info() != 1) goto err;
845 	if (test_sm2_enced_private_key_info() != 1) goto err;
846 	if (test_sm2_signature() != 1) goto err;
847 	if (test_sm2_sign() != 1) goto err;
848 	//if (test_sm2_ciphertext() != 1) goto err; // 需要正确的Ciphertext数据
849 	if (test_sm2_do_encrypt() != 1) goto err;
850 	if (test_sm2_encrypt() != 1) goto err;
851 	printf("%s all tests passed\n", __FILE__);
852 	return 0;
853 err:
854 	error_print();
855 	return -1;
856 }
857