Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/sm2/sm2_pmeth.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/cryptlib.h"
11
#include <openssl/asn1t.h>
12
#include <openssl/ec.h>
13
#include <openssl/evp.h>
14
#include "crypto/evp.h"
15
#include "crypto/sm2.h"
16
#include "crypto/sm2err.h"
17
18
/* EC pkey context structure */
19
20
typedef struct {
21
    /* Key and paramgen group */
22
    EC_GROUP *gen_group;
23
    /* message digest */
24
    const EVP_MD *md;
25
    /* Distinguishing Identifier, ISO/IEC 15946-3 */
26
    uint8_t *id;
27
    size_t id_len;
28
    /* id_set indicates if the 'id' field is set (1) or not (0) */
29
    int id_set;
30
} SM2_PKEY_CTX;
31
32
static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
33
0
{
34
0
    SM2_PKEY_CTX *smctx;
35
36
0
    if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) {
37
0
        SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
38
0
        return 0;
39
0
    }
40
41
0
    ctx->data = smctx;
42
0
    return 1;
43
0
}
44
45
static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
46
0
{
47
0
    SM2_PKEY_CTX *smctx = ctx->data;
48
49
0
    if (smctx != NULL) {
50
0
        EC_GROUP_free(smctx->gen_group);
51
0
        OPENSSL_free(smctx->id);
52
0
        OPENSSL_free(smctx);
53
0
        ctx->data = NULL;
54
0
    }
55
0
}
56
57
static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
58
0
{
59
0
    SM2_PKEY_CTX *dctx, *sctx;
60
61
0
    if (!pkey_sm2_init(dst))
62
0
        return 0;
63
0
    sctx = src->data;
64
0
    dctx = dst->data;
65
0
    if (sctx->gen_group != NULL) {
66
0
        dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
67
0
        if (dctx->gen_group == NULL) {
68
0
            pkey_sm2_cleanup(dst);
69
0
            return 0;
70
0
        }
71
0
    }
72
0
    if (sctx->id != NULL) {
73
0
        dctx->id = OPENSSL_malloc(sctx->id_len);
74
0
        if (dctx->id == NULL) {
75
0
            SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE);
76
0
            pkey_sm2_cleanup(dst);
77
0
            return 0;
78
0
        }
79
0
        memcpy(dctx->id, sctx->id, sctx->id_len);
80
0
    }
81
0
    dctx->id_len = sctx->id_len;
82
0
    dctx->id_set = sctx->id_set;
83
0
    dctx->md = sctx->md;
84
85
0
    return 1;
86
0
}
87
88
static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
89
                         const unsigned char *tbs, size_t tbslen)
90
0
{
91
0
    int ret;
92
0
    unsigned int sltmp;
93
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
94
0
    const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
95
96
0
    if (sig_sz <= 0) {
97
0
        return 0;
98
0
    }
99
100
0
    if (sig == NULL) {
101
0
        *siglen = (size_t)sig_sz;
102
0
        return 1;
103
0
    }
104
105
0
    if (*siglen < (size_t)sig_sz) {
106
0
        SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
107
0
        return 0;
108
0
    }
109
110
0
    ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
111
112
0
    if (ret <= 0)
113
0
        return ret;
114
0
    *siglen = (size_t)sltmp;
115
0
    return 1;
116
0
}
117
118
static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
119
                           const unsigned char *sig, size_t siglen,
120
                           const unsigned char *tbs, size_t tbslen)
121
0
{
122
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
123
124
0
    return sm2_verify(tbs, tbslen, sig, siglen, ec);
125
0
}
126
127
static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
128
                            unsigned char *out, size_t *outlen,
129
                            const unsigned char *in, size_t inlen)
130
0
{
131
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
132
0
    SM2_PKEY_CTX *dctx = ctx->data;
133
0
    const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
134
135
0
    if (out == NULL) {
136
0
        if (!sm2_ciphertext_size(ec, md, inlen, outlen))
137
0
            return -1;
138
0
        else
139
0
            return 1;
140
0
    }
141
142
0
    return sm2_encrypt(ec, md, in, inlen, out, outlen);
143
0
}
144
145
static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
146
                            unsigned char *out, size_t *outlen,
147
                            const unsigned char *in, size_t inlen)
148
0
{
149
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
150
0
    SM2_PKEY_CTX *dctx = ctx->data;
151
0
    const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
152
153
0
    if (out == NULL) {
154
0
        if (!sm2_plaintext_size(in, inlen, outlen))
155
0
            return -1;
156
0
        else
157
0
            return 1;
158
0
    }
159
160
0
    return sm2_decrypt(ec, md, in, inlen, out, outlen);
161
0
}
162
163
static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
164
0
{
165
0
    SM2_PKEY_CTX *smctx = ctx->data;
166
0
    EC_GROUP *group;
167
0
    uint8_t *tmp_id;
168
169
0
    switch (type) {
170
0
    case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
171
0
        group = EC_GROUP_new_by_curve_name(p1);
172
0
        if (group == NULL) {
173
0
            SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
174
0
            return 0;
175
0
        }
176
0
        EC_GROUP_free(smctx->gen_group);
177
0
        smctx->gen_group = group;
178
0
        return 1;
179
180
0
    case EVP_PKEY_CTRL_EC_PARAM_ENC:
181
0
        if (smctx->gen_group == NULL) {
182
0
            SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
183
0
            return 0;
184
0
        }
185
0
        EC_GROUP_set_asn1_flag(smctx->gen_group, p1);
186
0
        return 1;
187
188
0
    case EVP_PKEY_CTRL_MD:
189
0
        smctx->md = p2;
190
0
        return 1;
191
192
0
    case EVP_PKEY_CTRL_GET_MD:
193
0
        *(const EVP_MD **)p2 = smctx->md;
194
0
        return 1;
195
196
0
    case EVP_PKEY_CTRL_SET1_ID:
197
0
        if (p1 > 0) {
198
0
            tmp_id = OPENSSL_malloc(p1);
199
0
            if (tmp_id == NULL) {
200
0
                SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE);
201
0
                return 0;
202
0
            }
203
0
            memcpy(tmp_id, p2, p1);
204
0
            OPENSSL_free(smctx->id);
205
0
            smctx->id = tmp_id;
206
0
        } else {
207
            /* set null-ID */
208
0
            OPENSSL_free(smctx->id);
209
0
            smctx->id = NULL;
210
0
        }
211
0
        smctx->id_len = (size_t)p1;
212
0
        smctx->id_set = 1;
213
0
        return 1;
214
215
0
    case EVP_PKEY_CTRL_GET1_ID:
216
0
        memcpy(p2, smctx->id, smctx->id_len);
217
0
        return 1;
218
219
0
    case EVP_PKEY_CTRL_GET1_ID_LEN:
220
0
        *(size_t *)p2 = smctx->id_len;
221
0
        return 1;
222
223
0
    case EVP_PKEY_CTRL_DIGESTINIT:
224
        /* nothing to be inited, this is to suppress the error... */
225
0
        return 1;
226
227
0
    default:
228
0
        return -2;
229
0
    }
230
0
}
231
232
static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
233
                             const char *type, const char *value)
234
0
{
235
0
    if (strcmp(type, "ec_paramgen_curve") == 0) {
236
0
        int nid = NID_undef;
237
238
0
        if (((nid = EC_curve_nist2nid(value)) == NID_undef)
239
0
            && ((nid = OBJ_sn2nid(value)) == NID_undef)
240
0
            && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
241
0
            SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
242
0
            return 0;
243
0
        }
244
0
        return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
245
0
    } else if (strcmp(type, "ec_param_enc") == 0) {
246
0
        int param_enc;
247
248
0
        if (strcmp(value, "explicit") == 0)
249
0
            param_enc = 0;
250
0
        else if (strcmp(value, "named_curve") == 0)
251
0
            param_enc = OPENSSL_EC_NAMED_CURVE;
252
0
        else
253
0
            return -2;
254
0
        return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
255
0
    }
256
257
0
    return -2;
258
0
}
259
260
static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
261
0
{
262
0
    uint8_t z[EVP_MAX_MD_SIZE];
263
0
    SM2_PKEY_CTX *smctx = ctx->data;
264
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
265
0
    const EVP_MD *md = EVP_MD_CTX_md(mctx);
266
0
    int mdlen = EVP_MD_size(md);
267
268
0
    if (!smctx->id_set) {
269
        /*
270
         * An ID value must be set. The specifications are not clear whether a
271
         * NULL is allowed. We only allow it if set explicitly for maximum
272
         * flexibility.
273
         */
274
0
        SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET);
275
0
        return 0;
276
0
    }
277
278
0
    if (mdlen < 0) {
279
0
        SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
280
0
        return 0;
281
0
    }
282
283
    /* get hashed prefix 'z' of tbs message */
284
0
    if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
285
0
        return 0;
286
287
0
    return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
288
0
}
289
290
const EVP_PKEY_METHOD sm2_pkey_meth = {
291
    EVP_PKEY_SM2,
292
    0,
293
    pkey_sm2_init,
294
    pkey_sm2_copy,
295
    pkey_sm2_cleanup,
296
297
    0,
298
    0,
299
300
    0,
301
    0,
302
303
    0,
304
    pkey_sm2_sign,
305
306
    0,
307
    pkey_sm2_verify,
308
309
    0, 0,
310
311
    0, 0, 0, 0,
312
313
    0,
314
    pkey_sm2_encrypt,
315
316
    0,
317
    pkey_sm2_decrypt,
318
319
    0,
320
    0,
321
    pkey_sm2_ctrl,
322
    pkey_sm2_ctrl_str,
323
324
    0, 0,
325
326
    0, 0, 0,
327
328
    pkey_sm2_digest_custom
329
};