2 * Copyright 2017-2020 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" /* ec_group_do_inverse_ord() */
17 #include "internal/numbers.h"
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/err.h>
21 #include <openssl/bn.h>
24 int sm2_compute_z_digest(uint8_t *out,
31 const EC_GROUP *group = EC_KEY_get0_group(key);
33 EVP_MD_CTX *hash = NULL;
46 hash = EVP_MD_CTX_new();
47 ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
48 if (hash == NULL || ctx == NULL) {
49 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
62 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
66 if (!EVP_DigestInit(hash, digest)) {
67 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
71 /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
73 if (id_len >= (UINT16_MAX / 8)) {
75 ERR_raise(ERR_LIB_SM2, SM2_R_ID_TOO_LARGE);
79 entl = (uint16_t)(8 * id_len);
82 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
83 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
87 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
88 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
92 if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
93 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
97 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
98 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
102 p_bytes = BN_num_bytes(p);
103 buf = OPENSSL_zalloc(p_bytes);
105 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
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),
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),
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)) {
128 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
137 EVP_MD_CTX_free(hash);
141 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
145 const uint8_t *msg, size_t msg_len)
147 EVP_MD_CTX *hash = EVP_MD_CTX_new();
148 const int md_size = EVP_MD_size(digest);
151 EVP_MD *fetched_digest = NULL;
152 OSSL_LIB_CTX *libctx = ec_key_get_libctx(key);
153 const char *propq = ec_key_get0_propq(key);
156 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_DIGEST);
160 z = OPENSSL_zalloc(md_size);
161 if (hash == NULL || z == NULL) {
162 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
166 fetched_digest = EVP_MD_fetch(libctx, EVP_MD_name(digest), propq);
167 if (fetched_digest == NULL) {
168 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
172 if (!sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
173 /* SM2err already called */
177 if (!EVP_DigestInit(hash, fetched_digest)
178 || !EVP_DigestUpdate(hash, z, md_size)
179 || !EVP_DigestUpdate(hash, msg, msg_len)
180 /* reuse z buffer to hold H(Z || M) */
181 || !EVP_DigestFinal(hash, z, NULL)) {
182 ERR_raise(ERR_LIB_SM2, ERR_R_EVP_LIB);
186 e = BN_bin2bn(z, md_size, NULL);
188 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
191 EVP_MD_free(fetched_digest);
193 EVP_MD_CTX_free(hash);
197 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
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);
202 ECDSA_SIG *sig = NULL;
211 OSSL_LIB_CTX *libctx = ec_key_get_libctx(key);
213 kG = EC_POINT_new(group);
214 ctx = BN_CTX_new_ex(libctx);
215 if (kG == NULL || ctx == NULL) {
216 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
222 rk = BN_CTX_get(ctx);
223 x1 = BN_CTX_get(ctx);
224 tmp = BN_CTX_get(ctx);
226 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
231 * These values are returned and so should not be allocated out of the
237 if (r == NULL || s == NULL) {
238 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
243 if (!BN_priv_rand_range_ex(k, order, ctx)) {
244 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
248 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
249 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
251 || !BN_mod_add(r, e, x1, order, ctx)) {
252 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
256 /* try again if r == 0 or r+k == n */
260 if (!BN_add(rk, r, k)) {
261 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
265 if (BN_cmp(rk, order) == 0)
268 if (!BN_add(s, dA, BN_value_one())
269 || !ec_group_do_inverse_ord(group, s, s, ctx)
270 || !BN_mod_mul(tmp, dA, r, order, ctx)
271 || !BN_sub(tmp, k, tmp)
272 || !BN_mod_mul(s, s, tmp, order, ctx)) {
273 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
277 sig = ECDSA_SIG_new();
279 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
283 /* takes ownership of r and s */
284 ECDSA_SIG_set0(sig, r, s);
299 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
303 const EC_GROUP *group = EC_KEY_get0_group(key);
304 const BIGNUM *order = EC_GROUP_get0_order(group);
309 const BIGNUM *r = NULL;
310 const BIGNUM *s = NULL;
311 OSSL_LIB_CTX *libctx = ec_key_get_libctx(key);
313 ctx = BN_CTX_new_ex(libctx);
314 pt = EC_POINT_new(group);
315 if (ctx == NULL || pt == NULL) {
316 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
322 x1 = BN_CTX_get(ctx);
324 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
329 * B1: verify whether r' in [1,n-1], verification failed if not
330 * B2: verify whether s' in [1,n-1], verification failed if not
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
335 * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
338 ECDSA_SIG_get0(sig, &r, &s);
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) {
344 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
348 if (!BN_mod_add(t, r, s, order, ctx)) {
349 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
354 ERR_raise(ERR_LIB_SM2, SM2_R_BAD_SIGNATURE);
358 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
359 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
360 ERR_raise(ERR_LIB_SM2, ERR_R_EC_LIB);
364 if (!BN_mod_add(t, e, x1, order, ctx)) {
365 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
369 if (BN_cmp(r, t) == 0)
378 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
379 const EVP_MD *digest,
382 const uint8_t *msg, size_t msg_len)
385 ECDSA_SIG *sig = NULL;
387 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
389 /* SM2err already called */
393 sig = sm2_sig_gen(key, e);
400 int sm2_do_verify(const EC_KEY *key,
401 const EVP_MD *digest,
402 const ECDSA_SIG *sig,
405 const uint8_t *msg, size_t msg_len)
410 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
412 /* SM2err already called */
416 ret = sm2_sig_verify(key, sig, e);
423 int sm2_internal_sign(const unsigned char *dgst, int dgstlen,
424 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
431 e = BN_bin2bn(dgst, dgstlen, NULL);
433 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
437 s = sm2_sig_gen(eckey, e);
439 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
443 sigleni = i2d_ECDSA_SIG(s, &sig);
445 ERR_raise(ERR_LIB_SM2, ERR_R_INTERNAL_ERROR);
448 *siglen = (unsigned int)sigleni;
458 int sm2_internal_verify(const unsigned char *dgst, int dgstlen,
459 const unsigned char *sig, int sig_len, EC_KEY *eckey)
463 const unsigned char *p = sig;
464 unsigned char *der = NULL;
470 ERR_raise(ERR_LIB_SM2, ERR_R_MALLOC_FAILURE);
473 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
474 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
477 /* Ensure signature uses DER and doesn't have trailing garbage */
478 derlen = i2d_ECDSA_SIG(s, &der);
479 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
480 ERR_raise(ERR_LIB_SM2, SM2_R_INVALID_ENCODING);
484 e = BN_bin2bn(dgst, dgstlen, NULL);
486 ERR_raise(ERR_LIB_SM2, ERR_R_BN_LIB);
490 ret = sm2_sig_verify(eckey, s, e);