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 "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();
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
);
151 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, SM2_R_INVALID_DIGEST
);
155 z
= OPENSSL_zalloc(md_size
);
156 if (hash
== NULL
|| z
== NULL
) {
157 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_MALLOC_FAILURE
);
161 if (!sm2_compute_z_digest(z
, digest
, id
, id_len
, key
)) {
162 /* SM2err already called */
166 if (!EVP_DigestInit(hash
, digest
)
167 || !EVP_DigestUpdate(hash
, z
, md_size
)
168 || !EVP_DigestUpdate(hash
, msg
, msg_len
)
169 /* reuse z buffer to hold H(Z || M) */
170 || !EVP_DigestFinal(hash
, z
, NULL
)) {
171 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_EVP_LIB
);
175 e
= BN_bin2bn(z
, md_size
, NULL
);
177 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH
, ERR_R_INTERNAL_ERROR
);
181 EVP_MD_CTX_free(hash
);
185 static ECDSA_SIG
*sm2_sig_gen(const EC_KEY
*key
, const BIGNUM
*e
)
187 const BIGNUM
*dA
= EC_KEY_get0_private_key(key
);
188 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
189 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
190 ECDSA_SIG
*sig
= NULL
;
200 kG
= EC_POINT_new(group
);
202 if (kG
== NULL
|| ctx
== NULL
) {
203 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
209 rk
= BN_CTX_get(ctx
);
210 x1
= BN_CTX_get(ctx
);
211 tmp
= BN_CTX_get(ctx
);
213 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
218 * These values are returned and so should not be allocated out of the
224 if (r
== NULL
|| s
== NULL
) {
225 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
230 if (!BN_priv_rand_range(k
, order
)) {
231 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
235 if (!EC_POINT_mul(group
, kG
, k
, NULL
, NULL
, ctx
)
236 || !EC_POINT_get_affine_coordinates(group
, kG
, x1
, NULL
,
238 || !BN_mod_add(r
, e
, x1
, order
, ctx
)) {
239 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
243 /* try again if r == 0 or r+k == n */
247 if (!BN_add(rk
, r
, k
)) {
248 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_INTERNAL_ERROR
);
252 if (BN_cmp(rk
, order
) == 0)
255 if (!BN_add(s
, dA
, BN_value_one())
256 || !ec_group_do_inverse_ord(group
, s
, s
, ctx
)
257 || !BN_mod_mul(tmp
, dA
, r
, order
, ctx
)
258 || !BN_sub(tmp
, k
, tmp
)
259 || !BN_mod_mul(s
, s
, tmp
, order
, ctx
)) {
260 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_BN_LIB
);
264 sig
= ECDSA_SIG_new();
266 SM2err(SM2_F_SM2_SIG_GEN
, ERR_R_MALLOC_FAILURE
);
270 /* takes ownership of r and s */
271 ECDSA_SIG_set0(sig
, r
, s
);
286 static int sm2_sig_verify(const EC_KEY
*key
, const ECDSA_SIG
*sig
,
290 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
291 const BIGNUM
*order
= EC_GROUP_get0_order(group
);
296 const BIGNUM
*r
= NULL
;
297 const BIGNUM
*s
= NULL
;
300 pt
= EC_POINT_new(group
);
301 if (ctx
== NULL
|| pt
== NULL
) {
302 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_MALLOC_FAILURE
);
308 x1
= BN_CTX_get(ctx
);
310 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_MALLOC_FAILURE
);
315 * B1: verify whether r' in [1,n-1], verification failed if not
316 * B2: vefify whether s' in [1,n-1], verification failed if not
317 * B3: set M'~=ZA || M'
318 * B4: calculate e'=Hv(M'~)
319 * B5: calculate t = (r' + s') modn, verification failed if t=0
320 * B6: calculate the point (x1', y1')=[s']G + [t]PA
321 * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
324 ECDSA_SIG_get0(sig
, &r
, &s
);
326 if (BN_cmp(r
, BN_value_one()) < 0
327 || BN_cmp(s
, BN_value_one()) < 0
328 || BN_cmp(order
, r
) <= 0
329 || BN_cmp(order
, s
) <= 0) {
330 SM2err(SM2_F_SM2_SIG_VERIFY
, SM2_R_BAD_SIGNATURE
);
334 if (!BN_mod_add(t
, r
, s
, order
, ctx
)) {
335 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_BN_LIB
);
340 SM2err(SM2_F_SM2_SIG_VERIFY
, SM2_R_BAD_SIGNATURE
);
344 if (!EC_POINT_mul(group
, pt
, s
, EC_KEY_get0_public_key(key
), t
, ctx
)
345 || !EC_POINT_get_affine_coordinates(group
, pt
, x1
, NULL
, ctx
)) {
346 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_EC_LIB
);
350 if (!BN_mod_add(t
, e
, x1
, order
, ctx
)) {
351 SM2err(SM2_F_SM2_SIG_VERIFY
, ERR_R_BN_LIB
);
355 if (BN_cmp(r
, t
) == 0)
364 ECDSA_SIG
*sm2_do_sign(const EC_KEY
*key
,
365 const EVP_MD
*digest
,
368 const uint8_t *msg
, size_t msg_len
)
371 ECDSA_SIG
*sig
= NULL
;
373 e
= sm2_compute_msg_hash(digest
, key
, id
, id_len
, msg
, msg_len
);
375 /* SM2err already called */
379 sig
= sm2_sig_gen(key
, e
);
386 int sm2_do_verify(const EC_KEY
*key
,
387 const EVP_MD
*digest
,
388 const ECDSA_SIG
*sig
,
391 const uint8_t *msg
, size_t msg_len
)
396 e
= sm2_compute_msg_hash(digest
, key
, id
, id_len
, msg
, msg_len
);
398 /* SM2err already called */
402 ret
= sm2_sig_verify(key
, sig
, e
);
409 int sm2_sign(const unsigned char *dgst
, int dgstlen
,
410 unsigned char *sig
, unsigned int *siglen
, EC_KEY
*eckey
)
417 e
= BN_bin2bn(dgst
, dgstlen
, NULL
);
419 SM2err(SM2_F_SM2_SIGN
, ERR_R_BN_LIB
);
423 s
= sm2_sig_gen(eckey
, e
);
425 sigleni
= i2d_ECDSA_SIG(s
, &sig
);
427 SM2err(SM2_F_SM2_SIGN
, ERR_R_INTERNAL_ERROR
);
430 *siglen
= (unsigned int)sigleni
;
440 int sm2_verify(const unsigned char *dgst
, int dgstlen
,
441 const unsigned char *sig
, int sig_len
, EC_KEY
*eckey
)
445 const unsigned char *p
= sig
;
446 unsigned char *der
= NULL
;
452 SM2err(SM2_F_SM2_VERIFY
, ERR_R_MALLOC_FAILURE
);
455 if (d2i_ECDSA_SIG(&s
, &p
, sig_len
) == NULL
) {
456 SM2err(SM2_F_SM2_VERIFY
, SM2_R_INVALID_ENCODING
);
459 /* Ensure signature uses DER and doesn't have trailing garbage */
460 derlen
= i2d_ECDSA_SIG(s
, &der
);
461 if (derlen
!= sig_len
|| memcmp(sig
, der
, derlen
) != 0) {
462 SM2err(SM2_F_SM2_VERIFY
, SM2_R_INVALID_ENCODING
);
466 e
= BN_bin2bn(dgst
, dgstlen
, NULL
);
468 SM2err(SM2_F_SM2_VERIFY
, ERR_R_BN_LIB
);
472 ret
= sm2_sig_verify(eckey
, s
, e
);