Skip to content

Commit 815f672

Browse files
committed
Fix copilot review feedback
- wp_ecx_kmgmt.c — Added bounds guard (outLen < 2 / p->data_size < outLen) before unclamped byte restoration - test_hkdf.c — Added mdSize <= 0 early-return check after EVP_MD_get_size - test_aestag.c — Changed sizeof(aad) / sizeof(pt) to exclude trailing NUL in both GCM and CCM helpers (encrypt + decrypt AAD, and ptLen) - unit.h — Added /* WP_HAVE_X25519 */ comment to #endif - test_rand_seed.c const cast — Skipped; (char*) is the established pattern in this file (4 pre-existing instances)
1 parent a088735 commit 815f672

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/wp_ecx_kmgmt.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,13 @@ static int wp_ecx_get_params_priv_key(wp_Ecx* ecx, OSSL_PARAM params[])
561561
ok = 0;
562562
}
563563
if (ok && ecx->clamped) {
564-
((unsigned char*)p->data)[0 ] = ecx->unclamped[0];
565-
((unsigned char*)p->data)[outLen - 1] = ecx->unclamped[1];
564+
if ((outLen < 2) || (p->data_size < outLen)) {
565+
ok = 0;
566+
}
567+
else {
568+
((unsigned char*)p->data)[0 ] = ecx->unclamped[0];
569+
((unsigned char*)p->data)[outLen - 1] = ecx->unclamped[1];
570+
}
566571
}
567572
}
568573
p->return_size = outLen;

test/test_aestag.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ static int test_aes_gcm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
13221322
unsigned char iv[12];
13231323
unsigned char aad[] = "additional data";
13241324
unsigned char pt[] = "GCM plaintext for tag test";
1325+
int ptLen = (int)(sizeof(pt) - 1);
13251326
unsigned char ct[64];
13261327
unsigned char tag[16];
13271328
unsigned char dec[64];
@@ -1346,11 +1347,10 @@ static int test_aes_gcm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
13461347
}
13471348
if (err == 0) {
13481349
err = EVP_EncryptUpdate(ctx, NULL, &outLen, aad,
1349-
(int)sizeof(aad)) != 1;
1350+
(int)(sizeof(aad) - 1)) != 1;
13501351
}
13511352
if (err == 0) {
1352-
err = EVP_EncryptUpdate(ctx, ct, &outLen, pt,
1353-
(int)sizeof(pt)) != 1;
1353+
err = EVP_EncryptUpdate(ctx, ct, &outLen, pt, ptLen) != 1;
13541354
}
13551355
if (err == 0) {
13561356
err = EVP_EncryptFinal_ex(ctx, ct + outLen, &fLen) != 1;
@@ -1378,7 +1378,7 @@ static int test_aes_gcm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
13781378
}
13791379
if (err == 0) {
13801380
err = EVP_DecryptUpdate(ctx, NULL, &fLen, aad,
1381-
(int)sizeof(aad)) != 1;
1381+
(int)(sizeof(aad) - 1)) != 1;
13821382
}
13831383
if (err == 0) {
13841384
err = EVP_DecryptUpdate(ctx, dec, &fLen, ct, outLen) != 1;
@@ -1481,7 +1481,7 @@ static int test_aes_ccm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
14811481
unsigned char iv[13];
14821482
unsigned char aad[] = "additional data";
14831483
unsigned char pt[] = "CCM plaintext for tag test";
1484-
int ptLen = (int)sizeof(pt);
1484+
int ptLen = (int)(sizeof(pt) - 1);
14851485
unsigned char ct[64];
14861486
unsigned char tag[16];
14871487
unsigned char dec[64];
@@ -1519,7 +1519,7 @@ static int test_aes_ccm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
15191519
}
15201520
if (err == 0) {
15211521
err = EVP_EncryptUpdate(ctx, NULL, &outLen, aad,
1522-
(int)sizeof(aad)) != 1;
1522+
(int)(sizeof(aad) - 1)) != 1;
15231523
}
15241524
if (err == 0) {
15251525
err = EVP_EncryptUpdate(ctx, ct, &outLen, pt, ptLen) != 1;
@@ -1563,7 +1563,7 @@ static int test_aes_ccm_bad_tag_helper(OSSL_LIB_CTX *libCtx,
15631563
}
15641564
if (err == 0) {
15651565
err = EVP_DecryptUpdate(ctx, NULL, &fLen, aad,
1566-
(int)sizeof(aad)) != 1;
1566+
(int)(sizeof(aad) - 1)) != 1;
15671567
}
15681568
if (err == 0) {
15691569
/* CCM DecryptUpdate should fail with bad tag */

test/test_hkdf.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,16 @@ static int test_hkdf_extract_only_bad_len(OSSL_LIB_CTX *libCtx)
552552
PRINT_MSG("HKDF Extract-Only with wrong output length");
553553

554554
mdSize = EVP_MD_get_size(EVP_sha256());
555-
556-
ctx = EVP_PKEY_CTX_new_from_name(libCtx, "HKDF", NULL);
557-
if (ctx == NULL) {
555+
if (mdSize <= 0) {
556+
PRINT_ERR_MSG("EVP_MD_get_size(EVP_sha256()) failed: %d", mdSize);
558557
err = 1;
559558
}
559+
if (err == 0) {
560+
ctx = EVP_PKEY_CTX_new_from_name(libCtx, "HKDF", NULL);
561+
if (ctx == NULL) {
562+
err = 1;
563+
}
564+
}
560565
if (err == 0) {
561566
err = EVP_PKEY_derive_init(ctx) != 1;
562567
}

test/unit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ int test_ecx_misc(void *data);
458458
int test_ecx_null_init(void *data);
459459
#ifdef WP_HAVE_X25519
460460
int test_ecx_x25519_raw_priv_roundtrip(void *data);
461-
#endif
461+
#endif /* WP_HAVE_X25519 */
462462
#endif
463463

464464
int test_pkcs7_x509_sign_verify(void *data);

0 commit comments

Comments
 (0)