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