Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/sm2/sm2_sign.c

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