@@ -383,13 +383,8 @@ static void *uadk_keymgmt_dh_dup(const void *keydata_from, int selection)
383383 return get_default_dh_keymgmt ().dup (keydata_from , selection );
384384}
385385
386- static int uadk_DH_set0_key (DH * dh , BIGNUM * pub_key , BIGNUM * priv_key )
386+ static void uadk_DH_set0_key (DH * dh , BIGNUM * pub_key , BIGNUM * priv_key )
387387{
388- if (dh == NULL ) {
389- UADK_ERR ("invalid: dh is NULL\n" );
390- return UADK_P_FAIL ;
391- }
392-
393388 if (pub_key != NULL ) {
394389 BN_clear_free (dh -> pub_key );
395390 dh -> pub_key = pub_key ;
@@ -401,8 +396,6 @@ static int uadk_DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
401396 }
402397
403398 dh -> dirty_cnt ++ ;
404-
405- return UADK_P_SUCCESS ;
406399}
407400
408401static FFC_PARAMS * ossl_dh_get0_params (DH * dh )
@@ -937,20 +930,17 @@ static int uadk_prov_dh_do_crypto(struct uadk_dh_sess *dh_sess)
937930 return UADK_P_FAIL ;
938931}
939932
940- static int uadk_prov_dh_set_pkey (DH * dh , BIGNUM * pubkey , BIGNUM * prikey )
933+ static void uadk_prov_dh_set_pkey (DH * dh , BIGNUM * pubkey , BIGNUM * prikey )
941934{
942935 const BIGNUM * old_priv = uadk_DH_get0_priv_key (dh );
943936 const BIGNUM * old_pub = uadk_DH_get0_pub_key (dh );
944- int ret = UADK_P_SUCCESS ;
945937
946938 if (old_pub != pubkey && old_priv != prikey )
947- ret = uadk_DH_set0_key (dh , pubkey , prikey );
939+ uadk_DH_set0_key (dh , pubkey , prikey );
948940 else if (old_pub != pubkey )
949- ret = uadk_DH_set0_key (dh , pubkey , NULL );
941+ uadk_DH_set0_key (dh , pubkey , NULL );
950942 else if (old_priv != prikey )
951- ret = uadk_DH_set0_key (dh , NULL , prikey );
952-
953- return ret ;
943+ uadk_DH_set0_key (dh , NULL , prikey );
954944}
955945
956946static int uadk_prov_dh_generate_key (DH * dh )
@@ -962,11 +952,6 @@ static int uadk_prov_dh_generate_key(DH *dh)
962952 BIGNUM * pubkey = NULL ;
963953 int ret ;
964954
965- if (dh == NULL ) {
966- UADK_ERR ("invalid: dh is NULL\n" );
967- return UADK_P_FAIL ;
968- }
969-
970955 ret = uadk_prov_dh_init ();
971956 if (ret ) {
972957 UADK_ERR ("failed to init dh\n" );
@@ -1005,14 +990,12 @@ static int uadk_prov_dh_generate_key(DH *dh)
1005990 goto free_req ;
1006991 }
1007992
1008- ret = uadk_prov_dh_set_pkey (dh , pubkey , prikey );
1009- if (ret == UADK_P_FAIL )
1010- UADK_ERR ("failed to set dh pkey\n" );
993+ uadk_prov_dh_set_pkey (dh , pubkey , prikey );
1011994
1012995 uadk_prov_dh_free_genkey_req (dh_sess );
1013996 uadk_prov_dh_free_session (dh_sess );
1014997
1015- return ret ;
998+ return UADK_P_SUCCESS ;
1016999
10171000free_req :
10181001 uadk_prov_dh_free_genkey_req (dh_sess );
@@ -1087,10 +1070,22 @@ static int ossl_dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbit
10871070 return ret ;
10881071}
10891072
1073+ static int dh_down_ref (int * val )
1074+ {
1075+ int i ;
1076+
1077+ i = __atomic_fetch_sub (val , 1 , __ATOMIC_RELAXED ) - 1 ;
1078+ if (i == 0 )
1079+ __atomic_thread_fence (__ATOMIC_ACQUIRE );
1080+
1081+ return i ;
1082+ }
1083+
10901084static DH * ossl_dh_new_ex (OSSL_LIB_CTX * libctx )
10911085{
1092- DH * dh = OPENSSL_zalloc ( sizeof ( * dh )) ;
1086+ DH * dh ;
10931087
1088+ dh = OPENSSL_zalloc (sizeof (* dh ));
10941089 if (dh == NULL ) {
10951090 UADK_ERR ("failed to alloc dh\n" );
10961091 return NULL ;
@@ -1100,21 +1095,63 @@ static DH *ossl_dh_new_ex(OSSL_LIB_CTX *libctx)
11001095 dh -> lock = CRYPTO_THREAD_lock_new ();
11011096 if (dh -> lock == NULL ) {
11021097 UADK_ERR ("failed to new dh thread lock\n" );
1103- OPENSSL_free (dh );
1104- return NULL ;
1098+ goto free_dh ;
11051099 }
11061100
11071101 dh -> libctx = libctx ;
1102+ dh -> meth = DH_get_default_method ();
1103+ dh -> flags = dh -> meth -> flags ;
1104+
1105+ #ifndef FIPS_MODULE
1106+ if (!CRYPTO_new_ex_data (CRYPTO_EX_INDEX_DH , dh , & dh -> ex_data )) {
1107+ UADK_ERR ("failed to new ex_data\n" );
1108+ goto free_lock ;
1109+ }
1110+ #endif /* FIPS_MODULE */
1111+
1112+ ossl_ffc_params_init (& dh -> params );
1113+
1114+ if ((dh -> meth -> init != NULL ) && !dh -> meth -> init (dh )) {
1115+ UADK_ERR ("failed to init dh\n" );
1116+ goto free_ex_data ;
1117+ }
11081118
11091119 return dh ;
1120+
1121+ free_ex_data :
1122+ #ifndef FIPS_MODULE
1123+ CRYPTO_free_ex_data (CRYPTO_EX_INDEX_DH , dh , & dh -> ex_data );
1124+ free_lock :
1125+ #endif /* FIPS_MODULE */
1126+ CRYPTO_THREAD_lock_free (dh -> lock );
1127+ free_dh :
1128+ OPENSSL_free (dh );
1129+ return NULL ;
11101130}
11111131
11121132static void ossl_dh_free_ex (DH * dh )
11131133{
1114- if (dh ) {
1115- CRYPTO_THREAD_lock_free (dh -> lock );
1116- OPENSSL_free (dh );
1117- }
1134+ int i ;
1135+
1136+ if (dh == NULL )
1137+ return ;
1138+
1139+ i = dh_down_ref (& dh -> references );
1140+ if (i > 0 )
1141+ return ;
1142+
1143+ if (dh -> meth != NULL && dh -> meth -> finish != NULL )
1144+ dh -> meth -> finish (dh );
1145+ #ifndef FIPS_MODULE
1146+ CRYPTO_free_ex_data (CRYPTO_EX_INDEX_DH , dh , & dh -> ex_data );
1147+ #endif /* FIPS_MODULE */
1148+
1149+ CRYPTO_THREAD_lock_free (dh -> lock );
1150+
1151+ ossl_ffc_params_cleanup (& dh -> params );
1152+ BN_clear_free (dh -> pub_key );
1153+ BN_clear_free (dh -> priv_key );
1154+ OPENSSL_free (dh );
11181155}
11191156
11201157static DH * uadk_prov_dh_gen_params_with_group (PROV_DH_KEYMGMT_CTX * gctx , FFC_PARAMS * * ffc )
@@ -1143,7 +1180,6 @@ static DH *uadk_prov_dh_gen_params_with_group(PROV_DH_KEYMGMT_CTX *gctx, FFC_PAR
11431180 return NULL ;
11441181 }
11451182
1146- dh -> meth = DH_get_default_method ();
11471183 ossl_ffc_named_group_set (& dh -> params , group );
11481184 dh -> params .nid = ossl_ffc_named_group_get_uid (group );
11491185 dh -> dirty_cnt ++ ;
@@ -1280,12 +1316,6 @@ static DH *uadk_prov_dh_gen_params(PROV_DH_KEYMGMT_CTX *gctx, FFC_PARAMS **ffc,
12801316
12811317static void uadk_prov_dh_free_params (DH * dh )
12821318{
1283- FFC_PARAMS * ffc ;
1284-
1285- ffc = ossl_dh_get0_params (dh );
1286- if (ffc )
1287- ossl_ffc_params_cleanup (ffc );
1288-
12891319 /*
12901320 * Release DH object that allocated by uadk_prov_dh_gen_params_ex() or
12911321 * uadk_prov_dh_gen_params_with_group().
@@ -1306,8 +1336,8 @@ static void *uadk_keymgmt_dh_gen(void *genctx, OSSL_CALLBACK *cb, void *cb_param
13061336{
13071337 PROV_DH_KEYMGMT_CTX * gctx = (PROV_DH_KEYMGMT_CTX * )genctx ;
13081338 FFC_PARAMS * ffc = NULL ;
1339+ int ret = UADK_P_FAIL ;
13091340 DH * dh = NULL ;
1310- int ret ;
13111341
13121342 if (gctx == NULL ) {
13131343 UADK_ERR ("invalid: keygen ctx is NULL\n" );
0 commit comments