/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 | | #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 "prov/provider_ctx.h" |
21 | | #include "prov/implementations.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "prov/provider_util.h" |
24 | | |
25 | | static OSSL_FUNC_asym_cipher_newctx_fn sm2_newctx; |
26 | | static OSSL_FUNC_asym_cipher_encrypt_init_fn sm2_init; |
27 | | static OSSL_FUNC_asym_cipher_encrypt_fn sm2_asym_encrypt; |
28 | | static OSSL_FUNC_asym_cipher_decrypt_init_fn sm2_init; |
29 | | static OSSL_FUNC_asym_cipher_decrypt_fn sm2_asym_decrypt; |
30 | | static OSSL_FUNC_asym_cipher_freectx_fn sm2_freectx; |
31 | | static OSSL_FUNC_asym_cipher_dupctx_fn sm2_dupctx; |
32 | | static OSSL_FUNC_asym_cipher_get_ctx_params_fn sm2_get_ctx_params; |
33 | | static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn sm2_gettable_ctx_params; |
34 | | static OSSL_FUNC_asym_cipher_set_ctx_params_fn sm2_set_ctx_params; |
35 | | static OSSL_FUNC_asym_cipher_settable_ctx_params_fn sm2_settable_ctx_params; |
36 | | |
37 | | /* |
38 | | * What's passed as an actual key is defined by the KEYMGMT interface. |
39 | | * We happen to know that our KEYMGMT simply passes EC_KEY structures, so |
40 | | * we use that here too. |
41 | | */ |
42 | | |
43 | | typedef struct { |
44 | | OSSL_LIB_CTX *libctx; |
45 | | EC_KEY *key; |
46 | | PROV_DIGEST md; |
47 | | } PROV_SM2_CTX; |
48 | | |
49 | | static void *sm2_newctx(void *provctx) |
50 | 0 | { |
51 | 0 | PROV_SM2_CTX *psm2ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX)); |
52 | |
|
53 | 0 | if (psm2ctx == NULL) |
54 | 0 | return NULL; |
55 | 0 | psm2ctx->libctx = PROV_LIBCTX_OF(provctx); |
56 | |
|
57 | 0 | return psm2ctx; |
58 | 0 | } |
59 | | |
60 | | static int sm2_init(void *vpsm2ctx, void *vkey, const OSSL_PARAM params[]) |
61 | 0 | { |
62 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
63 | |
|
64 | 0 | if (psm2ctx == NULL || vkey == NULL || !EC_KEY_up_ref(vkey)) |
65 | 0 | return 0; |
66 | 0 | EC_KEY_free(psm2ctx->key); |
67 | 0 | psm2ctx->key = vkey; |
68 | |
|
69 | 0 | return sm2_set_ctx_params(psm2ctx, params); |
70 | 0 | } |
71 | | |
72 | | static const EVP_MD *sm2_get_md(PROV_SM2_CTX *psm2ctx) |
73 | 0 | { |
74 | 0 | const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md); |
75 | |
|
76 | 0 | if (md == NULL) |
77 | 0 | md = ossl_prov_digest_fetch(&psm2ctx->md, psm2ctx->libctx, "SM3", NULL); |
78 | |
|
79 | 0 | return md; |
80 | 0 | } |
81 | | |
82 | | static int sm2_asym_encrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen, |
83 | | size_t outsize, const unsigned char *in, |
84 | | size_t inlen) |
85 | 0 | { |
86 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
87 | 0 | const EVP_MD *md = sm2_get_md(psm2ctx); |
88 | |
|
89 | 0 | if (md == NULL) |
90 | 0 | return 0; |
91 | | |
92 | 0 | if (out == NULL) { |
93 | 0 | if (!ossl_sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) { |
94 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
95 | 0 | return 0; |
96 | 0 | } |
97 | 0 | return 1; |
98 | 0 | } |
99 | | |
100 | 0 | return ossl_sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen); |
101 | 0 | } |
102 | | |
103 | | static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen, |
104 | | size_t outsize, const unsigned char *in, |
105 | | size_t inlen) |
106 | 0 | { |
107 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
108 | 0 | const EVP_MD *md = sm2_get_md(psm2ctx); |
109 | |
|
110 | 0 | if (md == NULL) |
111 | 0 | return 0; |
112 | | |
113 | 0 | if (out == NULL) { |
114 | 0 | if (!ossl_sm2_plaintext_size(in, inlen, outlen)) |
115 | 0 | return 0; |
116 | 0 | return 1; |
117 | 0 | } |
118 | | |
119 | 0 | return ossl_sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen); |
120 | 0 | } |
121 | | |
122 | | static void sm2_freectx(void *vpsm2ctx) |
123 | 0 | { |
124 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
125 | |
|
126 | 0 | EC_KEY_free(psm2ctx->key); |
127 | 0 | ossl_prov_digest_reset(&psm2ctx->md); |
128 | |
|
129 | 0 | OPENSSL_free(psm2ctx); |
130 | 0 | } |
131 | | |
132 | | static void *sm2_dupctx(void *vpsm2ctx) |
133 | 0 | { |
134 | 0 | PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx; |
135 | 0 | PROV_SM2_CTX *dstctx; |
136 | |
|
137 | 0 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
138 | 0 | if (dstctx == NULL) |
139 | 0 | return NULL; |
140 | | |
141 | 0 | *dstctx = *srcctx; |
142 | 0 | memset(&dstctx->md, 0, sizeof(dstctx->md)); |
143 | |
|
144 | 0 | if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) { |
145 | 0 | OPENSSL_free(dstctx); |
146 | 0 | return NULL; |
147 | 0 | } |
148 | | |
149 | 0 | if (!ossl_prov_digest_copy(&dstctx->md, &srcctx->md)) { |
150 | 0 | sm2_freectx(dstctx); |
151 | 0 | return NULL; |
152 | 0 | } |
153 | | |
154 | 0 | return dstctx; |
155 | 0 | } |
156 | | |
157 | | static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params) |
158 | 0 | { |
159 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
160 | 0 | OSSL_PARAM *p; |
161 | |
|
162 | 0 | if (vpsm2ctx == NULL) |
163 | 0 | return 0; |
164 | | |
165 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_DIGEST); |
166 | 0 | if (p != NULL) { |
167 | 0 | const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md); |
168 | |
|
169 | 0 | if (!OSSL_PARAM_set_utf8_string(p, md == NULL ? "" |
170 | 0 | : EVP_MD_get0_name(md))) |
171 | 0 | return 0; |
172 | 0 | } |
173 | | |
174 | 0 | return 1; |
175 | 0 | } |
176 | | |
177 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
178 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0), |
179 | | OSSL_PARAM_END |
180 | | }; |
181 | | |
182 | | static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx, |
183 | | ossl_unused void *provctx) |
184 | 0 | { |
185 | 0 | return known_gettable_ctx_params; |
186 | 0 | } |
187 | | |
188 | | static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[]) |
189 | 0 | { |
190 | 0 | PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx; |
191 | |
|
192 | 0 | if (psm2ctx == NULL) |
193 | 0 | return 0; |
194 | 0 | if (ossl_param_is_empty(params)) |
195 | 0 | return 1; |
196 | | |
197 | 0 | if (!ossl_prov_digest_load_from_params(&psm2ctx->md, params, |
198 | 0 | psm2ctx->libctx)) |
199 | 0 | return 0; |
200 | | |
201 | 0 | return 1; |
202 | 0 | } |
203 | | |
204 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
205 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0), |
206 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PROPERTIES, NULL, 0), |
207 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_ENGINE, NULL, 0), |
208 | | OSSL_PARAM_END |
209 | | }; |
210 | | |
211 | | static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx, |
212 | | ossl_unused void *provctx) |
213 | 0 | { |
214 | 0 | return known_settable_ctx_params; |
215 | 0 | } |
216 | | |
217 | | const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = { |
218 | | { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx }, |
219 | | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init }, |
220 | | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt }, |
221 | | { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init }, |
222 | | { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt }, |
223 | | { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx }, |
224 | | { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx }, |
225 | | { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS, |
226 | | (void (*)(void))sm2_get_ctx_params }, |
227 | | { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS, |
228 | | (void (*)(void))sm2_gettable_ctx_params }, |
229 | | { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS, |
230 | | (void (*)(void))sm2_set_ctx_params }, |
231 | | { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS, |
232 | | (void (*)(void))sm2_settable_ctx_params }, |
233 | | OSSL_DISPATCH_END |
234 | | }; |