Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
10
#include "internal/deprecated.h"
11
12
#include <openssl/crypto.h>
13
#include <openssl/evp.h>
14
#include <openssl/core_dispatch.h>
15
#include <openssl/core_names.h>
16
#include <openssl/params.h>
17
#include <openssl/err.h>
18
#include <openssl/proverr.h>
19
#include "crypto/sm2.h"
20
#include "internal/cryptlib.h"
21
#include "prov/provider_ctx.h"
22
#include "prov/implementations.h"
23
#include "prov/providercommon.h"
24
#include "prov/provider_util.h"
25
#include "providers/implementations/asymciphers/sm2_enc.inc"
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
static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
160
0
{
161
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
162
0
    struct sm2_get_ctx_params_st p;
163
164
0
    if (psm2ctx == NULL || !sm2_get_ctx_params_decoder(params, &p))
165
0
        return 0;
166
167
0
    if (p.digest != NULL) {
168
0
        const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
169
170
0
        if (!OSSL_PARAM_set_utf8_string(p.digest,
171
0
                                        md == NULL ? "" : EVP_MD_get0_name(md)))
172
0
            return 0;
173
0
    }
174
175
0
    return 1;
176
0
}
177
178
static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,
179
                                                 ossl_unused void *provctx)
180
0
{
181
0
    return sm2_get_ctx_params_list;
182
0
}
183
184
static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
185
0
{
186
0
    PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
187
0
    struct sm2_set_ctx_params_st p;
188
189
0
    if (psm2ctx == NULL || !sm2_set_ctx_params_decoder(params, &p))
190
0
        return 0;
191
192
0
    if (!ossl_prov_digest_load(&psm2ctx->md, p.digest, p.propq, p.engine,
193
0
                               psm2ctx->libctx))
194
0
        return 0;
195
196
0
    return 1;
197
0
}
198
199
static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,
200
                                                 ossl_unused void *provctx)
201
0
{
202
0
    return sm2_set_ctx_params_list;
203
0
}
204
205
const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {
206
    { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },
207
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },
208
    { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },
209
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init },
210
    { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt },
211
    { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx },
212
    { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx },
213
    { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
214
      (void (*)(void))sm2_get_ctx_params },
215
    { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
216
      (void (*)(void))sm2_gettable_ctx_params },
217
    { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
218
      (void (*)(void))sm2_set_ctx_params },
219
    { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
220
      (void (*)(void))sm2_settable_ctx_params },
221
    OSSL_DISPATCH_END
222
};