]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/sm2/sm2_sign.c
Add check for public key presence on sm2 signing
[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);
d6a8adec 31 const EC_POINT *pubkey = EC_KEY_get0_public_key(key);
5bd0abe7
PY
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;
4803717f 43 uint16_t entl = 0;
5bd0abe7
PY
44 uint8_t e_byte = 0;
45
d6a8adec
NH
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
5bd0abe7 52 hash = EVP_MD_CTX_new();
e077455e
RL
53 if (hash == NULL) {
54 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
55 goto done;
56 }
32ab57cb 57 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
e077455e
RL
58 if (ctx == NULL) {
59 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
5bd0abe7
PY
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) {
e077455e 72 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
5bd0abe7
PY
73 goto done;
74 }
75
76 if (!EVP_DigestInit(hash, digest)) {
9311d0c4 77 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
5bd0abe7
PY
78 goto done;
79 }
80
4803717f 81 /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
5bd0abe7 82
00433bad 83 if (id_len >= (UINT16_MAX / 8)) {
5bd0abe7 84 /* too large */
9311d0c4 85 ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
5bd0abe7
PY
86 goto done;
87 }
88
4803717f 89 entl = (uint16_t)(8 * id_len);
5bd0abe7 90
4803717f 91 e_byte = entl >> 8;
5bd0abe7 92 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
9311d0c4 93 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
5bd0abe7
PY
94 goto done;
95 }
4803717f
PY
96 e_byte = entl & 0xFF;
97 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
9311d0c4 98 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
4803717f
PY
99 goto done;
100 }
101
102 if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
9311d0c4 103 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
5bd0abe7
PY
104 goto done;
105 }
106
107 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
9311d0c4 108 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
5bd0abe7
PY
109 goto done;
110 }
111
112 p_bytes = BN_num_bytes(p);
113 buf = OPENSSL_zalloc(p_bytes);
e077455e 114 if (buf == NULL)
5bd0abe7 115 goto done;
5bd0abe7
PY
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,
d6a8adec 129 pubkey,
5bd0abe7
PY
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)) {
9311d0c4 136 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
5bd0abe7
PY
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
2167239a 149static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
2398404e 150 const EC_KEY *key,
00433bad
PY
151 const uint8_t *id,
152 const size_t id_len,
2398404e 153 const uint8_t *msg, size_t msg_len)
3d328a44
JL
154{
155 EVP_MD_CTX *hash = EVP_MD_CTX_new();
ed576acd 156 const int md_size = EVP_MD_get_size(digest);
4803717f 157 uint8_t *z = NULL;
3d328a44 158 BIGNUM *e = NULL;
5ccada09 159 EVP_MD *fetched_digest = NULL;
32ab57cb
SL
160 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
161 const char *propq = ossl_ec_key_get0_propq(key);
3d328a44 162
3e0076c2 163 if (md_size < 0) {
9311d0c4 164 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
3d328a44 165 goto done;
245be530 166 }
e077455e
RL
167 if (hash == NULL) {
168 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
169 goto done;
170 }
3d328a44 171
4803717f 172 z = OPENSSL_zalloc(md_size);
e077455e 173 if (z == NULL)
3d328a44
JL
174 goto done;
175
ed576acd 176 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
5ccada09 177 if (fetched_digest == NULL) {
9311d0c4 178 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
5ccada09
SL
179 goto done;
180 }
181
32ab57cb 182 if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
245be530 183 /* SM2err already called */
3d328a44 184 goto done;
245be530 185 }
3d328a44 186
5ccada09 187 if (!EVP_DigestInit(hash, fetched_digest)
4803717f 188 || !EVP_DigestUpdate(hash, z, md_size)
245be530 189 || !EVP_DigestUpdate(hash, msg, msg_len)
4803717f
PY
190 /* reuse z buffer to hold H(Z || M) */
191 || !EVP_DigestFinal(hash, z, NULL)) {
9311d0c4 192 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
3d328a44 193 goto done;
245be530 194 }
3d328a44 195
4803717f 196 e = BN_bin2bn(z, md_size, NULL);
245be530 197 if (e == NULL)
9311d0c4 198 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
3d328a44
JL
199
200 done:
5ccada09 201 EVP_MD_free(fetched_digest);
4803717f 202 OPENSSL_free(z);
3d328a44
JL
203 EVP_MD_CTX_free(hash);
204 return e;
205}
206
2167239a 207static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
3d328a44
JL
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);
3d328a44
JL
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;
32ab57cb 221 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
3d328a44
JL
222
223 kG = EC_POINT_new(group);
e077455e
RL
224 if (kG == NULL) {
225 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
226 goto done;
227 }
5ccada09 228 ctx = BN_CTX_new_ex(libctx);
e077455e
RL
229 if (ctx == NULL) {
230 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
3d328a44 231 goto done;
245be530 232 }
3d328a44 233
245be530 234 BN_CTX_start(ctx);
3d328a44
JL
235 k = BN_CTX_get(ctx);
236 rk = BN_CTX_get(ctx);
237 x1 = BN_CTX_get(ctx);
238 tmp = BN_CTX_get(ctx);
245be530 239 if (tmp == NULL) {
e077455e 240 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
3d328a44 241 goto done;
245be530 242 }
3d328a44 243
245be530
MC
244 /*
245 * These values are returned and so should not be allocated out of the
246 * context
247 */
3d328a44
JL
248 r = BN_new();
249 s = BN_new();
250
245be530 251 if (r == NULL || s == NULL) {
e077455e 252 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
3d328a44 253 goto done;
245be530 254 }
3d328a44 255
e81c81c9
MY
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 */
3d328a44 265 for (;;) {
5cbd2ea3 266 if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
9311d0c4 267 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
9e4c9777 268 goto done;
245be530 269 }
3d328a44 270
245be530 271 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
9cc570d4 272 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
5bd0abe7 273 ctx)
245be530 274 || !BN_mod_add(r, e, x1, order, ctx)) {
9311d0c4 275 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
3d328a44 276 goto done;
245be530 277 }
3d328a44
JL
278
279 /* try again if r == 0 or r+k == n */
280 if (BN_is_zero(r))
281 continue;
282
245be530 283 if (!BN_add(rk, r, k)) {
9311d0c4 284 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
245be530
MC
285 goto done;
286 }
3d328a44
JL
287
288 if (BN_cmp(rk, order) == 0)
289 continue;
290
245be530 291 if (!BN_add(s, dA, BN_value_one())
32ab57cb 292 || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
245be530
MC
293 || !BN_mod_mul(tmp, dA, r, order, ctx)
294 || !BN_sub(tmp, k, tmp)
295 || !BN_mod_mul(s, s, tmp, order, ctx)) {
9311d0c4 296 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
245be530
MC
297 goto done;
298 }
3d328a44 299
e81c81c9
MY
300 /* try again if s == 0 */
301 if (BN_is_zero(s))
302 continue;
303
3d328a44 304 sig = ECDSA_SIG_new();
245be530 305 if (sig == NULL) {
e077455e 306 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
3d328a44 307 goto done;
245be530 308 }
3d328a44
JL
309
310 /* takes ownership of r and s */
311 ECDSA_SIG_set0(sig, r, s);
312 break;
313 }
314
315 done:
3d328a44
JL
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;
3d328a44
JL
324}
325
2167239a 326static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
245be530 327 const BIGNUM *e)
3d328a44
JL
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;
3d328a44
JL
334 BIGNUM *t = NULL;
335 BIGNUM *x1 = NULL;
336 const BIGNUM *r = NULL;
337 const BIGNUM *s = NULL;
32ab57cb 338 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
3d328a44 339
5ccada09 340 ctx = BN_CTX_new_ex(libctx);
3d328a44 341 pt = EC_POINT_new(group);
245be530 342 if (ctx == NULL || pt == NULL) {
e077455e 343 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
3d328a44 344 goto done;
245be530 345 }
3d328a44
JL
346
347 BN_CTX_start(ctx);
3d328a44
JL
348 t = BN_CTX_get(ctx);
349 x1 = BN_CTX_get(ctx);
245be530 350 if (x1 == NULL) {
e077455e 351 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
245be530
MC
352 goto done;
353 }
3d328a44
JL
354
355 /*
245be530 356 * B1: verify whether r' in [1,n-1], verification failed if not
c2969ff6 357 * B2: verify whether s' in [1,n-1], verification failed if not
245be530
MC
358 * B3: set M'~=ZA || M'
359 * B4: calculate e'=Hv(M'~)
360 * B5: calculate t = (r' + s') modn, verification failed if t=0
361 * B6: calculate the point (x1', y1')=[s']G + [t]PA
c2969ff6 362 * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
3d328a44
JL
363 */
364
365 ECDSA_SIG_get0(sig, &r, &s);
366
245be530
MC
367 if (BN_cmp(r, BN_value_one()) < 0
368 || BN_cmp(s, BN_value_one()) < 0
369 || BN_cmp(order, r) <= 0
370 || BN_cmp(order, s) <= 0) {
9311d0c4 371 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
3d328a44 372 goto done;
245be530 373 }
3d328a44 374
245be530 375 if (!BN_mod_add(t, r, s, order, ctx)) {
9311d0c4 376 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
3d328a44 377 goto done;
245be530 378 }
3d328a44 379
245be530 380 if (BN_is_zero(t)) {
9311d0c4 381 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
3d328a44 382 goto done;
245be530 383 }
3d328a44 384
245be530 385 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
9cc570d4 386 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
9311d0c4 387 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
3d328a44 388 goto done;
245be530 389 }
3d328a44 390
245be530 391 if (!BN_mod_add(t, e, x1, order, ctx)) {
9311d0c4 392 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
3d328a44 393 goto done;
245be530 394 }
3d328a44
JL
395
396 if (BN_cmp(r, t) == 0)
397 ret = 1;
398
399 done:
050dddb0 400 BN_CTX_end(ctx);
3d328a44
JL
401 EC_POINT_free(pt);
402 BN_CTX_free(ctx);
403 return ret;
404}
405
32ab57cb
SL
406ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
407 const EVP_MD *digest,
408 const uint8_t *id,
409 const size_t id_len,
410 const uint8_t *msg, size_t msg_len)
3d328a44
JL
411{
412 BIGNUM *e = NULL;
413 ECDSA_SIG *sig = NULL;
414
00433bad 415 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
245be530
MC
416 if (e == NULL) {
417 /* SM2err already called */
3d328a44 418 goto done;
245be530 419 }
3d328a44 420
2167239a 421 sig = sm2_sig_gen(key, e);
3d328a44
JL
422
423 done:
424 BN_free(e);
425 return sig;
426}
427
32ab57cb
SL
428int ossl_sm2_do_verify(const EC_KEY *key,
429 const EVP_MD *digest,
430 const ECDSA_SIG *sig,
431 const uint8_t *id,
432 const size_t id_len,
433 const uint8_t *msg, size_t msg_len)
3d328a44
JL
434{
435 BIGNUM *e = NULL;
245be530 436 int ret = 0;
3d328a44 437
00433bad 438 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
245be530
MC
439 if (e == NULL) {
440 /* SM2err already called */
3d328a44 441 goto done;
245be530 442 }
3d328a44 443
2167239a 444 ret = sm2_sig_verify(key, sig, e);
3d328a44
JL
445
446 done:
447 BN_free(e);
448 return ret;
449}
450
32ab57cb
SL
451int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
452 unsigned char *sig, unsigned int *siglen,
453 EC_KEY *eckey)
3d328a44
JL
454{
455 BIGNUM *e = NULL;
456 ECDSA_SIG *s = NULL;
245be530 457 int sigleni;
3d328a44
JL
458 int ret = -1;
459
1fa2bf9b
BE
460 if (sig == NULL) {
461 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
462 goto done;
463 }
464
3d328a44 465 e = BN_bin2bn(dgst, dgstlen, NULL);
245be530 466 if (e == NULL) {
9311d0c4 467 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
245be530
MC
468 goto done;
469 }
3d328a44 470
2167239a 471 s = sm2_sig_gen(eckey, e);
5ccada09 472 if (s == NULL) {
9311d0c4 473 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
5ccada09
SL
474 goto done;
475 }
3d328a44 476
1fa2bf9b 477 sigleni = i2d_ECDSA_SIG(s, &sig);
245be530 478 if (sigleni < 0) {
9311d0c4 479 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
245be530
MC
480 goto done;
481 }
482 *siglen = (unsigned int)sigleni;
3d328a44 483
67cc2bae 484 ret = 1;
3d328a44
JL
485
486 done:
487 ECDSA_SIG_free(s);
488 BN_free(e);
489 return ret;
490}
491
32ab57cb
SL
492int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
493 const unsigned char *sig, int sig_len,
494 EC_KEY *eckey)
3d328a44
JL
495{
496 ECDSA_SIG *s = NULL;
497 BIGNUM *e = NULL;
498 const unsigned char *p = sig;
499 unsigned char *der = NULL;
500 int derlen = -1;
501 int ret = -1;
502
3d328a44 503 s = ECDSA_SIG_new();
245be530 504 if (s == NULL) {
e077455e 505 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
3d328a44 506 goto done;
245be530
MC
507 }
508 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
9311d0c4 509 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
3d328a44 510 goto done;
245be530 511 }
3d328a44
JL
512 /* Ensure signature uses DER and doesn't have trailing garbage */
513 derlen = i2d_ECDSA_SIG(s, &der);
245be530 514 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
9311d0c4 515 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
3d328a44 516 goto done;
245be530 517 }
3d328a44
JL
518
519 e = BN_bin2bn(dgst, dgstlen, NULL);
245be530 520 if (e == NULL) {
9311d0c4 521 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
245be530
MC
522 goto done;
523 }
3d328a44 524
2167239a 525 ret = sm2_sig_verify(eckey, s, e);
3d328a44
JL
526
527 done:
528 OPENSSL_free(der);
529 BN_free(e);
530 ECDSA_SIG_free(s);
531 return ret;
532}