sm2_sign.c 9.1 KB
Newer Older
J
Jack Lloyd 已提交
1
/*
M
Matt Caswell 已提交
2
 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
J
Jack Lloyd 已提交
3 4 5 6 7 8 9 10 11
 * Copyright 2017 Ribose Inc. All Rights Reserved.
 * Ported from Ribose contributions from Botan.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

J
Jack Lloyd 已提交
12
#include "internal/sm2.h"
J
Jack Lloyd 已提交
13
#include "internal/sm2err.h"
14
#include "internal/ec_int.h" /* ec_group_do_inverse_ord() */
J
Jack Lloyd 已提交
15
#include <openssl/err.h>
J
Jack Lloyd 已提交
16
#include <openssl/evp.h>
J
Jack Lloyd 已提交
17
#include <openssl/err.h>
J
Jack Lloyd 已提交
18 19 20
#include <openssl/bn.h>
#include <string.h>

21
static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
J
Jack Lloyd 已提交
22 23 24
                                    const EC_KEY *key,
                                    const char *user_id,
                                    const uint8_t *msg, size_t msg_len)
J
Jack Lloyd 已提交
25 26 27 28 29 30
{
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
    const int md_size = EVP_MD_size(digest);
    uint8_t *za = OPENSSL_zalloc(md_size);
    BIGNUM *e = NULL;

31
    if (hash == NULL || za == NULL) {
J
Jack Lloyd 已提交
32
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
33
        goto done;
34
    }
J
Jack Lloyd 已提交
35

36 37
    if (md_size < 0) {
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
J
Jack Lloyd 已提交
38
        goto done;
39
    }
J
Jack Lloyd 已提交
40

41
    if (!sm2_compute_userid_digest(za, digest, user_id, key)) {
42
        /* SM2err already called */
J
Jack Lloyd 已提交
43
        goto done;
44
    }
J
Jack Lloyd 已提交
45

46 47 48 49 50
    if (!EVP_DigestInit(hash, digest)
            || !EVP_DigestUpdate(hash, za, md_size)
            || !EVP_DigestUpdate(hash, msg, msg_len)
               /* reuse za buffer to hold H(ZA || M) */
            || !EVP_DigestFinal(hash, za, NULL)) {
J
Jack Lloyd 已提交
51
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
J
Jack Lloyd 已提交
52
        goto done;
53
    }
J
Jack Lloyd 已提交
54 55

    e = BN_bin2bn(za, md_size, NULL);
56 57
    if (e == NULL)
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
J
Jack Lloyd 已提交
58 59 60 61 62 63 64

 done:
    OPENSSL_free(za);
    EVP_MD_CTX_free(hash);
    return e;
}

65
static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
J
Jack Lloyd 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
    const BIGNUM *dA = EC_KEY_get0_private_key(key);
    const EC_GROUP *group = EC_KEY_get0_group(key);
    const BIGNUM *order = EC_GROUP_get0_order(group);
    ECDSA_SIG *sig = NULL;
    EC_POINT *kG = NULL;
    BN_CTX *ctx = NULL;
    BIGNUM *k = NULL;
    BIGNUM *rk = NULL;
    BIGNUM *r = NULL;
    BIGNUM *s = NULL;
    BIGNUM *x1 = NULL;
    BIGNUM *tmp = NULL;

    kG = EC_POINT_new(group);
    ctx = BN_CTX_new();
82
    if (kG == NULL || ctx == NULL) {
J
Jack Lloyd 已提交
83
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
84
        goto done;
85
    }
J
Jack Lloyd 已提交
86 87


88
    BN_CTX_start(ctx);
J
Jack Lloyd 已提交
89 90 91 92
    k = BN_CTX_get(ctx);
    rk = BN_CTX_get(ctx);
    x1 = BN_CTX_get(ctx);
    tmp = BN_CTX_get(ctx);
93 94
    if (tmp == NULL) {
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
95
        goto done;
96
    }
J
Jack Lloyd 已提交
97

98 99 100 101
    /*
     * These values are returned and so should not be allocated out of the
     * context
     */
J
Jack Lloyd 已提交
102 103 104
    r = BN_new();
    s = BN_new();

105
    if (r == NULL || s == NULL) {
J
Jack Lloyd 已提交
106
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
107
        goto done;
108
    }
J
Jack Lloyd 已提交
109 110

    for (;;) {
111 112 113 114
        if (!BN_priv_rand_range(k, order)) {
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
                goto done;
        }
J
Jack Lloyd 已提交
115

116 117 118 119 120
        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
                || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL,
                                                        ctx)
                || !BN_mod_add(r, e, x1, order, ctx)) {
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
J
Jack Lloyd 已提交
121
            goto done;
122
        }
J
Jack Lloyd 已提交
123 124 125 126 127

        /* try again if r == 0 or r+k == n */
        if (BN_is_zero(r))
            continue;

128 129 130 131
        if (!BN_add(rk, r, k)) {
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
            goto done;
        }
J
Jack Lloyd 已提交
132 133 134 135

        if (BN_cmp(rk, order) == 0)
            continue;

136
        if (!BN_add(s, dA, BN_value_one())
137
                || !ec_group_do_inverse_ord(group, s, s, ctx)
138 139 140 141 142 143
                || !BN_mod_mul(tmp, dA, r, order, ctx)
                || !BN_sub(tmp, k, tmp)
                || !BN_mod_mul(s, s, tmp, order, ctx)) {
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
            goto done;
        }
J
Jack Lloyd 已提交
144 145

        sig = ECDSA_SIG_new();
146
        if (sig == NULL) {
J
Jack Lloyd 已提交
147
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
148
            goto done;
149
        }
J
Jack Lloyd 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

         /* takes ownership of r and s */
        ECDSA_SIG_set0(sig, r, s);
        break;
    }

 done:
    if (sig == NULL) {
        BN_free(r);
        BN_free(s);
    }

    BN_CTX_free(ctx);
    EC_POINT_free(kG);
    return sig;
}

167
static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
168
                          const BIGNUM *e)
J
Jack Lloyd 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
{
    int ret = 0;
    const EC_GROUP *group = EC_KEY_get0_group(key);
    const BIGNUM *order = EC_GROUP_get0_order(group);
    BN_CTX *ctx = NULL;
    EC_POINT *pt = NULL;
    BIGNUM *t = NULL;
    BIGNUM *x1 = NULL;
    const BIGNUM *r = NULL;
    const BIGNUM *s = NULL;

    ctx = BN_CTX_new();
    pt = EC_POINT_new(group);
182
    if (ctx == NULL || pt == NULL) {
J
Jack Lloyd 已提交
183
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
184
        goto done;
185
    }
J
Jack Lloyd 已提交
186 187 188 189

    BN_CTX_start(ctx);
    t = BN_CTX_get(ctx);
    x1 = BN_CTX_get(ctx);
190 191 192 193
    if (x1 == NULL) {
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
        goto done;
    }
J
Jack Lloyd 已提交
194 195

    /*
196 197 198 199 200 201 202
     * B1: verify whether r' in [1,n-1], verification failed if not
     * B2: vefify whether s' in [1,n-1], verification failed if not
     * B3: set M'~=ZA || M'
     * B4: calculate e'=Hv(M'~)
     * B5: calculate t = (r' + s') modn, verification failed if t=0
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
     * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
J
Jack Lloyd 已提交
203 204 205 206
     */

    ECDSA_SIG_get0(sig, &r, &s);

207 208 209 210
    if (BN_cmp(r, BN_value_one()) < 0
            || BN_cmp(s, BN_value_one()) < 0
            || BN_cmp(order, r) <= 0
            || BN_cmp(order, s) <= 0) {
J
Jack Lloyd 已提交
211
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
J
Jack Lloyd 已提交
212
        goto done;
213
    }
J
Jack Lloyd 已提交
214

215
    if (!BN_mod_add(t, r, s, order, ctx)) {
J
Jack Lloyd 已提交
216
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
J
Jack Lloyd 已提交
217
        goto done;
218
    }
J
Jack Lloyd 已提交
219

220
    if (BN_is_zero(t)) {
J
Jack Lloyd 已提交
221
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
J
Jack Lloyd 已提交
222
        goto done;
223
    }
J
Jack Lloyd 已提交
224

225 226
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
            || !EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx)) {
J
Jack Lloyd 已提交
227
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
J
Jack Lloyd 已提交
228
        goto done;
229
    }
J
Jack Lloyd 已提交
230

231
    if (!BN_mod_add(t, e, x1, order, ctx)) {
J
Jack Lloyd 已提交
232
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
J
Jack Lloyd 已提交
233
        goto done;
234
    }
J
Jack Lloyd 已提交
235 236 237 238 239 240 241 242 243 244

    if (BN_cmp(r, t) == 0)
        ret = 1;

 done:
    EC_POINT_free(pt);
    BN_CTX_free(ctx);
    return ret;
}

245
ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
J
Jack Lloyd 已提交
246 247 248 249 250 251
                       const EVP_MD *digest,
                       const char *user_id, const uint8_t *msg, size_t msg_len)
{
    BIGNUM *e = NULL;
    ECDSA_SIG *sig = NULL;

252
    e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
253 254
    if (e == NULL) {
        /* SM2err already called */
J
Jack Lloyd 已提交
255
        goto done;
256
    }
J
Jack Lloyd 已提交
257

258
    sig = sm2_sig_gen(key, e);
J
Jack Lloyd 已提交
259 260 261 262 263 264

 done:
    BN_free(e);
    return sig;
}

265
int sm2_do_verify(const EC_KEY *key,
J
Jack Lloyd 已提交
266 267 268 269 270
                  const EVP_MD *digest,
                  const ECDSA_SIG *sig,
                  const char *user_id, const uint8_t *msg, size_t msg_len)
{
    BIGNUM *e = NULL;
271
    int ret = 0;
J
Jack Lloyd 已提交
272

273
    e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
274 275
    if (e == NULL) {
        /* SM2err already called */
J
Jack Lloyd 已提交
276
        goto done;
277
    }
J
Jack Lloyd 已提交
278

279
    ret = sm2_sig_verify(key, sig, e);
J
Jack Lloyd 已提交
280 281 282 283 284 285

 done:
    BN_free(e);
    return ret;
}

286
int sm2_sign(const unsigned char *dgst, int dgstlen,
J
Jack Lloyd 已提交
287 288 289 290
             unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
{
    BIGNUM *e = NULL;
    ECDSA_SIG *s = NULL;
291
    int sigleni;
J
Jack Lloyd 已提交
292 293 294
    int ret = -1;

    e = BN_bin2bn(dgst, dgstlen, NULL);
295 296 297 298
    if (e == NULL) {
       SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
       goto done;
    }
J
Jack Lloyd 已提交
299

300
    s = sm2_sig_gen(eckey, e);
J
Jack Lloyd 已提交
301

302 303 304 305 306 307
    sigleni = i2d_ECDSA_SIG(s, &sig);
    if (sigleni < 0) {
       SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
       goto done;
    }
    *siglen = (unsigned int)sigleni;
J
Jack Lloyd 已提交
308

309
    ret = 1;
J
Jack Lloyd 已提交
310 311 312 313 314 315 316

 done:
    ECDSA_SIG_free(s);
    BN_free(e);
    return ret;
}

317
int sm2_verify(const unsigned char *dgst, int dgstlen,
J
Jack Lloyd 已提交
318 319 320 321 322 323 324 325 326 327
               const unsigned char *sig, int sig_len, EC_KEY *eckey)
{
    ECDSA_SIG *s = NULL;
    BIGNUM *e = NULL;
    const unsigned char *p = sig;
    unsigned char *der = NULL;
    int derlen = -1;
    int ret = -1;

    s = ECDSA_SIG_new();
328
    if (s == NULL) {
J
Jack Lloyd 已提交
329
        SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
J
Jack Lloyd 已提交
330
        goto done;
331 332
    }
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
J
Jack Lloyd 已提交
333
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
J
Jack Lloyd 已提交
334
        goto done;
335
    }
J
Jack Lloyd 已提交
336 337
    /* Ensure signature uses DER and doesn't have trailing garbage */
    derlen = i2d_ECDSA_SIG(s, &der);
338
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
J
Jack Lloyd 已提交
339
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
J
Jack Lloyd 已提交
340
        goto done;
341
    }
J
Jack Lloyd 已提交
342 343

    e = BN_bin2bn(dgst, dgstlen, NULL);
344 345 346 347
    if (e == NULL) {
        SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
        goto done;
    }
J
Jack Lloyd 已提交
348

349
    ret = sm2_sig_verify(eckey, s, e);
J
Jack Lloyd 已提交
350 351 352 353 354 355 356

 done:
    OPENSSL_free(der);
    BN_free(e);
    ECDSA_SIG_free(s);
    return ret;
}