Make the SM2 group the default group for the SM2 algorithm

Fixes #14481

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14684)
This commit is contained in:
Tomas Mraz 2021-03-25 14:57:16 +01:00
parent cede07dc51
commit bf5b37cedf
2 changed files with 45 additions and 2 deletions

View File

@ -57,6 +57,8 @@ static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
#ifndef FIPS_MODULE
# ifndef OPENSSL_NO_SM2
static OSSL_FUNC_keymgmt_new_fn sm2_newdata;
static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init;
static OSSL_FUNC_keymgmt_gen_fn sm2_gen;
static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params;
static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params;
@ -271,6 +273,18 @@ void *ec_newdata(void *provctx)
return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL);
}
#ifndef FIPS_MODULE
# ifndef OPENSSL_NO_SM2
static
void *sm2_newdata(void *provctx)
{
if (!ossl_prov_is_running())
return NULL;
return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2);
}
# endif
#endif
static
void ec_freedata(void *keydata)
{
@ -956,6 +970,26 @@ static void *ec_gen_init(void *provctx, int selection,
return gctx;
}
#ifndef FIPS_MODULE
# ifndef OPENSSL_NO_SM2
static void *sm2_gen_init(void *provctx, int selection,
const OSSL_PARAM params[])
{
struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
if (gctx != NULL) {
if (gctx->group_name != NULL)
return gctx;
if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
return gctx;
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
ec_gen_cleanup(gctx);
}
return NULL;
}
# endif
#endif
static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
{
struct ec_gen_ctx *gctx = genctx;
@ -1358,8 +1392,8 @@ const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
#ifndef FIPS_MODULE
# ifndef OPENSSL_NO_SM2
const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata },
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init },
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
(void (*)(void))ec_gen_set_template },
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },

View File

@ -61,3 +61,12 @@ Ctrl = digest:SM3
Input = D7AD397F6FFA5D4F7F11E7217F241607DC30618C236D2C09C1B9EA8FDADEE2E8
Output = 3045022100f11bf36e75bb304f094fb42a4ca22377d0cc768637c5011cd59fb9ed4b130c98022035545ffe2c2efb3abee4fee661468946d886004fae8ea5311593e48f7fe21b91
Result = KEYOP_MISMATCH
Title = SM2 key generation tests
KeyGen = SM2
KeyName = SM2_genkey1
KeyGen = SM2
Ctrl = group:sm2
KeyName = SM2_genkey2