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 OpenSSL license (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/sm2.h"
13 #include "internal/sm2err.h"
14 #include "internal/ec_int.h" /* ec_group_do_inverse_ord() */
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/bn.h>
21 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
24 const uint8_t *msg, size_t msg_len)
26 EVP_MD_CTX *hash = EVP_MD_CTX_new();
27 const int md_size = EVP_MD_size(digest);
28 uint8_t *za = OPENSSL_zalloc(md_size);
31 if (hash == NULL || za == NULL) {
32 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
37 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
41 if (!sm2_compute_userid_digest(za, digest, user_id, key)) {
42 /* SM2err already called */
46 if (!EVP_DigestInit(hash, digest)
47 || !EVP_DigestUpdate(hash, za, md_size)
48 || !EVP_DigestUpdate(hash, msg, msg_len)
49 /* reuse za buffer to hold H(ZA || M) */
50 || !EVP_DigestFinal(hash, za, NULL)) {
51 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
55 e = BN_bin2bn(za, md_size, NULL);
57 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
61 EVP_MD_CTX_free(hash);
65 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
67 const BIGNUM *dA = EC_KEY_get0_private_key(key);
68 const EC_GROUP *group = EC_KEY_get0_group(key);
69 const BIGNUM *order = EC_GROUP_get0_order(group);
70 ECDSA_SIG *sig = NULL;
80 kG = EC_POINT_new(group);
82 if (kG == NULL || ctx == NULL) {
83 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
92 tmp = BN_CTX_get(ctx);
94 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
99 * These values are returned and so should not be allocated out of the
105 if (r == NULL || s == NULL) {
106 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
111 if (!BN_priv_rand_range(k, order)) {
112 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
116 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
117 || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL,
119 || !BN_mod_add(r, e, x1, order, ctx)) {
120 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
124 /* try again if r == 0 or r+k == n */
128 if (!BN_add(rk, r, k)) {
129 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
133 if (BN_cmp(rk, order) == 0)
136 if (!BN_add(s, dA, BN_value_one())
137 || !ec_group_do_inverse_ord(group, s, s, ctx)
138 || !BN_mod_mul(tmp, dA, r, order, ctx)
139 || !BN_sub(tmp, k, tmp)
140 || !BN_mod_mul(s, s, tmp, order, ctx)) {
141 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
145 sig = ECDSA_SIG_new();
147 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
151 /* takes ownership of r and s */
152 ECDSA_SIG_set0(sig, r, s);
167 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
171 const EC_GROUP *group = EC_KEY_get0_group(key);
172 const BIGNUM *order = EC_GROUP_get0_order(group);
177 const BIGNUM *r = NULL;
178 const BIGNUM *s = NULL;
181 pt = EC_POINT_new(group);
182 if (ctx == NULL || pt == NULL) {
183 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
189 x1 = BN_CTX_get(ctx);
191 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
196 * B1: verify whether r' in [1,n-1], verification failed if not
197 * B2: vefify whether s' in [1,n-1], verification failed if not
198 * B3: set M'~=ZA || M'
199 * B4: calculate e'=Hv(M'~)
200 * B5: calculate t = (r' + s') modn, verification failed if t=0
201 * B6: calculate the point (x1', y1')=[s']G + [t]PA
202 * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
205 ECDSA_SIG_get0(sig, &r, &s);
207 if (BN_cmp(r, BN_value_one()) < 0
208 || BN_cmp(s, BN_value_one()) < 0
209 || BN_cmp(order, r) <= 0
210 || BN_cmp(order, s) <= 0) {
211 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
215 if (!BN_mod_add(t, r, s, order, ctx)) {
216 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
221 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
225 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
226 || !EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx)) {
227 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
231 if (!BN_mod_add(t, e, x1, order, ctx)) {
232 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
236 if (BN_cmp(r, t) == 0)
245 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
246 const EVP_MD *digest,
247 const char *user_id, const uint8_t *msg, size_t msg_len)
250 ECDSA_SIG *sig = NULL;
252 e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
254 /* SM2err already called */
258 sig = sm2_sig_gen(key, e);
265 int sm2_do_verify(const EC_KEY *key,
266 const EVP_MD *digest,
267 const ECDSA_SIG *sig,
268 const char *user_id, const uint8_t *msg, size_t msg_len)
273 e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
275 /* SM2err already called */
279 ret = sm2_sig_verify(key, sig, e);
286 int sm2_sign(const unsigned char *dgst, int dgstlen,
287 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
294 e = BN_bin2bn(dgst, dgstlen, NULL);
296 SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
300 s = sm2_sig_gen(eckey, e);
302 sigleni = i2d_ECDSA_SIG(s, &sig);
304 SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
307 *siglen = (unsigned int)sigleni;
317 int sm2_verify(const unsigned char *dgst, int dgstlen,
318 const unsigned char *sig, int sig_len, EC_KEY *eckey)
322 const unsigned char *p = sig;
323 unsigned char *der = NULL;
329 SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
332 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
333 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
336 /* Ensure signature uses DER and doesn't have trailing garbage */
337 derlen = i2d_ECDSA_SIG(s, &der);
338 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
339 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
343 e = BN_bin2bn(dgst, dgstlen, NULL);
345 SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
349 ret = sm2_sig_verify(eckey, s, e);