Skip to content

Commit aa84d7d

Browse files
committed
fix: openssl-3.0-fips should use separate private rand
1 parent 9d02146 commit aa84d7d

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
/*
17+
* Not all libcryptos support RAND_priv_bytes.
18+
*
19+
* Note: the existence of RAND_priv_bytes() does NOT mean that the libcrypto
20+
* actually supports a separate, private source of randomness. Some libcryptos
21+
* just alias RAND_priv_bytes to RAND_bytes.
22+
*/
23+
24+
#include <openssl/rand.h>
25+
26+
int main()
27+
{
28+
uint8_t bytes[10] = { 0 };
29+
RAND_priv_bytes(bytes, sizeof(bytes));
30+
return 0;
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/unit/s2n_random_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,12 @@ int main(int argc, char **argv)
902902
if (s2n_libcrypto_is_awslc()) {
903903
#if defined(S2N_LIBCRYPTO_SUPPORTS_ENGINE)
904904
FAIL_MSG("Expected ENGINE feature probe to be disabled with AWS-LC");
905+
#endif
906+
}
907+
908+
if (s2n_libcrypto_is_openssl_fips()) {
909+
#if !S2N_LIBCRYPTO_SUPPORTS_PRIVATE_RAND
910+
FAIL_MSG("Expected private rand support from openssl3 fips");
905911
#endif
906912
}
907913
};

utils/s2n_random.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ static S2N_RESULT s2n_ensure_uniqueness(void)
276276
return S2N_RESULT_OK;
277277
}
278278

279+
#if S2N_LIBCRYPTO_SUPPORTS_PRIVATE_RAND
280+
static S2N_RESULT s2n_get_libcrypto_private_random_data(struct s2n_blob *out_blob)
281+
{
282+
RESULT_GUARD_PTR(out_blob);
283+
RESULT_GUARD_OSSL(RAND_priv_bytes(out_blob->data, out_blob->size), S2N_ERR_DRBG);
284+
return S2N_RESULT_OK;
285+
}
286+
#else
287+
#define s2n_get_libcrypto_private_random_data s2n_get_libcrypto_random_data
288+
#endif
289+
279290
static S2N_RESULT s2n_get_libcrypto_random_data(struct s2n_blob *out_blob)
280291
{
281292
RESULT_GUARD_PTR(out_blob);
@@ -308,33 +319,23 @@ static S2N_RESULT s2n_get_custom_random_data(struct s2n_blob *out_blob, struct s
308319
return S2N_RESULT_OK;
309320
}
310321

311-
static S2N_RESULT s2n_get_random_data(struct s2n_blob *blob, struct s2n_drbg *drbg_state)
322+
S2N_RESULT s2n_get_public_random_data(struct s2n_blob *blob)
312323
{
313-
/* By default, s2n-tls uses a custom random implementation to generate random data for the TLS
314-
* handshake. When operating in FIPS mode, the FIPS-validated libcrypto implementation is used
315-
* instead.
316-
*/
317324
if (s2n_is_in_fips_mode()) {
318325
RESULT_GUARD(s2n_get_libcrypto_random_data(blob));
319-
return S2N_RESULT_OK;
326+
} else {
327+
RESULT_GUARD(s2n_get_custom_random_data(blob, &s2n_per_thread_rand_state.public_drbg));
320328
}
321-
322-
RESULT_GUARD(s2n_get_custom_random_data(blob, drbg_state));
323-
324-
return S2N_RESULT_OK;
325-
}
326-
327-
S2N_RESULT s2n_get_public_random_data(struct s2n_blob *blob)
328-
{
329-
RESULT_GUARD(s2n_get_random_data(blob, &s2n_per_thread_rand_state.public_drbg));
330-
331329
return S2N_RESULT_OK;
332330
}
333331

334332
S2N_RESULT s2n_get_private_random_data(struct s2n_blob *blob)
335333
{
336-
RESULT_GUARD(s2n_get_random_data(blob, &s2n_per_thread_rand_state.private_drbg));
337-
334+
if (s2n_is_in_fips_mode()) {
335+
RESULT_GUARD(s2n_get_libcrypto_private_random_data(blob));
336+
} else {
337+
RESULT_GUARD(s2n_get_custom_random_data(blob, &s2n_per_thread_rand_state.private_drbg));
338+
}
338339
return S2N_RESULT_OK;
339340
}
340341

0 commit comments

Comments
 (0)