Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/sm2/sm2_pmeth.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2018 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 "internal/evp_int.h"
15
#include "internal/sm2.h"
16
#include "internal/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
} SM2_PKEY_CTX;
26
27
static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
28
0
{
29
0
    SM2_PKEY_CTX *dctx;
30
0
31
0
    if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
32
0
        SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
33
0
        return 0;
34
0
    }
35
0
36
0
    ctx->data = dctx;
37
0
    return 1;
38
0
}
39
40
static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
41
0
{
42
0
    SM2_PKEY_CTX *dctx = ctx->data;
43
0
44
0
    if (dctx != NULL) {
45
0
        EC_GROUP_free(dctx->gen_group);
46
0
        OPENSSL_free(dctx);
47
0
        ctx->data = NULL;
48
0
    }
49
0
}
50
51
static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
52
0
{
53
0
    SM2_PKEY_CTX *dctx, *sctx;
54
0
55
0
    if (!pkey_sm2_init(dst))
56
0
        return 0;
57
0
    sctx = src->data;
58
0
    dctx = dst->data;
59
0
    if (sctx->gen_group != NULL) {
60
0
        dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
61
0
        if (dctx->gen_group == NULL) {
62
0
            pkey_sm2_cleanup(dst);
63
0
            return 0;
64
0
        }
65
0
    }
66
0
    dctx->md = sctx->md;
67
0
68
0
    return 1;
69
0
}
70
71
static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
72
                         const unsigned char *tbs, size_t tbslen)
73
0
{
74
0
    int ret;
75
0
    unsigned int sltmp;
76
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
77
0
    const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
78
0
79
0
    if (sig_sz <= 0) {
80
0
        return 0;
81
0
    }
82
0
83
0
    if (sig == NULL) {
84
0
        *siglen = (size_t)sig_sz;
85
0
        return 1;
86
0
    }
87
0
88
0
    if (*siglen < (size_t)sig_sz) {
89
0
        SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
90
0
        return 0;
91
0
    }
92
0
93
0
    ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
94
0
95
0
    if (ret <= 0)
96
0
        return ret;
97
0
    *siglen = (size_t)sltmp;
98
0
    return 1;
99
0
}
100
101
static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
102
                           const unsigned char *sig, size_t siglen,
103
                           const unsigned char *tbs, size_t tbslen)
104
0
{
105
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
106
0
107
0
    return sm2_verify(tbs, tbslen, sig, siglen, ec);
108
0
}
109
110
static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
111
                            unsigned char *out, size_t *outlen,
112
                            const unsigned char *in, size_t inlen)
113
0
{
114
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
115
0
    SM2_PKEY_CTX *dctx = ctx->data;
116
0
    const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
117
0
118
0
    if (out == NULL) {
119
0
        if (!sm2_ciphertext_size(ec, md, inlen, outlen))
120
0
            return -1;
121
0
        else
122
0
            return 1;
123
0
    }
124
0
125
0
    return sm2_encrypt(ec, md, in, inlen, out, outlen);
126
0
}
127
128
static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
129
                            unsigned char *out, size_t *outlen,
130
                            const unsigned char *in, size_t inlen)
131
0
{
132
0
    EC_KEY *ec = ctx->pkey->pkey.ec;
133
0
    SM2_PKEY_CTX *dctx = ctx->data;
134
0
    const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
135
0
136
0
    if (out == NULL) {
137
0
        if (!sm2_plaintext_size(ec, md, inlen, outlen))
138
0
            return -1;
139
0
        else
140
0
            return 1;
141
0
    }
142
0
143
0
    return sm2_decrypt(ec, md, in, inlen, out, outlen);
144
0
}
145
146
static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
147
0
{
148
0
    SM2_PKEY_CTX *dctx = ctx->data;
149
0
    EC_GROUP *group;
150
0
151
0
    switch (type) {
152
0
    case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
153
0
        group = EC_GROUP_new_by_curve_name(p1);
154
0
        if (group == NULL) {
155
0
            SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
156
0
            return 0;
157
0
        }
158
0
        EC_GROUP_free(dctx->gen_group);
159
0
        dctx->gen_group = group;
160
0
        return 1;
161
0
162
0
    case EVP_PKEY_CTRL_EC_PARAM_ENC:
163
0
        if (dctx->gen_group == NULL) {
164
0
            SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
165
0
            return 0;
166
0
        }
167
0
        EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
168
0
        return 1;
169
0
170
0
    case EVP_PKEY_CTRL_MD:
171
0
        dctx->md = p2;
172
0
        return 1;
173
0
174
0
    case EVP_PKEY_CTRL_GET_MD:
175
0
        *(const EVP_MD **)p2 = dctx->md;
176
0
        return 1;
177
0
178
0
    default:
179
0
        return -2;
180
0
181
0
    }
182
0
}
183
184
static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
185
                             const char *type, const char *value)
186
{
187
    if (strcmp(type, "ec_paramgen_curve") == 0) {
188
        int nid = NID_undef;
189
190
        if (((nid = EC_curve_nist2nid(value)) == NID_undef)
191
            && ((nid = OBJ_sn2nid(value)) == NID_undef)
192
            && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
193
            SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
194
            return 0;
195
        }
196
        return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
197
    } else if (strcmp(type, "ec_param_enc") == 0) {
198
        int param_enc;
199
200
        if (strcmp(value, "explicit") == 0)
201
            param_enc = 0;
202
        else if (strcmp(value, "named_curve") == 0)
203
            param_enc = OPENSSL_EC_NAMED_CURVE;
204
        else
205
            return -2;
206
        return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
207
    }
208
209
    return -2;
210
}
211
212
const EVP_PKEY_METHOD sm2_pkey_meth = {
213
    EVP_PKEY_SM2,
214
    0,
215
    pkey_sm2_init,
216
    pkey_sm2_copy,
217
    pkey_sm2_cleanup,
218
219
    0,
220
    0,
221
222
    0,
223
    0,
224
225
    0,
226
    pkey_sm2_sign,
227
228
    0,
229
    pkey_sm2_verify,
230
231
    0, 0,
232
233
    0, 0, 0, 0,
234
235
    0,
236
    pkey_sm2_encrypt,
237
238
    0,
239
    pkey_sm2_decrypt,
240
241
    0,
242
    0,
243
    pkey_sm2_ctrl,
244
    pkey_sm2_ctrl_str
245
};