aes ctr_drbg: add cavs tests
[openssl.git] / test / sm2sigtest.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2017 Ribose Inc. All Rights Reserved.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <openssl/bio.h>
16 #include <openssl/evp.h>
17 #include <openssl/bn.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
21 #include "testutil.h"
22
23 #ifndef OPENSSL_NO_SM2
24
25 # include <openssl/sm2.h>
26
27 static RAND_METHOD fake_rand;
28 static const RAND_METHOD *saved_rand;
29
30 static uint8_t *fake_rand_bytes = NULL;
31 static size_t fake_rand_bytes_offset = 0;
32
33 static int get_faked_bytes(unsigned char *buf, int num)
34 {
35     int i;
36
37     if (fake_rand_bytes == NULL)
38         return saved_rand->bytes(buf, num);
39
40     for (i = 0; i != num; ++i)
41         buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
42     fake_rand_bytes_offset += num;
43     return 1;
44 }
45
46 static int start_fake_rand(const char *hex_bytes)
47 {
48     /* save old rand method */
49     if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
50         return 0;
51
52     fake_rand = *saved_rand;
53     /* use own random function */
54     fake_rand.bytes = get_faked_bytes;
55
56     fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
57     fake_rand_bytes_offset = 0;
58
59     /* set new RAND_METHOD */
60     if (!TEST_true(RAND_set_rand_method(&fake_rand)))
61         return 0;
62     return 1;
63 }
64
65 static int restore_rand(void)
66 {
67     OPENSSL_free(fake_rand_bytes);
68     fake_rand_bytes = NULL;
69     fake_rand_bytes_offset = 0;
70     if (!TEST_true(RAND_set_rand_method(saved_rand)))
71         return 0;
72     return 1;
73 }
74
75 static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
76                                  const char *b_hex, const char *x_hex,
77                                  const char *y_hex, const char *order_hex,
78                                  const char *cof_hex)
79 {
80     BIGNUM *p = NULL;
81     BIGNUM *a = NULL;
82     BIGNUM *b = NULL;
83     BIGNUM *g_x = NULL;
84     BIGNUM *g_y = NULL;
85     BIGNUM *order = NULL;
86     BIGNUM *cof = NULL;
87     EC_POINT *generator = NULL;
88     EC_GROUP *group = NULL;
89
90     BN_hex2bn(&p, p_hex);
91     BN_hex2bn(&a, a_hex);
92     BN_hex2bn(&b, b_hex);
93
94     group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
95     BN_free(p);
96     BN_free(a);
97     BN_free(b);
98
99     if (group == NULL)
100         return NULL;
101
102     generator = EC_POINT_new(group);
103     if (generator == NULL)
104         return NULL;
105
106     BN_hex2bn(&g_x, x_hex);
107     BN_hex2bn(&g_y, y_hex);
108
109     if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
110         0)
111         return NULL;
112
113     BN_free(g_x);
114     BN_free(g_y);
115
116     BN_hex2bn(&order, order_hex);
117     BN_hex2bn(&cof, cof_hex);
118
119     if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
120         return NULL;
121
122     EC_POINT_free(generator);
123     BN_free(order);
124     BN_free(cof);
125
126     return group;
127 }
128
129
130 static int test_sm2(const EC_GROUP *group,
131                     const char *userid,
132                     const char *privkey_hex,
133                     const char *message,
134                     const char *k_hex, const char *r_hex, const char *s_hex)
135 {
136     const size_t msg_len = strlen(message);
137     int ok = -1;
138     BIGNUM *priv = NULL;
139     EC_POINT *pt = NULL;
140     EC_KEY *key = NULL;
141     ECDSA_SIG *sig = NULL;
142     const BIGNUM *sig_r = NULL;
143     const BIGNUM *sig_s = NULL;
144     BIGNUM *r = NULL;
145     BIGNUM *s = NULL;
146
147     BN_hex2bn(&priv, privkey_hex);
148
149     key = EC_KEY_new();
150     EC_KEY_set_group(key, group);
151     EC_KEY_set_private_key(key, priv);
152
153     pt = EC_POINT_new(group);
154     EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
155     EC_KEY_set_public_key(key, pt);
156
157     start_fake_rand(k_hex);
158     sig = SM2_do_sign(key, EVP_sm3(), userid, (const uint8_t *)message, msg_len);
159     restore_rand();
160
161     if (sig == NULL)
162         return 0;
163
164     ECDSA_SIG_get0(sig, &sig_r, &sig_s);
165
166     BN_hex2bn(&r, r_hex);
167     BN_hex2bn(&s, s_hex);
168
169     if (BN_cmp(r, sig_r) != 0) {
170         printf("Signature R mismatch: ");
171         BN_print_fp(stdout, r);
172         printf(" != ");
173         BN_print_fp(stdout, sig_r);
174         printf("\n");
175         ok = 0;
176     }
177     if (BN_cmp(s, sig_s) != 0) {
178         printf("Signature S mismatch: ");
179         BN_print_fp(stdout, s);
180         printf(" != ");
181         BN_print_fp(stdout, sig_s);
182         printf("\n");
183         ok = 0;
184     }
185
186     ok = SM2_do_verify(key, EVP_sm3(), sig, userid, (const uint8_t *)message, msg_len);
187
188     ECDSA_SIG_free(sig);
189     EC_POINT_free(pt);
190     EC_KEY_free(key);
191     BN_free(priv);
192     BN_free(r);
193     BN_free(s);
194
195     return ok;
196 }
197
198 static int sm2_sig_test(void)
199 {
200     int rc = 0;
201     /* From draft-shen-sm2-ecdsa-02 */
202     EC_GROUP *test_group =
203         create_EC_group
204         ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
205          "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
206          "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
207          "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
208          "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
209          "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
210          "1");
211
212     if (test_group == NULL)
213         return 0;
214
215     rc = test_sm2(test_group,
216                     "ALICE123@YAHOO.COM",
217                     "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
218                     "message digest",
219                     "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
220                     "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
221                     "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
222
223     EC_GROUP_free(test_group);
224
225     return rc;
226 }
227
228 #endif
229
230 int setup_tests(void)
231 {
232 #ifdef OPENSSL_NO_SM2
233     TEST_note("SM2 is disabled.");
234 #else
235     ADD_TEST(sm2_sig_test);
236 #endif
237     return 1;
238 }