Skip to content

Commit 814e034

Browse files
committed
Increase buffer size and add checks in wp_kbkdf_init_hmac
1 parent 4cf2bd7 commit 814e034

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/wp_kbkdf.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ static void wp_c32toa(word32 wc_u32, byte* c) {
370370
}
371371

372372
#ifdef WP_HAVE_HMAC
373-
#define WP_MAX_HASH_BLOCK_SIZE 128
373+
/* Must be large enough for the HMAC block size of every supported hash.
374+
* SHA3-224 has the largest HMAC block size of any currently supported
375+
* digest at 144 bytes. */
376+
#define WP_MAX_HASH_BLOCK_SIZE 144
374377

375378
static int wp_kbkdf_init_hmac(wp_KbkdfCtx* ctx, unsigned char* key,
376379
size_t keyLen)
@@ -383,14 +386,18 @@ static int wp_kbkdf_init_hmac(wp_KbkdfCtx* ctx, unsigned char* key,
383386

384387
WOLFPROV_ENTER(WP_LOG_COMP_KDF, "wp_kbkdf_init_hmac");
385388

386-
if (keyLen < blockSize) {
389+
if (blockSize > sizeof(localKey) || keyLen > sizeof(localKey)) {
390+
ok = 0;
391+
}
392+
393+
if (ok && keyLen < blockSize) {
387394
/* wolfSSL FIPS needs a key that is at least block size in length with
388395
* the unused parts zeroed out.
389396
*/
390397
XMEMSET(localKey + keyLen, 0, blockSize - keyLen);
391398
localKeyLen = blockSize;
392399
}
393-
else {
400+
else if (ok) {
394401
localKeyLen = (word32)keyLen;
395402
}
396403

test/test_kbkdf.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,21 @@ static int test_kbkdf_feedback(void)
242242
err = test_kbkdf_hmac_compare(osslLibCtx, wpLibCtx, key256, sizeof(key256), "SHA384");
243243
}
244244
}
245+
/* SHA3-224 (144 byte HMAC block) and SHA3-256 (136 byte block) both
246+
* exceed the legacy 128-byte localKey buffer in wp_kbkdf_init_hmac;
247+
* exercising them here guards against the stack overflow regressing. */
248+
if (err == 0) {
249+
err = test_kbkdf_hmac_compare(osslLibCtx, wpLibCtx, key128, sizeof(key128), "SHA3-224");
250+
if (err == 0) {
251+
err = test_kbkdf_hmac_compare(osslLibCtx, wpLibCtx, key256, sizeof(key256), "SHA3-224");
252+
}
253+
}
254+
if (err == 0) {
255+
err = test_kbkdf_hmac_compare(osslLibCtx, wpLibCtx, key128, sizeof(key128), "SHA3-256");
256+
if (err == 0) {
257+
err = test_kbkdf_hmac_compare(osslLibCtx, wpLibCtx, key256, sizeof(key256), "SHA3-256");
258+
}
259+
}
245260

246261
return err;
247262
}

0 commit comments

Comments
 (0)