]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/sm2/sm2_sign.c
Following the license change, modify the boilerplates in crypto/smN/
[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
e425f90f 12#include "internal/sm2.h"
2398404e 13#include "internal/sm2err.h"
469c2c4a 14#include "internal/ec_int.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();
45 ctx = BN_CTX_new();
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
JL
148 BIGNUM *e = NULL;
149
3e0076c2
MC
150 if (md_size < 0) {
151 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
3d328a44 152 goto done;
245be530 153 }
3d328a44 154
4803717f
PY
155 z = OPENSSL_zalloc(md_size);
156 if (hash == NULL || z == NULL) {
3e0076c2 157 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
3d328a44 158 goto done;
245be530 159 }
3d328a44 160
4803717f 161 if (!sm2_compute_z_digest(z, digest, id, id_len, key)) {
245be530 162 /* SM2err already called */
3d328a44 163 goto done;
245be530 164 }
3d328a44 165
245be530 166 if (!EVP_DigestInit(hash, digest)
4803717f 167 || !EVP_DigestUpdate(hash, z, md_size)
245be530 168 || !EVP_DigestUpdate(hash, msg, msg_len)
4803717f
PY
169 /* reuse z buffer to hold H(Z || M) */
170 || !EVP_DigestFinal(hash, z, NULL)) {
2398404e 171 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
3d328a44 172 goto done;
245be530 173 }
3d328a44 174
4803717f 175 e = BN_bin2bn(z, md_size, NULL);
245be530
MC
176 if (e == NULL)
177 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
3d328a44
JL
178
179 done:
4803717f 180 OPENSSL_free(z);
3d328a44
JL
181 EVP_MD_CTX_free(hash);
182 return e;
183}
184
2167239a 185static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
3d328a44
JL
186{
187 const BIGNUM *dA = EC_KEY_get0_private_key(key);
188 const EC_GROUP *group = EC_KEY_get0_group(key);
189 const BIGNUM *order = EC_GROUP_get0_order(group);
3d328a44
JL
190 ECDSA_SIG *sig = NULL;
191 EC_POINT *kG = NULL;
192 BN_CTX *ctx = NULL;
193 BIGNUM *k = NULL;
194 BIGNUM *rk = NULL;
195 BIGNUM *r = NULL;
196 BIGNUM *s = NULL;
197 BIGNUM *x1 = NULL;
198 BIGNUM *tmp = NULL;
199
200 kG = EC_POINT_new(group);
3d328a44 201 ctx = BN_CTX_new();
245be530 202 if (kG == NULL || ctx == NULL) {
2398404e 203 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
3d328a44 204 goto done;
245be530 205 }
3d328a44 206
245be530 207 BN_CTX_start(ctx);
3d328a44
JL
208 k = BN_CTX_get(ctx);
209 rk = BN_CTX_get(ctx);
210 x1 = BN_CTX_get(ctx);
211 tmp = BN_CTX_get(ctx);
245be530
MC
212 if (tmp == NULL) {
213 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
3d328a44 214 goto done;
245be530 215 }
3d328a44 216
245be530
MC
217 /*
218 * These values are returned and so should not be allocated out of the
219 * context
220 */
3d328a44
JL
221 r = BN_new();
222 s = BN_new();
223
245be530 224 if (r == NULL || s == NULL) {
2398404e 225 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
3d328a44 226 goto done;
245be530 227 }
3d328a44
JL
228
229 for (;;) {
245be530
MC
230 if (!BN_priv_rand_range(k, order)) {
231 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
9e4c9777 232 goto done;
245be530 233 }
3d328a44 234
245be530 235 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
9cc570d4 236 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
5bd0abe7 237 ctx)
245be530
MC
238 || !BN_mod_add(r, e, x1, order, ctx)) {
239 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
3d328a44 240 goto done;
245be530 241 }
3d328a44
JL
242
243 /* try again if r == 0 or r+k == n */
244 if (BN_is_zero(r))
245 continue;
246
245be530
MC
247 if (!BN_add(rk, r, k)) {
248 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
249 goto done;
250 }
3d328a44
JL
251
252 if (BN_cmp(rk, order) == 0)
253 continue;
254
245be530 255 if (!BN_add(s, dA, BN_value_one())
469c2c4a 256 || !ec_group_do_inverse_ord(group, s, s, ctx)
245be530
MC
257 || !BN_mod_mul(tmp, dA, r, order, ctx)
258 || !BN_sub(tmp, k, tmp)
259 || !BN_mod_mul(s, s, tmp, order, ctx)) {
260 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
261 goto done;
262 }
3d328a44
JL
263
264 sig = ECDSA_SIG_new();
245be530 265 if (sig == NULL) {
2398404e 266 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
3d328a44 267 goto done;
245be530 268 }
3d328a44
JL
269
270 /* takes ownership of r and s */
271 ECDSA_SIG_set0(sig, r, s);
272 break;
273 }
274
275 done:
3d328a44
JL
276 if (sig == NULL) {
277 BN_free(r);
278 BN_free(s);
279 }
280
281 BN_CTX_free(ctx);
282 EC_POINT_free(kG);
283 return sig;
3d328a44
JL
284}
285
2167239a 286static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
245be530 287 const BIGNUM *e)
3d328a44
JL
288{
289 int ret = 0;
290 const EC_GROUP *group = EC_KEY_get0_group(key);
291 const BIGNUM *order = EC_GROUP_get0_order(group);
292 BN_CTX *ctx = NULL;
293 EC_POINT *pt = NULL;
3d328a44
JL
294 BIGNUM *t = NULL;
295 BIGNUM *x1 = NULL;
296 const BIGNUM *r = NULL;
297 const BIGNUM *s = NULL;
298
299 ctx = BN_CTX_new();
3d328a44 300 pt = EC_POINT_new(group);
245be530 301 if (ctx == NULL || pt == NULL) {
2398404e 302 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
3d328a44 303 goto done;
245be530 304 }
3d328a44
JL
305
306 BN_CTX_start(ctx);
3d328a44
JL
307 t = BN_CTX_get(ctx);
308 x1 = BN_CTX_get(ctx);
245be530
MC
309 if (x1 == NULL) {
310 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
311 goto done;
312 }
3d328a44
JL
313
314 /*
245be530
MC
315 * B1: verify whether r' in [1,n-1], verification failed if not
316 * B2: vefify whether s' in [1,n-1], verification failed if not
317 * B3: set M'~=ZA || M'
318 * B4: calculate e'=Hv(M'~)
319 * B5: calculate t = (r' + s') modn, verification failed if t=0
320 * B6: calculate the point (x1', y1')=[s']G + [t]PA
321 * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
3d328a44
JL
322 */
323
324 ECDSA_SIG_get0(sig, &r, &s);
325
245be530
MC
326 if (BN_cmp(r, BN_value_one()) < 0
327 || BN_cmp(s, BN_value_one()) < 0
328 || BN_cmp(order, r) <= 0
329 || BN_cmp(order, s) <= 0) {
2398404e 330 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
3d328a44 331 goto done;
245be530 332 }
3d328a44 333
245be530 334 if (!BN_mod_add(t, r, s, order, ctx)) {
2398404e 335 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
3d328a44 336 goto done;
245be530 337 }
3d328a44 338
245be530 339 if (BN_is_zero(t)) {
2398404e 340 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
3d328a44 341 goto done;
245be530 342 }
3d328a44 343
245be530 344 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
9cc570d4 345 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
2398404e 346 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
3d328a44 347 goto done;
245be530 348 }
3d328a44 349
245be530 350 if (!BN_mod_add(t, e, x1, order, ctx)) {
2398404e 351 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
3d328a44 352 goto done;
245be530 353 }
3d328a44
JL
354
355 if (BN_cmp(r, t) == 0)
356 ret = 1;
357
358 done:
359 EC_POINT_free(pt);
360 BN_CTX_free(ctx);
361 return ret;
362}
363
2167239a 364ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
3d328a44 365 const EVP_MD *digest,
00433bad
PY
366 const uint8_t *id,
367 const size_t id_len,
368 const uint8_t *msg, size_t msg_len)
3d328a44
JL
369{
370 BIGNUM *e = NULL;
371 ECDSA_SIG *sig = NULL;
372
00433bad 373 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
245be530
MC
374 if (e == NULL) {
375 /* SM2err already called */
3d328a44 376 goto done;
245be530 377 }
3d328a44 378
2167239a 379 sig = sm2_sig_gen(key, e);
3d328a44
JL
380
381 done:
382 BN_free(e);
383 return sig;
384}
385
2167239a 386int sm2_do_verify(const EC_KEY *key,
3d328a44
JL
387 const EVP_MD *digest,
388 const ECDSA_SIG *sig,
00433bad
PY
389 const uint8_t *id,
390 const size_t id_len,
391 const uint8_t *msg, size_t msg_len)
3d328a44
JL
392{
393 BIGNUM *e = NULL;
245be530 394 int ret = 0;
3d328a44 395
00433bad 396 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
245be530
MC
397 if (e == NULL) {
398 /* SM2err already called */
3d328a44 399 goto done;
245be530 400 }
3d328a44 401
2167239a 402 ret = sm2_sig_verify(key, sig, e);
3d328a44
JL
403
404 done:
405 BN_free(e);
406 return ret;
407}
408
ddb634fe 409int sm2_sign(const unsigned char *dgst, int dgstlen,
3d328a44
JL
410 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
411{
412 BIGNUM *e = NULL;
413 ECDSA_SIG *s = NULL;
245be530 414 int sigleni;
3d328a44
JL
415 int ret = -1;
416
3d328a44 417 e = BN_bin2bn(dgst, dgstlen, NULL);
245be530
MC
418 if (e == NULL) {
419 SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
420 goto done;
421 }
3d328a44 422
2167239a 423 s = sm2_sig_gen(eckey, e);
3d328a44 424
245be530
MC
425 sigleni = i2d_ECDSA_SIG(s, &sig);
426 if (sigleni < 0) {
427 SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
428 goto done;
429 }
430 *siglen = (unsigned int)sigleni;
3d328a44 431
67cc2bae 432 ret = 1;
3d328a44
JL
433
434 done:
435 ECDSA_SIG_free(s);
436 BN_free(e);
437 return ret;
438}
439
ddb634fe 440int sm2_verify(const unsigned char *dgst, int dgstlen,
3d328a44
JL
441 const unsigned char *sig, int sig_len, EC_KEY *eckey)
442{
443 ECDSA_SIG *s = NULL;
444 BIGNUM *e = NULL;
445 const unsigned char *p = sig;
446 unsigned char *der = NULL;
447 int derlen = -1;
448 int ret = -1;
449
3d328a44 450 s = ECDSA_SIG_new();
245be530 451 if (s == NULL) {
2398404e 452 SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
3d328a44 453 goto done;
245be530
MC
454 }
455 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
2398404e 456 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
3d328a44 457 goto done;
245be530 458 }
3d328a44
JL
459 /* Ensure signature uses DER and doesn't have trailing garbage */
460 derlen = i2d_ECDSA_SIG(s, &der);
245be530 461 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
2398404e 462 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
3d328a44 463 goto done;
245be530 464 }
3d328a44
JL
465
466 e = BN_bin2bn(dgst, dgstlen, NULL);
245be530
MC
467 if (e == NULL) {
468 SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
469 goto done;
470 }
3d328a44 471
2167239a 472 ret = sm2_sig_verify(eckey, s, e);
3d328a44
JL
473
474 done:
475 OPENSSL_free(der);
476 BN_free(e);
477 ECDSA_SIG_free(s);
478 return ret;
479}