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