Skip to content

Commit 283a73d

Browse files
authored
Merge pull request #12249 from jackctj117/v7-followups
v7.0.0-user-space: EMS refusal in wc_PRF, hybrid group constants, RAND_load_file single reseed, -209 comment
2 parents 72c06c2 + eb09bd8 commit 283a73d

9 files changed

Lines changed: 173 additions & 37 deletions

File tree

‎src/ssl_crypto.c‎

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4576,7 +4576,10 @@ void wolfSSL_RAND_screen(void)
45764576

45774577
/* Seed the random number generator with the contents of a file.
45784578
*
4579-
* Data read is used to reseed the global DRBG.
4579+
* Data read is stirred into the global DRBG. Where the DRBG has no stir
4580+
* (FIPS v7, whose reseed draws fresh entropy from the module's source), the
4581+
* data is hashed as it is read and the digest is passed as additional input to
4582+
* a single reseed once the file is read.
45804583
*
45814584
* @param [in] fname Name of file to read seed data from.
45824585
* @param [in] len Maximum number of bytes to read. -1 to read up to
@@ -4599,6 +4602,16 @@ int wolfSSL_RAND_load_file(const char* fname, long len)
45994602
#else
46004603
unsigned char* buf;
46014604
#endif
4605+
#ifndef WC_RNG_HAVE_STIR
4606+
#ifndef NO_SHA256
4607+
const enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
4608+
#else
4609+
const enum wc_HashType hashType = WC_HASH_TYPE_SHA512;
4610+
#endif
4611+
int hashInit = 0;
4612+
byte digest[WC_MAX_DIGEST_SIZE];
4613+
WC_DECLARE_VAR(hash, wc_HashAlg, 1, NULL);
4614+
#endif
46024615

46034616
WOLFSSL_ENTER("wolfSSL_RAND_load_file");
46044617

@@ -4627,6 +4640,12 @@ int wolfSSL_RAND_load_file(const char* fname, long len)
46274640
return WOLFSSL_FATAL_ERROR;
46284641
}
46294642
#endif
4643+
#ifndef WC_RNG_HAVE_STIR
4644+
WC_ALLOC_VAR_EX(hash, wc_HashAlg, 1, NULL, DYNAMIC_TYPE_HASHES,
4645+
{ XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4646+
XFCLOSE(f);
4647+
return WOLFSSL_FATAL_ERROR; });
4648+
#endif
46304649
#ifdef WOLFSSL_CHECK_MEM_ZERO
46314650
wc_MemZero_Add("wolfSSL_RAND_load_file buf", buf,
46324651
WOLFSSL_RAND_LOAD_FILE_BUF_SZ);
@@ -4638,18 +4657,26 @@ int wolfSSL_RAND_load_file(const char* fname, long len)
46384657
goto cleanup;
46394658
}
46404659

4660+
#ifndef WC_RNG_HAVE_STIR
4661+
if (wc_HashInit(hash, hashType) != 0) {
4662+
ret = WOLFSSL_FATAL_ERROR;
4663+
goto cleanup;
4664+
}
4665+
hashInit = 1;
4666+
#endif
4667+
46414668
while (readSoFar < maxBytes) {
46424669
size_t toRead = (size_t)((maxBytes - readSoFar) <
46434670
WOLFSSL_RAND_LOAD_FILE_BUF_SZ
46444671
? (maxBytes - readSoFar) : WOLFSSL_RAND_LOAD_FILE_BUF_SZ);
46454672
size_t n = XFREAD(buf, 1, toRead, f);
46464673
if (n == 0)
46474674
break;
4675+
#ifdef WC_RNG_HAVE_STIR
46484676
if (wc_LockMutex(&globalRNGMutex) != 0) {
46494677
ret = WOLFSSL_FATAL_ERROR;
46504678
break;
46514679
}
4652-
#ifdef WC_RNG_HAVE_STIR
46534680
/* File material carries no entropy credit; mix it as an uncredited
46544681
* stir. A standing reseed or recovery obligation (NEEDS_RECOVERY_E,
46554682
* NOT_READY_E -- see wolfSSL_RAND_egd()) is not a failure. */
@@ -4659,24 +4686,52 @@ int wolfSSL_RAND_load_file(const char* fname, long len)
46594686
(stir_ret != WC_NO_ERR_TRACE(NOT_READY_E)) &&
46604687
(stir_ret != WC_NO_ERR_TRACE(NEEDS_RECOVERY_E)))
46614688
{
4662-
wc_UnLockMutex(&globalRNGMutex);
4689+
(void)wc_UnLockMutex(&globalRNGMutex);
46634690
WOLFSSL_MSG("RAND_load_file: DRBG stir failed");
46644691
ret = WOLFSSL_FATAL_ERROR;
46654692
break;
46664693
}
46674694
}
4695+
if (wc_UnLockMutex(&globalRNGMutex) != 0) {
4696+
WOLFSSL_MSG("RAND_load_file: unlock failed");
4697+
ret = WOLFSSL_FATAL_ERROR;
4698+
break;
4699+
}
46684700
#else
4669-
if (wc_RNG_DRBG_Reseed(&globalRNG, buf, (word32)n) != 0) {
4670-
wc_UnLockMutex(&globalRNGMutex);
4671-
WOLFSSL_MSG("RAND_load_file: DRBG reseed failed");
4701+
/* Each reseed here draws fresh entropy, so collect the file and
4702+
* reseed once below. */
4703+
if (wc_HashUpdate(hash, hashType, buf, (word32)n) != 0) {
4704+
WOLFSSL_MSG("RAND_load_file: hash update failed");
46724705
ret = WOLFSSL_FATAL_ERROR;
46734706
break;
46744707
}
46754708
#endif
4676-
wc_UnLockMutex(&globalRNGMutex);
46774709
readSoFar += (long)n;
46784710
}
46794711

4712+
#ifndef WC_RNG_HAVE_STIR
4713+
if ((ret == 0) && (readSoFar > 0)) {
4714+
if (wc_HashFinal(hash, hashType, digest) != 0) {
4715+
WOLFSSL_MSG("RAND_load_file: hash final failed");
4716+
ret = WOLFSSL_FATAL_ERROR;
4717+
}
4718+
else if (wc_LockMutex(&globalRNGMutex) != 0) {
4719+
ret = WOLFSSL_FATAL_ERROR;
4720+
}
4721+
else {
4722+
if (wc_RNG_DRBG_Reseed(&globalRNG, digest,
4723+
(word32)wc_HashGetDigestSize(hashType)) != 0) {
4724+
WOLFSSL_MSG("RAND_load_file: DRBG reseed failed");
4725+
ret = WOLFSSL_FATAL_ERROR;
4726+
}
4727+
if (wc_UnLockMutex(&globalRNGMutex) != 0) {
4728+
WOLFSSL_MSG("RAND_load_file: unlock failed");
4729+
ret = WOLFSSL_FATAL_ERROR;
4730+
}
4731+
}
4732+
}
4733+
#endif
4734+
46804735
cleanup:
46814736
XFCLOSE(f);
46824737
ForceZero(buf, WOLFSSL_RAND_LOAD_FILE_BUF_SZ);
@@ -4685,6 +4740,15 @@ int wolfSSL_RAND_load_file(const char* fname, long len)
46854740
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
46864741
wc_MemZero_Check(buf, WOLFSSL_RAND_LOAD_FILE_BUF_SZ);
46874742
#endif
4743+
#ifndef WC_RNG_HAVE_STIR
4744+
if (hashInit && (wc_HashFree(hash, hashType) != 0)) {
4745+
WOLFSSL_MSG("RAND_load_file: hash free failed");
4746+
ret = WOLFSSL_FATAL_ERROR;
4747+
}
4748+
ForceZero(hash, sizeof(*hash));
4749+
ForceZero(digest, sizeof(digest));
4750+
WC_FREE_VAR_EX(hash, NULL, DYNAMIC_TYPE_HASHES);
4751+
#endif
46884752

46894753
if (ret < 0)
46904754
return WOLFSSL_FATAL_ERROR;

‎src/tls.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@
163163

164164
#ifndef NO_TLS
165165

166+
#ifdef WOLFSSL_HAVE_TLS13_HYBRID_SECRET
167+
/* wolfCrypt's hybrid join takes the TLS code point as its group. A FIPS
168+
* bundle's older kdf.h has neither the join nor these names. */
169+
wc_static_assert((int)WC_SECP256R1MLKEM768 == (int)WOLFSSL_SECP256R1MLKEM768);
170+
wc_static_assert((int)WC_X25519MLKEM768 == (int)WOLFSSL_X25519MLKEM768);
171+
wc_static_assert((int)WC_SECP384R1MLKEM1024 ==
172+
(int)WOLFSSL_SECP384R1MLKEM1024);
173+
#endif
174+
166175
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
167176
static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
168177
#endif

‎tests/api/test_curve25519.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,13 @@ int test_wc_curve25519_shared_secret_ex_kat(void)
560560
WC_NO_ERR_TRACE(FIPS_WRONG_API_E));
561561
#ifdef WOLFSSL_HAVE_TLS13_HYBRID_X25519
562562
hybridSz = sizeof(hybrid);
563-
ExpectIntEQ(wc_Tls13_HybridSharedSecretX25519(4587, 0, &private_key,
563+
ExpectIntEQ(wc_Tls13_HybridSharedSecretX25519(
564+
WC_SECP256R1MLKEM768, 0, &private_key,
564565
&public_key, hybrid, sizeof(hybrid), &hybridSz, kemSs,
565566
sizeof(kemSs)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
566567
hybridSz = sizeof(hybrid);
567-
ExpectIntEQ(wc_Tls13_HybridSharedSecretX25519(4588, 1, &private_key,
568+
ExpectIntEQ(wc_Tls13_HybridSharedSecretX25519(
569+
WC_X25519MLKEM768, 1, &private_key,
568570
&public_key, hybrid, sizeof(hybrid), &hybridSz, kemSs,
569571
sizeof(kemSs)), 0);
570572
ExpectIntEQ(hybridSz, sizeof(hybrid));

‎tests/api/test_kdf.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ int test_wc_Tls13_HybridSharedSecret(void)
11711171
/* SecP256r1MLKEM768: ECDH (32) then ML-KEM (32), exact fit */
11721172
XMEMCPY(ss, ec, 32);
11731173
ssSz = 32;
1174-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1174+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
11751175
64, &ssSz, kem, sizeof(kem)), 0);
11761176
ExpectIntEQ(ssSz, 64);
11771177
XMEMCPY(want, ec, 32);
@@ -1181,7 +1181,7 @@ int test_wc_Tls13_HybridSharedSecret(void)
11811181
/* SecP384r1MLKEM1024: ECDH (48) then ML-KEM (32) */
11821182
XMEMCPY(ss, ec, 48);
11831183
ssSz = 48;
1184-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP384R1MLKEM1024, 0, ss,
1184+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP384R1MLKEM1024, 0, ss,
11851185
sizeof(ss), &ssSz, kem, sizeof(kem)), 0);
11861186
ExpectIntEQ(ssSz, 80);
11871187
XMEMCPY(want, ec, 48);
@@ -1193,10 +1193,10 @@ int test_wc_Tls13_HybridSharedSecret(void)
11931193
ssSz = 32;
11941194
#ifdef WC_X25519_HYBRID_ONLY
11951195
/* Only the join that computes X25519 itself takes X25519MLKEM768. */
1196-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_X25519MLKEM768, 1, ss,
1196+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_X25519MLKEM768, 1, ss,
11971197
64, &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
11981198
#else
1199-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_X25519MLKEM768, 1, ss,
1199+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_X25519MLKEM768, 1, ss,
12001200
64, &ssSz, kem, sizeof(kem)), 0);
12011201
ExpectIntEQ(ssSz, 64);
12021202
XMEMCPY(want, kem, sizeof(kem));
@@ -1206,35 +1206,35 @@ int test_wc_Tls13_HybridSharedSecret(void)
12061206

12071207
/* Bad arguments and a result that does not fit. */
12081208
ssSz = 32;
1209-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, NULL,
1209+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, NULL,
12101210
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1211-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1211+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12121212
sizeof(ss), NULL, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1213-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1213+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12141214
sizeof(ss), &ssSz, NULL, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1215-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1215+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12161216
sizeof(ss), &ssSz, kem, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1217-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1217+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12181218
63, &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BUFFER_E));
12191219
ssSz = 0;
1220-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1220+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12211221
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12221222
#ifndef HAVE_FIPS
12231223
ssSz = 97;
1224-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1224+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12251225
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BUFFER_E));
12261226
#else
12271227
/* FIPS: only the module's groups, each in its own order and sizes. */
12281228
ssSz = 32;
1229-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 1, ss,
1229+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 1, ss,
12301230
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1231-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_X25519MLKEM768, 0, ss,
1231+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_X25519MLKEM768, 0, ss,
12321232
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12331233
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM512, 0, ss,
12341234
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1235-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP384R1MLKEM1024, 0, ss,
1235+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP384R1MLKEM1024, 0, ss,
12361236
sizeof(ss), &ssSz, kem, sizeof(kem)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
1237-
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WOLFSSL_SECP256R1MLKEM768, 0, ss,
1237+
ExpectIntEQ(wc_Tls13_HybridSharedSecret(WC_SECP256R1MLKEM768, 0, ss,
12381238
sizeof(ss), &ssSz, kem, 16), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
12391239
#endif
12401240
#endif

‎tests/api/test_ossl_rand.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ int test_wolfSSL_RAND_load_file(void)
289289
ExpectIntEQ(RAND_load_file("/no/such/file/wolfssl_rand_load_file_test",
290290
32), -1);
291291

292+
/* A regular file read over several chunks: len caps the read, -1 reads to
293+
* the end of the file. */
294+
ExpectIntEQ(RAND_load_file("./certs/ca-cert.der", 600), 600);
295+
ExpectIntGT(RAND_load_file("./certs/ca-cert.der", -1), 600);
296+
292297
#if defined(__linux__) || defined(__FreeBSD__)
293298
/* Reading from the OS entropy source returns the requested byte count. */
294299
ExpectIntEQ(RAND_load_file("/dev/urandom", 32), 32);

‎wolfcrypt/src/kdf.c‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@
6969
#define P_HASH_MAX_SIZE WC_SHA256_DIGEST_SIZE
7070
#endif
7171

72+
/* WC_FIPS_TLS12_EMS_ONLY comes from the module's fips.h once its TLS 1.2 KDF
73+
* CAST runs on an extended master secret; until then the module's wrapper only
74+
* flags a master secret WC_FIPS_NOT_APPROVED. */
75+
#if defined(HAVE_FIPS) && FIPS_VERSION3_GE(7,0,0) && \
76+
defined(WC_FIPS_TLS12_EMS_ONLY)
77+
#define WC_PRF_REFUSE_MASTER_SECRET
78+
#endif
79+
80+
#ifdef WC_PRF_REFUSE_MASTER_SECRET
81+
/* FIPS 140-3 IG D.Q: the TLS 1.2 KDF is approved only for the extended master
82+
* secret (RFC 7627), so a master secret (RFC 5246 sec 8.1) is refused. The
83+
* label is the start of the PRF seed, also where OpenSSL-style callers put it.
84+
*/
85+
static int PrfIsMasterSecret(const byte* seed, word32 seedLen)
86+
{
87+
static const byte ms[] = "master secret";
88+
89+
return (seed != NULL) && (seedLen >= (word32)sizeof(ms) - 1) &&
90+
(XMEMCMP(seed, ms, sizeof(ms) - 1) == 0);
91+
}
92+
#endif
93+
7294
/* Pseudo Random Function for MD5, SHA-1, SHA-256, SHA-384, or SHA-512 */
7395
int wc_PRF(byte* result, word32 resLen, const byte* secret,
7496
word32 secLen, const byte* seed, word32 seedLen,
@@ -90,6 +112,14 @@ int wc_PRF(byte* result, word32 resLen, const byte* secret,
90112
if ((result == NULL && resLen != 0) || (secret == NULL && secLen != 0) ||
91113
(seed == NULL && seedLen != 0))
92114
return BAD_FUNC_ARG;
115+
#ifdef WC_PRF_REFUSE_MASTER_SECRET
116+
/* wc_PRF_TLS() and wc_PRF_TLSv1() pass label || seed here. */
117+
if (PrfIsMasterSecret(seed, seedLen)) {
118+
WOLFSSL_MSG("TLS 1.2 master secret refused: extended master secret "
119+
"required");
120+
return BAD_FUNC_ARG;
121+
}
122+
#endif
93123

94124
switch (hash_type) {
95125
#ifndef NO_MD5
@@ -634,9 +664,9 @@ static int Tls13HybridJoin(word16 group, int kemFirst, byte* ss,
634664
#ifdef HAVE_FIPS
635665
/* TLS code point, ML-KEM first, ECDH size; ML-KEM is 32 (FIPS 203). */
636666
static const word16 hybrid[][3] = {
637-
{ 4587, 0, 32 }, /* SecP256r1MLKEM768 */
638-
{ 4588, 1, 32 }, /* X25519MLKEM768 */
639-
{ 4589, 0, 48 } /* SecP384r1MLKEM1024 */
667+
{ WC_SECP256R1MLKEM768, 0, 32 },
668+
{ WC_X25519MLKEM768, 1, 32 },
669+
{ WC_SECP384R1MLKEM1024, 0, 48 }
640670
};
641671
word32 i;
642672
#endif
@@ -681,7 +711,7 @@ int wc_Tls13_HybridSharedSecret(word16 group, int kemFirst, byte* ss,
681711
{
682712
#ifdef WC_X25519_HYBRID_ONLY
683713
/* X25519MLKEM768 is joined only where the module computes the X25519. */
684-
if (group == 4588)
714+
if (group == WC_X25519MLKEM768)
685715
return BAD_FUNC_ARG;
686716
#endif
687717
return Tls13HybridJoin(group, kemFirst, ss, ssMax, ssSz, kemSs, kemSsSz);
@@ -702,7 +732,7 @@ int wc_Tls13_HybridSharedSecretX25519(word16 group, int kemFirst,
702732
return BAD_FUNC_ARG;
703733
#ifdef HAVE_FIPS
704734
/* X25519MLKEM768 is the only hybrid with X25519 (RFC 10024 sec 4.3). */
705-
if (group != 4588)
735+
if (group != WC_X25519MLKEM768)
706736
return BAD_FUNC_ARG;
707737
#endif
708738

0 commit comments

Comments
 (0)