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 int sm2_compute_userid_digest(uint8_t *out,
27 const EC_GROUP *group = EC_KEY_get0_group(key);
29 EVP_MD_CTX *hash = NULL;
43 hash = EVP_MD_CTX_new();
45 if (hash == NULL || ctx == NULL) {
46 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_MALLOC_FAILURE);
59 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_MALLOC_FAILURE);
63 if (!EVP_DigestInit(hash, digest)) {
64 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_EVP_LIB);
68 /* Z = SM3(ENTLA || IDA || a || b || xG || yG || xA || yA) */
70 uid_len = strlen(user_id);
71 if (uid_len >= (UINT16_MAX / 8)) {
73 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, SM2_R_USER_ID_TOO_LARGE);
77 entla = (uint16_t)(8 * uid_len);
80 if (!EVP_DigestUpdate(hash, &e_byte, 1)) {
81 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_EVP_LIB);
84 e_byte = entla & 0xFF;
85 if (!EVP_DigestUpdate(hash, &e_byte, 1)
86 || !EVP_DigestUpdate(hash, user_id, uid_len)) {
87 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_EVP_LIB);
91 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
92 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_EC_LIB);
96 p_bytes = BN_num_bytes(p);
97 buf = OPENSSL_zalloc(p_bytes);
99 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_MALLOC_FAILURE);
103 if (BN_bn2binpad(a, buf, p_bytes) < 0
104 || !EVP_DigestUpdate(hash, buf, p_bytes)
105 || BN_bn2binpad(b, buf, p_bytes) < 0
106 || !EVP_DigestUpdate(hash, buf, p_bytes)
107 || !EC_POINT_get_affine_coordinates(group,
108 EC_GROUP_get0_generator(group),
110 || BN_bn2binpad(xG, buf, p_bytes) < 0
111 || !EVP_DigestUpdate(hash, buf, p_bytes)
112 || BN_bn2binpad(yG, buf, p_bytes) < 0
113 || !EVP_DigestUpdate(hash, buf, p_bytes)
114 || !EC_POINT_get_affine_coordinates(group,
115 EC_KEY_get0_public_key(key),
117 || BN_bn2binpad(xA, buf, p_bytes) < 0
118 || !EVP_DigestUpdate(hash, buf, p_bytes)
119 || BN_bn2binpad(yA, buf, p_bytes) < 0
120 || !EVP_DigestUpdate(hash, buf, p_bytes)
121 || !EVP_DigestFinal(hash, out, NULL)) {
122 SM2err(SM2_F_SM2_COMPUTE_USERID_DIGEST, ERR_R_INTERNAL_ERROR);
131 EVP_MD_CTX_free(hash);
135 static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
138 const uint8_t *msg, size_t msg_len)
140 EVP_MD_CTX *hash = EVP_MD_CTX_new();
141 const int md_size = EVP_MD_size(digest);
146 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
150 za = OPENSSL_zalloc(md_size);
151 if (hash == NULL || za == NULL) {
152 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
156 if (!sm2_compute_userid_digest(za, digest, user_id, key)) {
157 /* SM2err already called */
161 if (!EVP_DigestInit(hash, digest)
162 || !EVP_DigestUpdate(hash, za, md_size)
163 || !EVP_DigestUpdate(hash, msg, msg_len)
164 /* reuse za buffer to hold H(ZA || M) */
165 || !EVP_DigestFinal(hash, za, NULL)) {
166 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
170 e = BN_bin2bn(za, md_size, NULL);
172 SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
176 EVP_MD_CTX_free(hash);
180 static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
182 const BIGNUM *dA = EC_KEY_get0_private_key(key);
183 const EC_GROUP *group = EC_KEY_get0_group(key);
184 const BIGNUM *order = EC_GROUP_get0_order(group);
185 ECDSA_SIG *sig = NULL;
195 kG = EC_POINT_new(group);
197 if (kG == NULL || ctx == NULL) {
198 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
204 rk = BN_CTX_get(ctx);
205 x1 = BN_CTX_get(ctx);
206 tmp = BN_CTX_get(ctx);
208 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
213 * These values are returned and so should not be allocated out of the
219 if (r == NULL || s == NULL) {
220 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
225 if (!BN_priv_rand_range(k, order)) {
226 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
230 if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
231 || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
233 || !BN_mod_add(r, e, x1, order, ctx)) {
234 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
238 /* try again if r == 0 or r+k == n */
242 if (!BN_add(rk, r, k)) {
243 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
247 if (BN_cmp(rk, order) == 0)
250 if (!BN_add(s, dA, BN_value_one())
251 || !ec_group_do_inverse_ord(group, s, s, ctx)
252 || !BN_mod_mul(tmp, dA, r, order, ctx)
253 || !BN_sub(tmp, k, tmp)
254 || !BN_mod_mul(s, s, tmp, order, ctx)) {
255 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
259 sig = ECDSA_SIG_new();
261 SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
265 /* takes ownership of r and s */
266 ECDSA_SIG_set0(sig, r, s);
281 static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
285 const EC_GROUP *group = EC_KEY_get0_group(key);
286 const BIGNUM *order = EC_GROUP_get0_order(group);
291 const BIGNUM *r = NULL;
292 const BIGNUM *s = NULL;
295 pt = EC_POINT_new(group);
296 if (ctx == NULL || pt == NULL) {
297 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
303 x1 = BN_CTX_get(ctx);
305 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
310 * B1: verify whether r' in [1,n-1], verification failed if not
311 * B2: vefify whether s' in [1,n-1], verification failed if not
312 * B3: set M'~=ZA || M'
313 * B4: calculate e'=Hv(M'~)
314 * B5: calculate t = (r' + s') modn, verification failed if t=0
315 * B6: calculate the point (x1', y1')=[s']G + [t]PA
316 * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
319 ECDSA_SIG_get0(sig, &r, &s);
321 if (BN_cmp(r, BN_value_one()) < 0
322 || BN_cmp(s, BN_value_one()) < 0
323 || BN_cmp(order, r) <= 0
324 || BN_cmp(order, s) <= 0) {
325 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
329 if (!BN_mod_add(t, r, s, order, ctx)) {
330 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
335 SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
339 if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
340 || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
341 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
345 if (!BN_mod_add(t, e, x1, order, ctx)) {
346 SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
350 if (BN_cmp(r, t) == 0)
359 ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
360 const EVP_MD *digest,
361 const char *user_id, const uint8_t *msg, size_t msg_len)
364 ECDSA_SIG *sig = NULL;
366 e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
368 /* SM2err already called */
372 sig = sm2_sig_gen(key, e);
379 int sm2_do_verify(const EC_KEY *key,
380 const EVP_MD *digest,
381 const ECDSA_SIG *sig,
382 const char *user_id, const uint8_t *msg, size_t msg_len)
387 e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
389 /* SM2err already called */
393 ret = sm2_sig_verify(key, sig, e);
400 int sm2_sign(const unsigned char *dgst, int dgstlen,
401 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
408 e = BN_bin2bn(dgst, dgstlen, NULL);
410 SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
414 s = sm2_sig_gen(eckey, e);
416 sigleni = i2d_ECDSA_SIG(s, &sig);
418 SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
421 *siglen = (unsigned int)sigleni;
431 int sm2_verify(const unsigned char *dgst, int dgstlen,
432 const unsigned char *sig, int sig_len, EC_KEY *eckey)
436 const unsigned char *p = sig;
437 unsigned char *der = NULL;
443 SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
446 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
447 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
450 /* Ensure signature uses DER and doesn't have trailing garbage */
451 derlen = i2d_ECDSA_SIG(s, &der);
452 if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
453 SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
457 e = BN_bin2bn(dgst, dgstlen, NULL);
459 SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
463 ret = sm2_sig_verify(eckey, s, e);