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 <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17 #include <openssl/bn.h>
20 static BIGNUM
*sm2_compute_msg_hash(const EVP_MD
*digest
,
23 const uint8_t *msg
, size_t msg_len
)
25 EVP_MD_CTX
*hash
= EVP_MD_CTX_new();
26 const int md_size
= EVP_MD_size(digest
);
27 uint8_t *za
= OPENSSL_zalloc(md_size
);
30 if (hash
== NULL
|| za
== NULL
) {
31 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_MALLOC_FAILURE
);
36 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, SM2_R_INVALID_DIGEST
);
40 if (!sm2_compute_userid_digest(za
, digest
, user_id
, key
)) {
41 /* SM2err already called */
45 if (!EVP_DigestInit(hash
, digest
)
46 || !EVP_DigestUpdate(hash
, za
, md_size
)
47 || !EVP_DigestUpdate(hash
, msg
, msg_len
)
48 /* reuse za buffer to hold H(ZA || M) */
49 || !EVP_DigestFinal(hash
, za
, NULL
)) {
50 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_EVP_LIB
);
54 e
= BN_bin2bn(za
, md_size
, NULL
);
56 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_INTERNAL_ERROR
);
60 EVP_MD_CTX_free(hash
);
64 static ECDSA_SIG
*sm2_sig_gen(const EC_KEY
*key
, const BIGNUM
*e
)
66 const BIGNUM
*dA
= EC_KEY_get0_private_key(key
);
67 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
68 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
69 ECDSA_SIG
*sig
= NULL
;
79 kG
= EC_POINT_new(group
);
81 if (kG
== NULL
|| ctx
== NULL
) {
82 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
91 tmp
= BN_CTX_get(ctx
);
93 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
98 * These values are returned and so should not be allocated out of the
104 if (r
== NULL
|| s
== NULL
) {
105 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
110 if (!BN_priv_rand_range(k
, order
)) {
111 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
115 if (!EC_POINT_mul(group
, kG
, k
, NULL
, NULL
, ctx
)
116 || !EC_POINT_get_affine_coordinates_GFp(group
, kG
, x1
, NULL
,
118 || !BN_mod_add(r
, e
, x1
, order
, ctx
)) {
119 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
123 /* try again if r == 0 or r+k == n */
127 if (!BN_add(rk
, r
, k
)) {
128 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
132 if (BN_cmp(rk
, order
) == 0)
135 if (!BN_add(s
, dA
, BN_value_one())
136 || !BN_mod_inverse(s
, s
, order
, ctx
)
137 || !BN_mod_mul(tmp
, dA
, r
, order
, ctx
)
138 || !BN_sub(tmp
, k
, tmp
)
139 || !BN_mod_mul(s
, s
, tmp
, order
, ctx
)) {
140 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_BN_LIB
);
144 sig
= ECDSA_SIG_new();
146 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
150 /* takes ownership of r and s */
151 ECDSA_SIG_set0(sig
, r
, s
);
166 static int sm2_sig_verify(const EC_KEY
*key
, const ECDSA_SIG
*sig
,
170 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
171 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
176 const BIGNUM
*r
= NULL
;
177 const BIGNUM
*s
= NULL
;
180 pt
= EC_POINT_new(group
);
181 if (ctx
== NULL
|| pt
== NULL
) {
182 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_MALLOC_FAILURE
);
188 x1
= BN_CTX_get(ctx
);
190 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_MALLOC_FAILURE
);
195 * B1: verify whether r' in [1,n-1], verification failed if not
196 * B2: vefify whether s' in [1,n-1], verification failed if not
197 * B3: set M'~=ZA || M'
198 * B4: calculate e'=Hv(M'~)
199 * B5: calculate t = (r' + s') modn, verification failed if t=0
200 * B6: calculate the point (x1', y1')=[s']G + [t]PA
201 * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
204 ECDSA_SIG_get0(sig
, &r
, &s
);
206 if (BN_cmp(r
, BN_value_one()) < 0
207 || BN_cmp(s
, BN_value_one()) < 0
208 || BN_cmp(order
, r
) <= 0
209 || BN_cmp(order
, s
) <= 0) {
210 SM2err(SM2_F_SM2_SIG_VERIFY
, SM2_R_BAD_SIGNATURE
);
214 if (!BN_mod_add(t
, r
, s
, order
, ctx
)) {
215 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_BN_LIB
);
220 SM2err(SM2_F_SM2_SIG_VERIFY
, SM2_R_BAD_SIGNATURE
);
224 if (!EC_POINT_mul(group
, pt
, s
, EC_KEY_get0_public_key(key
), t
, ctx
)
225 || !EC_POINT_get_affine_coordinates_GFp(group
, pt
, x1
, NULL
, ctx
)) {
226 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_EC_LIB
);
230 if (!BN_mod_add(t
, e
, x1
, order
, ctx
)) {
231 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_BN_LIB
);
235 if (BN_cmp(r
, t
) == 0)
244 ECDSA_SIG
*sm2_do_sign(const EC_KEY
*key
,
245 const EVP_MD
*digest
,
246 const char *user_id
, const uint8_t *msg
, size_t msg_len
)
249 ECDSA_SIG
*sig
= NULL
;
251 e
= sm2_compute_msg_hash(digest
, key
, user_id
, msg
, msg_len
);
253 /* SM2err already called */
257 sig
= sm2_sig_gen(key
, e
);
264 int sm2_do_verify(const EC_KEY
*key
,
265 const EVP_MD
*digest
,
266 const ECDSA_SIG
*sig
,
267 const char *user_id
, const uint8_t *msg
, size_t msg_len
)
272 e
= sm2_compute_msg_hash(digest
, key
, user_id
, msg
, msg_len
);
274 /* SM2err already called */
278 ret
= sm2_sig_verify(key
, sig
, e
);
285 int sm2_sign(const unsigned char *dgst
, int dgstlen
,
286 unsigned char *sig
, unsigned int *siglen
, EC_KEY
*eckey
)
293 e
= BN_bin2bn(dgst
, dgstlen
, NULL
);
295 SM2err(SM2_F_SM2_SIGN
, ERR_R_BN_LIB
);
299 s
= sm2_sig_gen(eckey
, e
);
301 sigleni
= i2d_ECDSA_SIG(s
, &sig
);
303 SM2err(SM2_F_SM2_SIGN
, ERR_R_INTERNAL_ERROR
);
306 *siglen
= (unsigned int)sigleni
;
316 int sm2_verify(const unsigned char *dgst
, int dgstlen
,
317 const unsigned char *sig
, int sig_len
, EC_KEY
*eckey
)
321 const unsigned char *p
= sig
;
322 unsigned char *der
= NULL
;
328 SM2err(SM2_F_SM2_VERIFY
, ERR_R_MALLOC_FAILURE
);
331 if (d2i_ECDSA_SIG(&s
, &p
, sig_len
) == NULL
) {
332 SM2err(SM2_F_SM2_VERIFY
, SM2_R_INVALID_ENCODING
);
335 /* Ensure signature uses DER and doesn't have trailing garbage */
336 derlen
= i2d_ECDSA_SIG(s
, &der
);
337 if (derlen
!= sig_len
|| memcmp(sig
, der
, derlen
) != 0) {
338 SM2err(SM2_F_SM2_VERIFY
, SM2_R_INVALID_ENCODING
);
342 e
= BN_bin2bn(dgst
, dgstlen
, NULL
);
344 SM2err(SM2_F_SM2_VERIFY
, ERR_R_BN_LIB
);
348 ret
= sm2_sig_verify(eckey
, s
, e
);