Path: blob/main/crypto/openssl/providers/implementations/asymciphers/sm2_enc.c
107750 views
/*1* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include "internal/deprecated.h"1011#include <openssl/crypto.h>12#include <openssl/evp.h>13#include <openssl/core_dispatch.h>14#include <openssl/core_names.h>15#include <openssl/params.h>16#include <openssl/err.h>17#include <openssl/proverr.h>18#include "crypto/sm2.h"19#include "prov/provider_ctx.h"20#include "prov/implementations.h"21#include "prov/providercommon.h"22#include "prov/provider_util.h"2324static OSSL_FUNC_asym_cipher_newctx_fn sm2_newctx;25static OSSL_FUNC_asym_cipher_encrypt_init_fn sm2_init;26static OSSL_FUNC_asym_cipher_encrypt_fn sm2_asym_encrypt;27static OSSL_FUNC_asym_cipher_decrypt_init_fn sm2_init;28static OSSL_FUNC_asym_cipher_decrypt_fn sm2_asym_decrypt;29static OSSL_FUNC_asym_cipher_freectx_fn sm2_freectx;30static OSSL_FUNC_asym_cipher_dupctx_fn sm2_dupctx;31static OSSL_FUNC_asym_cipher_get_ctx_params_fn sm2_get_ctx_params;32static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn sm2_gettable_ctx_params;33static OSSL_FUNC_asym_cipher_set_ctx_params_fn sm2_set_ctx_params;34static OSSL_FUNC_asym_cipher_settable_ctx_params_fn sm2_settable_ctx_params;3536/*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, so39* we use that here too.40*/4142typedef struct {43OSSL_LIB_CTX *libctx;44EC_KEY *key;45PROV_DIGEST md;46} PROV_SM2_CTX;4748static void *sm2_newctx(void *provctx)49{50PROV_SM2_CTX *psm2ctx = OPENSSL_zalloc(sizeof(PROV_SM2_CTX));5152if (psm2ctx == NULL)53return NULL;54psm2ctx->libctx = PROV_LIBCTX_OF(provctx);5556return psm2ctx;57}5859static int sm2_init(void *vpsm2ctx, void *vkey, const OSSL_PARAM params[])60{61PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;6263if (psm2ctx == NULL || vkey == NULL || !EC_KEY_up_ref(vkey))64return 0;65EC_KEY_free(psm2ctx->key);66psm2ctx->key = vkey;6768return sm2_set_ctx_params(psm2ctx, params);69}7071static const EVP_MD *sm2_get_md(PROV_SM2_CTX *psm2ctx)72{73const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);7475if (md == NULL)76md = ossl_prov_digest_fetch(&psm2ctx->md, psm2ctx->libctx, "SM3", NULL);7778return md;79}8081static int sm2_asym_encrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,82size_t outsize, const unsigned char *in,83size_t inlen)84{85PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;86const EVP_MD *md = sm2_get_md(psm2ctx);8788if (md == NULL)89return 0;9091if (out == NULL) {92if (!ossl_sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) {93ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);94return 0;95}96return 1;97}9899return ossl_sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen);100}101102static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,103size_t outsize, const unsigned char *in,104size_t inlen)105{106PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;107const EVP_MD *md = sm2_get_md(psm2ctx);108109if (md == NULL)110return 0;111112if (out == NULL) {113if (!ossl_sm2_plaintext_size(in, inlen, outlen))114return 0;115return 1;116}117118return ossl_sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen);119}120121static void sm2_freectx(void *vpsm2ctx)122{123PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;124125EC_KEY_free(psm2ctx->key);126ossl_prov_digest_reset(&psm2ctx->md);127128OPENSSL_free(psm2ctx);129}130131static void *sm2_dupctx(void *vpsm2ctx)132{133PROV_SM2_CTX *srcctx = (PROV_SM2_CTX *)vpsm2ctx;134PROV_SM2_CTX *dstctx;135136dstctx = OPENSSL_zalloc(sizeof(*srcctx));137if (dstctx == NULL)138return NULL;139140*dstctx = *srcctx;141memset(&dstctx->md, 0, sizeof(dstctx->md));142143if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) {144OPENSSL_free(dstctx);145return NULL;146}147148if (!ossl_prov_digest_copy(&dstctx->md, &srcctx->md)) {149sm2_freectx(dstctx);150return NULL;151}152153return dstctx;154}155156static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)157{158PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;159OSSL_PARAM *p;160161if (vpsm2ctx == NULL)162return 0;163164p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_DIGEST);165if (p != NULL) {166const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);167168if (!OSSL_PARAM_set_utf8_string(p, md == NULL ? "" : EVP_MD_get0_name(md)))169return 0;170}171172return 1;173}174175static const OSSL_PARAM known_gettable_ctx_params[] = {176OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),177OSSL_PARAM_END178};179180static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,181ossl_unused void *provctx)182{183return known_gettable_ctx_params;184}185186static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])187{188PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;189190if (psm2ctx == NULL)191return 0;192if (ossl_param_is_empty(params))193return 1;194195if (!ossl_prov_digest_load_from_params(&psm2ctx->md, params,196psm2ctx->libctx))197return 0;198199return 1;200}201202static const OSSL_PARAM known_settable_ctx_params[] = {203OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST, NULL, 0),204OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PROPERTIES, NULL, 0),205OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_ENGINE, NULL, 0),206OSSL_PARAM_END207};208209static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,210ossl_unused void *provctx)211{212return known_settable_ctx_params;213}214215const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {216{ OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },217{ OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },218{ OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },219{ OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))sm2_init },220{ OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))sm2_asym_decrypt },221{ OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))sm2_freectx },222{ OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))sm2_dupctx },223{ OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,224(void (*)(void))sm2_get_ctx_params },225{ OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,226(void (*)(void))sm2_gettable_ctx_params },227{ OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,228(void (*)(void))sm2_set_ctx_params },229{ OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,230(void (*)(void))sm2_settable_ctx_params },231OSSL_DISPATCH_END232};233234235