Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/sm2/sm2_sign.c
Line
Count
Source (jump to first uncovered line)
1
/*
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.
5
 *
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
10
 */
11
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>
19
#include <string.h>
20
21
static BIGNUM *sm2_compute_msg_hash(const EVP_MD *digest,
22
                                    const EC_KEY *key,
23
                                    const char *user_id,
24
                                    const uint8_t *msg, size_t msg_len)
25
0
{
26
0
    EVP_MD_CTX *hash = EVP_MD_CTX_new();
27
0
    const int md_size = EVP_MD_size(digest);
28
0
    uint8_t *za = NULL;
29
0
    BIGNUM *e = NULL;
30
0
31
0
    if (md_size < 0) {
32
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
33
0
        goto done;
34
0
    }
35
0
36
0
    za = OPENSSL_zalloc(md_size);
37
0
    if (hash == NULL || za == NULL) {
38
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
39
0
        goto done;
40
0
    }
41
0
42
0
    if (!sm2_compute_userid_digest(za, digest, user_id, key)) {
43
0
        /* SM2err already called */
44
0
        goto done;
45
0
    }
46
0
47
0
    if (!EVP_DigestInit(hash, digest)
48
0
            || !EVP_DigestUpdate(hash, za, md_size)
49
0
            || !EVP_DigestUpdate(hash, msg, msg_len)
50
0
               /* reuse za buffer to hold H(ZA || M) */
51
0
            || !EVP_DigestFinal(hash, za, NULL)) {
52
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
53
0
        goto done;
54
0
    }
55
0
56
0
    e = BN_bin2bn(za, md_size, NULL);
57
0
    if (e == NULL)
58
0
        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
59
0
60
0
 done:
61
0
    OPENSSL_free(za);
62
0
    EVP_MD_CTX_free(hash);
63
0
    return e;
64
0
}
65
66
static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)
67
0
{
68
0
    const BIGNUM *dA = EC_KEY_get0_private_key(key);
69
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
70
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
71
0
    ECDSA_SIG *sig = NULL;
72
0
    EC_POINT *kG = NULL;
73
0
    BN_CTX *ctx = NULL;
74
0
    BIGNUM *k = NULL;
75
0
    BIGNUM *rk = NULL;
76
0
    BIGNUM *r = NULL;
77
0
    BIGNUM *s = NULL;
78
0
    BIGNUM *x1 = NULL;
79
0
    BIGNUM *tmp = NULL;
80
0
81
0
    kG = EC_POINT_new(group);
82
0
    ctx = BN_CTX_new();
83
0
    if (kG == NULL || ctx == NULL) {
84
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
85
0
        goto done;
86
0
    }
87
0
88
0
89
0
    BN_CTX_start(ctx);
90
0
    k = BN_CTX_get(ctx);
91
0
    rk = BN_CTX_get(ctx);
92
0
    x1 = BN_CTX_get(ctx);
93
0
    tmp = BN_CTX_get(ctx);
94
0
    if (tmp == NULL) {
95
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
96
0
        goto done;
97
0
    }
98
0
99
0
    /*
100
0
     * These values are returned and so should not be allocated out of the
101
0
     * context
102
0
     */
103
0
    r = BN_new();
104
0
    s = BN_new();
105
0
106
0
    if (r == NULL || s == NULL) {
107
0
        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
108
0
        goto done;
109
0
    }
110
0
111
0
    for (;;) {
112
0
        if (!BN_priv_rand_range(k, order)) {
113
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
114
0
            goto done;
115
0
        }
116
0
117
0
        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
118
0
                || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,
119
0
                                                        ctx)
120
0
                || !BN_mod_add(r, e, x1, order, ctx)) {
121
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
122
0
            goto done;
123
0
        }
124
0
125
0
        /* try again if r == 0 or r+k == n */
126
0
        if (BN_is_zero(r))
127
0
            continue;
128
0
129
0
        if (!BN_add(rk, r, k)) {
130
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
131
0
            goto done;
132
0
        }
133
0
134
0
        if (BN_cmp(rk, order) == 0)
135
0
            continue;
136
0
137
0
        if (!BN_add(s, dA, BN_value_one())
138
0
                || !ec_group_do_inverse_ord(group, s, s, ctx)
139
0
                || !BN_mod_mul(tmp, dA, r, order, ctx)
140
0
                || !BN_sub(tmp, k, tmp)
141
0
                || !BN_mod_mul(s, s, tmp, order, ctx)) {
142
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
143
0
            goto done;
144
0
        }
145
0
146
0
        sig = ECDSA_SIG_new();
147
0
        if (sig == NULL) {
148
0
            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
149
0
            goto done;
150
0
        }
151
0
152
0
         /* takes ownership of r and s */
153
0
        ECDSA_SIG_set0(sig, r, s);
154
0
        break;
155
0
    }
156
0
157
0
 done:
158
0
    if (sig == NULL) {
159
0
        BN_free(r);
160
0
        BN_free(s);
161
0
    }
162
0
163
0
    BN_CTX_free(ctx);
164
0
    EC_POINT_free(kG);
165
0
    return sig;
166
0
}
167
168
static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
169
                          const BIGNUM *e)
170
0
{
171
0
    int ret = 0;
172
0
    const EC_GROUP *group = EC_KEY_get0_group(key);
173
0
    const BIGNUM *order = EC_GROUP_get0_order(group);
174
0
    BN_CTX *ctx = NULL;
175
0
    EC_POINT *pt = NULL;
176
0
    BIGNUM *t = NULL;
177
0
    BIGNUM *x1 = NULL;
178
0
    const BIGNUM *r = NULL;
179
0
    const BIGNUM *s = NULL;
180
0
181
0
    ctx = BN_CTX_new();
182
0
    pt = EC_POINT_new(group);
183
0
    if (ctx == NULL || pt == NULL) {
184
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
185
0
        goto done;
186
0
    }
187
0
188
0
    BN_CTX_start(ctx);
189
0
    t = BN_CTX_get(ctx);
190
0
    x1 = BN_CTX_get(ctx);
191
0
    if (x1 == NULL) {
192
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
193
0
        goto done;
194
0
    }
195
0
196
0
    /*
197
0
     * B1: verify whether r' in [1,n-1], verification failed if not
198
0
     * B2: vefify whether s' in [1,n-1], verification failed if not
199
0
     * B3: set M'~=ZA || M'
200
0
     * B4: calculate e'=Hv(M'~)
201
0
     * B5: calculate t = (r' + s') modn, verification failed if t=0
202
0
     * B6: calculate the point (x1', y1')=[s']G + [t]PA
203
0
     * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
204
0
     */
205
0
206
0
    ECDSA_SIG_get0(sig, &r, &s);
207
0
208
0
    if (BN_cmp(r, BN_value_one()) < 0
209
0
            || BN_cmp(s, BN_value_one()) < 0
210
0
            || BN_cmp(order, r) <= 0
211
0
            || BN_cmp(order, s) <= 0) {
212
0
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
213
0
        goto done;
214
0
    }
215
0
216
0
    if (!BN_mod_add(t, r, s, order, ctx)) {
217
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
218
0
        goto done;
219
0
    }
220
0
221
0
    if (BN_is_zero(t)) {
222
0
        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
223
0
        goto done;
224
0
    }
225
0
226
0
    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
227
0
            || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {
228
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
229
0
        goto done;
230
0
    }
231
0
232
0
    if (!BN_mod_add(t, e, x1, order, ctx)) {
233
0
        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
234
0
        goto done;
235
0
    }
236
0
237
0
    if (BN_cmp(r, t) == 0)
238
0
        ret = 1;
239
0
240
0
 done:
241
0
    EC_POINT_free(pt);
242
0
    BN_CTX_free(ctx);
243
0
    return ret;
244
0
}
245
246
ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
247
                       const EVP_MD *digest,
248
                       const char *user_id, const uint8_t *msg, size_t msg_len)
249
0
{
250
0
    BIGNUM *e = NULL;
251
0
    ECDSA_SIG *sig = NULL;
252
0
253
0
    e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
254
0
    if (e == NULL) {
255
0
        /* SM2err already called */
256
0
        goto done;
257
0
    }
258
0
259
0
    sig = sm2_sig_gen(key, e);
260
0
261
0
 done:
262
0
    BN_free(e);
263
0
    return sig;
264
0
}
265
266
int sm2_do_verify(const EC_KEY *key,
267
                  const EVP_MD *digest,
268
                  const ECDSA_SIG *sig,
269
                  const char *user_id, const uint8_t *msg, size_t msg_len)
270
0
{
271
0
    BIGNUM *e = NULL;
272
0
    int ret = 0;
273
0
274
0
    e = sm2_compute_msg_hash(digest, key, user_id, msg, msg_len);
275
0
    if (e == NULL) {
276
0
        /* SM2err already called */
277
0
        goto done;
278
0
    }
279
0
280
0
    ret = sm2_sig_verify(key, sig, e);
281
0
282
0
 done:
283
0
    BN_free(e);
284
0
    return ret;
285
0
}
286
287
int sm2_sign(const unsigned char *dgst, int dgstlen,
288
             unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
289
0
{
290
0
    BIGNUM *e = NULL;
291
0
    ECDSA_SIG *s = NULL;
292
0
    int sigleni;
293
0
    int ret = -1;
294
0
295
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
296
0
    if (e == NULL) {
297
0
       SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
298
0
       goto done;
299
0
    }
300
0
301
0
    s = sm2_sig_gen(eckey, e);
302
0
303
0
    sigleni = i2d_ECDSA_SIG(s, &sig);
304
0
    if (sigleni < 0) {
305
0
       SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
306
0
       goto done;
307
0
    }
308
0
    *siglen = (unsigned int)sigleni;
309
0
310
0
    ret = 1;
311
0
312
0
 done:
313
0
    ECDSA_SIG_free(s);
314
0
    BN_free(e);
315
0
    return ret;
316
0
}
317
318
int sm2_verify(const unsigned char *dgst, int dgstlen,
319
               const unsigned char *sig, int sig_len, EC_KEY *eckey)
320
0
{
321
0
    ECDSA_SIG *s = NULL;
322
0
    BIGNUM *e = NULL;
323
0
    const unsigned char *p = sig;
324
0
    unsigned char *der = NULL;
325
0
    int derlen = -1;
326
0
    int ret = -1;
327
0
328
0
    s = ECDSA_SIG_new();
329
0
    if (s == NULL) {
330
0
        SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
331
0
        goto done;
332
0
    }
333
0
    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
334
0
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
335
0
        goto done;
336
0
    }
337
0
    /* Ensure signature uses DER and doesn't have trailing garbage */
338
0
    derlen = i2d_ECDSA_SIG(s, &der);
339
0
    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
340
0
        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
341
0
        goto done;
342
0
    }
343
0
344
0
    e = BN_bin2bn(dgst, dgstlen, NULL);
345
0
    if (e == NULL) {
346
0
        SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
347
0
        goto done;
348
0
    }
349
0
350
0
    ret = sm2_sig_verify(eckey, s, e);
351
0
352
0
 done:
353
0
    OPENSSL_free(der);
354
0
    BN_free(e);
355
0
    ECDSA_SIG_free(s);
356
0
    return ret;
357
0
}