Skip to content

Commit 6b89cb7

Browse files
authored
Merge pull request #928 from JacobBarthelmeh/cpp
fixes for c++ error on missing enum cast and warning on MlKemKey init
2 parents 0049c55 + 6013f12 commit 6b89cb7

1 file changed

Lines changed: 41 additions & 20 deletions

File tree

src/internal.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5527,12 +5527,14 @@ static int KeyAgreeEcdhMlKem_client(WOLFSSH* ssh, byte hashId,
55275527
int ret = WS_SUCCESS;
55285528
byte sharedSecretHashSz = 0;
55295529
byte *sharedSecretHash = NULL;
5530-
MlKemKey kem = {0};
5530+
MlKemKey kem;
55315531
word32 length_ciphertext = 0;
55325532
word32 length_sharedsecret = 0;
55335533
word32 length_privatekey = 0;
55345534
int mlKemType = WC_ML_KEM_768;
55355535
byte kexId = ssh->handshake->kexId;
5536+
5537+
WMEMSET(&kem, 0, sizeof(kem));
55365538
#if !defined(WOLFSSH_NO_NISTP256_MLKEM768_SHA256) || \
55375539
!defined(WOLFSSH_NO_NISTP384_MLKEM1024_SHA384)
55385540
ecc_key *key_ptr = NULL;
@@ -5694,18 +5696,26 @@ static int KeyAgreeEcdhMlKem_client(WOLFSSH* ssh, byte hashId,
56945696
/* Replace the concatenated shared secrets with the hash. That
56955697
* will become the new shared secret. */
56965698
if (ret == 0) {
5697-
sharedSecretHashSz = wc_HashGetDigestSize(hashId);
5698-
sharedSecretHash = (byte *)WMALLOC(sharedSecretHashSz,
5699-
ssh->ctx->heap,
5700-
DYNTYPE_PRIVKEY);
5701-
if (sharedSecretHash == NULL) {
5702-
ret = WS_MEMORY_E;
5699+
int digestSz;
5700+
5701+
digestSz = wc_HashGetDigestSize((enum wc_HashType)hashId);
5702+
if (digestSz <= 0) {
5703+
ret = WS_INVALID_ALGO_ID;
5704+
}
5705+
else {
5706+
sharedSecretHashSz = (byte)digestSz;
5707+
sharedSecretHash = (byte *)WMALLOC(sharedSecretHashSz,
5708+
ssh->ctx->heap,
5709+
DYNTYPE_PRIVKEY);
5710+
if (sharedSecretHash == NULL) {
5711+
ret = WS_MEMORY_E;
5712+
}
57035713
}
57045714
}
57055715

57065716
if (ret == 0) {
5707-
ret = wc_Hash(hashId, ssh->k, ssh->kSz, sharedSecretHash,
5708-
sharedSecretHashSz);
5717+
ret = wc_Hash((enum wc_HashType)hashId, ssh->k, ssh->kSz,
5718+
sharedSecretHash, sharedSecretHashSz);
57095719
}
57105720

57115721
if (ret == 0) {
@@ -12144,12 +12154,14 @@ static int KeyAgreeEcdhMlKem_server(WOLFSSH* ssh, byte hashId,
1214412154
int ret = WS_SUCCESS;
1214512155
byte sharedSecretHashSz = 0;
1214612156
byte *sharedSecretHash = NULL;
12147-
MlKemKey kem = {0};
12157+
MlKemKey kem;
1214812158
word32 length_publickey = 0;
1214912159
word32 length_ciphertext = 0;
1215012160
word32 length_sharedsecret = 0;
1215112161
int mlKemType = WC_ML_KEM_768;
1215212162
byte kexId = ssh->handshake->kexId;
12163+
12164+
WMEMSET(&kem, 0, sizeof(kem));
1215312165
#if !defined(WOLFSSH_NO_NISTP256_MLKEM768_SHA256) || \
1215412166
!defined(WOLFSSH_NO_NISTP384_MLKEM1024_SHA384)
1215512167
ecc_key* pubKey = NULL;
@@ -12366,16 +12378,24 @@ static int KeyAgreeEcdhMlKem_server(WOLFSSH* ssh, byte hashId,
1236612378
/* Replace the concatenated shared secrets with the hash. That
1236712379
* will become the new shared secret.*/
1236812380
if (ret == 0) {
12369-
sharedSecretHashSz = wc_HashGetDigestSize(hashId);
12370-
sharedSecretHash = (byte *)WMALLOC(sharedSecretHashSz,
12371-
ssh->ctx->heap, DYNTYPE_PRIVKEY);
12372-
if (sharedSecretHash == NULL) {
12373-
ret = WS_MEMORY_E;
12381+
int digestSz;
12382+
12383+
digestSz = wc_HashGetDigestSize((enum wc_HashType)hashId);
12384+
if (digestSz <= 0) {
12385+
ret = WS_INVALID_ALGO_ID;
12386+
}
12387+
else {
12388+
sharedSecretHashSz = (byte)digestSz;
12389+
sharedSecretHash = (byte *)WMALLOC(sharedSecretHashSz,
12390+
ssh->ctx->heap, DYNTYPE_PRIVKEY);
12391+
if (sharedSecretHash == NULL) {
12392+
ret = WS_MEMORY_E;
12393+
}
1237412394
}
1237512395
}
1237612396
if (ret == 0) {
12377-
ret = wc_Hash(hashId, ssh->k, ssh->kSz, sharedSecretHash,
12378-
sharedSecretHashSz);
12397+
ret = wc_Hash((enum wc_HashType)hashId, ssh->k, ssh->kSz,
12398+
sharedSecretHash, sharedSecretHashSz);
1237912399
}
1238012400
if (ret == 0) {
1238112401
XMEMCPY(ssh->k, sharedSecretHash, sharedSecretHashSz);
@@ -13553,12 +13573,13 @@ int SendKexDhInit(WOLFSSH* ssh)
1355313573
#if !defined(WOLFSSH_NO_NISTP256_MLKEM768_SHA256) || \
1355413574
!defined(WOLFSSH_NO_NISTP384_MLKEM1024_SHA384) || \
1355513575
!defined(WOLFSSH_NO_CURVE25519_MLKEM768_SHA256)
13556-
if (ssh->handshake->useEccMlKem) {
13557-
MlKemKey kem = {0};
13576+
if (ret == WS_SUCCESS && ssh->handshake->useEccMlKem) {
13577+
MlKemKey kem;
1355813578
word32 length_publickey = 0;
1355913579
word32 length_privatekey = 0;
1356013580
int mlKemType = WC_ML_KEM_768;
13561-
ret = 0;
13581+
13582+
WMEMSET(&kem, 0, sizeof(kem));
1356213583

1356313584
#ifndef WOLFSSH_NO_NISTP384_MLKEM1024_SHA384
1356413585
if (ssh->handshake->kexId == ID_NISTP384_MLKEM1024_SHA384) {

0 commit comments

Comments
 (0)