2 * Copyright 2017-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
12 #include "internal/deprecated.h"
14 #include "crypto/sm2.h"
15 #include "crypto/sm2err.h"
16 #include "crypto/ec.h" /* ossl_ec_group_do_inverse_ord() */
17 #include "internal/numbers.h"
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/bn.h>
23 int ossl_sm2_compute_z_digest(uint8_t *out,
30 const EC_GROUP *group = EC_KEY_get0_group(key);
31 const EC_POINT *pubkey = EC_KEY_get0_public_key(key);
33 EVP_MD_CTX *hash = NULL;
46 /* SM2 Signatures require a public key, check for it */
48 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
52 hash = EVP_MD_CTX_new();
54 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
57 ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
59 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
72 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
76 if (!EVP_DigestInit(hash, digest)) {
77 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
81 /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
83 if (id_len >= (UINT16_MAX / 8)) {
85 ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
89 entl = (uint16_t)(8 * id_len);
92 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
93 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
97 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
98 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
102 if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
103 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
107 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
108 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
112 p_bytes = BN_num_bytes(p);
113 buf = OPENSSL_zalloc(p_bytes);
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),
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,
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)) {
136 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
145 EVP_MD_CTX_free(hash);
149 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
153 const uint8_t *msg, size_t msg_len)
155 EVP_MD_CTX *hash = EVP_MD_CTX_new();
156 const int md_size = EVP_MD_get_size(digest);
159 EVP_MD *fetched_digest = NULL;
160 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
161 const char *propq = ossl_ec_key_get0_propq(key);
164 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
168 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
172 z = OPENSSL_zalloc(md_size);
176 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_get0_name(digest), propq);
177 if (fetched_digest == NULL) {
178 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
182 if (!ossl_sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
183 /* SM2err already called */
187 if (!EVP_DigestInit(hash, fetched_digest)
188 || !EVP_DigestUpdate(hash, z, md_size)
189 || !EVP_DigestUpdate(hash, msg, msg_len)
190 /* reuse z buffer to hold H(Z || M) */
191 || !EVP_DigestFinal(hash, z, NULL)) {
192 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
196 e = BN_bin2bn(z, md_size, NULL);
198 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
201 EVP_MD_free(fetched_digest);
203 EVP_MD_CTX_free(hash);
207 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
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);
212 ECDSA_SIG *sig = NULL;
221 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
223 kG = EC_POINT_new(group);
225 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
228 ctx = BN_CTX_new_ex(libctx);
230 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
236 rk = BN_CTX_get(ctx);
237 x1 = BN_CTX_get(ctx);
238 tmp = BN_CTX_get(ctx);
240 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
245 * These values are returned and so should not be allocated out of the
251 if (r == NULL || s == NULL) {
252 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
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).
266 if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
267 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
271 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
272 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
274 || !BN_mod_add(r, e, x1, order, ctx)) {
275 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
279 /* try again if r == 0 or r+k == n */
283 if (!BN_add(rk, r, k)) {
284 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
288 if (BN_cmp(rk, order) == 0)
291 if (!BN_add(s, dA, BN_value_one())
292 || !ossl_ec_group_do_inverse_ord(group, s, s, ctx)
293 || !BN_mod_mul(tmp, dA, r, order, ctx)
294 || !BN_sub(tmp, k, tmp)
295 || !BN_mod_mul(s, s, tmp, order, ctx)) {
296 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
300 /* try again if s == 0 */
304 sig = ECDSA_SIG_new();
306 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
310 /* takes ownership of r and s */
311 ECDSA_SIG_set0(sig, r, s);
326 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
330 const EC_GROUP *group = EC_KEY_get0_group(key);
331 const BIGNUM *order = EC_GROUP_get0_order(group);
336 const BIGNUM *r = NULL;
337 const BIGNUM *s = NULL;
338 OSSL_LIB_CTX *libctx = ossl_ec_key_get_libctx(key);
340 ctx = BN_CTX_new_ex(libctx);
341 pt = EC_POINT_new(group);
342 if (ctx == NULL || pt == NULL) {
343 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
349 x1 = BN_CTX_get(ctx);
351 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
356 * B1: verify whether r' in [1,n-1], verification failed if not
357 * B2: verify whether s' in [1,n-1], verification failed if not
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
362 * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
365 ECDSA_SIG_get0(sig, &r, &s);
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) {
371 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
375 if (!BN_mod_add(t, r, s, order, ctx)) {
376 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
381 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
385 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
386 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
387 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
391 if (!BN_mod_add(t, e, x1, order, ctx)) {
392 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
396 if (BN_cmp(r, t) == 0)
406 ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
407 const EVP_MD *digest,
410 const uint8_t *msg, size_t msg_len)
413 ECDSA_SIG *sig = NULL;
415 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
417 /* SM2err already called */
421 sig = sm2_sig_gen(key, e);
428 int ossl_sm2_do_verify(const EC_KEY *key,
429 const EVP_MD *digest,
430 const ECDSA_SIG *sig,
433 const uint8_t *msg, size_t msg_len)
438 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
440 /* SM2err already called */
444 ret = sm2_sig_verify(key, sig, e);
451 int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
452 unsigned char *sig, unsigned int *siglen,
461 ERR_raise(ERR_LIB_SM2, ERR_R_PASSED_NULL_PARAMETER);
465 e = BN_bin2bn(dgst, dgstlen, NULL);
467 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
471 s = sm2_sig_gen(eckey, e);
473 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
477 sigleni = i2d_ECDSA_SIG(s, &sig);
479 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
482 *siglen = (unsigned int)sigleni;
492 int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
493 const unsigned char *sig, int sig_len,
498 const unsigned char *p = sig;
499 unsigned char *der = NULL;
505 ERR_raise(ERR_LIB_SM2, ERR_R_ECDSA_LIB);
508 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
509 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
512 /* Ensure signature uses DER and doesn't have trailing garbage */
513 derlen = i2d_ECDSA_SIG(s, &der);
514 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
515 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
519 e = BN_bin2bn(dgst, dgstlen, NULL);
521 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
525 ret = sm2_sig_verify(eckey, s, e);