1*c9675a23Stb /* $OpenBSD: sm2sigtest.c,v 1.2 2022/11/26 16:08:56 tb Exp $ */
2640e966cStb /*
3640e966cStb * Copyright (c) 2017, 2019 Ribose Inc
4640e966cStb *
5640e966cStb * Permission to use, copy, modify, and/or distribute this software for any
6640e966cStb * purpose with or without fee is hereby granted, provided that the above
7640e966cStb * copyright notice and this permission notice appear in all copies.
8640e966cStb *
9640e966cStb * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10640e966cStb * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11640e966cStb * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12640e966cStb * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13640e966cStb * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14640e966cStb * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15640e966cStb * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16640e966cStb */
17640e966cStb
18640e966cStb #include <stdio.h>
19640e966cStb #include <stdlib.h>
20640e966cStb #include <string.h>
21640e966cStb
22640e966cStb #include <openssl/bio.h>
23640e966cStb #include <openssl/evp.h>
24640e966cStb #include <openssl/bn.h>
25640e966cStb #include <openssl/crypto.h>
26640e966cStb #include <openssl/err.h>
27640e966cStb #include <openssl/rand.h>
28640e966cStb
29640e966cStb #ifdef OPENSSL_NO_SM2
30640e966cStb int
main(int argc,char * argv[])31640e966cStb main(int argc, char *argv[])
32640e966cStb {
33640e966cStb printf("No SM2 support\n");
34640e966cStb return (0);
35640e966cStb }
36640e966cStb #else
37640e966cStb #include <openssl/sm2.h>
38*c9675a23Stb #include "sm2_local.h"
39640e966cStb
40640e966cStb 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)41640e966cStb create_EC_group(const char *p_hex, const char *a_hex, const char *b_hex,
42640e966cStb const char *x_hex, const char *y_hex, const char *order_hex,
43640e966cStb const char *cof_hex)
44640e966cStb {
45640e966cStb BIGNUM *p = NULL;
46640e966cStb BIGNUM *a = NULL;
47640e966cStb BIGNUM *b = NULL;
48640e966cStb BIGNUM *g_x = NULL;
49640e966cStb BIGNUM *g_y = NULL;
50640e966cStb BIGNUM *order = NULL;
51640e966cStb BIGNUM *cof = NULL;
52640e966cStb EC_POINT *generator = NULL;
53640e966cStb EC_GROUP *group = NULL;
54640e966cStb
55640e966cStb BN_hex2bn(&p, p_hex);
56640e966cStb BN_hex2bn(&a, a_hex);
57640e966cStb BN_hex2bn(&b, b_hex);
58640e966cStb
59640e966cStb group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
60640e966cStb BN_free(p);
61640e966cStb BN_free(a);
62640e966cStb BN_free(b);
63640e966cStb
64640e966cStb if (group == NULL)
65640e966cStb return NULL;
66640e966cStb
67640e966cStb generator = EC_POINT_new(group);
68640e966cStb if (generator == NULL)
69640e966cStb return NULL;
70640e966cStb
71640e966cStb BN_hex2bn(&g_x, x_hex);
72640e966cStb BN_hex2bn(&g_y, y_hex);
73640e966cStb
74640e966cStb if (EC_POINT_set_affine_coordinates(group, generator, g_x, g_y,
75640e966cStb NULL) == 0)
76640e966cStb return NULL;
77640e966cStb
78640e966cStb BN_free(g_x);
79640e966cStb BN_free(g_y);
80640e966cStb
81640e966cStb BN_hex2bn(&order, order_hex);
82640e966cStb BN_hex2bn(&cof, cof_hex);
83640e966cStb
84640e966cStb if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
85640e966cStb return NULL;
86640e966cStb
87640e966cStb EC_POINT_free(generator);
88640e966cStb BN_free(order);
89640e966cStb BN_free(cof);
90640e966cStb
91640e966cStb return group;
92640e966cStb }
93640e966cStb
94640e966cStb
95640e966cStb static int
test_sm2(const EC_GROUP * group,const char * userid,const char * privkey_hex,const char * message)96640e966cStb test_sm2(const EC_GROUP *group, const char *userid, const char *privkey_hex,
97640e966cStb const char *message)
98640e966cStb {
99640e966cStb const size_t msg_len = strlen(message);
100640e966cStb int ok = -1;
101640e966cStb BIGNUM *priv = NULL;
102640e966cStb EC_POINT *pt = NULL;
103640e966cStb EC_KEY *key = NULL;
104640e966cStb ECDSA_SIG *sig = NULL;
105640e966cStb const BIGNUM *sig_r = NULL;
106640e966cStb const BIGNUM *sig_s = NULL;
107640e966cStb
108640e966cStb BN_hex2bn(&priv, privkey_hex);
109640e966cStb
110640e966cStb key = EC_KEY_new();
111640e966cStb EC_KEY_set_group(key, group);
112640e966cStb EC_KEY_set_private_key(key, priv);
113640e966cStb
114640e966cStb pt = EC_POINT_new(group);
115640e966cStb EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
116640e966cStb EC_KEY_set_public_key(key, pt);
117640e966cStb
118640e966cStb sig = sm2_do_sign(key, EVP_sm3(), userid, strlen(userid),
119640e966cStb (const uint8_t *)message, msg_len);
120640e966cStb
121640e966cStb if (sig == NULL)
122640e966cStb return 0;
123640e966cStb
124640e966cStb ECDSA_SIG_get0(sig, &sig_r, &sig_s);
125640e966cStb
126640e966cStb ok = sm2_do_verify(key, EVP_sm3(), sig, userid, strlen(userid),
127640e966cStb (const uint8_t *)message, msg_len);
128640e966cStb
129640e966cStb ECDSA_SIG_free(sig);
130640e966cStb EC_POINT_free(pt);
131640e966cStb EC_KEY_free(key);
132640e966cStb BN_free(priv);
133640e966cStb
134640e966cStb return ok;
135640e966cStb }
136640e966cStb
137640e966cStb int
main(int argc,char ** argv)138640e966cStb main(int argc, char **argv)
139640e966cStb {
140640e966cStb int rc = 0;
141640e966cStb /* From draft-shen-sm2-ecdsa-02 */
142640e966cStb EC_GROUP *test_group =
143640e966cStb create_EC_group
144640e966cStb ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
145640e966cStb "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
146640e966cStb "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
147640e966cStb "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
148640e966cStb "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
149640e966cStb "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
150640e966cStb "1");
151640e966cStb
152640e966cStb if (test_group == NULL)
153640e966cStb return 1;
154640e966cStb
155640e966cStb rc = test_sm2(test_group, "ALICE123@YAHOO.COM",
156640e966cStb "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
157640e966cStb "message digest");
158640e966cStb
159640e966cStb EC_GROUP_free(test_group);
160640e966cStb
161640e966cStb if (rc <= 0)
162640e966cStb return 1;
163640e966cStb
164640e966cStb
165640e966cStb printf("SUCCESS\n");
166640e966cStb
167640e966cStb return 0;
168640e966cStb }
169640e966cStb
170640e966cStb #endif
171