Support setting SM2 ID
[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 int sm2_compute_z_digest(uint8_t *out,
22                          const EVP_MD *digest,
23                          const uint8_t *id,
24                          const size_t id_len,
25                          const EC_KEY *key)
26 {
27     int rc = 0;
28     const EC_GROUP *group = EC_KEY_get0_group(key);
29     BN_CTX *ctx = NULL;
30     EVP_MD_CTX *hash = NULL;
31     BIGNUM *p = NULL;
32     BIGNUM *a = NULL;
33     BIGNUM *b = NULL;
34     BIGNUM *xG = NULL;
35     BIGNUM *yG = NULL;
36     BIGNUM *xA = NULL;
37     BIGNUM *yA = NULL;
38     int p_bytes = 0;
39     uint8_t *buf = NULL;
40     uint16_t entl = 0;
41     uint8_t e_byte = 0;
42
43     hash = EVP_MD_CTX_new();
44     ctx = BN_CTX_new();
45     if (hash == NULL || ctx == NULL) {
46         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
47         goto done;
48     }
49
50     p = BN_CTX_get(ctx);
51     a = BN_CTX_get(ctx);
52     b = BN_CTX_get(ctx);
53     xG = BN_CTX_get(ctx);
54     yG = BN_CTX_get(ctx);
55     xA = BN_CTX_get(ctx);
56     yA = BN_CTX_get(ctx);
57
58     if (yA == NULL) {
59         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
60         goto done;
61     }
62
63     if (!EVP_DigestInit(hash, digest)) {
64         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
65         goto done;
66     }
67
68     /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
69
70     if (id_len >= (UINT16_MAX / 8)) {
71         /* too large */
72         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, SM2_R_ID_TOO_LARGE);
73         goto done;
74     }
75
76     entl = (uint16_t)(8 * id_len);
77
78     e_byte = entl >> 8;
79     if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
80         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
81         goto done;
82     }
83     e_byte = entl & 0xFF;
84     if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
85         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
86         goto done;
87     }
88
89     if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
90         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
91         goto done;
92     }
93
94     if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
95         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EC_LIB);
96         goto done;
97     }
98
99     p_bytes = BN_num_bytes(p);
100     buf = OPENSSL_zalloc(p_bytes);
101     if (buf == NULL) {
102         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
103         goto done;
104     }
105
106     if (BN_bn2binpad(a, buf, p_bytes) < 0
107             || !EVP_DigestUpdate(hash, buf, p_bytes)
108             || BN_bn2binpad(b, buf, p_bytes) < 0
109             || !EVP_DigestUpdate(hash, buf, p_bytes)
110             || !EC_POINT_get_affine_coordinates(group,
111                                                 EC_GROUP_get0_generator(group),
112                                                 xG, yG, ctx)
113             || BN_bn2binpad(xG, buf, p_bytes) < 0
114             || !EVP_DigestUpdate(hash, buf, p_bytes)
115             || BN_bn2binpad(yG, buf, p_bytes) < 0
116             || !EVP_DigestUpdate(hash, buf, p_bytes)
117             || !EC_POINT_get_affine_coordinates(group,
118                                                 EC_KEY_get0_public_key(key),
119                                                 xA, yA, ctx)
120             || BN_bn2binpad(xA, buf, p_bytes) < 0
121             || !EVP_DigestUpdate(hash, buf, p_bytes)
122             || BN_bn2binpad(yA, buf, p_bytes) < 0
123             || !EVP_DigestUpdate(hash, buf, p_bytes)
124             || !EVP_DigestFinal(hash, out, NULL)) {
125         SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_INTERNAL_ERROR);
126         goto done;
127     }
128
129     rc = 1;
130
131  done:
132     OPENSSL_free(buf);
133     BN_CTX_free(ctx);
134     EVP_MD_CTX_free(hash);
135     return rc;
136 }
137
138 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
139                                     const EC_KEY *key,
140                                     const uint8_t *id,
141                                     const size_t id_len,
142                                     const uint8_t *msg, size_t msg_len)
143 {
144     EVP_MD_CTX *hash = EVP_MD_CTX_new();
145     const int md_size = EVP_MD_size(digest);
146     uint8_t *z = NULL;
147     BIGNUM *e = NULL;
148
149     if (md_size < 0) {
150         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
151         goto done;
152     }
153
154     z = OPENSSL_zalloc(md_size);
155     if (hash == NULL || z == NULL) {
156         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
157         goto done;
158     }
159
160     if (!sm2_compute_z_digest(z, digest, id, id_len, key)) {
161         /* SM2err already called */
162         goto done;
163     }
164
165     if (!EVP_DigestInit(hash, digest)
166             || !EVP_DigestUpdate(hash, z, md_size)
167             || !EVP_DigestUpdate(hash, msg, msg_len)
168                /* reuse z buffer to hold H(Z || M) */
169             || !EVP_DigestFinal(hash, z, NULL)) {
170         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
171         goto done;
172     }
173
174     e = BN_bin2bn(z, md_size, NULL);
175     if (e == NULL)
176         SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
177
178  done:
179     OPENSSL_free(z);
180     EVP_MD_CTX_free(hash);
181     return e;
182 }
183
184 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
185 {
186     const BIGNUM *dA = EC_KEY_get0_private_key(key);
187     const EC_GROUP *group = EC_KEY_get0_group(key);
188     const BIGNUM *order = EC_GROUP_get0_order(group);
189     ECDSA_SIG *sig = NULL;
190     EC_POINT *kG = NULL;
191     BN_CTX *ctx = NULL;
192     BIGNUM *k = NULL;
193     BIGNUM *rk = NULL;
194     BIGNUM *r = NULL;
195     BIGNUM *s = NULL;
196     BIGNUM *x1 = NULL;
197     BIGNUM *tmp = NULL;
198
199     kG = EC_POINT_new(group);
200     ctx = BN_CTX_new();
201     if (kG == NULL || ctx == NULL) {
202         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
203         goto done;
204     }
205
206     BN_CTX_start(ctx);
207     k = BN_CTX_get(ctx);
208     rk = BN_CTX_get(ctx);
209     x1 = BN_CTX_get(ctx);
210     tmp = BN_CTX_get(ctx);
211     if (tmp == NULL) {
212         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
213         goto done;
214     }
215
216     /*
217      * These values are returned and so should not be allocated out of the
218      * context
219      */
220     r = BN_new();
221     s = BN_new();
222
223     if (r == NULL || s == NULL) {
224         SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
225         goto done;
226     }
227
228     for (;;) {
229         if (!BN_priv_rand_range(k, order)) {
230             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
231             goto done;
232         }
233
234         if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
235                 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
236                                                     ctx)
237                 || !BN_mod_add(r, e, x1, order, ctx)) {
238             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
239             goto done;
240         }
241
242         /* try again if r == 0 or r+k == n */
243         if (BN_is_zero(r))
244             continue;
245
246         if (!BN_add(rk, r, k)) {
247             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
248             goto done;
249         }
250
251         if (BN_cmp(rk, order) == 0)
252             continue;
253
254         if (!BN_add(s, dA, BN_value_one())
255                 || !ec_group_do_inverse_ord(group, s, s, ctx)
256                 || !BN_mod_mul(tmp, dA, r, order, ctx)
257                 || !BN_sub(tmp, k, tmp)
258                 || !BN_mod_mul(s, s, tmp, order, ctx)) {
259             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
260             goto done;
261         }
262
263         sig = ECDSA_SIG_new();
264         if (sig == NULL) {
265             SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
266             goto done;
267         }
268
269          /* takes ownership of r and s */
270         ECDSA_SIG_set0(sig, r, s);
271         break;
272     }
273
274  done:
275     if (sig == NULL) {
276         BN_free(r);
277         BN_free(s);
278     }
279
280     BN_CTX_free(ctx);
281     EC_POINT_free(kG);
282     return sig;
283 }
284
285 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
286                           const BIGNUM *e)
287 {
288     int ret = 0;
289     const EC_GROUP *group = EC_KEY_get0_group(key);
290     const BIGNUM *order = EC_GROUP_get0_order(group);
291     BN_CTX *ctx = NULL;
292     EC_POINT *pt = NULL;
293     BIGNUM *t = NULL;
294     BIGNUM *x1 = NULL;
295     const BIGNUM *r = NULL;
296     const BIGNUM *s = NULL;
297
298     ctx = BN_CTX_new();
299     pt = EC_POINT_new(group);
300     if (ctx == NULL || pt == NULL) {
301         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
302         goto done;
303     }
304
305     BN_CTX_start(ctx);
306     t = BN_CTX_get(ctx);
307     x1 = BN_CTX_get(ctx);
308     if (x1 == NULL) {
309         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
310         goto done;
311     }
312
313     /*
314      * B1: verify whether r' in [1,n-1], verification failed if not
315      * B2: vefify whether s' in [1,n-1], verification failed if not
316      * B3: set M'~=ZA || M'
317      * B4: calculate e'=Hv(M'~)
318      * B5: calculate t = (r' + s') modn, verification failed if t=0
319      * B6: calculate the point (x1', y1')=[s']G + [t]PA
320      * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
321      */
322
323     ECDSA_SIG_get0(sig, &r, &s);
324
325     if (BN_cmp(r, BN_value_one()) < 0
326             || BN_cmp(s, BN_value_one()) < 0
327             || BN_cmp(order, r) <= 0
328             || BN_cmp(order, s) <= 0) {
329         SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
330         goto done;
331     }
332
333     if (!BN_mod_add(t, r, s, order, ctx)) {
334         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
335         goto done;
336     }
337
338     if (BN_is_zero(t)) {
339         SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
340         goto done;
341     }
342
343     if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
344             || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
345         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
346         goto done;
347     }
348
349     if (!BN_mod_add(t, e, x1, order, ctx)) {
350         SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
351         goto done;
352     }
353
354     if (BN_cmp(r, t) == 0)
355         ret = 1;
356
357  done:
358     EC_POINT_free(pt);
359     BN_CTX_free(ctx);
360     return ret;
361 }
362
363 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
364                        const EVP_MD *digest,
365                        const uint8_t *id,
366                        const size_t id_len,
367                        const uint8_t *msg, size_t msg_len)
368 {
369     BIGNUM *e = NULL;
370     ECDSA_SIG *sig = NULL;
371
372     e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
373     if (e == NULL) {
374         /* SM2err already called */
375         goto done;
376     }
377
378     sig = sm2_sig_gen(key, e);
379
380  done:
381     BN_free(e);
382     return sig;
383 }
384
385 int sm2_do_verify(const EC_KEY *key,
386                   const EVP_MD *digest,
387                   const ECDSA_SIG *sig,
388                   const uint8_t *id,
389                   const size_t id_len,
390                   const uint8_t *msg, size_t msg_len)
391 {
392     BIGNUM *e = NULL;
393     int ret = 0;
394
395     e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
396     if (e == NULL) {
397         /* SM2err already called */
398         goto done;
399     }
400
401     ret = sm2_sig_verify(key, sig, e);
402
403  done:
404     BN_free(e);
405     return ret;
406 }
407
408 int sm2_sign(const unsigned char *dgst, int dgstlen,
409              unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
410 {
411     BIGNUM *e = NULL;
412     ECDSA_SIG *s = NULL;
413     int sigleni;
414     int ret = -1;
415
416     e = BN_bin2bn(dgst, dgstlen, NULL);
417     if (e == NULL) {
418        SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
419        goto done;
420     }
421
422     s = sm2_sig_gen(eckey, e);
423
424     sigleni = i2d_ECDSA_SIG(s, &sig);
425     if (sigleni < 0) {
426        SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
427        goto done;
428     }
429     *siglen = (unsigned int)sigleni;
430
431     ret = 1;
432
433  done:
434     ECDSA_SIG_free(s);
435     BN_free(e);
436     return ret;
437 }
438
439 int sm2_verify(const unsigned char *dgst, int dgstlen,
440                const unsigned char *sig, int sig_len, EC_KEY *eckey)
441 {
442     ECDSA_SIG *s = NULL;
443     BIGNUM *e = NULL;
444     const unsigned char *p = sig;
445     unsigned char *der = NULL;
446     int derlen = -1;
447     int ret = -1;
448
449     s = ECDSA_SIG_new();
450     if (s == NULL) {
451         SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
452         goto done;
453     }
454     if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
455         SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
456         goto done;
457     }
458     /* Ensure signature uses DER and doesn't have trailing garbage */
459     derlen = i2d_ECDSA_SIG(s, &der);
460     if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
461         SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
462         goto done;
463     }
464
465     e = BN_bin2bn(dgst, dgstlen, NULL);
466     if (e == NULL) {
467         SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
468         goto done;
469     }
470
471     ret = sm2_sig_verify(eckey, s, e);
472
473  done:
474     OPENSSL_free(der);
475     BN_free(e);
476     ECDSA_SIG_free(s);
477     return ret;
478 }