Use ec_group_do_inverse_ord() in SM2
[openssl.git] / crypto / sm2 / sm2_sign.c
1 /*
2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2017 Ribose Inc. All Rights Reserved.
4  * Ported from Ribose contributions from Botan.
5  *
6  * Licensed under the OpenSSL license (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 #include "internal/sm2.h"
13 #include "internal/sm2err.h"
14 #include "internal/ec_int.h" /* ec_group_do_inverse_ord() */
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/bn.h>
19 #include <string.h>
20
21 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
22                                     const EC_KEY *key,
23                                     const char *user_id,
24                                     const uint8_t *msg, size_t msg_len)
25 {
26     EVP_MD_CTX *hash = EVP_MD_CTX_new();
27     const int md_size = EVP_MD_size(digest);
28     uint8_t *za = OPENSSL_zalloc(md_size);
29     BIGNUM *e = NULL;
30
31     if (hash == NULL || za == NULL) {
32         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
33         goto done;
34     }
35
36     if (md_size < 0) {
37         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
38         goto done;
39     }
40
41     if (!sm2_compute_userid_digest(za, digest, user_id, key)) {
42         /* SM2err already called */
43         goto done;
44     }
45
46     if (!EVP_DigestInit(hash, digest)
47             || !EVP_DigestUpdate(hash, za, md_size)
48             || !EVP_DigestUpdate(hash, msg, msg_len)
49                /* reuse za buffer to hold H(ZA || M) */
50             || !EVP_DigestFinal(hash, za, NULL)) {
51         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
52         goto done;
53     }
54
55     e = BN_bin2bn(za, md_size, NULL);
56     if (e == NULL)
57         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
58
59  done:
60     OPENSSL_free(za);
61     EVP_MD_CTX_free(hash);
62     return e;
63 }
64
65 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
66 {
67     const BIGNUM *dA = EC_KEY_get0_private_key(key);
68     const EC_GROUP *group = EC_KEY_get0_group(key);
69     const BIGNUM *order = EC_GROUP_get0_order(group);
70     ECDSA_SIG *sig = NULL;
71     EC_POINT *kG = NULL;
72     BN_CTX *ctx = NULL;
73     BIGNUM *k = NULL;
74     BIGNUM *rk = NULL;
75     BIGNUM *r = NULL;
76     BIGNUM *s = NULL;
77     BIGNUM *x1 = NULL;
78     BIGNUM *tmp = NULL;
79
80     kG = EC_POINT_new(group);
81     ctx = BN_CTX_new();
82     if (kG == NULL || ctx == NULL) {
83         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
84         goto done;
85     }
86
87
88     BN_CTX_start(ctx);
89     k = BN_CTX_get(ctx);
90     rk = BN_CTX_get(ctx);
91     x1 = BN_CTX_get(ctx);
92     tmp = BN_CTX_get(ctx);
93     if (tmp == NULL) {
94         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
95         goto done;
96     }
97
98     /*
99      * These values are returned and so should not be allocated out of the
100      * context
101      */
102     r = BN_new();
103     s = BN_new();
104
105     if (r == NULL || s == NULL) {
106         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
107         goto done;
108     }
109
110     for (;;) {
111         if (!BN_priv_rand_range(k, order)) {
112             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
113                 goto done;
114         }
115
116         if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
117                 || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL,
118                                                         ctx)
119                 || !BN_mod_add(r, e, x1, order, ctx)) {
120             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
121             goto done;
122         }
123
124         /* try again if r == 0 or r+k == n */
125         if (BN_is_zero(r))
126             continue;
127
128         if (!BN_add(rk, r, k)) {
129             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
130             goto done;
131         }
132
133         if (BN_cmp(rk, order) == 0)
134             continue;
135
136         if (!BN_add(s, dA, BN_value_one())
137                 || !ec_group_do_inverse_ord(group, s, s, ctx)
138                 || !BN_mod_mul(tmp, dA, r, order, ctx)
139                 || !BN_sub(tmp, k, tmp)
140                 || !BN_mod_mul(s, s, tmp, order, ctx)) {
141             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
142             goto done;
143         }
144
145         sig = ECDSA_SIG_new();
146         if (sig == NULL) {
147             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
148             goto done;
149         }
150
151          /* takes ownership of r and s */
152         ECDSA_SIG_set0(sig, r, s);
153         break;
154     }
155
156  done:
157     if (sig == NULL) {
158         BN_free(r);
159         BN_free(s);
160     }
161
162     BN_CTX_free(ctx);
163     EC_POINT_free(kG);
164     return sig;
165 }
166
167 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
168                           const BIGNUM *e)
169 {
170     int ret = 0;
171     const EC_GROUP *group = EC_KEY_get0_group(key);
172     const BIGNUM *order = EC_GROUP_get0_order(group);
173     BN_CTX *ctx = NULL;
174     EC_POINT *pt = NULL;
175     BIGNUM *t = NULL;
176     BIGNUM *x1 = NULL;
177     const BIGNUM *r = NULL;
178     const BIGNUM *s = NULL;
179
180     ctx = BN_CTX_new();
181     pt = EC_POINT_new(group);
182     if (ctx == NULL || pt == NULL) {
183         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
184         goto done;
185     }
186
187     BN_CTX_start(ctx);
188     t = BN_CTX_get(ctx);
189     x1 = BN_CTX_get(ctx);
190     if (x1 == NULL) {
191         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
192         goto done;
193     }
194
195     /*
196      * B1: verify whether r' in [1,n-1], verification failed if not
197      * B2: vefify whether s' in [1,n-1], verification failed if not
198      * B3: set M'~=ZA || M'
199      * B4: calculate e'=Hv(M'~)
200      * B5: calculate t = (r' + s') modn, verification failed if t=0
201      * B6: calculate the point (x1', y1')=[s']G + [t]PA
202      * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
203      */
204
205     ECDSA_SIG_get0(sig, &r, &s);
206
207     if (BN_cmp(r, BN_value_one()) < 0
208             || BN_cmp(s, BN_value_one()) < 0
209             || BN_cmp(order, r) <= 0
210             || BN_cmp(order, s) <= 0) {
211         SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
212         goto done;
213     }
214
215     if (!BN_mod_add(t, r, s, order, ctx)) {
216         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
217         goto done;
218     }
219
220     if (BN_is_zero(t)) {
221         SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
222         goto done;
223     }
224
225     if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
226             || !EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx)) {
227         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
228         goto done;
229     }
230
231     if (!BN_mod_add(t, e, x1, order, ctx)) {
232         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
233         goto done;
234     }
235
236     if (BN_cmp(r, t) == 0)
237         ret = 1;
238
239  done:
240     EC_POINT_free(pt);
241     BN_CTX_free(ctx);
242     return ret;
243 }
244
245 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
246                        const EVP_MD *digest,
247                        const char *user_id, const uint8_t *msg, size_t msg_len)
248 {
249     BIGNUM *e = NULL;
250     ECDSA_SIG *sig = NULL;
251
252     e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
253     if (e == NULL) {
254         /* SM2err already called */
255         goto done;
256     }
257
258     sig = sm2_sig_gen(key, e);
259
260  done:
261     BN_free(e);
262     return sig;
263 }
264
265 int sm2_do_verify(const EC_KEY *key,
266                   const EVP_MD *digest,
267                   const ECDSA_SIG *sig,
268                   const char *user_id, const uint8_t *msg, size_t msg_len)
269 {
270     BIGNUM *e = NULL;
271     int ret = 0;
272
273     e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
274     if (e == NULL) {
275         /* SM2err already called */
276         goto done;
277     }
278
279     ret = sm2_sig_verify(key, sig, e);
280
281  done:
282     BN_free(e);
283     return ret;
284 }
285
286 int sm2_sign(const unsigned char *dgst, int dgstlen,
287              unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
288 {
289     BIGNUM *e = NULL;
290     ECDSA_SIG *s = NULL;
291     int sigleni;
292     int ret = -1;
293
294     e = BN_bin2bn(dgst, dgstlen, NULL);
295     if (e == NULL) {
296        SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
297        goto done;
298     }
299
300     s = sm2_sig_gen(eckey, e);
301
302     sigleni = i2d_ECDSA_SIG(s, &sig);
303     if (sigleni < 0) {
304        SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
305        goto done;
306     }
307     *siglen = (unsigned int)sigleni;
308
309     ret = 1;
310
311  done:
312     ECDSA_SIG_free(s);
313     BN_free(e);
314     return ret;
315 }
316
317 int sm2_verify(const unsigned char *dgst, int dgstlen,
318                const unsigned char *sig, int sig_len, EC_KEY *eckey)
319 {
320     ECDSA_SIG *s = NULL;
321     BIGNUM *e = NULL;
322     const unsigned char *p = sig;
323     unsigned char *der = NULL;
324     int derlen = -1;
325     int ret = -1;
326
327     s = ECDSA_SIG_new();
328     if (s == NULL) {
329         SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
330         goto done;
331     }
332     if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
333         SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
334         goto done;
335     }
336     /* Ensure signature uses DER and doesn't have trailing garbage */
337     derlen = i2d_ECDSA_SIG(s, &der);
338     if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
339         SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
340         goto done;
341     }
342
343     e = BN_bin2bn(dgst, dgstlen, NULL);
344     if (e == NULL) {
345         SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
346         goto done;
347     }
348
349     ret = sm2_sig_verify(eckey, s, e);
350
351  done:
352     OPENSSL_free(der);
353     BN_free(e);
354     ECDSA_SIG_free(s);
355     return ret;
356 }