Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/asymciphers/sm2_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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
11
#include "internal/deprecated.h"
12
13
#include <openssl/crypto.h>
14
#include <openssl/evp.h>
15
#include <openssl/core_dispatch.h>
16
#include <openssl/core_names.h>
17
#include <openssl/params.h>
18
#include <openssl/err.h>
19
#include <openssl/proverr.h>
20
#include "crypto/sm2.h"
21
#include "internal/cryptlib.h"
22
#include "prov/provider_ctx.h"
23
#include "prov/implementations.h"
24
#include "prov/providercommon.h"
25
#include "prov/provider_util.h"
26
27
static OSSL_FUNC_asym_cipher_newctx_fn sm2_newctx;
28
static OSSL_FUNC_asym_cipher_encrypt_init_fn sm2_init;
29
static OSSL_FUNC_asym_cipher_encrypt_fn sm2_asym_encrypt;
30
static OSSL_FUNC_asym_cipher_decrypt_init_fn sm2_init;
31
static OSSL_FUNC_asym_cipher_decrypt_fn sm2_asym_decrypt;
32
static OSSL_FUNC_asym_cipher_freectx_fn sm2_freectx;
33
static OSSL_FUNC_asym_cipher_dupctx_fn sm2_dupctx;
34
static OSSL_FUNC_asym_cipher_get_ctx_params_fn sm2_get_ctx_params;
35
static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn sm2_gettable_ctx_params;
36
static OSSL_FUNC_asym_cipher_set_ctx_params_fn sm2_set_ctx_params;
37
static OSSL_FUNC_asym_cipher_settable_ctx_params_fn sm2_settable_ctx_params;
38
39
/*
40
 * What's passed as an actual key is defined by the KEYMGMT interface.
41
 * We happen to know that our KEYMGMT simply passes EC_KEY structures, so
42
 * we use that here too.
43
 */
44
45
typedef struct {
46
    OSSL_LIB_CTX *libctx;
47
    EC_KEY *key;
48
    PROV_DIGEST md;
49
} PROV_SM2_CTX;
50
51
static void *sm2_newctx(void *provctx)
52
0
{
53
0
    PROV_SM2_CTX *psm2ctx =  OPENSSL_zalloc(sizeof(PROV_SM2_CTX));
54
55
0
    if (psm2ctx == NULL)
56
0
        return NULL;
57
0
    psm2ctx->libctx = PROV_LIBCTX_OF(provctx);
58
59
0
    return psm2ctx;
60
0
}
61
62
static int sm2_init(void *vpsm2ctx, void *vkey, const OSSL_PARAM params[])
63
0
{
64
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
65
66
0
    if (psm2ctx == NULL || vkey == NULL || !EC_KEY_up_ref(vkey))
67
0
        return 0;
68
0
    EC_KEY_free(psm2ctx->key);
69
0
    psm2ctx->key = vkey;
70
71
0
    return sm2_set_ctx_params(psm2ctx, params);
72
0
}
73
74
static const EVP_MD *sm2_get_md(PROV_SM2_CTX *psm2ctx)
75
0
{
76
0
    const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
77
78
0
    if (md == NULL)
79
0
        md = ossl_prov_digest_fetch(&psm2ctx->md, psm2ctx->libctx, "SM3", NULL);
80
81
0
    return md;
82
0
}
83
84
static int sm2_asym_encrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
85
                            size_t outsize, const unsigned char *in,
86
                            size_t inlen)
87
0
{
88
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
89
0
    const EVP_MD *md = sm2_get_md(psm2ctx);
90
91
0
    if (md == NULL)
92
0
        return 0;
93
94
0
    if (out == NULL) {
95
0
        if (!ossl_sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) {
96
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
97
0
            return 0;
98
0
        }
99
0
        return 1;
100
0
    }
101
102
0
    return ossl_sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen);
103
0
}
104
105
static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
106
                            size_t outsize, const unsigned char *in,
107
                            size_t inlen)
108
0
{
109
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
110
0
    const EVP_MD *md = sm2_get_md(psm2ctx);
111
112
0
    if (md == NULL)
113
0
        return 0;
114
115
0
    if (out == NULL) {
116
0
        if (!ossl_sm2_plaintext_size(in, inlen, outlen))
117
0
            return 0;
118
0
        return 1;
119
0
    }
120
121
0
    return ossl_sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen);
122
0
}
123
124
static void sm2_freectx(void *vpsm2ctx)
125
0
{
126
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
127
128
0
    EC_KEY_free(psm2ctx->key);
129
0
    ossl_prov_digest_reset(&psm2ctx->md);
130
131
0
    OPENSSL_free(psm2ctx);
132
0
}
133
134
static void *sm2_dupctx(void *vpsm2ctx)
135
0
{
136
0
    PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;
137
0
    PROV_SM2_CTX *dstctx;
138
139
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
140
0
    if (dstctx == NULL)
141
0
        return NULL;
142
143
0
    *dstctx = *srcctx;
144
0
    memset(&dstctx->md, 0, sizeof(dstctx->md));
145
146
0
    if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) {
147
0
        OPENSSL_free(dstctx);
148
0
        return NULL;
149
0
    }
150
151
0
    if (!ossl_prov_digest_copy(&dstctx->md, &srcctx->md)) {
152
0
        sm2_freectx(dstctx);
153
0
        return NULL;
154
0
    }
155
156
0
    return dstctx;
157
0
}
158
159
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
160
#ifndef sm2_get_ctx_params_list
161
static const OSSL_PARAM sm2_get_ctx_params_list[] = {
162
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
163
    OSSL_PARAM_END
164
};
165
#endif
166
167
#ifndef sm2_get_ctx_params_st
168
struct sm2_get_ctx_params_st {
169
    OSSL_PARAM *digest;
170
};
171
#endif
172
173
#ifndef sm2_get_ctx_params_decoder
174
static int sm2_get_ctx_params_decoder
175
    (const OSSL_PARAM *p, struct sm2_get_ctx_params_st *r)
176
0
{
177
0
    const char *s;
178
179
0
    memset(r, 0, sizeof(*r));
180
0
    if (p != NULL)
181
0
        for (; (s = p->key) != NULL; p++)
182
0
            if (ossl_likely(strcmp("digest", s + 0) == 0)) {
183
                /* ASYM_CIPHER_PARAM_DIGEST */
184
0
                if (ossl_unlikely(r->digest != NULL)) {
185
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
186
0
                                   "param %s is repeated", s);
187
0
                    return 0;
188
0
                }
189
0
                r->digest = (OSSL_PARAM *)p;
190
0
            }
191
0
    return 1;
192
0
}
193
#endif
194
/* End of machine generated */
195
196
static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
197
0
{
198
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
199
0
    struct sm2_get_ctx_params_st p;
200
201
0
    if (psm2ctx == NULL || !sm2_get_ctx_params_decoder(params, &p))
202
0
        return 0;
203
204
0
    if (p.digest != NULL) {
205
0
        const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
206
207
0
        if (!OSSL_PARAM_set_utf8_string(p.digest,
208
0
                                        md == NULL ? "" : EVP_MD_get0_name(md)))
209
0
            return 0;
210
0
    }
211
212
0
    return 1;
213
0
}
214
215
static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,
216
                                                 ossl_unused void *provctx)
217
0
{
218
0
    return sm2_get_ctx_params_list;
219
0
}
220
221
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
222
#ifndef sm2_set_ctx_params_list
223
static const OSSL_PARAM sm2_set_ctx_params_list[] = {
224
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),
225
    OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PROPERTIES, NULL, 0),
226
    OSSL_PARAM_END
227
};
228
#endif
229
230
#ifndef sm2_set_ctx_params_st
231
struct sm2_set_ctx_params_st {
232
    OSSL_PARAM *digest;
233
    OSSL_PARAM *engine;
234
    OSSL_PARAM *propq;
235
};
236
#endif
237
238
#ifndef sm2_set_ctx_params_decoder
239
static int sm2_set_ctx_params_decoder
240
    (const OSSL_PARAM *p, struct sm2_set_ctx_params_st *r)
241
0
{
242
0
    const char *s;
243
244
0
    memset(r, 0, sizeof(*r));
245
0
    if (p != NULL)
246
0
        for (; (s = p->key) != NULL; p++)
247
0
            switch(s[0]) {
248
0
            default:
249
0
                break;
250
0
            case 'd':
251
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
252
                    /* ASYM_CIPHER_PARAM_DIGEST */
253
0
                    if (ossl_unlikely(r->digest != NULL)) {
254
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
255
0
                                       "param %s is repeated", s);
256
0
                        return 0;
257
0
                    }
258
0
                    r->digest = (OSSL_PARAM *)p;
259
0
                }
260
0
                break;
261
0
            case 'e':
262
0
                if (ossl_likely(strcmp("ngine", s + 1) == 0)) {
263
                    /* ASYM_CIPHER_PARAM_ENGINE */
264
0
                    if (ossl_unlikely(r->engine != NULL)) {
265
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
266
0
                                       "param %s is repeated", s);
267
0
                        return 0;
268
0
                    }
269
0
                    r->engine = (OSSL_PARAM *)p;
270
0
                }
271
0
                break;
272
0
            case 'p':
273
0
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
274
                    /* ASYM_CIPHER_PARAM_PROPERTIES */
275
0
                    if (ossl_unlikely(r->propq != NULL)) {
276
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
277
0
                                       "param %s is repeated", s);
278
0
                        return 0;
279
0
                    }
280
0
                    r->propq = (OSSL_PARAM *)p;
281
0
                }
282
0
            }
283
0
    return 1;
284
0
}
285
#endif
286
/* End of machine generated */
287
288
static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
289
0
{
290
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
291
0
    struct sm2_set_ctx_params_st p;
292
293
0
    if (psm2ctx == NULL || !sm2_set_ctx_params_decoder(params, &p))
294
0
        return 0;
295
296
0
    if (!ossl_prov_digest_load(&psm2ctx->md, p.digest, p.propq, p.engine,
297
0
                               psm2ctx->libctx))
298
0
        return 0;
299
300
0
    return 1;
301
0
}
302
303
static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,
304
                                                 ossl_unused void *provctx)
305
0
{
306
0
    return sm2_set_ctx_params_list;
307
0
}
308
309
const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {
310
    { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },
311
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },
312
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },
313
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init },
314
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt },
315
    { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx },
316
    { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx },
317
    { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
318
      (void (*)(void))sm2_get_ctx_params },
319
    { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
320
      (void (*)(void))sm2_gettable_ctx_params },
321
    { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
322
      (void (*)(void))sm2_set_ctx_params },
323
    { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
324
      (void (*)(void))sm2_settable_ctx_params },
325
    OSSL_DISPATCH_END
326
};