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