xref: /openbsd/regress/lib/libcrypto/sm2/sm2sigtest.c (revision c9675a23)
1 /*	$OpenBSD: sm2sigtest.c,v 1.2 2022/11/26 16:08:56 tb Exp $ */
2 /*
3  * Copyright (c) 2017, 2019 Ribose Inc
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <openssl/bio.h>
23 #include <openssl/evp.h>
24 #include <openssl/bn.h>
25 #include <openssl/crypto.h>
26 #include <openssl/err.h>
27 #include <openssl/rand.h>
28 
29 #ifdef OPENSSL_NO_SM2
30 int
main(int argc,char * argv[])31 main(int argc, char *argv[])
32 {
33     printf("No SM2 support\n");
34     return (0);
35 }
36 #else
37 #include <openssl/sm2.h>
38 #include "sm2_local.h"
39 
40 static EC_GROUP *
create_EC_group(const char * p_hex,const char * a_hex,const char * b_hex,const char * x_hex,const char * y_hex,const char * order_hex,const char * cof_hex)41 create_EC_group(const char *p_hex, const char *a_hex, const char *b_hex,
42     const char *x_hex, const char *y_hex, const char *order_hex,
43     const char *cof_hex)
44 {
45 	BIGNUM *p = NULL;
46 	BIGNUM *a = NULL;
47 	BIGNUM *b = NULL;
48 	BIGNUM *g_x = NULL;
49 	BIGNUM *g_y = NULL;
50 	BIGNUM *order = NULL;
51 	BIGNUM *cof = NULL;
52 	EC_POINT *generator = NULL;
53 	EC_GROUP *group = NULL;
54 
55 	BN_hex2bn(&p, p_hex);
56 	BN_hex2bn(&a, a_hex);
57 	BN_hex2bn(&b, b_hex);
58 
59 	group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
60 	BN_free(p);
61 	BN_free(a);
62 	BN_free(b);
63 
64 	if (group == NULL)
65 		return NULL;
66 
67 	generator = EC_POINT_new(group);
68 	if (generator == NULL)
69 		return NULL;
70 
71 	BN_hex2bn(&g_x, x_hex);
72 	BN_hex2bn(&g_y, y_hex);
73 
74 	if (EC_POINT_set_affine_coordinates(group, generator, g_x, g_y,
75 	    NULL) == 0)
76 		return NULL;
77 
78 	BN_free(g_x);
79 	BN_free(g_y);
80 
81 	BN_hex2bn(&order, order_hex);
82 	BN_hex2bn(&cof, cof_hex);
83 
84 	if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
85 		return NULL;
86 
87 	EC_POINT_free(generator);
88 	BN_free(order);
89 	BN_free(cof);
90 
91 	return group;
92 }
93 
94 
95 static int
test_sm2(const EC_GROUP * group,const char * userid,const char * privkey_hex,const char * message)96 test_sm2(const EC_GROUP *group, const char *userid, const char *privkey_hex,
97     const char *message)
98 {
99 	const size_t msg_len = strlen(message);
100 	int ok = -1;
101 	BIGNUM *priv = NULL;
102 	EC_POINT *pt = NULL;
103 	EC_KEY *key = NULL;
104 	ECDSA_SIG *sig = NULL;
105 	const BIGNUM *sig_r = NULL;
106 	const BIGNUM *sig_s = NULL;
107 
108 	BN_hex2bn(&priv, privkey_hex);
109 
110 	key = EC_KEY_new();
111 	EC_KEY_set_group(key, group);
112 	EC_KEY_set_private_key(key, priv);
113 
114 	pt = EC_POINT_new(group);
115 	EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
116 	EC_KEY_set_public_key(key, pt);
117 
118 	sig = sm2_do_sign(key, EVP_sm3(), userid, strlen(userid),
119 	    (const uint8_t *)message, msg_len);
120 
121 	if (sig == NULL)
122 		return 0;
123 
124 	ECDSA_SIG_get0(sig, &sig_r, &sig_s);
125 
126 	ok = sm2_do_verify(key, EVP_sm3(), sig, userid, strlen(userid),
127 	    (const uint8_t *)message, msg_len);
128 
129 	ECDSA_SIG_free(sig);
130 	EC_POINT_free(pt);
131 	EC_KEY_free(key);
132 	BN_free(priv);
133 
134 	return ok;
135 }
136 
137 int
main(int argc,char ** argv)138 main(int argc, char **argv)
139 {
140 	int rc = 0;
141 	/* From draft-shen-sm2-ecdsa-02 */
142 	EC_GROUP *test_group =
143 		create_EC_group
144 		("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
145 		 "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
146 		 "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
147 		 "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
148 		 "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
149 		 "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
150 		 "1");
151 
152 	if (test_group == NULL)
153 		return 1;
154 
155 	rc = test_sm2(test_group, "ALICE123@YAHOO.COM",
156 	    "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
157 	    "message digest");
158 
159 	EC_GROUP_free(test_group);
160 
161 	if (rc <= 0)
162 		return 1;
163 
164 
165 	printf("SUCCESS\n");
166 
167 	return 0;
168 }
169 
170 #endif
171