1 /*
2 * Copyright 2017-2024 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 Apache License 2.0 (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/deprecated.h"
13
14 #include "crypto/sm2.h"
15 #include "crypto/sm2err.h"
16 #include "crypto/ec.h" /* ossl_ec_group_do_inverse_ord() */
17 #include "internal/numbers.h"
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/bn.h>
21 #include <string.h>
22
ossl_sm2_compute_z_digest(uint8_t * out,const EVP_MD * digest,const uint8_t * id,const size_t id_len,const EC_KEY * key)23 int ossl_sm2_compute_z_digest(uint8_t *out,
24 const EVP_MD *digest,
25 const uint8_t *id,
26 const size_t id_len,
27 const EC_KEY *key)
28 {
29 int rc = 0;
30 const EC_GROUP *group = EC_KEY_get0_group(key);
31 const EC_POINT *pubkey = EC_KEY_get0_public_key(key);
32 BN_CTX *ctx = NULL;
33 EVP_MD_CTX *hash = NULL;
34 BIGNUM *p = NULL;
35 BIGNUM *a = NULL;
36 BIGNUM *b = NULL;
37 BIGNUM *xG = NULL;
38 BIGNUM *yG = NULL;
39 BIGNUM *xA = NULL;
40 BIGNUM *yA = NULL;
41 int p_bytes = 0;
42 uint8_t *buf = NULL;
43 uint16_t entl = 0;
44 uint8_t e_byte = 0;
45
46 /* SM2 Signatures require a public key, check for it */
47 if (pubkey == NULL) {
48 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
49 goto done;
50 }
51
52 hash = EVP_MD_CTX_new();
53 if (hash == NULL) {
54 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
55 goto done;
56 }
57 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
58 if (ctx == NULL) {
59 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
60 goto done;
61 }
62
63 p = BN_CTX_get(ctx);
64 a = BN_CTX_get(ctx);
65 b = BN_CTX_get(ctx);
66 xG = BN_CTX_get(ctx);
67 yG = BN_CTX_get(ctx);
68 xA = BN_CTX_get(ctx);
69 yA = BN_CTX_get(ctx);
70
71 if (yA == NULL) {
72 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
73 goto done;
74 }
75
76 if (!EVP_DigestInit(hash, digest)) {
77 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
78 goto done;
79 }
80
81 /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
82
83 if (id_len >= (UINT16_MAX / 8)) {
84 /* too large */
85 ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
86 goto done;
87 }
88
89 entl = (uint16_t)(8 * id_len);
90
91 e_byte = entl >> 8;
92 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
93 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
94 goto done;
95 }
96 e_byte = entl & 0xFF;
97 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
98 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
99 goto done;
100 }
101
102 if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
103 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
104 goto done;
105 }
106
107 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
108 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
109 goto done;
110 }
111
112 p_bytes = BN_num_bytes(p);
113 buf = OPENSSL_zalloc(p_bytes);
114 if (buf == NULL)
115 goto done;
116
117 if (BN_bn2binpad(a, buf, p_bytes) < 0
118 || !EVP_DigestUpdate(hash, buf, p_bytes)
119 || BN_bn2binpad(b, buf, p_bytes) < 0
120 || !EVP_DigestUpdate(hash, buf, p_bytes)
121 || !EC_POINT_get_affine_coordinates(group,
122 EC_GROUP_get0_generator(group),
123 xG, yG, ctx)
124 || BN_bn2binpad(xG, buf, p_bytes) < 0
125 || !EVP_DigestUpdate(hash, buf, p_bytes)
126 || BN_bn2binpad(yG, buf, p_bytes) < 0
127 || !EVP_DigestUpdate(hash, buf, p_bytes)
128 || !EC_POINT_get_affine_coordinates(group,
129 pubkey,
130 xA, yA, ctx)
131 || BN_bn2binpad(xA, buf, p_bytes) < 0
132 || !EVP_DigestUpdate(hash, buf, p_bytes)
133 || BN_bn2binpad(yA, buf, p_bytes) < 0
134 || !EVP_DigestUpdate(hash, buf, p_bytes)
135 || !EVP_DigestFinal(hash, out, NULL)) {
136 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
137 goto done;
138 }
139
140 rc = 1;
141
142 done:
143 OPENSSL_free(buf);
144 BN_CTX_free(ctx);
145 EVP_MD_CTX_free(hash);
146 return rc;
147 }
148
sm2_compute_msg_hash(const EVP_MD * digest,const EC_KEY * key,const uint8_t * id,const size_t id_len,const uint8_t * msg,size_t msg_len)149 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
150 const EC_KEY *key,
151 const uint8_t *id,
152 const size_t id_len,
153 const uint8_t *msg, size_t msg_len)
154 {
155 EVP_MD_CTX *hash = EVP_MD_CTX_new();
156 const int md_size = EVP_MD_get_size(digest);
157 uint8_t *z = NULL;
158 BIGNUM *e = NULL;
159 EVP_MD *fetched_digest = NULL;
160 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
161 const char *propq = ossl_ec_key_get0_propq(key);
162
163 if (md_size <= 0) {
164 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
165 goto done;
166 }
167 if (hash == NULL) {
168 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
169 goto done;
170 }
171
172 z = OPENSSL_zalloc(md_size);
173 if (z == NULL)
174 goto done;
175
176 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
177 if (fetched_digest == NULL) {
178 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
179 goto done;
180 }
181
182 if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
183 /* SM2err already called */
184 goto done;
185 }
186
187 if (!EVP_DigestInit(hash, fetched_digest)
188 || !EVP_DigestUpdate(hash, z, md_size)
189 || !EVP_DigestUpdate(hash, msg, msg_len)
190 /* reuse z buffer to hold H(Z || M) */
191 || !EVP_DigestFinal(hash, z, NULL)) {
192 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
193 goto done;
194 }
195
196 e = BN_bin2bn(z, md_size, NULL);
197 if (e == NULL)
198 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
199
200 done:
201 EVP_MD_free(fetched_digest);
202 OPENSSL_free(z);
203 EVP_MD_CTX_free(hash);
204 return e;
205 }
206
sm2_sig_gen(const EC_KEY * key,const BIGNUM * e)207 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
208 {
209 const BIGNUM *dA = EC_KEY_get0_private_key(key);
210 const EC_GROUP *group = EC_KEY_get0_group(key);
211 const BIGNUM *order = EC_GROUP_get0_order(group);
212 ECDSA_SIG *sig = NULL;
213 EC_POINT *kG = NULL;
214 BN_CTX *ctx = NULL;
215 BIGNUM *k = NULL;
216 BIGNUM *rk = NULL;
217 BIGNUM *r = NULL;
218 BIGNUM *s = NULL;
219 BIGNUM *x1 = NULL;
220 BIGNUM *tmp = NULL;
221 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
222
223 kG = EC_POINT_new(group);
224 if (kG == NULL) {
225 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
226 goto done;
227 }
228 ctx = BN_CTX_new_ex(libctx);
229 if (ctx == NULL) {
230 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
231 goto done;
232 }
233
234 BN_CTX_start(ctx);
235 k = BN_CTX_get(ctx);
236 rk = BN_CTX_get(ctx);
237 x1 = BN_CTX_get(ctx);
238 tmp = BN_CTX_get(ctx);
239 if (tmp == NULL) {
240 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
241 goto done;
242 }
243
244 /*
245 * These values are returned and so should not be allocated out of the
246 * context
247 */
248 r = BN_new();
249 s = BN_new();
250
251 if (r == NULL || s == NULL) {
252 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
253 goto done;
254 }
255
256 /*
257 * A3: Generate a random number k in [1,n-1] using random number generators;
258 * A4: Compute (x1,y1)=[k]G, and convert the type of data x1 to be integer
259 * as specified in clause 4.2.8 of GM/T 0003.1-2012;
260 * A5: Compute r=(e+x1) mod n. If r=0 or r+k=n, then go to A3;
261 * A6: Compute s=(1/(1+dA)*(k-r*dA)) mod n. If s=0, then go to A3;
262 * A7: Convert the type of data (r,s) to be bit strings according to the details
263 * in clause 4.2.2 of GM/T 0003.1-2012. Then the signature of message M is (r,s).
264 */
265 for (;;) {
266 if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
267 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
268 goto done;
269 }
270
271 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
272 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
273 ctx)
274 || !BN_mod_add(r, e, x1, order, ctx)) {
275 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
276 goto done;
277 }
278
279 /* try again if r == 0 or r+k == n */
280 if (BN_is_zero(r))
281 continue;
282
283 if (!BN_add(rk, r, k)) {
284 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
285 goto done;
286 }
287
288 if (BN_cmp(rk, order) == 0)
289 continue;
290
291 if (!BN_add(s, dA, BN_value_one())
292 || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
293 || !BN_mod_mul(tmp, dA, r, order, ctx)
294 || !BN_sub(tmp, k, tmp)
295 || !BN_mod_mul(s, s, tmp, order, ctx)) {
296 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
297 goto done;
298 }
299
300 /* try again if s == 0 */
301 if (BN_is_zero(s))
302 continue;
303
304 sig = ECDSA_SIG_new();
305 if (sig == NULL) {
306 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
307 goto done;
308 }
309
310 /* takes ownership of r and s */
311 ECDSA_SIG_set0(sig, r, s);
312 break;
313 }
314
315 done:
316 if (sig == NULL) {
317 BN_free(r);
318 BN_free(s);
319 }
320
321 BN_CTX_free(ctx);
322 EC_POINT_free(kG);
323 return sig;
324 }
325
sm2_sig_verify(const EC_KEY * key,const ECDSA_SIG * sig,const BIGNUM * e)326 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
327 const BIGNUM *e)
328 {
329 int ret = 0;
330 const EC_GROUP *group = EC_KEY_get0_group(key);
331 const BIGNUM *order = EC_GROUP_get0_order(group);
332 BN_CTX *ctx = NULL;
333 EC_POINT *pt = NULL;
334 BIGNUM *t = NULL;
335 BIGNUM *x1 = NULL;
336 const BIGNUM *r = NULL;
337 const BIGNUM *s = NULL;
338 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
339
340 ctx = BN_CTX_new_ex(libctx);
341 if (ctx == NULL) {
342 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
343 goto done;
344 }
345 BN_CTX_start(ctx);
346 t = BN_CTX_get(ctx);
347 x1 = BN_CTX_get(ctx);
348 if (x1 == NULL) {
349 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
350 goto done;
351 }
352
353 pt = EC_POINT_new(group);
354 if (pt == NULL) {
355 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
356 goto done;
357 }
358
359 /*
360 * B1: verify whether r' in [1,n-1], verification failed if not
361 * B2: verify whether s' in [1,n-1], verification failed if not
362 * B3: set M'~=ZA || M'
363 * B4: calculate e'=Hv(M'~)
364 * B5: calculate t = (r' + s') modn, verification failed if t=0
365 * B6: calculate the point (x1', y1')=[s']G + [t]PA
366 * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
367 */
368
369 ECDSA_SIG_get0(sig, &r, &s);
370
371 if (BN_cmp(r, BN_value_one()) < 0
372 || BN_cmp(s, BN_value_one()) < 0
373 || BN_cmp(order, r) <= 0
374 || BN_cmp(order, s) <= 0) {
375 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
376 goto done;
377 }
378
379 if (!BN_mod_add(t, r, s, order, ctx)) {
380 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
381 goto done;
382 }
383
384 if (BN_is_zero(t)) {
385 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
386 goto done;
387 }
388
389 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
390 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
391 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
392 goto done;
393 }
394
395 if (!BN_mod_add(t, e, x1, order, ctx)) {
396 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
397 goto done;
398 }
399
400 if (BN_cmp(r, t) == 0)
401 ret = 1;
402
403 done:
404 BN_CTX_end(ctx);
405 EC_POINT_free(pt);
406 BN_CTX_free(ctx);
407 return ret;
408 }
409
ossl_sm2_do_sign(const EC_KEY * key,const EVP_MD * digest,const uint8_t * id,const size_t id_len,const uint8_t * msg,size_t msg_len)410 ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
411 const EVP_MD *digest,
412 const uint8_t *id,
413 const size_t id_len,
414 const uint8_t *msg, size_t msg_len)
415 {
416 BIGNUM *e = NULL;
417 ECDSA_SIG *sig = NULL;
418
419 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
420 if (e == NULL) {
421 /* SM2err already called */
422 goto done;
423 }
424
425 sig = sm2_sig_gen(key, e);
426
427 done:
428 BN_free(e);
429 return sig;
430 }
431
ossl_sm2_do_verify(const EC_KEY * key,const EVP_MD * digest,const ECDSA_SIG * sig,const uint8_t * id,const size_t id_len,const uint8_t * msg,size_t msg_len)432 int ossl_sm2_do_verify(const EC_KEY *key,
433 const EVP_MD *digest,
434 const ECDSA_SIG *sig,
435 const uint8_t *id,
436 const size_t id_len,
437 const uint8_t *msg, size_t msg_len)
438 {
439 BIGNUM *e = NULL;
440 int ret = 0;
441
442 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
443 if (e == NULL) {
444 /* SM2err already called */
445 goto done;
446 }
447
448 ret = sm2_sig_verify(key, sig, e);
449
450 done:
451 BN_free(e);
452 return ret;
453 }
454
ossl_sm2_internal_sign(const unsigned char * dgst,int dgstlen,unsigned char * sig,unsigned int * siglen,EC_KEY * eckey)455 int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
456 unsigned char *sig, unsigned int *siglen,
457 EC_KEY *eckey)
458 {
459 BIGNUM *e = NULL;
460 ECDSA_SIG *s = NULL;
461 int sigleni;
462 int ret = -1;
463
464 if (sig == NULL) {
465 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
466 goto done;
467 }
468
469 e = BN_bin2bn(dgst, dgstlen, NULL);
470 if (e == NULL) {
471 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
472 goto done;
473 }
474
475 s = sm2_sig_gen(eckey, e);
476 if (s == NULL) {
477 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
478 goto done;
479 }
480
481 sigleni = i2d_ECDSA_SIG(s, &sig);
482 if (sigleni < 0) {
483 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
484 goto done;
485 }
486 *siglen = (unsigned int)sigleni;
487
488 ret = 1;
489
490 done:
491 ECDSA_SIG_free(s);
492 BN_free(e);
493 return ret;
494 }
495
ossl_sm2_internal_verify(const unsigned char * dgst,int dgstlen,const unsigned char * sig,int sig_len,EC_KEY * eckey)496 int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
497 const unsigned char *sig, int sig_len,
498 EC_KEY *eckey)
499 {
500 ECDSA_SIG *s = NULL;
501 BIGNUM *e = NULL;
502 const unsigned char *p = sig;
503 unsigned char *der = NULL;
504 int derlen = -1;
505 int ret = -1;
506
507 s = ECDSA_SIG_new();
508 if (s == NULL) {
509 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
510 goto done;
511 }
512 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
513 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
514 goto done;
515 }
516 /* Ensure signature uses DER and doesn't have trailing garbage */
517 derlen = i2d_ECDSA_SIG(s, &der);
518 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
519 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
520 goto done;
521 }
522
523 e = BN_bin2bn(dgst, dgstlen, NULL);
524 if (e == NULL) {
525 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
526 goto done;
527 }
528
529 ret = sm2_sig_verify(eckey, s, e);
530
531 done:
532 OPENSSL_free(der);
533 BN_free(e);
534 ECDSA_SIG_free(s);
535 return ret;
536 }
537