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