2 * Copyright 2017-2018 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 "crypto/sm2.h"
13 #include "crypto/sm2err.h"
14 #include "crypto/ec.h" /* ec_group_do_inverse_ord() */
15 #include "internal/numbers.h"
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/err.h>
19 #include <openssl/bn.h>
22 int sm2_compute_z_digest(uint8_t *out,
29 const EC_GROUP *group = EC_KEY_get0_group(key);
31 EVP_MD_CTX *hash = NULL;
44 hash = EVP_MD_CTX_new();
45 ctx = BN_CTX_new_ex(ec_key_get_libctx(key));
46 if (hash == NULL || ctx == NULL) {
47 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
60 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
64 if (!EVP_DigestInit(hash, digest)) {
65 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
69 /* Z = h(ENTL || ID || a || b || xG || yG || xA || yA) */
71 if (id_len >= (UINT16_MAX / 8)) {
73 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, SM2_R_ID_TOO_LARGE);
77 entl = (uint16_t)(8 * id_len);
80 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
81 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
85 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
86 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
90 if (id_len > 0 && !EVP_DigestUpdate(hash, id, id_len)) {
91 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EVP_LIB);
95 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
96 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_EC_LIB);
100 p_bytes = BN_num_bytes(p);
101 buf = OPENSSL_zalloc(p_bytes);
103 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_MALLOC_FAILURE);
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),
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),
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)) {
126 SM2err(SM2_F_SM2_COMPUTE_Z_DIGEST, ERR_R_INTERNAL_ERROR);
135 EVP_MD_CTX_free(hash);
139 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
143 const uint8_t *msg, size_t msg_len)
145 EVP_MD_CTX *hash = EVP_MD_CTX_new();
146 const int md_size = EVP_MD_size(digest);
149 EVP_MD *fetched_digest = NULL;
150 OPENSSL_CTX *libctx = ec_key_get_libctx(key);
151 const char *propq = ec_key_get0_propq(key);
154 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
158 z = OPENSSL_zalloc(md_size);
159 if (hash == NULL || z == NULL) {
160 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
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);
170 if (!sm2_compute_z_digest(z, fetched_digest, id, id_len, key)) {
171 /* SM2err already called */
175 if (!EVP_DigestInit(hash, fetched_digest)
176 || !EVP_DigestUpdate(hash, z, md_size)
177 || !EVP_DigestUpdate(hash, msg, msg_len)
178 /* reuse z buffer to hold H(Z || M) */
179 || !EVP_DigestFinal(hash, z, NULL)) {
180 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
184 e = BN_bin2bn(z, md_size, NULL);
186 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
189 EVP_MD_free(fetched_digest);
191 EVP_MD_CTX_free(hash);
195 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
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);
200 ECDSA_SIG *sig = NULL;
209 OPENSSL_CTX *libctx = ec_key_get_libctx(key);
211 kG = EC_POINT_new(group);
212 ctx = BN_CTX_new_ex(libctx);
213 if (kG == NULL || ctx == NULL) {
214 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
220 rk = BN_CTX_get(ctx);
221 x1 = BN_CTX_get(ctx);
222 tmp = BN_CTX_get(ctx);
224 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
229 * These values are returned and so should not be allocated out of the
235 if (r == NULL || s == NULL) {
236 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
241 if (!BN_priv_rand_range_ex(k, order, ctx)) {
242 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
246 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
247 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
249 || !BN_mod_add(r, e, x1, order, ctx)) {
250 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
254 /* try again if r == 0 or r+k == n */
258 if (!BN_add(rk, r, k)) {
259 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
263 if (BN_cmp(rk, order) == 0)
266 if (!BN_add(s, dA, BN_value_one())
267 || !ec_group_do_inverse_ord(group, s, s, ctx)
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);
275 sig = ECDSA_SIG_new();
277 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
281 /* takes ownership of r and s */
282 ECDSA_SIG_set0(sig, r, s);
297 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
301 const EC_GROUP *group = EC_KEY_get0_group(key);
302 const BIGNUM *order = EC_GROUP_get0_order(group);
307 const BIGNUM *r = NULL;
308 const BIGNUM *s = NULL;
309 OPENSSL_CTX *libctx = ec_key_get_libctx(key);
311 ctx = BN_CTX_new_ex(libctx);
312 pt = EC_POINT_new(group);
313 if (ctx == NULL || pt == NULL) {
314 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
320 x1 = BN_CTX_get(ctx);
322 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
327 * B1: verify whether r' in [1,n-1], verification failed if not
328 * B2: verify whether s' in [1,n-1], verification failed if not
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
333 * B7: calculate R=(e'+x1') modn, verification pass if yes, otherwise failed
336 ECDSA_SIG_get0(sig, &r, &s);
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) {
342 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
346 if (!BN_mod_add(t, r, s, order, ctx)) {
347 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
352 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
356 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
357 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
358 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
362 if (!BN_mod_add(t, e, x1, order, ctx)) {
363 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
367 if (BN_cmp(r, t) == 0)
376 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
377 const EVP_MD *digest,
380 const uint8_t *msg, size_t msg_len)
383 ECDSA_SIG *sig = NULL;
385 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
387 /* SM2err already called */
391 sig = sm2_sig_gen(key, e);
398 int sm2_do_verify(const EC_KEY *key,
399 const EVP_MD *digest,
400 const ECDSA_SIG *sig,
403 const uint8_t *msg, size_t msg_len)
408 e = sm2_compute_msg_hash(digest, key, id, id_len, msg, msg_len);
410 /* SM2err already called */
414 ret = sm2_sig_verify(key, sig, e);
421 int sm2_sign(const unsigned char *dgst, int dgstlen,
422 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
429 e = BN_bin2bn(dgst, dgstlen, NULL);
431 SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
435 s = sm2_sig_gen(eckey, e);
437 SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
441 sigleni = i2d_ECDSA_SIG(s, &sig);
443 SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
446 *siglen = (unsigned int)sigleni;
456 int sm2_verify(const unsigned char *dgst, int dgstlen,
457 const unsigned char *sig, int sig_len, EC_KEY *eckey)
461 const unsigned char *p = sig;
462 unsigned char *der = NULL;
468 SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
471 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
472 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
475 /* Ensure signature uses DER and doesn't have trailing garbage */
476 derlen = i2d_ECDSA_SIG(s, &der);
477 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
478 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
482 e = BN_bin2bn(dgst, dgstlen, NULL);
484 SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
488 ret = sm2_sig_verify(eckey, s, e);