Skip to content

Commit 3a813ec

Browse files
committed
crypto: Fix DH private key length type mismatch
Signed-off-by: ruslandoga <ruslandoga+gh@icloud.com>
1 parent 35b6828 commit 3a813ec

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/crypto/c_src/dh.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ERL_NIF_TERM dh_compute_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM arg
4444
ERL_NIF_TERM dh_generate_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
4545
{/* (PrivKey|undefined, DHParams=[P,G], 0, Len|0) */
4646
ErlNifUInt64 len = 0;
47+
uint64_t ossl_len = 0;
4748
int i = 0;
4849
OSSL_PARAM params[8];
4950
EVP_PKEY *pkey = NULL, *pkey_gen = NULL;
@@ -96,7 +97,8 @@ ERL_NIF_TERM dh_generate_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM ar
9697
if (len >= BN_num_bits(p_bn)) {
9798
len = BN_num_bits(p_bn) - 1;
9899
}
99-
params[i++] = OSSL_PARAM_construct_uint64("priv_len", &len);
100+
ossl_len = len;
101+
params[i++] = OSSL_PARAM_construct_uint64("priv_len", &ossl_len);
100102
}
101103

102104
/* End of parameter fetching */

lib/crypto/test/crypto_SUITE.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,11 @@ do_generate_compute({dh, P, G}) ->
21892189
{UserPub, UserPriv} = crypto:generate_key(dh, [P, G]),
21902190
{HostPub, HostPriv} = crypto:generate_key(dh, [P, G]),
21912191
SharedSecret = crypto:compute_key(dh, HostPub, UserPriv, [P, G]),
2192-
SharedSecret = crypto:compute_key(dh, UserPub, HostPriv, [P, G]).
2192+
SharedSecret = crypto:compute_key(dh, UserPub, HostPriv, [P, G]),
2193+
{UserPubWithLen, UserPrivWithLen} = crypto:generate_key(dh, [P, G, 224]),
2194+
{HostPubWithLen, HostPrivWithLen} = crypto:generate_key(dh, [P, G, 224]),
2195+
SharedSecretWithLen = crypto:compute_key(dh, HostPubWithLen, UserPrivWithLen, [P, G]),
2196+
SharedSecretWithLen = crypto:compute_key(dh, UserPubWithLen, HostPrivWithLen, [P, G]).
21932197

21942198
do_compute({ecdh = Type, Pub, Priv, Curve, SharedSecret}) ->
21952199
ct:log("~p ~p", [Type,Curve]),

0 commit comments

Comments
 (0)