Skip to content

Commit

Permalink
capture s2n_supports_custom_rand
Browse files Browse the repository at this point in the history
  • Loading branch information
toidiu committed Nov 9, 2024
1 parent f35ec75 commit 45885d4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/unit/s2n_random_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ static int s2n_random_rand_bytes_after_cleanup_cb(struct random_test_case *test_
static int s2n_random_rand_bytes_before_init(struct random_test_case *test_case)
{
#if S2N_LIBCRYPTO_SUPPORTS_ENGINE
if (!s2n_libcrypto_is_boringssl() && !s2n_libcrypto_is_libressl() && !s2n_libcrypto_is_awslc() && !s2n_libcrypto_is_fips()) {
if (s2n_supports_custom_rand()) {
/* Calling RAND_bytes will set a global random method */
unsigned char rndbytes[16] = { 0 };
EXPECT_EQUAL(RAND_bytes(rndbytes, sizeof(rndbytes)), 1);
Expand Down
9 changes: 7 additions & 2 deletions utils/s2n_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,19 @@ static int s2n_rand_init_cb_impl(void)
return S2N_SUCCESS;
}

bool s2n_supports_custom_rand()
{
return !s2n_libcrypto_is_boringssl() && !s2n_libcrypto_is_libressl() && !s2n_libcrypto_is_awslc() && !s2n_libcrypto_is_fips();
}

S2N_RESULT s2n_rand_init(void)
{
RESULT_ENSURE(s2n_rand_init_cb() >= S2N_SUCCESS, S2N_ERR_CANCELLED);

RESULT_GUARD(s2n_ensure_initialized_drbgs());

#if S2N_LIBCRYPTO_SUPPORTS_ENGINE
if (!s2n_libcrypto_is_boringssl() && !s2n_libcrypto_is_libressl() && !s2n_libcrypto_is_awslc() && !s2n_libcrypto_is_fips()) {
if (s2n_supports_custom_rand()) {
/* Unset any existing random engine */
RESULT_GUARD_OSSL(RAND_set_rand_engine(NULL), S2N_ERR_OPEN_RANDOM);

Expand Down Expand Up @@ -607,7 +612,7 @@ S2N_RESULT s2n_rand_cleanup(void)
RESULT_ENSURE(s2n_rand_cleanup_cb() >= S2N_SUCCESS, S2N_ERR_CANCELLED);

#if S2N_LIBCRYPTO_SUPPORTS_ENGINE
if (!s2n_libcrypto_is_boringssl() && !s2n_libcrypto_is_libressl() && !s2n_libcrypto_is_awslc() && !s2n_libcrypto_is_fips()) {
if (s2n_supports_custom_rand()) {
/* Cleanup our rand ENGINE in libcrypto */
ENGINE *rand_engine = ENGINE_by_id(s2n_rand_engine_id);
if (rand_engine) {
Expand Down
1 change: 1 addition & 0 deletions utils/s2n_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct s2n_rand_device {
dev_t rdev;
};

bool s2n_supports_custom_rand();
S2N_RESULT s2n_rand_init(void);
S2N_RESULT s2n_rand_cleanup(void);
S2N_RESULT s2n_get_seed_entropy(struct s2n_blob *blob);
Expand Down

0 comments on commit 45885d4

Please sign in to comment.