@@ -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 }
0 commit comments