summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/sm2/sm2sigtest.c
diff options
context:
space:
mode:
authortb <>2021-08-18 16:06:56 +0000
committertb <>2021-08-18 16:06:56 +0000
commita5c89764af8f7a72f1c0ddb9d42ec39de836d700 (patch)
tree4c8c274fa412a2c828b63050128d911a1fafa9f9 /src/regress/lib/libcrypto/sm2/sm2sigtest.c
parent27a4a421da356671ca87fff69b8b7340d284659c (diff)
downloadopenbsd-a5c89764af8f7a72f1c0ddb9d42ec39de836d700.tar.gz
openbsd-a5c89764af8f7a72f1c0ddb9d42ec39de836d700.tar.bz2
openbsd-a5c89764af8f7a72f1c0ddb9d42ec39de836d700.zip
Import regress tests for SM2. Not yet linked to the build.
Part of Github PR #105
Diffstat (limited to 'src/regress/lib/libcrypto/sm2/sm2sigtest.c')
-rw-r--r--src/regress/lib/libcrypto/sm2/sm2sigtest.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/sm2/sm2sigtest.c b/src/regress/lib/libcrypto/sm2/sm2sigtest.c
new file mode 100644
index 0000000000..bd156799b8
--- /dev/null
+++ b/src/regress/lib/libcrypto/sm2/sm2sigtest.c
@@ -0,0 +1,170 @@
1/* $OpenBSD: sm2sigtest.c,v 1.1.1.1 2021/08/18 16:06: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
30int
31main(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_locl.h"
39
40static EC_GROUP *
41create_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
95static int
96test_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
137int
138main(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