Skip to content

Commit db38351

Browse files
authored
Merge pull request #7470 from kaleb-himes/SRTP-KDF-OPTEST
Srtp kdf optest
2 parents ca47d49 + 76527c3 commit db38351

7 files changed

Lines changed: 68 additions & 11 deletions

File tree

wolfcrypt/benchmark/benchmark.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8158,6 +8158,7 @@ void bench_pbkdf2(void)
81588158
DECLARE_MULTI_VALUE_STATS_VARS()
81598159

81608160
bench_stats_start(&count, &start);
8161+
PRIVATE_KEY_UNLOCK();
81618162
do {
81628163
ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
81638164
salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
@@ -8168,6 +8169,7 @@ void bench_pbkdf2(void)
81688169
|| runs < minimum_runs
81698170
#endif
81708171
);
8172+
PRIVATE_KEY_LOCK();
81718173

81728174
bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
81738175
#ifdef MULTI_VALUE_STATISTICS
@@ -8248,6 +8250,7 @@ void bench_srtpkdf(void)
82488250
DECLARE_MULTI_VALUE_STATS_VARS()
82498251

82508252
bench_stats_start(&count, &start);
8253+
PRIVATE_KEY_UNLOCK();
82518254
do {
82528255
for (i = 0; i < numBlocks; i++) {
82538256
ret = wc_SRTP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
@@ -8261,6 +8264,7 @@ void bench_srtpkdf(void)
82618264
|| runs < minimum_runs
82628265
#endif
82638266
);
8267+
PRIVATE_KEY_LOCK();
82648268
bench_stats_asym_finish("KDF", 128, "SRTP", 0, count, start, ret);
82658269
#ifdef MULTI_VALUE_STATISTICS
82668270
bench_multi_value_stats(max, min, sum, squareSum, runs);
@@ -8269,6 +8273,7 @@ void bench_srtpkdf(void)
82698273
RESET_MULTI_VALUE_STATS_VARS();
82708274

82718275
bench_stats_start(&count, &start);
8276+
PRIVATE_KEY_UNLOCK();
82728277
do {
82738278
for (i = 0; i < numBlocks; i++) {
82748279
ret = wc_SRTP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
@@ -8282,6 +8287,7 @@ void bench_srtpkdf(void)
82828287
|| runs < minimum_runs
82838288
#endif
82848289
);
8290+
PRIVATE_KEY_LOCK();
82858291
bench_stats_asym_finish("KDF", 256, "SRTP", 0, count, start, ret);
82868292
#ifdef MULTI_VALUE_STATISTICS
82878293
bench_multi_value_stats(max, min, sum, squareSum, runs);
@@ -8290,6 +8296,7 @@ void bench_srtpkdf(void)
82908296
RESET_MULTI_VALUE_STATS_VARS();
82918297

82928298
bench_stats_start(&count, &start);
8299+
PRIVATE_KEY_UNLOCK();
82938300
do {
82948301
for (i = 0; i < numBlocks; i++) {
82958302
ret = wc_SRTCP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
@@ -8303,6 +8310,7 @@ void bench_srtpkdf(void)
83038310
|| runs < minimum_runs
83048311
#endif
83058312
);
8313+
PRIVATE_KEY_LOCK();
83068314
bench_stats_asym_finish("KDF", 128, "SRTCP", 0, count, start, ret);
83078315
#ifdef MULTI_VALUE_STATISTICS
83088316
bench_multi_value_stats(max, min, sum, squareSum, runs);
@@ -8311,6 +8319,7 @@ void bench_srtpkdf(void)
83118319
RESET_MULTI_VALUE_STATS_VARS();
83128320

83138321
bench_stats_start(&count, &start);
8322+
PRIVATE_KEY_UNLOCK();
83148323
do {
83158324
for (i = 0; i < numBlocks; i++) {
83168325
ret = wc_SRTCP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
@@ -8324,6 +8333,7 @@ void bench_srtpkdf(void)
83248333
|| runs < minimum_runs
83258334
#endif
83268335
);
8336+
PRIVATE_KEY_LOCK();
83278337
bench_stats_asym_finish("KDF", 256, "SRTCP", 0, count, start, ret);
83288338
#ifdef MULTI_VALUE_STATISTICS
83298339
bench_multi_value_stats(max, min, sum, squareSum, runs);

wolfcrypt/src/aes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6056,6 +6056,8 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
60566056
int ret = 0;
60576057
word32 processed;
60586058

6059+
XMEMSET(scratch, 0, sizeof(scratch));
6060+
60596061
if (aes == NULL || out == NULL || in == NULL) {
60606062
return BAD_FUNC_ARG;
60616063
}

wolfcrypt/src/hmac.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,12 @@ int wolfSSL_GetHmacMaxSize(void)
12751275

12761276
ret = wc_HmacInit(myHmac, heap, devId);
12771277
if (ret == 0) {
1278+
#if FIPS_VERSION3_GE(6,0,0)
1279+
ret = wc_HmacSetKey_ex(myHmac, type, localSalt, saltSz,
1280+
FIPS_ALLOW_SHORT);
1281+
#else
12781282
ret = wc_HmacSetKey(myHmac, type, localSalt, saltSz);
1283+
#endif
12791284
if (ret == 0)
12801285
ret = wc_HmacUpdate(myHmac, inKey, inKeySz);
12811286
if (ret == 0)
@@ -1356,7 +1361,12 @@ int wolfSSL_GetHmacMaxSize(void)
13561361
word32 tmpSz = (n == 1) ? 0 : hashSz;
13571362
word32 left = outSz - outIdx;
13581363

1364+
#if FIPS_VERSION3_GE(6,0,0)
1365+
ret = wc_HmacSetKey_ex(myHmac, type, inKey, inKeySz,
1366+
FIPS_ALLOW_SHORT);
1367+
#else
13591368
ret = wc_HmacSetKey(myHmac, type, inKey, inKeySz);
1369+
#endif
13601370
if (ret != 0)
13611371
break;
13621372
ret = wc_HmacUpdate(myHmac, tmp, tmpSz);

wolfcrypt/src/pwdbased.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
#endif
5353

5454
#if FIPS_VERSION3_GE(6,0,0)
55+
#ifdef DEBUG_WOLFSSL
56+
#include <wolfssl/wolfcrypt/logging.h>
57+
#endif
5558
const unsigned int wolfCrypt_FIPS_pbkdf_ro_sanity[2] =
5659
{ 0x1a2b3c4d, 0x00000010 };
5760
int wolfCrypt_FIPS_PBKDF_sanity(void)
@@ -183,6 +186,7 @@ int wc_PBKDF1_ex(byte* key, int keyLen, byte* iv, int ivLen,
183186
int wc_PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt,
184187
int sLen, int iterations, int kLen, int hashType)
185188
{
189+
186190
return wc_PBKDF1_ex(output, kLen, NULL, 0,
187191
passwd, pLen, salt, sLen, iterations, hashType, NULL);
188192
}
@@ -209,6 +213,24 @@ int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt,
209213
return BAD_FUNC_ARG;
210214
}
211215

216+
#if FIPS_VERSION3_GE(6,0,0)
217+
/* Per SP800-132 section 5 "The kLen value shall be at least 112 bits in
218+
* length", ensure the returned bits for the derived master key are at a
219+
* minimum 14-bytes or 112-bits after stretching and strengthening
220+
* (iterations) */
221+
if (kLen < HMAC_FIPS_MIN_KEY/8)
222+
return BAD_LENGTH_E;
223+
#endif
224+
225+
#if FIPS_VERSION3_GE(6,0,0) && defined(DEBUG_WOLFSSL)
226+
/* SP800-132 section 5.2 recommends an iteration count of 1000 but this is
227+
* not strictly enforceable and is listed in Appendix B Table 1 as a
228+
* non-testable requirement. wolfCrypt will log it when appropriate but
229+
* take no action */
230+
if (iterations < 1000) {
231+
WOLFSSL_MSG("WARNING: Iteration < 1,000, see SP800-132 section 5.2");
232+
}
233+
#endif
212234
if (iterations <= 0)
213235
iterations = 1;
214236

wolfcrypt/src/rsa.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4510,22 +4510,24 @@ static int _CheckProbablePrime(mp_int* p, mp_int* q, mp_int* e, int nlen,
45104510

45114511
if (q != NULL) {
45124512
int valid = 0;
4513-
/* 5.4 - check that |p-q| <= (2^(1/2))(2^((nlen/2)-1)) */
4513+
/* 5.4 (186-4) 5.5 (186-5) -
4514+
* check that |p-q| <= (2^(1/2))(2^((nlen/2)-1)) */
45144515
ret = wc_CompareDiffPQ(p, q, nlen, &valid);
45154516
if ((ret != MP_OKAY) || (!valid)) goto notOkay;
45164517
prime = q;
45174518
}
45184519
else
45194520
prime = p;
45204521

4521-
/* 4.4,5.5 - Check that prime >= (2^(1/2))(2^((nlen/2)-1))
4522+
/* 4.4,5.5 (186-4) 4.4,5.4 (186-5) -
4523+
* Check that prime >= (2^(1/2))(2^((nlen/2)-1))
45224524
* This is a comparison against lowerBound */
45234525
ret = mp_read_unsigned_bin(tmp1, lower_bound, (word32)nlen/16);
45244526
if (ret != MP_OKAY) goto notOkay;
45254527
ret = mp_cmp(prime, tmp1);
45264528
if (ret == MP_LT) goto exit;
45274529

4528-
/* 4.5,5.6 - Check that GCD(p-1, e) == 1 */
4530+
/* 4.5,5.6 (186-4 & 186-5) - Check that GCD(p-1, e) == 1 */
45294531
ret = mp_sub_d(prime, 1, tmp1); /* tmp1 = prime-1 */
45304532
if (ret != MP_OKAY) goto notOkay;
45314533
#ifdef WOLFSSL_CHECK_MEM_ZERO

wolfcrypt/test/test.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,7 +5888,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
58885888
wc_HmacFree(&hmac);
58895889
}
58905890

5891-
#ifndef HAVE_FIPS
5891+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
58925892
if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
58935893
return WC_TEST_RET_ENC_EC(ret);
58945894
#endif
@@ -5996,7 +5996,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
59965996
wc_HmacFree(&hmac);
59975997
}
59985998

5999-
#ifndef HAVE_FIPS
5999+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
60006000
if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
60016001
return WC_TEST_RET_ENC_EC(ret);
60026002
#endif
@@ -6096,7 +6096,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
60966096
wc_HmacFree(&hmac);
60976097
}
60986098

6099-
#ifndef HAVE_FIPS
6099+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
61006100
if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
61016101
return WC_TEST_RET_ENC_EC(ret);
61026102
#endif
@@ -6217,11 +6217,17 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
62176217
wc_HmacFree(&hmac);
62186218
}
62196219

6220-
#ifndef HAVE_FIPS
6220+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
62216221
if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
62226222
return WC_TEST_RET_ENC_EC(ret);
6223+
#if FIPS_VERSION3_GE(6,0,0)
6224+
if ((ret = wc_HmacSizeByType(21)) != HMAC_KAT_FIPS_E)
6225+
#else
62236226
if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
6227+
#endif
6228+
{
62246229
return WC_TEST_RET_ENC_EC(ret);
6230+
}
62256231
#endif
62266232
if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
62276233
return WC_TEST_RET_ENC_EC(ret);
@@ -6330,7 +6336,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
63306336
wc_HmacFree(&hmac);
63316337
}
63326338

6333-
#ifndef HAVE_FIPS
6339+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
63346340
if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
63356341
return WC_TEST_RET_ENC_EC(ret);
63366342
#endif
@@ -6443,7 +6449,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
64436449
wc_HmacFree(&hmac);
64446450
}
64456451

6446-
#ifndef HAVE_FIPS
6452+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
64476453
if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
64486454
return WC_TEST_RET_ENC_EC(ret);
64496455
#endif
@@ -6615,7 +6621,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
66156621
if (i > 0)
66166622
continue;
66176623

6618-
#ifndef HAVE_FIPS
6624+
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
66196625
ret = wc_HmacSizeByType(hashType[j]);
66206626
if (ret != hashSz[j])
66216627
return WC_TEST_RET_ENC_EC(ret);
@@ -26039,7 +26045,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
2603926045
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
2604026046
{
2604126047
char passwd[] = "passwordpassword";
26042-
WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
26048+
WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a,
26049+
0x5d, 0x63, 0xcb, 0x06 };
2604326050
int iterations = 2048;
2604426051
int kLen = 24;
2604526052
byte derived[64];

wolfssl/wolfcrypt/hmac.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
WOLFSSL_LOCAL int wolfCrypt_FIPS_HMAC_sanity(void);
4444
#endif
4545

46+
#if FIPS_VERSION3_GE(6,0,0)
47+
#define FIPS_ALLOW_SHORT 1
48+
#endif
49+
4650
/* avoid redefinition of structs */
4751
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(2,0,0)
4852

0 commit comments

Comments
 (0)