kex: keep GenerateKey hash error in loop

- gate the multi-block loop on ret so the first wc_HashFinal
  result is not discarded
- add a SHA-256 two-block key expansion known-answer test

Issue: F-7507
pull/1154/head
John Safranek 2026-08-04 15:32:12 -07:00 committed by JacobBarthelmeh
parent 28185e4d90
commit 7c4b1d3549
2 changed files with 65 additions and 1 deletions

View File

@ -2770,7 +2770,8 @@ int GenerateKey(byte hashId, byte keyId,
runningKeySz = digestSz;
ret = wc_HashFinal(&hash, enmhashId, key);
for (curBlock = 1; curBlock < blocks; curBlock++) {
for (curBlock = 1; ret == WS_SUCCESS && curBlock < blocks;
curBlock++) {
ret = wc_HashInit(&hash, enmhashId);
if (ret != WS_SUCCESS) break;
ret = HashUpdate(&hash, enmhashId, kSzFlat, LENGTH_SZ);

View File

@ -5833,6 +5833,62 @@ done:
return result;
}
#ifndef NO_SHA256
/* RFC 4253 sec 7.2 key expansion, SHA-256, keySz = two digests plus a
* remainder, so the multi-block loop runs. Guards the derived key only;
* the loop's error propagation is not covered. */
static int test_GenerateKey_multiBlock(void)
{
static const byte kBuf[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20
};
static const byte hBuf[] = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50
};
static const byte sessionId[] = {
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60
};
static const byte expected[] = {
0xE1, 0x5C, 0x71, 0xCF, 0x66, 0xC2, 0x63, 0x7C,
0xC7, 0xB5, 0x07, 0x41, 0x3E, 0x3A, 0x1A, 0x74,
0x16, 0xC6, 0xE8, 0x63, 0x0F, 0xFC, 0x4B, 0x52,
0xB0, 0x70, 0x6C, 0xC2, 0xE4, 0x5B, 0x1D, 0x22,
0x15, 0xCB, 0xFE, 0x3C, 0xB2, 0x14, 0x5A, 0xF0,
0x02, 0x54, 0xC1, 0x44, 0x6D, 0xCF, 0xED, 0x58,
0x7B, 0xE2, 0x4C, 0x6E, 0xE9, 0x14, 0x01, 0xA0,
0x09, 0x36, 0x16, 0xBE, 0xE8, 0xD5, 0x08, 0xBA,
0x9B, 0x2C, 0xD0, 0x51, 0x7B
};
byte key[sizeof(expected)];
int ret;
WMEMSET(key, 0, sizeof(key));
ret = GenerateKey(WC_HASH_TYPE_SHA256, 'A', key, (word32)sizeof(key),
kBuf, (word32)sizeof(kBuf), hBuf, (word32)sizeof(hBuf),
sessionId, (word32)sizeof(sessionId), 1);
if (ret != WS_SUCCESS) {
printf("GenerateKey_multiBlock: ret=%d\n", ret);
return -7000;
}
if (WMEMCMP(key, expected, sizeof(expected)) != 0) {
printf("GenerateKey_multiBlock: derived key mismatch\n");
return -7001;
}
return 0;
}
#endif /* !NO_SHA256 */
static int test_DoChannelRequest(void)
{
WOLFSSH_CTX* ctx = NULL;
@ -13685,6 +13741,13 @@ int wolfSSH_UnitTest(int argc, char** argv)
(unitResult == 0 ? "SUCCESS" : "FAILED"));
testResult = testResult || unitResult;
#ifndef NO_SHA256
unitResult = test_GenerateKey_multiBlock();
printf("GenerateKey_multiBlock: %s\n",
(unitResult == 0 ? "SUCCESS" : "FAILED"));
testResult = testResult || unitResult;
#endif
unitResult = test_DoChannelRequest();
printf("DoChannelRequest: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
testResult = testResult || unitResult;