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