Skip to content

Commit 37f5b5a

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

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

lib/crypto/c_src/dh.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ERL_NIF_TERM dh_compute_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM arg
4242
ERL_NIF_TERM dh_generate_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
4343
{/* (PrivKey|undefined, DHParams=[P,G], 0, Len|0) */
4444
ErlNifUInt64 len = 0;
45+
uint64_t ossl_len = 0;
4546
int i = 0;
4647
OSSL_PARAM params[8];
4748
EVP_PKEY *pkey = NULL, *pkey_gen = NULL;
@@ -89,8 +90,10 @@ ERL_NIF_TERM dh_generate_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM ar
8990
ret = EXCP_BADARG_N(env, 3, "Bad value of length element");
9091
goto done;
9192
}
92-
else if (len)
93-
params[i++] = OSSL_PARAM_construct_uint64("priv_len", &len);
93+
else if (len) {
94+
ossl_len = len;
95+
params[i++] = OSSL_PARAM_construct_uint64("priv_len", &ossl_len);
96+
}
9497

9598
/* End of parameter fetching */
9699
params[i++] = OSSL_PARAM_construct_end();

lib/crypto/test/crypto_SUITE.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,11 @@ do_generate_compute({dh, P, G}) ->
18951895
{UserPub, UserPriv} = crypto:generate_key(dh, [P, G]),
18961896
{HostPub, HostPriv} = crypto:generate_key(dh, [P, G]),
18971897
SharedSecret = crypto:compute_key(dh, HostPub, UserPriv, [P, G]),
1898-
SharedSecret = crypto:compute_key(dh, UserPub, HostPriv, [P, G]).
1898+
SharedSecret = crypto:compute_key(dh, UserPub, HostPriv, [P, G]),
1899+
{UserPubWithLen, UserPrivWithLen} = crypto:generate_key(dh, [P, G, 224]),
1900+
{HostPubWithLen, HostPrivWithLen} = crypto:generate_key(dh, [P, G, 224]),
1901+
SharedSecretWithLen = crypto:compute_key(dh, HostPubWithLen, UserPrivWithLen, [P, G]),
1902+
SharedSecretWithLen = crypto:compute_key(dh, UserPubWithLen, HostPrivWithLen, [P, G]).
18991903

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

0 commit comments

Comments
 (0)