[PATCH 00/23] uadk_engine: fix correctness and robustness issues
This series fixes correctness and robustness issues in uadk_engine and uadk_provider, covering OpenSSL 3.x callback gaps, DH/ECDH/ ECDSA/RSA/SM2 algorithm paths, and shared infrastructure. Chenghai Huang (2): uadk_engine: fix integer type issues uadk: replace magic expt=1 with named poll constants Weili Qian (21): uadk_provider: add gen_get_params callbacks for OpenSSL 3.4+ uadk_provider: fix log message in ec software fallback uadk_provider: add EC_GROUP_CHECK_TYPE to EC gen_settable_params uadk_provider: fallback to software for DHKEM IKM key derivation uadk/dh: clear priv_key bit 0 for g=2 quadratic non-residue uadk/dh: allow q parameter in hardware key generation uadk/dh: add small subgroup and derived secret validation uadk: support cofactor ECDH mode with hardware acceleration uadk_provider/ec: validate preset private key upper bound in keygen uadk_provider/ecx: fix return values for keyexch optional callbacks uadk_provider/rsa: fallback to software for multi-prime keygen uadk_provider/rsa: copy PSS params to generated key uadk_engine: use BN_priv_rand_range for EC private key uadk_engine: do not fallback on RSA decrypt padding failure uadk_provider: check sig_r/s size before BN_bn2bin uadk_provider: relax RSA decrypt flen check uadk_engine: return -1 instead of -ENOMEM from RSA callbacks uadk_engine: merge cipher_ber_to_bin into sm2_decrypt_init_iot uadk_provider: return FAIL from verify_recover stubs uadk_provider: remove redundant c3 check from sm2 decrypt uadk: remove redundant NULL checks and initializations src/uadk.h | 2 + src/uadk_aead.c | 12 ++-- src/uadk_cipher.c | 8 +-- src/uadk_cipher_adapter.h | 2 +- src/uadk_dh.c | 61 ++++++++++++++++-- src/uadk_ec.c | 83 ++++++++++++++++++++++--- src/uadk_ecx.c | 1 - src/uadk_pkey.c | 6 +- src/uadk_prov.h | 2 + src/uadk_prov_aead.c | 3 +- src/uadk_prov_cipher.c | 3 +- src/uadk_prov_dh.c | 69 ++++++++++++++++++++- src/uadk_prov_digest.c | 3 +- src/uadk_prov_ec_kmgmt.c | 40 +++++++++++- src/uadk_prov_ecdh_exch.c | 124 +++++++++++++++++++++++-------------- src/uadk_prov_ecdsa.c | 9 ++- src/uadk_prov_ecx.c | 127 ++++++++++++++++++++------------------ src/uadk_prov_hmac.c | 3 +- src/uadk_prov_pkey.c | 2 +- src/uadk_prov_pkey.h | 33 ++++++++-- src/uadk_prov_rsa.c | 3 +- src/uadk_prov_rsa_enc.c | 7 ++- src/uadk_prov_rsa_kmgmt.c | 36 +++++++++++ src/uadk_prov_sm2_enc.c | 11 +--- src/uadk_prov_sm2_kmgmt.c | 19 ++++++ src/uadk_prov_sm2_sign.c | 4 +- src/uadk_rsa.c | 25 ++++---- src/uadk_sm2.c | 115 +++++++++++++--------------------- 29 files changed, 564 insertions(+), 258 deletions(-) -- 2.43.0
OpenSSL 3.4 added the OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS and OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS callbacks for querying key generation context parameters. The uadk provider does not register them, so EVP_PKEY_CTX_get_params() on a generation context fails as unsupported whenever the uadk keymgmt is selected. Add gen_get_params and gen_gettable_params callbacks to the DH, EC, ECX (X25519/X448), RSA, and SM2 keymgmt implementations, delegating to the default provider on OpenSSL 3.4+. Remove redundant ossl_ecx_gen_set_params definition and delegate directly to default provider's gen_set_params, avoiding duplicate parameter processing (group name, propq, DHKEM IKM). Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_dh.c | 19 ++++++++ src/uadk_prov_ec_kmgmt.c | 19 ++++++++ src/uadk_prov_ecx.c | 92 +++++++++++++++++++-------------------- src/uadk_prov_pkey.h | 33 +++++++++++--- src/uadk_prov_rsa_kmgmt.c | 25 +++++++++++ src/uadk_prov_sm2_kmgmt.c | 19 ++++++++ 6 files changed, 156 insertions(+), 51 deletions(-) diff --git a/src/uadk_prov_dh.c b/src/uadk_prov_dh.c index d78cd88..bd403d7 100644 --- a/src/uadk_prov_dh.c +++ b/src/uadk_prov_dh.c @@ -367,6 +367,25 @@ static const OSSL_PARAM *uadk_keymgmt_dh_gen_settable_params(ossl_unused void *g return get_default_dh_keymgmt().gen_settable_params(genctx, provctx); } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_dh_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (get_default_dh_keymgmt().gen_get_params == NULL) + return UADK_P_FAIL; + + return get_default_dh_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_dh_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (get_default_dh_keymgmt().gen_gettable_params == NULL) + return NULL; + + return get_default_dh_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static int uadk_keymgmt_dh_gen_set_params(void *genctx, const OSSL_PARAM params[]) { diff --git a/src/uadk_prov_ec_kmgmt.c b/src/uadk_prov_ec_kmgmt.c index 9896d09..c93fd13 100644 --- a/src/uadk_prov_ec_kmgmt.c +++ b/src/uadk_prov_ec_kmgmt.c @@ -659,6 +659,25 @@ static const OSSL_PARAM *uadk_keymgmt_ec_gen_settable_params(ossl_unused void *g return settable; } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_ec_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (!get_default_ec_keymgmt().gen_get_params) + return UADK_P_FAIL; + + return get_default_ec_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_ec_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (!get_default_ec_keymgmt().gen_gettable_params) + return NULL; + + return get_default_ec_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static void *uadk_keymgmt_ec_new(void *provctx) { if (!get_default_ec_keymgmt().new_fun) diff --git a/src/uadk_prov_ecx.c b/src/uadk_prov_ecx.c index 27e9b86..e76d646 100644 --- a/src/uadk_prov_ecx.c +++ b/src/uadk_prov_ecx.c @@ -302,6 +302,25 @@ static const OSSL_PARAM *uadk_keymgmt_x448_gen_settable_params(ossl_unused void return get_default_x448_keymgmt().gen_settable_params(genctx, provctx); } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_x448_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (get_default_x448_keymgmt().gen_get_params == NULL) + return UADK_P_FAIL; + + return get_default_x448_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_x448_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (get_default_x448_keymgmt().gen_gettable_params == NULL) + return NULL; + + return get_default_x448_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static int uadk_keymgmt_x448_gen_set_template(void *genctx, void *templ) { return UADK_P_SUCCESS; @@ -378,50 +397,6 @@ static int uadk_keymgmt_x448_get_params(void *key, OSSL_PARAM params[]) X448_KEYLEN); } -static int ossl_ecx_gen_set_params(void *genctx, const OSSL_PARAM params[]) -{ - PROV_ECX_KEYMGMT_CTX *gctx = (PROV_ECX_KEYMGMT_CTX *)genctx; - const char *groupname = NULL; - const OSSL_PARAM *p; - - p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); - if (p != NULL) { - /* - * We optionally allow setting a group name - but each algorithm only - * support one such name, so all we do is verify that it is the one we - * expected. - */ - switch (gctx->type) { - case ECX_KEY_TYPE_X25519: - groupname = "x25519"; - break; - case ECX_KEY_TYPE_X448: - groupname = "x448"; - break; - default: - /* We only support this for key exchange at the moment */ - break; - } - if (p->data_type != OSSL_PARAM_UTF8_STRING || groupname == NULL || - OPENSSL_strcasecmp(p->data, groupname) != 0) { - UADK_ERR("invalid ecx params\n"); - return UADK_P_FAIL; - } - } - p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES); - if (p != NULL) { - if (p->data_type != OSSL_PARAM_UTF8_STRING) - return UADK_P_FAIL; - - OPENSSL_free(gctx->propq); - gctx->propq = OPENSSL_strdup(p->data); - if (gctx->propq == NULL) - return UADK_P_FAIL; - } - - return UADK_P_SUCCESS; -} - static handle_t uadk_prov_ecx_alloc_sess(int type) { struct wd_ecc_sess_setup setup = {0}; @@ -465,7 +440,7 @@ static void *ossl_ecx_gen_init(void *provctx, int selection, const OSSL_PARAM pa { OSSL_LIB_CTX *libctx = prov_libctx_of(provctx); PROV_ECX_KEYMGMT_CTX *gctx; - int ret; + int ret = UADK_P_SUCCESS; gctx = OPENSSL_zalloc(sizeof(PROV_ECX_KEYMGMT_CTX)); if (gctx == NULL) { @@ -477,7 +452,13 @@ static void *ossl_ecx_gen_init(void *provctx, int selection, const OSSL_PARAM pa gctx->type = type; gctx->selection = selection; - ret = ossl_ecx_gen_set_params(gctx, params); + if (type == ECX_KEY_TYPE_X448) { + if (get_default_x448_keymgmt().gen_set_params != NULL) + ret = get_default_x448_keymgmt().gen_set_params(gctx, params); + } else { + if (get_default_x25519_keymgmt().gen_set_params != NULL) + ret = get_default_x25519_keymgmt().gen_set_params(gctx, params); + } if (ret == UADK_P_FAIL) { UADK_ERR("failed to set ecx params\n"); OPENSSL_free(gctx); @@ -1426,6 +1407,25 @@ static const OSSL_PARAM *uadk_keymgmt_x25519_gen_settable_params(ossl_unused voi return get_default_x25519_keymgmt().gen_settable_params(genctx, provctx); } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_x25519_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (get_default_x25519_keymgmt().gen_get_params == NULL) + return UADK_P_FAIL; + + return get_default_x25519_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_x25519_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (get_default_x25519_keymgmt().gen_gettable_params == NULL) + return NULL; + + return get_default_x25519_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static int uadk_keymgmt_x25519_gen_set_template(void *genctx, void *templ) { return UADK_P_SUCCESS; diff --git a/src/uadk_prov_pkey.h b/src/uadk_prov_pkey.h index aa7a07b..e7bb089 100644 --- a/src/uadk_prov_pkey.h +++ b/src/uadk_prov_pkey.h @@ -134,14 +134,14 @@ typedef struct { #if OPENSSL_VERSION_NUMBER >= 0x30300000L /* NID for the legacy alg if there is one */ int legacy_alg; -# endif +#endif char *type_name; const char *description; OSSL_PROVIDER *prov; int refcnt; #if OPENSSL_VERSION_NUMBER < 0x30200000L void *lock; -# endif +#endif /* Constructor(s), destructor, information */ OSSL_FUNC_keymgmt_new_fn *new_fun; OSSL_FUNC_keymgmt_free_fn *free; @@ -156,7 +156,7 @@ typedef struct { #if OPENSSL_VERSION_NUMBER >= 0x30400000L OSSL_FUNC_keymgmt_gen_get_params_fn *gen_get_params; OSSL_FUNC_keymgmt_gen_gettable_params_fn *gen_gettable_params; -# endif +#endif OSSL_FUNC_keymgmt_gen_set_params_fn *gen_set_params; OSSL_FUNC_keymgmt_gen_settable_params_fn *gen_settable_params; OSSL_FUNC_keymgmt_gen_fn *gen; @@ -174,15 +174,34 @@ typedef struct { OSSL_FUNC_keymgmt_import_types_fn *import_types; #if OPENSSL_VERSION_NUMBER >= 0x30200000L OSSL_FUNC_keymgmt_import_types_ex_fn *import_types_ex; -# endif +#endif OSSL_FUNC_keymgmt_export_fn *export_fun; OSSL_FUNC_keymgmt_export_types_fn *export_types; #if OPENSSL_VERSION_NUMBER >= 0x30200000L OSSL_FUNC_keymgmt_export_types_ex_fn *export_types_ex; -# endif +#endif OSSL_FUNC_keymgmt_dup_fn *dup; } UADK_PKEY_KEYMGMT; +#if OPENSSL_VERSION_NUMBER < 0x30400000L +OSSL_CORE_MAKE_FUNC(int, keymgmt_gen_get_params, + (void *genctx, OSSL_PARAM params[])) +OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, keymgmt_gen_gettable_params, + (void *genctx, void *provctx)) +#endif + +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +#define UADK_KEYMGMT_GEN_GET_PARAMS_ENTRY(nm) \ + { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, \ + (void (*)(void))uadk_keymgmt_##nm##_gen_get_params }, +#define UADK_KEYMGMT_GEN_GETTABLE_PARAMS_ENTRY(nm) \ + { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, \ + (void (*)(void))uadk_keymgmt_##nm##_gen_gettable_params }, +#else +#define UADK_KEYMGMT_GEN_GET_PARAMS_ENTRY(nm) +#define UADK_KEYMGMT_GEN_GETTABLE_PARAMS_ENTRY(nm) +#endif + #define UADK_PKEY_KEYMGMT_DESCR(nm, alg) \ static OSSL_FUNC_keymgmt_new_fn uadk_keymgmt_##nm##_new; \ static OSSL_FUNC_keymgmt_free_fn uadk_keymgmt_##nm##_free; \ @@ -194,6 +213,8 @@ static OSSL_FUNC_keymgmt_gen_init_fn uadk_keymgmt_##nm##_gen_init; \ static OSSL_FUNC_keymgmt_gen_set_template_fn uadk_keymgmt_##nm##_gen_set_template; \ static OSSL_FUNC_keymgmt_gen_set_params_fn uadk_keymgmt_##nm##_gen_set_params; \ static OSSL_FUNC_keymgmt_gen_settable_params_fn uadk_keymgmt_##nm##_gen_settable_params; \ +static OSSL_FUNC_keymgmt_gen_get_params_fn uadk_keymgmt_##nm##_gen_get_params; \ +static OSSL_FUNC_keymgmt_gen_gettable_params_fn uadk_keymgmt_##nm##_gen_gettable_params; \ static OSSL_FUNC_keymgmt_gen_fn uadk_keymgmt_##nm##_gen; \ static OSSL_FUNC_keymgmt_gen_cleanup_fn uadk_keymgmt_##nm##_gen_cleanup; \ static OSSL_FUNC_keymgmt_load_fn uadk_keymgmt_##nm##_load; \ @@ -218,6 +239,8 @@ const OSSL_DISPATCH uadk_##nm##_keymgmt_functions[] = { \ { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))uadk_keymgmt_##nm##_gen_init }, \ { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, \ (void (*)(void))uadk_keymgmt_##nm##_gen_set_template }, \ + UADK_KEYMGMT_GEN_GET_PARAMS_ENTRY(nm) \ + UADK_KEYMGMT_GEN_GETTABLE_PARAMS_ENTRY(nm) \ { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, \ (void (*)(void))uadk_keymgmt_##nm##_gen_set_params }, \ { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \ diff --git a/src/uadk_prov_rsa_kmgmt.c b/src/uadk_prov_rsa_kmgmt.c index 2da157c..208b65e 100644 --- a/src/uadk_prov_rsa_kmgmt.c +++ b/src/uadk_prov_rsa_kmgmt.c @@ -820,6 +820,25 @@ static const OSSL_PARAM *uadk_keymgmt_rsa_gen_settable_params(ossl_unused void * return get_default_rsa_keymgmt().gen_settable_params(genctx, provctx); } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_rsa_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (!get_default_rsa_keymgmt().gen_get_params) + return UADK_P_FAIL; + + return get_default_rsa_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_rsa_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (!get_default_rsa_keymgmt().gen_gettable_params) + return NULL; + + return get_default_rsa_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static void *uadk_rsa_sw_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { if (!enable_sw_offload || !get_default_rsa_keymgmt().gen) @@ -1006,6 +1025,12 @@ const OSSL_DISPATCH uadk_rsapss_keymgmt_functions[] = { (void (*)(void))uadk_keymgmt_rsa_gen_set_params }, { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (void (*)(void))uadk_keymgmt_rsapss_gen_settable_params }, +#if OPENSSL_VERSION_NUMBER >= 0x30400000L + { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, + (void (*)(void))uadk_keymgmt_rsa_gen_get_params }, + { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, + (void (*)(void))uadk_keymgmt_rsa_gen_gettable_params }, +#endif { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))uadk_keymgmt_rsa_gen }, { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))uadk_keymgmt_rsa_gen_cleanup }, { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))uadk_keymgmt_rsapss_load }, diff --git a/src/uadk_prov_sm2_kmgmt.c b/src/uadk_prov_sm2_kmgmt.c index 0085251..78937a1 100644 --- a/src/uadk_prov_sm2_kmgmt.c +++ b/src/uadk_prov_sm2_kmgmt.c @@ -315,6 +315,25 @@ static const OSSL_PARAM *uadk_keymgmt_sm2_gen_settable_params(ossl_unused void * return get_default_sm2_keymgmt().gen_settable_params(genctx, provctx); } +#if OPENSSL_VERSION_NUMBER >= 0x30400000L +static int uadk_keymgmt_sm2_gen_get_params(void *genctx, OSSL_PARAM params[]) +{ + if (!get_default_sm2_keymgmt().gen_get_params) + return UADK_P_FAIL; + + return get_default_sm2_keymgmt().gen_get_params(genctx, params); +} + +static const OSSL_PARAM *uadk_keymgmt_sm2_gen_gettable_params(ossl_unused void *genctx, + ossl_unused void *provctx) +{ + if (!get_default_sm2_keymgmt().gen_gettable_params) + return NULL; + + return get_default_sm2_keymgmt().gen_gettable_params(genctx, provctx); +} +#endif + static int uadk_prov_sm2_set_key_to_ec_key(EC_KEY *ec, struct wd_ecc_req *req) { unsigned char key_buff[ECC_POINT_SIZE(SM2_KEY_BYTES) + 1] = {0}; -- 2.43.0
The log message in uadk_ec_sw_gen() incorrectly says "ecx generation" instead of "ec generation". Fix the message to match the actual algorithm being executed. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ec_kmgmt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uadk_prov_ec_kmgmt.c b/src/uadk_prov_ec_kmgmt.c index c93fd13..c98ac85 100644 --- a/src/uadk_prov_ec_kmgmt.c +++ b/src/uadk_prov_ec_kmgmt.c @@ -346,7 +346,7 @@ static void *uadk_ec_sw_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) if (!enable_sw_offload || !get_default_ec_keymgmt().gen) return NULL; - UADK_INFO("switch to openssl software calculation in ecx generation.\n"); + UADK_INFO("switch to openssl software calculation in ec generation.\n"); return get_default_ec_keymgmt().gen(genctx, osslcb, cbarg); } -- 2.43.0
The EC keymgmt gen_set_params handler already processes OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, but the gen_settable_params list does not declare it. This causes the parameter to be silently rejected by the OpenSSL framework when the application queries settable params before setting it. Add the missing entry to the settable array. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ec_kmgmt.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uadk_prov_ec_kmgmt.c b/src/uadk_prov_ec_kmgmt.c index c98ac85..f143b77 100644 --- a/src/uadk_prov_ec_kmgmt.c +++ b/src/uadk_prov_ec_kmgmt.c @@ -641,6 +641,7 @@ static const OSSL_PARAM *uadk_keymgmt_ec_gen_settable_params(ossl_unused void *g OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0), + OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0), OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), -- 2.43.0
The hardware does not support DHKEM IKM-based key derivation. When OSSL_PKEY_PARAM_DHKEM_IKM is set on the key generation context, fall back to the default provider's software implementation for EC, X25519 and X448 key generation. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ec_kmgmt.c | 8 ++++++++ src/uadk_prov_ecx.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uadk_prov_ec_kmgmt.c b/src/uadk_prov_ec_kmgmt.c index f143b77..2b44d20 100644 --- a/src/uadk_prov_ec_kmgmt.c +++ b/src/uadk_prov_ec_kmgmt.c @@ -374,6 +374,14 @@ static void *uadk_keymgmt_ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbar goto free_ec_key; } +# if OPENSSL_VERSION_NUMBER >= 0x30200000L + /* DHKEM IKM-based key derivation is not supported by hardware */ + if (gctx->dhkem_ikm) { + ret = UADK_DO_SOFT; + goto free_ec_key; + } +# endif + /* Whether you want it or not, you get a keypair, not just one half */ if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { ret = ec_hw_keygen(ec, gctx->priv_key); diff --git a/src/uadk_prov_ecx.c b/src/uadk_prov_ecx.c index e76d646..9f0335f 100644 --- a/src/uadk_prov_ecx.c +++ b/src/uadk_prov_ecx.c @@ -841,6 +841,13 @@ static void *uadk_keymgmt_x448_gen(void *genctx, OSSL_CALLBACK *cb, void *cb_par return NULL; } +# if OPENSSL_VERSION_NUMBER >= 0x30200000L + if (gctx->dhkem_ikm) { + ret = UADK_DO_SOFT; + goto exe_soft; + } +# endif + ret = uadk_prov_keymgmt_get_support_state(KEYMGMT_X448); if (ret == UADK_P_FAIL) { UADK_ERR("failed to get hardware x448 keygen support\n"); @@ -1474,6 +1481,13 @@ static void *uadk_keymgmt_x25519_gen(void *genctx, OSSL_CALLBACK *cb, void *cb_p return NULL; } +# if OPENSSL_VERSION_NUMBER >= 0x30200000L + if (gctx->dhkem_ikm) { + ret = UADK_DO_SOFT; + goto exe_soft; + } +# endif + ret = uadk_prov_keymgmt_get_support_state(KEYMGMT_X25519); if (ret == UADK_P_FAIL) { UADK_ERR("failed to get hardware x25519 keygen support\n"); -- 2.43.0
From: Chenghai Huang <huangchenghai2@huawei.com> Several variables use narrower types than their assignments, causing implicit narrowing and sign-comparison warnings under static analysis and stricter toolchains. - widen destroy_aead and uadk_e_destroy_aead num to __u32 - widen digest processing_len to size_t in uadk_digest - cast BN_bn2bin results to __u32 for dsize in uadk_rsa - remove redundant blank line in ecx compkey functions Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com> --- src/uadk_aead.c | 4 ++-- src/uadk_cipher_adapter.h | 2 +- src/uadk_digest.c | 3 ++- src/uadk_ecx.c | 1 - src/uadk_prov_ecx.c | 1 - src/uadk_rsa.c | 6 +++--- 6 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/uadk_aead.c b/src/uadk_aead.c index a026284..b88d034 100644 --- a/src/uadk_aead.c +++ b/src/uadk_aead.c @@ -796,7 +796,7 @@ EVP_CIPHER *uadk_create_gcm_cipher_meth(int nid) return aead; } -static void destroy_aead(struct engine_cipher_info *info, int num) +static void destroy_aead(struct engine_cipher_info *info, __u32 num) { __u32 i; @@ -808,7 +808,7 @@ static void destroy_aead(struct engine_cipher_info *info, int num) } } -void uadk_e_destroy_aead(struct engine_cipher_info *info, int num) +void uadk_e_destroy_aead(struct engine_cipher_info *info, __u32 num) { __u32 i; int ret; diff --git a/src/uadk_cipher_adapter.h b/src/uadk_cipher_adapter.h index f7b6fb4..011f98c 100644 --- a/src/uadk_cipher_adapter.h +++ b/src/uadk_cipher_adapter.h @@ -28,7 +28,7 @@ struct engine_cipher_info { EVP_CIPHER *uadk_create_gcm_cipher_meth(int nid); EVP_CIPHER *uadk_create_cipher_meth(int nid); -void uadk_e_destroy_aead(struct engine_cipher_info *info, int num); +void uadk_e_destroy_aead(struct engine_cipher_info *info, __u32 num); void uadk_e_destroy_cipher(struct engine_cipher_info *info, int num); int uadk_e_bind_ciphers(ENGINE *e); diff --git a/src/uadk_digest.c b/src/uadk_digest.c index 29448b7..e18b25b 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -679,7 +679,8 @@ static int digest_update_inner(EVP_MD_CTX *ctx, const void *data, size_t data_le (struct digest_priv_ctx *)EVP_MD_CTX_md_data(ctx); const unsigned char *tmpdata = (const unsigned char *)data; size_t left_len = data_len; - int ret, processing_len; + size_t processing_len; + int ret; if (unlikely(!priv)) { fprintf(stderr, "priv get from digest ctx is NULL.\n"); diff --git a/src/uadk_ecx.c b/src/uadk_ecx.c index 3a33968..cbec886 100644 --- a/src/uadk_ecx.c +++ b/src/uadk_ecx.c @@ -652,7 +652,6 @@ static int ecx_compkey_init_iot(struct ecx_ctx *ecx_ctx, struct wd_ecc_req *req, uadk_ecc_fill_req(req, WD_ECXDH_COMPUTE_KEY, ecx_in, ecx_out); return UADK_E_SUCCESS; - } static void ecx_compkey_uninit_iot(handle_t sess, struct wd_ecc_req *req) diff --git a/src/uadk_prov_ecx.c b/src/uadk_prov_ecx.c index 9f0335f..7820b29 100644 --- a/src/uadk_prov_ecx.c +++ b/src/uadk_prov_ecx.c @@ -1058,7 +1058,6 @@ static int uadk_prov_ecx_compkey_init_iot(PROV_ECX_KEYEXCH_CTX *ecxctx, struct w uadk_prov_ecc_fill_req(req, WD_ECXDH_COMPUTE_KEY, ecx_in, ecx_out); return UADK_P_SUCCESS; - } static void uadk_prov_ecx_compkey_uninit_iot(PROV_ECX_KEYEXCH_CTX *ecxctx, struct wd_ecc_req *req) diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index 8774d5d..e692ea7 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -1288,7 +1288,7 @@ static int rsa_fill_keygen_data(struct uadk_rsa_sess *rsa_sess, if (!keygen_param->wd_e) return UADK_E_FAIL; - keygen_param->wd_e->dsize = BN_bn2bin(bn_param->e, + keygen_param->wd_e->dsize = (__u32)BN_bn2bin(bn_param->e, (unsigned char *)keygen_param->wd_e->data); wd_rsa_get_prikey(rsa_sess->sess, &key_pair->prikey); @@ -1300,9 +1300,9 @@ static int rsa_fill_keygen_data(struct uadk_rsa_sess *rsa_sess, if (!keygen_param->wd_q || !keygen_param->wd_p) return UADK_E_FAIL; - keygen_param->wd_q->dsize = BN_bn2bin(bn_param->q, + keygen_param->wd_q->dsize = (__u32)BN_bn2bin(bn_param->q, (unsigned char *)keygen_param->wd_q->data); - keygen_param->wd_p->dsize = BN_bn2bin(bn_param->p, + keygen_param->wd_p->dsize = (__u32)BN_bn2bin(bn_param->p, (unsigned char *)keygen_param->wd_p->data); rsa_sess->req.src_bytes = rsa_sess->key_size; -- 2.43.0
From: Chenghai Huang <huangchenghai2@huawei.com> Replace magic expt=1 with ENGINE_SYNC_POLL_NUM in engine files and PROV_SYNC_POLL_NUM in provider files, both defined as 1U to match the __u32 expt type. Provider uses its own macro to avoid coupling with engine's uadk.h header. Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com> --- src/uadk.h | 2 ++ src/uadk_aead.c | 8 ++++---- src/uadk_cipher.c | 8 ++++---- src/uadk_dh.c | 6 +++--- src/uadk_digest.c | 6 +++--- src/uadk_pkey.c | 6 +++--- src/uadk_prov.h | 2 ++ src/uadk_prov_aead.c | 3 +-- src/uadk_prov_cipher.c | 3 +-- src/uadk_prov_dh.c | 3 +-- src/uadk_prov_digest.c | 3 +-- src/uadk_prov_hmac.c | 3 +-- src/uadk_prov_pkey.c | 2 +- src/uadk_prov_rsa.c | 3 +-- src/uadk_rsa.c | 6 +++--- 15 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/uadk.h b/src/uadk.h index a6db774..b190233 100644 --- a/src/uadk.h +++ b/src/uadk.h @@ -24,6 +24,8 @@ #define ENGINE_SEND_MAX_CNT 90000000 #define ENGINE_RECV_MAX_CNT 60000000 #define ENGINE_ENV_RECV_MAX_CNT 60000000 +/* Number of packets polled in one synchronous completion check */ +#define ENGINE_SYNC_POLL_NUM 1U #define UADK_UNINIT 0 #define UADK_INIT_SUCCESS 1 #define UADK_INIT_FAIL 2 diff --git a/src/uadk_aead.c b/src/uadk_aead.c index b88d034..c4f83a2 100644 --- a/src/uadk_aead.c +++ b/src/uadk_aead.c @@ -78,10 +78,10 @@ static EVP_CIPHER *uadk_aes_256_gcm; static int uadk_e_aead_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { @@ -99,10 +99,10 @@ static int uadk_e_aead_env_poll(void *ctx) static int uadk_e_aead_poll(void *ctx) { struct aead_priv_ctx *priv = (struct aead_priv_ctx *) ctx; + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret, idx; if (priv->req.op_type == WD_CIPHER_ENCRYPTION_DIGEST) diff --git a/src/uadk_cipher.c b/src/uadk_cipher.c index 2b1537c..091c636 100644 --- a/src/uadk_cipher.c +++ b/src/uadk_cipher.c @@ -307,10 +307,10 @@ static int sched_single_poll_policy(handle_t h_sched_ctx, static int uadk_e_cipher_poll(void *ctx) { struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *) ctx; + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret, idx; if (priv->req.op_type == WD_CIPHER_ENCRYPTION) @@ -335,10 +335,10 @@ static int uadk_e_cipher_poll(void *ctx) static int uadk_e_cipher_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_dh.c b/src/uadk_dh.c index ab8a305..d5b1912 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -208,9 +208,9 @@ static void uadk_e_dh_set_status(void) static int uadk_e_dh_poll(void *ctx) { + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - int expt = 1; int idx = 1; int ret; @@ -282,10 +282,10 @@ static struct dh_res_config dh_res_config = { static int uadk_e_dh_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_digest.c b/src/uadk_digest.c index e18b25b..c5b8c99 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -394,9 +394,9 @@ static int sched_single_poll_policy(handle_t h_sched_ctx, static int uadk_e_digest_poll(void *ctx) { + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - int expt = 1; int ret = 0; do { @@ -416,10 +416,10 @@ static int uadk_e_digest_poll(void *ctx) static int uadk_e_digest_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_pkey.c b/src/uadk_pkey.c index 1fbee88..b2cfe48 100644 --- a/src/uadk_pkey.c +++ b/src/uadk_pkey.c @@ -119,9 +119,9 @@ static void uadk_e_ecc_set_status(void) static int uadk_ecc_poll(void *ctx) { + __u32 expt = ENGINE_SYNC_POLL_NUM; unsigned int recv = 0; __u64 rx_cnt = 0; - int expt = 1; int ret; do { @@ -173,10 +173,10 @@ static void uadk_e_ecc_set_support_state(int alg_tag, int value) static int uadk_e_ecc_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_prov.h b/src/uadk_prov.h index 41c61b6..ac71fe0 100644 --- a/src/uadk_prov.h +++ b/src/uadk_prov.h @@ -31,6 +31,8 @@ #define PROV_SEND_MAX_CNT 90000000 #define PROV_RECV_MAX_CNT 60000000 #define PROV_SCH_RECV_MAX_CNT 60000000 +/* Number of packets polled in one synchronous completion check */ +#define PROV_SYNC_POLL_NUM 1U #define UADK_P_SUCCESS 1 #define UADK_P_FAIL 0 #define UADK_DO_SOFT (-0xE0) diff --git a/src/uadk_prov_aead.c b/src/uadk_prov_aead.c index 83a7510..3747708 100644 --- a/src/uadk_prov_aead.c +++ b/src/uadk_prov_aead.c @@ -161,10 +161,9 @@ static EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in) static int uadk_aead_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_prov_cipher.c b/src/uadk_prov_cipher.c index 5c9dfe8..38ab088 100644 --- a/src/uadk_prov_cipher.c +++ b/src/uadk_prov_cipher.c @@ -444,10 +444,9 @@ static int uadk_prov_cipher_dev_init(struct cipher_priv_ctx *priv); static int uadk_cipher_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 recv_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_prov_dh.c b/src/uadk_prov_dh.c index bd403d7..d7d490d 100644 --- a/src/uadk_prov_dh.c +++ b/src/uadk_prov_dh.c @@ -648,10 +648,9 @@ static void uadk_prov_dh_cb(void *req_t) static int uadk_prov_dh_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c index 3334662..23c0c53 100644 --- a/src/uadk_prov_digest.c +++ b/src/uadk_prov_digest.c @@ -293,10 +293,9 @@ static void uadk_digest_reset(struct digest_priv_ctx *priv) static int uadk_digest_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - __u32 expt = 1; int ret; do { diff --git a/src/uadk_prov_hmac.c b/src/uadk_prov_hmac.c index 83b0809..53544f3 100644 --- a/src/uadk_prov_hmac.c +++ b/src/uadk_prov_hmac.c @@ -295,10 +295,9 @@ static void uadk_hmac_reset(struct hmac_priv_ctx *priv) static int uadk_hmac_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - __u32 expt = 1; int ret; do { diff --git a/src/uadk_prov_pkey.c b/src/uadk_prov_pkey.c index 0d392ca..2ff54ae 100644 --- a/src/uadk_prov_pkey.c +++ b/src/uadk_prov_pkey.c @@ -443,9 +443,9 @@ err: int uadk_prov_ecc_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; unsigned int recv = 0; __u64 rx_cnt = 0; - int expt = 1; int ret; do { diff --git a/src/uadk_prov_rsa.c b/src/uadk_prov_rsa.c index 208db37..5beece9 100644 --- a/src/uadk_prov_rsa.c +++ b/src/uadk_prov_rsa.c @@ -184,10 +184,9 @@ int rsa_fill_pubkey(struct rsa_pubkey_param *pubkey_param, static int uadk_rsa_env_poll(void *ctx) { + __u32 expt = PROV_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index e692ea7..a96d1b9 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -728,9 +728,9 @@ static void uadk_e_rsa_set_status(void) static int uadk_e_rsa_poll(void *ctx) { + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - int expt = 1; int ret; do { @@ -766,10 +766,10 @@ static struct rsa_res_config rsa_res_config = { static int uadk_e_rsa_env_poll(void *ctx) { + /* Poll one packet currently */ + __u32 expt = ENGINE_SYNC_POLL_NUM; __u64 rx_cnt = 0; __u32 recv = 0; - /* Poll one packet currently */ - int expt = 1; int ret; do { -- 2.43.0
When g=2 and bit 2 of p is clear (p%8==3), g is a quadratic non-residue mod p. An odd priv_key makes pub_key fall outside the (p-1)/2 subgroup, leaking the low bit. Clear bit 0 to keep priv_key even, matching the OpenSSL software implementation. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_dh.c | 14 +++++++++++++- src/uadk_prov_dh.c | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/uadk_dh.c b/src/uadk_dh.c index d5b1912..5e7b6d9 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -37,6 +37,7 @@ #define UADK_DH_MAX_MODULE_BIT 4096 #define DH_GENERATOR_2 2 #define DH_GENERATOR_5 5 +#define DH_QNR_BIT_INDEX 2 #define CHAR_BIT_SIZE 3 #define DH_PARAMS_CNT 3 #define CTX_MODE_NUM 2 @@ -146,6 +147,8 @@ static int uadk_e_dh_soft_compute_key(unsigned char *key, static int dh_generate_new_priv_key(const DH *dh, BIGNUM *new_priv_key) { const BIGNUM *q = DH_get0_q(dh); + const BIGNUM *g = DH_get0_g(dh); + const BIGNUM *p = DH_get0_p(dh); int bits; if (q) { @@ -155,10 +158,19 @@ static int dh_generate_new_priv_key(const DH *dh, BIGNUM *new_priv_key) } while (BN_is_zero(new_priv_key) || BN_is_one(new_priv_key)); } else { bits = DH_get_length(dh) ? - DH_get_length(dh) : BN_num_bits(DH_get0_p(dh)) - 1; + DH_get_length(dh) : BN_num_bits(p) - 1; if (!BN_priv_rand(new_priv_key, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) return UADK_E_FAIL; + + /* + * When g=2 and p%8==3, g is a quadratic non-residue. + * If priv_key is odd, pub_key = g^priv mod p falls outside + * the (p-1)/2 subgroup. Clear bit 0 to keep priv_key even, + * matching OpenSSL soft implementation. + */ + if (BN_is_word(g, DH_GENERATOR_2) && !BN_is_bit_set(p, DH_QNR_BIT_INDEX)) + BN_clear_bit(new_priv_key, 0); } return UADK_E_SUCCESS; diff --git a/src/uadk_prov_dh.c b/src/uadk_prov_dh.c index d7d490d..2e4a832 100644 --- a/src/uadk_prov_dh.c +++ b/src/uadk_prov_dh.c @@ -37,6 +37,7 @@ #define DH4096BITS 4096 #define UADK_DH_MAX_MODULE_BIT 4096 #define DH_GENERATOR_2 2 +#define DH_QNR_BIT_INDEX 2 #define CHAR_BIT_SIZE 3 #define DH_PARAMS_CNT 3 #define CTX_MODE_NUM 2 @@ -505,15 +506,27 @@ static void uadk_DH_set_flags(DH *dh, int flags) static int uadk_dh_gen_prikey_undef(const DH *dh, BIGNUM *new_prikey) { + const BIGNUM *g = NULL; + const BIGNUM *p = NULL; int bits; + uadk_DH_get0_pqg(dh, &p, NULL, &g); bits = uadk_DH_get_length(dh) ? - uadk_DH_get_length(dh) : BN_num_bits(uadk_DH_get0_p(dh)) - 1; + uadk_DH_get_length(dh) : BN_num_bits(p) - 1; if (!BN_priv_rand(new_prikey, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) { UADK_ERR("failed to BN_priv_rand\n"); return UADK_P_FAIL; } + /* + * When g=2 and p%8==3, g is a quadratic non-residue. + * If prikey is odd, pubkey = g^prikey mod p falls outside + * the (p-1)/2 subgroup. Clear bit 0 to keep prikey even, + * matching OpenSSL soft implementation. + */ + if (BN_is_word(g, DH_GENERATOR_2) && !BN_is_bit_set(p, DH_QNR_BIT_INDEX)) + BN_clear_bit(new_prikey, 0); + return UADK_P_SUCCESS; } -- 2.43.0
The q parameter only constrains the private key range, which dh_generate_new_priv_key already handles via DH_get0_q. Remove the `|| q` check that was blocking DHX / FFDHE groups from using hardware acceleration. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_dh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uadk_dh.c b/src/uadk_dh.c index 5e7b6d9..d138805 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -862,7 +862,7 @@ static int uadk_e_dh_generate_key(DH *dh) goto exe_soft; DH_get0_pqg(dh, &p, &q, &g); - if (!p || !g || q) + if (!p || !g) return UADK_E_FAIL; /* Get session and prepare private key */ -- 2.43.0
HPRE hardware only validates 1 < pub_key < p-1 but does not verify pub_key^q == 1 mod p. For DHX / FFDHE groups carrying q, add a small subgroup check on the engine path. On the provider path, validate the derived secret z satisfies 1 < z < p-1 (FIPS 186-4 section 6.2.2) after hardware returns. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_dh.c | 39 +++++++++++++++++++++++++++++++++++++++ src/uadk_prov_dh.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/uadk_dh.c b/src/uadk_dh.c index d138805..416b900 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -905,6 +905,35 @@ exe_soft: return uadk_e_dh_soft_generate_key(dh); } +static bool dh_check_small_subgroup(const BIGNUM *pub_key, const BIGNUM *q, + const BIGNUM *p) +{ + int valid = 0; + BIGNUM *tmp; + BN_CTX *ctx; + + ctx = BN_CTX_new(); + if (!ctx) { + fprintf(stderr, "failed to alloc BN_CTX for pub check\n"); + return false; + } + + BN_CTX_start(ctx); + tmp = BN_CTX_get(ctx); + if (tmp && BN_mod_exp(tmp, pub_key, q, p, ctx) && BN_is_one(tmp)) + valid = 1; + + BN_CTX_end(ctx); + BN_CTX_free(ctx); + + if (!valid) { + fprintf(stderr, "invalid peer pubkey: small subgroup check failed\n"); + return false; + } + + return true; +} + static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) { @@ -927,6 +956,16 @@ static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, if (!p || !g) return UADK_E_FAIL; + /* + * Hardware validates 1 < pub_key < p-1 (N>M + odd check on prime p), + * but does NOT verify pub_key^q == 1 mod p. For DHX / FFDHE groups + * carrying q, perform small subgroup check in software to defend + * against small subgroup attacks. Traditional safe prime DH (q==NULL) + * relies on hardware validation only. + */ + if (q && !dh_check_small_subgroup(pub_key, q, p)) + return UADK_E_FAIL; + ret = dh_prepare_data(g, dh, &dh_sess, &priv_key); if (!ret) { fprintf(stderr, "failed to prepare dh data\n"); diff --git a/src/uadk_prov_dh.c b/src/uadk_prov_dh.c index 2e4a832..33e5536 100644 --- a/src/uadk_prov_dh.c +++ b/src/uadk_prov_dh.c @@ -1656,6 +1656,33 @@ static void uadk_prov_dh_free_compkey_req(struct uadk_dh_sess *dh_sess) } } +static int dh_validate_derived_secret(const unsigned char *data, __u16 dlen, + const BIGNUM *p) +{ + int ret = UADK_P_FAIL; + BIGNUM *pminus1; + BIGNUM *z; + + z = BN_bin2bn(data, dlen, NULL); + pminus1 = BN_new(); + if (!z || !pminus1 || !BN_copy(pminus1, p) || !BN_sub_word(pminus1, 1)) { + UADK_ERR("failed to validate derived secret\n"); + goto end; + } + + if (BN_cmp(z, BN_value_one()) <= 0 || BN_cmp(z, pminus1) == 0) { + UADK_ERR("derived secret out of valid range\n"); + goto end; + } + + ret = UADK_P_SUCCESS; + +end: + BN_free(z); + BN_free(pminus1); + return ret; +} + static int uadk_dh_compute_key(unsigned char *key, const BIGNUM *pubkey, DH *dh) { struct uadk_dh_sess *dh_sess = NULL; @@ -1701,6 +1728,11 @@ static int uadk_dh_compute_key(unsigned char *key, const BIGNUM *pubkey, DH *dh) } memcpy(key, dh_sess->req.pri, dh_sess->req.pri_bytes); + + /* Validate derived secret: 1 < z < p-1 (FIPS 186-4 section 6.2.2) */ + ret = dh_validate_derived_secret(key, dh_sess->req.pri_bytes, p); + if (ret != UADK_P_SUCCESS) + goto free_req; ret = dh_sess->req.pri_bytes; uadk_prov_dh_free_compkey_req(dh_sess); /* key will be used by user, do not free it here */ -- 2.43.0
When EC_FLAG_COFACTOR_ECDH is set, the effective scalar is h * priv_key (SEC 4). Previously the engine fell back to software, and the provider fell back to software when the curve cofactor was not 1. Compute h * priv_key in software (a fast BN_mul) and pass the product to hardware for the point multiplication. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_ec.c | 74 ++++++++++++++++++++++- src/uadk_prov_ecdh_exch.c | 123 ++++++++++++++++++++++++-------------- 2 files changed, 151 insertions(+), 46 deletions(-) diff --git a/src/uadk_ec.c b/src/uadk_ec.c index 1114434..b99168e 100644 --- a/src/uadk_ec.c +++ b/src/uadk_ec.c @@ -1346,6 +1346,70 @@ static int ecc_compkey_check(unsigned char **out, return 1; } +static int uadk_ecc_set_private_key_bn(handle_t sess, const BIGNUM *prikey, + size_t key_bytes) +{ + unsigned char bin[UADK_ECC_MAX_KEY_BYTES]; + struct wd_ecc_key *ecc_key; + struct wd_dtb prikey_dtb; + int ret; + + ecc_key = wd_ecc_get_key(sess); + prikey_dtb.data = (void *)bin; + ret = BN_bn2binpad(prikey, bin, (int)key_bytes); + if (ret < 0) { + fprintf(stderr, "cofactor priv too large for hardware, fallback to software\n"); + return UADK_DO_SOFT; + } + prikey_dtb.dsize = ret; + + ret = wd_ecc_set_prikey(ecc_key, &prikey_dtb); + if (ret) { + fprintf(stderr, "failed to set ecc prikey, ret = %d\n", ret); + ret = UADK_DO_SOFT; + } + + return ret; +} + +static int ecdh_set_cofactor_privkey(handle_t sess, const EC_KEY *ecdh) +{ + const BIGNUM *cofactor; + const BIGNUM *priv_key; + const EC_GROUP *group; + BIGNUM *cofactor_priv; + size_t key_bytes; + BN_CTX *ctx; + int ret; + + group = EC_KEY_get0_group(ecdh); + priv_key = EC_KEY_get0_private_key(ecdh); + cofactor = EC_GROUP_get0_cofactor(group); + if (!cofactor || !priv_key) { + fprintf(stderr, "cofactor ecdh: missing cofactor or priv_key\n"); + return -EINVAL; + } + + ctx = BN_CTX_new(); + if (!ctx) + return -ENOMEM; + + cofactor_priv = BN_secure_new(); + if (!cofactor_priv || !BN_mul(cofactor_priv, cofactor, priv_key, ctx)) { + fprintf(stderr, "cofactor ecdh: BN_mul failed\n"); + ret = -EINVAL; + goto end; + } + + key_bytes = (EC_GROUP_get_degree(group) + 7) / 8; + ret = uadk_ecc_set_private_key_bn(sess, cofactor_priv, key_bytes); + +end: + BN_clear_free(cofactor_priv); + BN_CTX_free(ctx); + return ret; +} + static int ecdh_compute_key(unsigned char **out, size_t *outlen, const EC_POINT *pub_key, const EC_KEY *ecdh) { @@ -1374,7 +1438,15 @@ static int ecdh_compute_key(unsigned char **out, size_t *outlen, if (!ret) goto free_sess; - ret = uadk_ecc_set_private_key(sess, ecdh); + /* + * Cofactor ECDH: when EC_FLAG_COFACTOR_ECDH is set, the effective + * scalar is h * priv_key (RFC 2789 / SEC 4). Compute h*priv in + * a temporary BIGNUM to avoid modifying the shared EC_KEY object. + */ + if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) + ret = ecdh_set_cofactor_privkey(sess, ecdh); + else + ret = uadk_ecc_set_private_key(sess, ecdh); if (ret) goto uninit_iot; diff --git a/src/uadk_prov_ecdh_exch.c b/src/uadk_prov_ecdh_exch.c index 1b3b123..18627df 100644 --- a/src/uadk_prov_ecdh_exch.c +++ b/src/uadk_prov_ecdh_exch.c @@ -70,6 +70,7 @@ struct ecdh_sess_ctx { const EC_POINT *pub_key; const BIGNUM *cofactor; const EC_GROUP *group; + BIGNUM *cofactor_priv; }; UADK_PKEY_KEYEXCH_DESCR(ecdh, ECDH); @@ -102,6 +103,32 @@ static size_t ecdh_get_ec_size(const EC_GROUP *group) return BITS_TO_BYTES(degree); } +static int ecdh_set_private_key_bn(handle_t sess, const BIGNUM *prikey, + size_t key_bytes) +{ + unsigned char bin[UADK_ECC_MAX_KEY_BYTES]; + struct wd_ecc_key *ecc_key; + struct wd_dtb prikey_dtb; + int dsize; + + dsize = BN_bn2binpad(prikey, bin, (int)key_bytes); + if (dsize < 0) { + UADK_ERR("cofactor priv too large for hardware, fallback to software\n"); + return UADK_DO_SOFT; + } + + ecc_key = wd_ecc_get_key(sess); + prikey_dtb.data = (void *)bin; + prikey_dtb.dsize = dsize; + + if (wd_ecc_set_prikey(ecc_key, &prikey_dtb)) { + UADK_ERR("failed to set cofactor prikey\n"); + return UADK_P_FAIL; + } + + return UADK_P_SUCCESS; +} + static int ecdh_param_check(struct ecdh_ctx *pecdhctx, struct ecdh_sess_ctx *sess_ctx) { const EC_GROUP *group; @@ -145,43 +172,6 @@ static int ecdh_param_check(struct ecdh_ctx *pecdhctx, struct ecdh_sess_ctx *ses return UADK_P_SUCCESS; } -static int ecdh_set_privk(struct ecdh_ctx *pecdhctx, - struct ecdh_sess_ctx *sess_ctx) -{ - int key_cofactor_mode; - - /* - * The ctx->cofactor_mode flag has precedence over the - * cofactor_mode flag set on ctx->k. - * - * - if ctx->cofactor_mode == -1, use ctx->k directly - * - if ctx->cofactor_mode == key_cofactor_mode, use ctx->k directly - * - if ctx->cofactor_mode != key_cofactor_mode: - * - if ctx->k->cofactor == 1, the cofactor_mode flag is irrelevant, use - * ctx->k directly - * - if ctx->k->cofactor != 1, use a duplicate of ctx->k with the flag - * set to ctx->cofactor_mode - */ - key_cofactor_mode = (EC_KEY_get_flags(pecdhctx->k) & EC_FLAG_COFACTOR_ECDH) ? - COFACTOR_MODE_ENABLED : COFACTOR_MODE_DISABLED; - if (pecdhctx->cofactor_mode != COFACTOR_MODE_USE_KEY && - pecdhctx->cofactor_mode != key_cofactor_mode && - !BN_is_one(sess_ctx->cofactor)) { - sess_ctx->privk = EC_KEY_dup(pecdhctx->k); - if (!sess_ctx->privk) - return UADK_P_FAIL; - - if (pecdhctx->cofactor_mode == COFACTOR_MODE_ENABLED) - EC_KEY_set_flags(sess_ctx->privk, EC_FLAG_COFACTOR_ECDH); - else - EC_KEY_clear_flags(sess_ctx->privk, EC_FLAG_COFACTOR_ECDH); - } else { - sess_ctx->privk = pecdhctx->k; - } - - return UADK_P_SUCCESS; -} - static handle_t ecdh_alloc_sess(EC_KEY *privk) { int ret; @@ -308,6 +298,45 @@ static int ecdh_get_shared_key(unsigned char *secret, return UADK_P_SUCCESS; } +static int ecdh_compute_cofactor_priv(const struct ecdh_ctx *pecdhctx, + struct ecdh_sess_ctx *sess_ctx) +{ + const BIGNUM *priv_key; + int key_cofactor_mode; + BN_CTX *bn_ctx; + + sess_ctx->cofactor_priv = NULL; + + key_cofactor_mode = (EC_KEY_get_flags(pecdhctx->k) & EC_FLAG_COFACTOR_ECDH) ? + COFACTOR_MODE_ENABLED : COFACTOR_MODE_DISABLED; + if (!(pecdhctx->cofactor_mode == COFACTOR_MODE_ENABLED || + (pecdhctx->cofactor_mode == COFACTOR_MODE_USE_KEY && + key_cofactor_mode == COFACTOR_MODE_ENABLED)) || + BN_is_one(sess_ctx->cofactor)) + return UADK_P_SUCCESS; + + priv_key = EC_KEY_get0_private_key(pecdhctx->k); + bn_ctx = BN_CTX_new(); + if (!priv_key || !bn_ctx) { + UADK_ERR("cofactor ecdh: missing priv_key or BN_CTX\n"); + BN_CTX_free(bn_ctx); + return UADK_P_FAIL; + } + + sess_ctx->cofactor_priv = BN_secure_new(); + if (!sess_ctx->cofactor_priv || + !BN_mul(sess_ctx->cofactor_priv, sess_ctx->cofactor, priv_key, bn_ctx)) { + UADK_ERR("cofactor ecdh: BN_mul failed\n"); + BN_clear_free(sess_ctx->cofactor_priv); + sess_ctx->cofactor_priv = NULL; + BN_CTX_free(bn_ctx); + return UADK_P_FAIL; + } + + BN_CTX_free(bn_ctx); + return UADK_P_SUCCESS; +} + static int ecdh_compute_key(struct ecdh_sess_ctx *sess_ctx, unsigned char *secret, size_t *psecretlen, size_t size) @@ -322,8 +351,13 @@ static int ecdh_compute_key(struct ecdh_sess_ctx *sess_ctx, return UADK_DO_SOFT; } - ret = uadk_prov_ecc_set_private_key(sess, sess_ctx->privk); - if (!ret) { + if (sess_ctx->cofactor_priv) + ret = ecdh_set_private_key_bn(sess, + sess_ctx->cofactor_priv, + ecdh_get_ec_size(sess_ctx->group)); + else + ret = uadk_prov_ecc_set_private_key(sess, sess_ctx->privk); + if (ret != UADK_P_SUCCESS) { UADK_ERR("failed to set private key!\n"); goto free_sess; } @@ -368,17 +402,16 @@ static int ecdh_plain_derive(struct ecdh_ctx *pecdhctx, return UADK_P_SUCCESS; } - ret = ecdh_set_privk(pecdhctx, &sess_ctx); - if (!ret) { - UADK_ERR("failed to set private key!\n"); + ret = ecdh_compute_cofactor_priv(pecdhctx, &sess_ctx); + if (ret != UADK_P_SUCCESS) return ret; - } + + sess_ctx.privk = pecdhctx->k; size = outlen < ec_size ? outlen : ec_size; ret = ecdh_compute_key(&sess_ctx, secret, psecretlen, size); - if (sess_ctx.privk != pecdhctx->k) - EC_KEY_free(sess_ctx.privk); + BN_clear_free(sess_ctx.cofactor_priv); return ret; } -- 2.43.0
When a private key is preset via OSSL_PKEY_PARAM_EC_PRIV_KEY, the hardware keygen path skipped upper bound validation. The UADK driver only rejects d <= 1, so priv_k >= order would pass through and produce a non-canonical private key value. Check that priv_key < order before setting it on the hardware session. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ec_kmgmt.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/uadk_prov_ec_kmgmt.c b/src/uadk_prov_ec_kmgmt.c index 2b44d20..932b690 100644 --- a/src/uadk_prov_ec_kmgmt.c +++ b/src/uadk_prov_ec_kmgmt.c @@ -199,8 +199,15 @@ static int ec_set_private_key(EC_KEY *ec, BIGNUM *priv_key) const BIGNUM *order; int cnt = 0; - if (priv_k) + if (priv_k) { + group = EC_KEY_get0_group(ec); + order = EC_GROUP_get0_order(group); + if (BN_cmp(priv_k, order) >= 0) { + UADK_ERR("invalid: preset private key out of range\n"); + return UADK_P_FAIL; + } goto set_key; + } priv_k = BN_secure_new(); if (!priv_k) { -- 2.43.0
The X448/X25519 keyexch set_ctx_params and get_ctx_params callbacks returned UADK_P_FAIL when the default provider had no implementation. A missing optional callback is not an error; return UADK_P_SUCCESS. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ecx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/uadk_prov_ecx.c b/src/uadk_prov_ecx.c index 7820b29..cdd49ff 100644 --- a/src/uadk_prov_ecx.c +++ b/src/uadk_prov_ecx.c @@ -918,7 +918,7 @@ static void uadk_keyexch_x448_freectx(void *vecxctx) static int uadk_keyexch_x448_set_ctx_params(void *ecxctx, const OSSL_PARAM params[]) { if (get_default_x448_keyexch().set_ctx_params == NULL) - return UADK_P_FAIL; + return UADK_P_SUCCESS; return get_default_x448_keyexch().set_ctx_params(ecxctx, params); } @@ -944,7 +944,7 @@ static const OSSL_PARAM *uadk_keyexch_x448_gettable_ctx_params(ossl_unused void static int uadk_keyexch_x448_get_ctx_params(void *ecxctx, OSSL_PARAM params[]) { if (get_default_x448_keyexch().get_ctx_params == NULL) - return UADK_P_FAIL; + return UADK_P_SUCCESS; return get_default_x448_keyexch().get_ctx_params(ecxctx, params); } @@ -1557,7 +1557,7 @@ static void uadk_keyexch_x25519_freectx(void *vecxctx) static int uadk_keyexch_x25519_set_ctx_params(void *ecxctx, const OSSL_PARAM params[]) { if (get_default_x25519_keyexch().set_ctx_params == NULL) - return UADK_P_FAIL; + return UADK_P_SUCCESS; return get_default_x25519_keyexch().set_ctx_params(ecxctx, params); } @@ -1583,7 +1583,7 @@ static const OSSL_PARAM *uadk_keyexch_x25519_gettable_ctx_params(ossl_unused voi static int uadk_keyexch_x25519_get_ctx_params(void *ecxctx, OSSL_PARAM params[]) { if (get_default_x25519_keyexch().get_ctx_params == NULL) - return UADK_P_FAIL; + return UADK_P_SUCCESS; return get_default_x25519_keyexch().get_ctx_params(ecxctx, params); } -- 2.43.0
The UADK hardware only supports 2-prime RSA key generation. When gctx->primes > RSA_MAX_PRIME_NUM, fall back to the default provider instead of proceeding with hardware. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_rsa_kmgmt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/uadk_prov_rsa_kmgmt.c b/src/uadk_prov_rsa_kmgmt.c index 208b65e..c95b1ed 100644 --- a/src/uadk_prov_rsa_kmgmt.c +++ b/src/uadk_prov_rsa_kmgmt.c @@ -858,6 +858,10 @@ static void *uadk_keymgmt_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cba if (!gctx) return NULL; + /* UADK hardware only supports 2-prime RSA, fallback to software for multi-prime */ + if (gctx->primes > RSA_MAX_PRIME_NUM) + return uadk_rsa_sw_gen(genctx, osslcb, cbarg); + rsa = ossl_rsa_new_with_ctx(gctx->libctx); if (!rsa) return NULL; -- 2.43.0
Copy PSS restriction parameters from the genctx to the generated key so PSS keys retain their mandated digest or salt length. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_rsa_kmgmt.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/uadk_prov_rsa_kmgmt.c b/src/uadk_prov_rsa_kmgmt.c index c95b1ed..44bf0cd 100644 --- a/src/uadk_prov_rsa_kmgmt.c +++ b/src/uadk_prov_rsa_kmgmt.c @@ -17,6 +17,7 @@ * */ #include "uadk_prov_rsa.h" +#include "uadk_prov_rsa_utils.h" #define PRIME_CHECK_BIT_NUM 4 #define RSA_MAX_PRIME_NUM 2 @@ -850,6 +851,7 @@ static void *uadk_rsa_sw_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) static void *uadk_keymgmt_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) { + struct rsa_pss_params_30_st *pss_params; struct rsa_gen_ctx *gctx = genctx; BN_GENCB *gencb; RSA *rsa; @@ -881,6 +883,11 @@ static void *uadk_keymgmt_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cba uadk_rsa_clear_flags(rsa, RSA_FLAG_TYPE_MASK); uadk_rsa_set_flags(rsa, gctx->rsa_type); + + /* Copy PSS restriction parameters to the generated key */ + pss_params = ossl_rsa_get0_pss_params_30(rsa); + *pss_params = gctx->pss_params; + BN_GENCB_free(gencb); return rsa; -- 2.43.0
Use BN_priv_rand_range instead of BN_rand_range in ecdh_set_private_key to generate private keys via the private DRBG. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uadk_ec.c b/src/uadk_ec.c index b99168e..648cce9 100644 --- a/src/uadk_ec.c +++ b/src/uadk_ec.c @@ -906,7 +906,7 @@ static int ecdh_set_private_key(EC_KEY *eckey, BIGNUM *order) } do { - if (!BN_rand_range(priv_key, order)) { + if (!BN_priv_rand_range(priv_key, order)) { fprintf(stderr, "failed to generate random value\n"); ret = 0; goto free_priv_key; -- 2.43.0
When check_rsa_pridec_padding fails in uadk_e_rsa_private_decrypt, return the error directly instead of setting UADK_DO_SOFT which triggers a software retry that also fails on bad padding. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_rsa.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index a96d1b9..d8ca72e 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -1721,8 +1721,6 @@ static int uadk_e_rsa_private_decrypt(int flen, const unsigned char *from, } ret = check_rsa_pridec_padding(to, num_bytes, from_buf, len, padding); - if (!ret) - ret = UADK_DO_SOFT; free_dec_bn: BN_free(dec_bn); -- 2.43.0
Add sig_r/s size check in ecdsa_verify_init_iot before BN_bn2bin writes to the fixed-size stack buffer to prevent overflow. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ecdsa.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/uadk_prov_ecdsa.c b/src/uadk_prov_ecdsa.c index 032bc2a..bd7ca73 100644 --- a/src/uadk_prov_ecdsa.c +++ b/src/uadk_prov_ecdsa.c @@ -737,6 +737,11 @@ static int ecdsa_verify_init_iot(handle_t sess, struct wd_ecc_req *req, r.bsize = UADK_ECC_MAX_KEY_BYTES; s.bsize = UADK_ECC_MAX_KEY_BYTES; ECDSA_SIG_get0(opdata->sig, &sig_r, &sig_s); + if (BN_num_bytes(sig_r) > UADK_ECC_MAX_KEY_BYTES || + BN_num_bytes(sig_s) > UADK_ECC_MAX_KEY_BYTES) { + UADK_ERR("invalid: sig_r/s too large\n"); + return UADK_P_FAIL; + } r.dsize = BN_bn2bin(sig_r, (void *)r.data); s.dsize = BN_bn2bin(sig_s, (void *)s.data); ecc_in = wd_ecdsa_new_verf_in(sess, &e, &r, &s); -- 2.43.0
Relax the flen check in uadk_prov_rsa_private_decrypt from flen != num_bytes to flen > num_bytes since hardware handles short inputs correctly. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_rsa_enc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/uadk_prov_rsa_enc.c b/src/uadk_prov_rsa_enc.c index 80a40d5..23dd147 100644 --- a/src/uadk_prov_rsa_enc.c +++ b/src/uadk_prov_rsa_enc.c @@ -304,7 +304,7 @@ static int uadk_prov_rsa_private_decrypt(int flen, const unsigned char *from, goto free_sess; } - if (flen != num_bytes) { + if (flen > num_bytes) { ret = UADK_P_FAIL; goto free_buf; } @@ -315,7 +315,8 @@ static int uadk_prov_rsa_private_decrypt(int flen, const unsigned char *from, goto free_buf; } - memcpy(rsa_sess->req.src, from, rsa_sess->req.src_bytes); + memset(rsa_sess->req.src, 0, rsa_sess->req.src_bytes); + memcpy(rsa_sess->req.src + rsa_sess->req.src_bytes - flen, from, flen); ret = rsa_do_crypto(rsa_sess); if (!ret || rsa_sess->req.status) { @@ -327,7 +328,7 @@ static int uadk_prov_rsa_private_decrypt(int flen, const unsigned char *from, if (!ret) goto free_buf; - ret = check_rsa_pridec_padding(to, num_bytes, from_buf, flen, padding); + ret = check_rsa_pridec_padding(to, num_bytes, from_buf, num_bytes, padding); free_buf: rsa_free_pri_bn_ctx(from_buf); -- 2.43.0
-ENOMEM is not a valid OpenSSL RSA method return value. Define RSA_FAIL as -1 and return it from RSA method callbacks when parameter allocation fails. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_rsa.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index d8ca72e..1d9ad7b 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -51,6 +51,7 @@ #define UADK_DO_SOFT (-0xE0) #define UADK_E_INIT_SUCCESS 0 #define CHECK_PADDING_FAIL (-1) +#define RSA_FAIL (-1) #define ENV_ENABLED 1 #define PRIME_RETRY_COUNT 4 #define GENCB_NEXT 2 @@ -1507,7 +1508,7 @@ static int uadk_e_rsa_keygen(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb) ret = rsa_keygen_param_alloc(&keygen_param, &bn_param, &key_pair); if (ret == -ENOMEM) - return ret; + return RSA_FAIL; rsa_sess = rsa_get_eng_session(rsa, bits, is_crt); if (!rsa_sess) { @@ -1581,7 +1582,7 @@ static int uadk_e_rsa_public_encrypt(int flen, const unsigned char *from, ret = rsa_pkey_param_alloc(&pub_enc, NULL); if (ret == -ENOMEM) - return ret; + return RSA_FAIL; is_crt = check_rsa_is_crt(rsa); @@ -1677,7 +1678,7 @@ static int uadk_e_rsa_private_decrypt(int flen, const unsigned char *from, ret = rsa_pkey_param_alloc(NULL, &pri); if (ret == -ENOMEM) - return ret; + return RSA_FAIL; pri->is_crt = check_rsa_is_crt(rsa); @@ -1762,7 +1763,7 @@ static int uadk_e_rsa_private_sign(int flen, const unsigned char *from, ret = rsa_pkey_param_alloc(NULL, &pri); if (ret == -ENOMEM) - return ret; + return RSA_FAIL; pri->is_crt = check_rsa_is_crt(rsa); @@ -1869,7 +1870,7 @@ static int uadk_e_rsa_public_verify(int flen, const unsigned char *from, ret = rsa_pkey_param_alloc(&pub, NULL); if (ret == -ENOMEM) - return ret; + return RSA_FAIL; is_crt = check_rsa_is_crt(rsa); -- 2.43.0
Merge cipher_ber_to_bin into sm2_decrypt_init_iot to eliminate redundant malloc and memcpy, using stack buffers with size checks instead. Remove c3 hash size checks from sm2_encrypt/decrypt since the driver already guards c3 > h_byts; smaller c3 causes hardware task failure. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_sm2.c | 115 +++++++++++++++++++------------------------------ 1 file changed, 44 insertions(+), 71 deletions(-) diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index 1878ed4..cf56a47 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -583,34 +583,53 @@ free_x1: return ret; } -static int cipher_ber_to_bin(const EVP_MD *md, struct sm2_ciphertext *ctext_struct, - struct wd_ecc_point *c1, struct wd_dtb *c2, struct wd_dtb *c3) +static int sm2_decrypt_init_iot(handle_t sess, struct wd_ecc_req *req, + struct sm2_ciphertext *ctext_struct) { - int len, len1, md_size; - - len = BN_num_bytes(ctext_struct->C1x); - len1 = BN_num_bytes(ctext_struct->C1y); - c1->x.data = malloc(len + len1 + ctext_struct->C2->length + - ctext_struct->C3->length); - if (!c1->x.data) - return -ENOMEM; + char buf_x[SM2_KEY_BYTES] = {0}; + char buf_y[SM2_KEY_BYTES] = {0}; + struct wd_ecc_out *ecc_out; + struct wd_ecc_in *ecc_in; + struct wd_ecc_point c1; + struct wd_dtb c2, c3; + int c1x_len, c1y_len; - c1->y.data = c1->x.data + len; - c3->data = c1->y.data + len1; - c2->data = c3->data + ctext_struct->C3->length; - memcpy(c2->data, ctext_struct->C2->data, ctext_struct->C2->length); - memcpy(c3->data, ctext_struct->C3->data, ctext_struct->C3->length); - c2->dsize = ctext_struct->C2->length; - c3->dsize = ctext_struct->C3->length; - md_size = EVP_MD_size(md); - if (c3->dsize != md_size) { - fprintf(stderr, "invalid: c3 dsize(%u) != hash_size(%d)\n", c3->dsize, md_size); - free(c1->x.data); + c1x_len = BN_num_bytes(ctext_struct->C1x); + c1y_len = BN_num_bytes(ctext_struct->C1y); + if (c1x_len > SM2_KEY_BYTES || c1y_len > SM2_KEY_BYTES) { + fprintf(stderr, "invalid: C1 size too large\n"); return -EINVAL; } - c1->x.dsize = BN_bn2bin(ctext_struct->C1x, (void *)c1->x.data); - c1->y.dsize = BN_bn2bin(ctext_struct->C1y, (void *)c1->y.data); + c1.x.dsize = BN_bn2bin(ctext_struct->C1x, (void *)buf_x); + c1.y.dsize = BN_bn2bin(ctext_struct->C1y, (void *)buf_y); + c1.x.bsize = SM2_KEY_BYTES; + c1.y.bsize = SM2_KEY_BYTES; + c1.x.data = buf_x; + c1.y.data = buf_y; + + c2.data = (char *)ctext_struct->C2->data; + c2.dsize = ctext_struct->C2->length; + c2.bsize = ctext_struct->C2->length; + + c3.data = (char *)ctext_struct->C3->data; + c3.dsize = ctext_struct->C3->length; + c3.bsize = ctext_struct->C3->length; + + ecc_out = wd_sm2_new_dec_out(sess, c2.dsize); + if (!ecc_out) { + fprintf(stderr, "failed to new dec out\n"); + return UADK_DO_SOFT; + } + + ecc_in = wd_sm2_new_dec_in(sess, &c1, &c2, &c3); + if (!ecc_in) { + fprintf(stderr, "failed to new dec in\n"); + wd_ecc_del_out(sess, ecc_out); + return UADK_DO_SOFT; + } + + uadk_ecc_fill_req(req, WD_SM2_DECRYPT, ecc_in, ecc_out); return 0; } @@ -982,8 +1001,7 @@ static int sm2_encrypt(EVP_PKEY_CTX *ctx, struct wd_ecc_req req = {0}; struct wd_dtb *c2 = NULL; struct wd_dtb *c3 = NULL; - const EVP_MD *md; - int md_size, ret; + int ret; ret = sm2_encrypt_check(ctx, out, outlen, in, inlen); if (ret) @@ -1016,13 +1034,6 @@ static int sm2_encrypt(EVP_PKEY_CTX *ctx, if (ret) goto uninit_iot; - md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md; - md_size = EVP_MD_size(md); - if (c3->dsize != md_size) { - fprintf(stderr, "invalid: c3 dsize(%u) != hash_size(%d)\n", c3->dsize, md_size); - goto uninit_iot; - } - ret = 1; uninit_iot: @@ -1070,33 +1081,6 @@ static int sm2_decrypt_check(EVP_PKEY_CTX *ctx, return 0; } -static int sm2_decrypt_init_iot(handle_t sess, - struct wd_ecc_req *req, - struct wd_ecc_point *c1, - struct wd_dtb *c2, - struct wd_dtb *c3) -{ - struct wd_ecc_out *ecc_out; - struct wd_ecc_in *ecc_in; - - ecc_out = wd_sm2_new_dec_out(sess, c2->dsize); - if (!ecc_out) { - fprintf(stderr, "failed to new dec out\n"); - return UADK_DO_SOFT; - } - - ecc_in = wd_sm2_new_dec_in(sess, c1, c2, c3); - if (!ecc_in) { - fprintf(stderr, "failed to new dec in\n"); - wd_ecc_del_out(sess, ecc_out); - return UADK_DO_SOFT; - } - - uadk_ecc_fill_req(req, WD_SM2_DECRYPT, ecc_in, ecc_out); - - return 0; -} - static void sm2_decrypt_uninit_iot(handle_t sess, struct wd_ecc_req *req) { wd_ecc_del_in(sess, req->src); @@ -1133,31 +1117,22 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx, struct sm2_ciphertext *ctext_struct; const unsigned char *in_soft = in; struct wd_ecc_req req = {0}; - struct wd_ecc_point c1; - struct wd_dtb c2, c3; - const EVP_MD *md; int ret; ret = sm2_decrypt_check(ctx, out, outlen, in, inlen); if (ret) goto do_soft; - md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md; - ctext_struct = d2i_SM2_Ciphertext(NULL, &in, inlen); if (!ctext_struct) return 0; - ret = cipher_ber_to_bin(md, ctext_struct, &c1, &c2, &c3); + ret = sm2_decrypt_init_iot(smctx->sess, &req, ctext_struct); if (ret) { ret = UADK_DO_SOFT; goto free_ctext; } - ret = sm2_decrypt_init_iot(smctx->sess, &req, &c1, &c2, &c3); - if (ret) - goto free_c1; - ret = update_private_key(ctx); if (ret) { ret = UADK_DO_SOFT; @@ -1179,8 +1154,6 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx, uninit_iot: sm2_decrypt_uninit_iot(smctx->sess, &req); -free_c1: - free(c1.x.data); free_ctext: SM2_Ciphertext_free(ctext_struct); do_soft: -- 2.43.0
Return UADK_P_FAIL instead of UADK_P_SUCCESS from ECDSA and SM2 verify_recover functions. These are stubs with no implementation; returning success for unsupported operations masks caller errors. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_ecdsa.c | 4 ++-- src/uadk_prov_sm2_sign.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/uadk_prov_ecdsa.c b/src/uadk_prov_ecdsa.c index bd7ca73..b0d15b7 100644 --- a/src/uadk_prov_ecdsa.c +++ b/src/uadk_prov_ecdsa.c @@ -1217,12 +1217,12 @@ static const OSSL_PARAM *uadk_signature_ecdsa_settable_ctx_md_params(void *vctx) static int uadk_signature_ecdsa_verify_recover_init(void *vctx, void *vecdsa, const OSSL_PARAM params[]) { - return UADK_P_SUCCESS; + return UADK_P_FAIL; } static int uadk_signature_ecdsa_verify_recover(void *vctx, unsigned char *rout, size_t *routlen, size_t routsize, const unsigned char *sig, size_t siglen) { - return UADK_P_SUCCESS; + return UADK_P_FAIL; } diff --git a/src/uadk_prov_sm2_sign.c b/src/uadk_prov_sm2_sign.c index aa94875..272a223 100644 --- a/src/uadk_prov_sm2_sign.c +++ b/src/uadk_prov_sm2_sign.c @@ -1339,12 +1339,12 @@ free_ctx: static int uadk_signature_sm2_verify_recover_init(void *vpsm2ctx, void *vsm2, const OSSL_PARAM params[]) { - return UADK_P_SUCCESS; + return UADK_P_FAIL; } static int uadk_signature_sm2_verify_recover(void *vpsm2ctx, unsigned char *rout, size_t *routlen, size_t routsize, const unsigned char *sig, size_t siglen) { - return UADK_P_SUCCESS; + return UADK_P_FAIL; } -- 2.43.0
Remove redundant c3 length check from sm2_prov_decrypt_init_iot since the driver set_param_single already guards c3 > h_byts. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_prov_sm2_enc.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/uadk_prov_sm2_enc.c b/src/uadk_prov_sm2_enc.c index 16e85ae..8abeef7 100644 --- a/src/uadk_prov_sm2_enc.c +++ b/src/uadk_prov_sm2_enc.c @@ -600,7 +600,7 @@ static int sm2_prov_get_plaintext(struct wd_ecc_req *req, } static int sm2_prov_decrypt_init_iot(handle_t sess, struct wd_ecc_req *req, - int md_size, const unsigned char *in, size_t inlen) + const unsigned char *in, size_t inlen) { char buf_x[SM2_KEY_BYTES] = {0}; char buf_y[SM2_KEY_BYTES] = {0}; @@ -617,11 +617,6 @@ static int sm2_prov_decrypt_init_iot(handle_t sess, struct wd_ecc_req *req, if (!ctext) return UADK_P_FAIL; - if (ctext->C3->length != md_size) { - UADK_ERR("invalid: c3 dsize(%d) != md_size(%d)\n", ctext->C3->length, md_size); - goto free_ctext; - } - c1x_len = BN_num_bytes(ctext->C1x); c1y_len = BN_num_bytes(ctext->C1y); if (c1x_len > SM2_KEY_BYTES || c1y_len > SM2_KEY_BYTES) { @@ -680,8 +675,6 @@ static int sm2_prov_decrypt_sw(PROV_SM2_ASYM_CTX *ctx, static int sm2_prov_decrypt(PROV_SM2_ASYM_CTX *psm2ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) { - const EVP_MD *md = sm2_prov_digest_md(&psm2ctx->md); - int md_size = EVP_MD_get_size(md); struct wd_ecc_req req = {0}; handle_t sess; int ret; @@ -704,7 +697,7 @@ static int sm2_prov_decrypt(PROV_SM2_ASYM_CTX *psm2ctx, unsigned char *out, goto do_soft; } - ret = sm2_prov_decrypt_init_iot(sess, &req, md_size, in, inlen); + ret = sm2_prov_decrypt_init_iot(sess, &req, in, inlen); if (ret == UADK_P_FAIL) goto free_sess; -- 2.43.0
Remove NULL checks on inputs that are guaranteed non-NULL by the OpenSSL engine/provider framework or by internal call sites: - eckey_check() in uadk_ec.c: the eckey argument is provided by the ECDSA_METHOD / ECDH_METHOD callbacks. - uadk_prov_ecx_keygen/derive: static functions with always-valid args. Also remove redundant = NULL initializations on local variables that are assigned from function return values before any use, including: - BN_CTX_new / BN_CTX_get / BN_new / BN_bin2bn return values - OPENSSL_zalloc / create_ecdsa_sig return values Variables initialized via function output parameters (e.g. ECDSA_SIG_get0, wd_ecxdh_get_out_params, EC_KEY_METHOD_get_*) retain their = NULL initialization. Signed-off-by: Weili Qian <qianweili@huawei.com> --- src/uadk_ec.c | 9 ++------- src/uadk_prov_ecx.c | 10 +--------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/uadk_ec.c b/src/uadk_ec.c index 648cce9..e76a127 100644 --- a/src/uadk_ec.c +++ b/src/uadk_ec.c @@ -259,11 +259,6 @@ static int eckey_check(const EC_KEY *eckey) const EC_POINT *g; int bits; - if (!eckey) { - fprintf(stderr, "eckey is NULL\n"); - return -1; - } - group = EC_KEY_get0_group(eckey); if (!group) { fprintf(stderr, "group is NULL\n"); @@ -483,8 +478,8 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) { struct wd_ecc_req req; - ECDSA_SIG *sig = NULL; struct wd_dtb tdgst; + ECDSA_SIG *sig; handle_t sess; int ret; @@ -1401,7 +1396,7 @@ static int ecdh_set_cofactor_privkey(handle_t sess, const EC_KEY *ecdh) goto end; } - key_bytes = (EC_GROUP_get_degree(group) + 7) / 8; + key_bytes = (EC_GROUP_get_degree(group) + UADK_ECC_PADDING) >> TRANS_BITS_BYTES_SHIFT; ret = uadk_ecc_set_private_key_bn(sess, cofactor_priv, key_bytes); end: diff --git a/src/uadk_prov_ecx.c b/src/uadk_prov_ecx.c index cdd49ff..0b85d75 100644 --- a/src/uadk_prov_ecx.c +++ b/src/uadk_prov_ecx.c @@ -744,9 +744,6 @@ static int uadk_prov_ecx_keygen(PROV_ECX_KEYMGMT_CTX *gctx, ECX_KEY **ecx_key) struct wd_ecc_req req = {0}; int ret; - if (ecx_key == NULL) - return UADK_P_FAIL; - *ecx_key = uadk_prov_ecx_create_prikey(gctx); if (*ecx_key == NULL || !(gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR)) return UADK_P_FAIL; @@ -1126,11 +1123,6 @@ static int uadk_prov_ecx_derive(PROV_ECX_KEYEXCH_CTX *ecxctx, unsigned char *key ECX_KEY *ecx_key; int ret; - if (ecxctx == NULL) { - UADK_ERR("invalid: ctx is NULL\n"); - return UADK_P_FAIL; - } - peer_ecx_key = ecxctx->peerkey; ecx_key = ecxctx->key; if (peer_ecx_key == NULL || ecx_key == NULL) { @@ -1528,7 +1520,7 @@ exe_soft: static void *uadk_keyexch_x25519_newctx(void *provctx) { - PROV_ECX_KEYEXCH_CTX *ecxctx = NULL; + PROV_ECX_KEYEXCH_CTX *ecxctx; ecxctx = OPENSSL_zalloc(sizeof(PROV_ECX_KEYEXCH_CTX)); if (ecxctx == NULL) { -- 2.43.0
participants (1)
-
Weili Qian