1*c9675a23Stb /* $OpenBSD: sm2crypttest.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
main(int argc,char * argv[])30640e966cStb int main(int argc, char *argv[])
31640e966cStb {
32640e966cStb printf("No SM2 support\n");
33640e966cStb return (0);
34640e966cStb }
35640e966cStb #else
36640e966cStb #include <openssl/sm2.h>
37*c9675a23Stb #include "sm2_local.h"
38640e966cStb
39640e966cStb 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)40640e966cStb create_EC_group(const char *p_hex, const char *a_hex, const char *b_hex,
41640e966cStb const char *x_hex, const char *y_hex, const char *order_hex,
42640e966cStb const char *cof_hex)
43640e966cStb {
44640e966cStb BIGNUM *p = NULL;
45640e966cStb BIGNUM *a = NULL;
46640e966cStb BIGNUM *b = NULL;
47640e966cStb BIGNUM *g_x = NULL;
48640e966cStb BIGNUM *g_y = NULL;
49640e966cStb BIGNUM *order = NULL;
50640e966cStb BIGNUM *cof = NULL;
51640e966cStb EC_POINT *generator = NULL;
52640e966cStb EC_GROUP *group = NULL;
53640e966cStb
54640e966cStb BN_hex2bn(&p, p_hex);
55640e966cStb BN_hex2bn(&a, a_hex);
56640e966cStb BN_hex2bn(&b, b_hex);
57640e966cStb
58640e966cStb group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
59640e966cStb BN_free(p);
60640e966cStb BN_free(a);
61640e966cStb BN_free(b);
62640e966cStb
63640e966cStb if (group == NULL)
64640e966cStb return NULL;
65640e966cStb
66640e966cStb generator = EC_POINT_new(group);
67640e966cStb if (generator == NULL)
68640e966cStb return NULL;
69640e966cStb
70640e966cStb BN_hex2bn(&g_x, x_hex);
71640e966cStb BN_hex2bn(&g_y, y_hex);
72640e966cStb
73640e966cStb if (EC_POINT_set_affine_coordinates(group, generator, g_x, g_y,
74640e966cStb NULL) == 0)
75640e966cStb return NULL;
76640e966cStb
77640e966cStb BN_free(g_x);
78640e966cStb BN_free(g_y);
79640e966cStb
80640e966cStb BN_hex2bn(&order, order_hex);
81640e966cStb BN_hex2bn(&cof, cof_hex);
82640e966cStb
83640e966cStb if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
84640e966cStb return NULL;
85640e966cStb
86640e966cStb EC_POINT_free(generator);
87640e966cStb BN_free(order);
88640e966cStb BN_free(cof);
89640e966cStb
90640e966cStb return group;
91640e966cStb }
92640e966cStb
93640e966cStb static int
test_sm2(const EC_GROUP * group,const EVP_MD * digest,const char * privkey_hex,const char * message)94640e966cStb test_sm2(const EC_GROUP *group, const EVP_MD *digest, const char *privkey_hex,
95640e966cStb const char *message)
96640e966cStb {
97640e966cStb const size_t msg_len = strlen(message);
98640e966cStb
99640e966cStb BIGNUM *priv = NULL;
100640e966cStb EC_KEY *key = NULL;
101640e966cStb EC_POINT *pt = NULL;
102640e966cStb size_t ctext_len = 0;
103640e966cStb uint8_t *ctext = NULL;
104640e966cStb uint8_t *recovered = NULL;
105640e966cStb size_t recovered_len = msg_len;
106640e966cStb int rc = 0;
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
117640e966cStb EC_KEY_set_public_key(key, pt);
118640e966cStb BN_free(priv);
119640e966cStb EC_POINT_free(pt);
120640e966cStb
121640e966cStb if (!SM2_ciphertext_size(key, digest, msg_len, &ctext_len))
122640e966cStb goto done;
123640e966cStb ctext = calloc(1, ctext_len);
124640e966cStb if (ctext == NULL)
125640e966cStb goto done;
126640e966cStb
127640e966cStb rc = SM2_encrypt(key, digest, (const uint8_t *)message, msg_len, ctext, &ctext_len);
128640e966cStb
129640e966cStb if (rc == 0)
130640e966cStb goto done;
131640e966cStb
132640e966cStb recovered = calloc(1, msg_len);
133640e966cStb if (recovered == NULL)
134640e966cStb goto done;
135640e966cStb rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);
136640e966cStb
137640e966cStb if (rc == 0)
138640e966cStb goto done;
139640e966cStb if (recovered_len != msg_len)
140640e966cStb goto done;
141640e966cStb if (memcmp(recovered, message, msg_len) != 0)
142640e966cStb goto done;
143640e966cStb
144640e966cStb rc = 1;
145640e966cStb done:
146640e966cStb
147640e966cStb free(ctext);
148640e966cStb free(recovered);
149640e966cStb EC_KEY_free(key);
150640e966cStb return rc;
151640e966cStb }
152640e966cStb
153640e966cStb int
main(int argc,char ** argv)154640e966cStb main(int argc, char **argv)
155640e966cStb {
156640e966cStb int rc;
157640e966cStb EC_GROUP *test_group =
158640e966cStb create_EC_group
159640e966cStb ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
160640e966cStb "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
161640e966cStb "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
162640e966cStb "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
163640e966cStb "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
164640e966cStb "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
165640e966cStb "1");
166640e966cStb
167640e966cStb if (test_group == NULL)
168640e966cStb return 1;
169640e966cStb
170640e966cStb rc = test_sm2(test_group, EVP_sm3(),
171640e966cStb "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
172640e966cStb "encryption standard");
173640e966cStb
174640e966cStb if (rc == 0)
175640e966cStb return 1;
176640e966cStb
177640e966cStb /* Same test as above except using SHA-256 instead of SM3 */
178640e966cStb rc = test_sm2(test_group, EVP_sha256(),
179640e966cStb "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
180640e966cStb "encryption standard");
181640e966cStb if (rc == 0)
182640e966cStb return 1;
183640e966cStb
184640e966cStb EC_GROUP_free(test_group);
185640e966cStb
186640e966cStb printf("SUCCESS\n");
187640e966cStb
188640e966cStb return 0;
189640e966cStb }
190640e966cStb
191640e966cStb #endif
192