Coverage Report

Created: 2025-12-31 06:58

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