Skip to content

Commit 1744564

Browse files
committed
wolcrypt: NIST_SP_800_56C address reviewer's comments
1 parent 8d41e68 commit 1744564

4 files changed

Lines changed: 49 additions & 53 deletions

File tree

doc/dox_comments/header_files/kdf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ int wc_SRTP_KDF_kdr_to_idx(word32 kdr);
248248
unsigned char output[32];
249249
int ret;
250250
251-
ret = wc_SP80056C_KDF_single(z, sizeof(z), fixedInfo, sizeof(fixedInfo),
251+
ret = wc_KDA_KDF_onestep(z, sizeof(z), fixedInfo, sizeof(fixedInfo),
252252
sizeof(output), WC_HASH_TYPE_SHA256, output, sizeof(output));
253253
if (ret != 0) {
254-
WOLFSSL_MSG("wc_SP80056C_KDF_single failed");
254+
WOLFSSL_MSG("wc_KDA_KDF_onestep failed");
255255
}
256256
\endcode
257257
*/
258-
int wc_SP80056C_KDF_single(const byte* z, word32 zSz,
258+
int wc_KDA_KDF_onestep(const byte* z, word32 zSz,
259259
const byte* fixedInfo, word32 fixedInfoSz, word32 derivedSecretSz,
260260
enum wc_HashType hashType, byte* output, word32 outputSz);
261261

wolfcrypt/src/kdf.c

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,9 +1390,9 @@ int wc_SRTP_KDF_kdr_to_idx(word32 kdr)
13901390
#endif /* WC_SRTP_KDF */
13911391

13921392
#ifdef WC_KDF_NIST_SP_800_56C
1393-
static int wc_SP80056C_KDF_iteration(const byte* z, word32 zSz,
1394-
word32 counter, const byte* fixedInfo, word32 fixedInfoSz,
1395-
enum wc_HashType hashType, byte* output)
1393+
static int wc_KDA_KDF_iteration(const byte* z, word32 zSz, word32 counter,
1394+
const byte* fixedInfo, word32 fixedInfoSz, enum wc_HashType hashType,
1395+
byte* output)
13961396
{
13971397
byte counterBuf[4];
13981398
wc_HashAlg hash;
@@ -1433,17 +1433,14 @@ static int wc_SP80056C_KDF_iteration(const byte* z, word32 zSz,
14331433
* \return BAD_FUNC_ARG if the input parameters are invalid.
14341434
* \return negative error code if the KDF operation fails.
14351435
*/
1436-
int wc_SP80056C_KDF_single(const byte* z, word32 zSz,
1437-
const byte* fixedInfo, word32 fixedInfoSz, word32 derivedSecretSz,
1438-
enum wc_HashType hashType, byte* output, word32 outputSz)
1436+
int wc_KDA_KDF_onestep(const byte* z, word32 zSz, const byte* fixedInfo,
1437+
word32 fixedInfoSz, word32 derivedSecretSz, enum wc_HashType hashType,
1438+
byte* output, word32 outputSz)
14391439
{
14401440
byte hashTempBuf[WC_MAX_DIGEST_SIZE];
1441-
int ret = BAD_FUNC_ARG;
14421441
word32 counter, outIdx;
1443-
word32 inputSz;
1444-
byte* hashOut;
14451442
int hashOutSz;
1446-
word32 reps;
1443+
int ret;
14471444

14481445
if (output == NULL || outputSz < derivedSecretSz)
14491446
return BAD_FUNC_ARG;
@@ -1456,50 +1453,34 @@ int wc_SP80056C_KDF_single(const byte* z, word32 zSz,
14561453
if (hashOutSz == HASH_TYPE_E)
14571454
return BAD_FUNC_ARG;
14581455

1459-
/* According to SP800_56C reps shall not be greater than 2**32-1. This is
1460-
* not possible using word32 integers. The code checks for overflow. */
1461-
reps = derivedSecretSz / hashOutSz;
1462-
if (derivedSecretSz % hashOutSz) {
1463-
if (reps + 1 < reps)
1464-
return BAD_FUNC_ARG;
1465-
reps++;
1466-
}
1467-
14681456
/* According to SP800_56C, table 1, the max input size (max_H_inputBits)
14691457
* depends on the HASH algo. The smaller value in the table is (2**64-1)/8.
1470-
* This is larger than the possible length using word32 integers. The code
1471-
* checks for overflow. */
1472-
inputSz = zSz;
1473-
if (inputSz + 4 < inputSz)
1474-
return BAD_FUNC_ARG;
1475-
inputSz += 4;
1476-
if (inputSz + fixedInfoSz < inputSz)
1477-
return BAD_FUNC_ARG;
1458+
* This is larger than the possible length using word32 integers. */
14781459

1460+
counter = 1;
14791461
outIdx = 0;
1480-
for (counter = 1; counter <= reps; counter++) {
1481-
/* If the user provided a buffer output size bigger than the
1482-
* derivedSecretSz then the copy in hashTempBuf can be avoided.
1483-
* Nevertheless, the code conservatively does the copy anyway as the
1484-
* data is sensitive and the user may forget zeroing outputsz bytes
1485-
* instead of derivedSecretsz bytes. */
1486-
if (outIdx + hashOutSz <= derivedSecretSz) {
1487-
hashOut = output + outIdx;
1488-
}
1489-
else {
1490-
hashOut = hashTempBuf;
1491-
}
1492-
ret = wc_SP80056C_KDF_iteration(z, zSz, counter,
1493-
fixedInfo, fixedInfoSz, hashType, hashOut);
1494-
if (hashOut == hashTempBuf) {
1495-
XMEMCPY(output + outIdx, hashTempBuf, derivedSecretSz - outIdx);
1496-
ForceZero(hashTempBuf, sizeof(hashTempBuf));
1497-
}
1462+
ret = 0;
1463+
1464+
/* According to SP800_56C the number of iterations shall not be greater than
1465+
* 2**32-1. This is not possible using word32 integers.*/
1466+
while (outIdx + hashOutSz <= derivedSecretSz) {
1467+
ret = wc_KDA_KDF_iteration(z, zSz, counter, fixedInfo, fixedInfoSz,
1468+
hashType, output + outIdx);
14981469
if (ret != 0)
14991470
break;
1471+
counter++;
15001472
outIdx += hashOutSz;
15011473
}
15021474

1475+
if (ret == 0 && outIdx < derivedSecretSz) {
1476+
ret = wc_KDA_KDF_iteration(z, zSz, counter, fixedInfo, fixedInfoSz,
1477+
hashType, hashTempBuf);
1478+
if (ret == 0) {
1479+
XMEMCPY(output + outIdx, hashTempBuf, derivedSecretSz - outIdx);
1480+
}
1481+
ForceZero(hashTempBuf, hashOutSz);
1482+
}
1483+
15031484
if (ret != 0) {
15041485
ForceZero(output, derivedSecretSz);
15051486
}

wolfcrypt/test/test.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,11 +1226,26 @@ static WOLFSSL_TEST_SUBROUTINE wc_test_ret_t nist_sp80056c_kdf_test(void)
12261226
"\x1b\x17\xe1\x67\xfc\x43\x7f\x84\x86\x9d\x85\x49\x53\x7b\x33\x38",
12271227
WC_HASH_TYPE_SHA512),
12281228
#endif
1229+
INIT_SP80056C_TEST_VECTOR(
1230+
"\x00\xcd\xea\x89\x62\x1c\xfa\x46\xb1\x32\xf9\xe4\xcf\xe2\x26\x1c"
1231+
"\xde\x2d\x43\x68\xeb\x56\x56\x63\x4c\x7c\xc9\x8c\x7a\x00\xcd\xe5"
1232+
"\x4e\xd1\x86\x6a\x0d\xd3\xe6\x12\x6c\x9d\x2f\x84\x5d\xaf\xf8\x2c"
1233+
"\xeb\x1d\xa0\x8f\x5d\x87\x52\x1b\xb0\xeb\xec\xa7\x79\x11\x16\x9c"
1234+
"\x20\xcc\x01\x38\xa6\x72\xb6\x95\x8b\xd7\x84\xe5\xd7\xfa\x83\x73"
1235+
"\x8a\xc6\x8f\x9b\x34\x23\xb4\x83\xf9\xbf\x53\x9e\x71\x14\x1e\x45"
1236+
"\xdb\xfb\x7a\xfe\xd1\x8b\x11\xc0\x02\x8b\x13\xf1\xf8\x60\xef\x43"
1237+
"\xc4\x80\xf4\xda\xcd\xa2\x08\x10\x59\xd3\x97\x8c\x99\x9d\x5d\x1a"
1238+
"\xde\x34\x54\xe4",
1239+
"\x12\x34\x56\x78\x9a\xbc\xde\xf0\x41\x4c\x49\x43\x45\x31\x32\x33"
1240+
"\x42\x4f\x42\x42\x59\x34\x35\x36",
1241+
"\x2d\x4a",
1242+
WC_HASH_TYPE_SHA512),
1243+
12291244
};
12301245

12311246
for (i = 0; i < sizeof(vctors) / sizeof(vctors[0]); i++) {
12321247
v = &vctors[i];
1233-
ret = wc_SP80056C_KDF_single(v->z, v->zSz, v->fixedInfo, v->fixedInfoSz,
1248+
ret = wc_KDA_KDF_onestep(v->z, v->zSz, v->fixedInfo, v->fixedInfoSz,
12341249
v->derivedKeySz, v->hashType, output,
12351250
/* use derivedKeySz to force the function to use a temporary buff
12361251
for the last block */
@@ -1242,17 +1257,17 @@ static WOLFSSL_TEST_SUBROUTINE wc_test_ret_t nist_sp80056c_kdf_test(void)
12421257
}
12431258

12441259
/* negative tests */
1245-
ret = wc_SP80056C_KDF_single(NULL, 0, (byte*)"fixed_info",
1260+
ret = wc_KDA_KDF_onestep(NULL, 0, (byte*)"fixed_info",
12461261
sizeof("fixed_info"), 16, WC_HASH_TYPE_SHA256, output, 16);
12471262
if (ret != BAD_FUNC_ARG)
12481263
return WC_TEST_RET_ENC_NC;
1249-
ret = wc_SP80056C_KDF_single((byte*)"secret", sizeof("secret"), NULL, 1, 16,
1264+
ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 1, 16,
12501265
WC_HASH_TYPE_SHA256, output, 16);
12511266
if (ret != BAD_FUNC_ARG)
12521267
return WC_TEST_RET_ENC_NC;
12531268

12541269
/* allow empty FixedInfo */
1255-
ret = wc_SP80056C_KDF_single((byte*)"secret", sizeof("secret"), NULL, 0, 16,
1270+
ret = wc_KDA_KDF_onestep((byte*)"secret", sizeof("secret"), NULL, 0, 16,
12561271
WC_HASH_TYPE_SHA256, output, 16);
12571272
if (ret != 0)
12581273
return WC_TEST_RET_ENC_EC(ret);

wolfssl/wolfcrypt/kdf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ WOLFSSL_API int wc_SRTP_KDF_kdr_to_idx(word32 kdr);
167167
#endif /* WC_SRTP_KDF */
168168

169169
#ifdef WC_KDF_NIST_SP_800_56C
170-
WOLFSSL_API int wc_SP80056C_KDF_single(const byte* z, word32 zSz,
170+
WOLFSSL_API int wc_KDA_KDF_onestep(const byte* z, word32 zSz,
171171
const byte* fixedInfo, word32 fixedInfoSz, word32 derivedSecretSz,
172172
enum wc_HashType hashType, byte* output, word32 outputSz);
173173
#endif

0 commit comments

Comments
 (0)