Skip to content

Commit e5a89cb

Browse files
Add build-time option to opt-out of CPU Jitter Entropy (#2733)
Adds a build-time option DISABLE_USAGE_OF_CPU_JITTER_ENTROPY to opt-out of CPU Jitter Entropy. That is, with this option CPU Jitter Entropy will never be configured, the tree-DRBG will not be used. Furthermore, CPU Jitter Entropy will not even be included in any compilation units. When using DISABLE_USAGE_OF_CPU_JITTER_ENTROPY we can no longer guarantee two entropy sources because only the Operating System type will be required to be available. rdrand and rndr will be used if supported in any given run-time, but it's not a guarantee.
1 parent a795654 commit e5a89cb

File tree

13 files changed

+117
-33
lines changed

13 files changed

+117
-33
lines changed

CMakeLists.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ option(ENABLE_DATA_INDEPENDENT_TIMING "Enable automatic setting/resetting Data-I
9595
(DIT) flag in cryptographic functions. Currently only applicable to Arm64 (except on Windows)" OFF)
9696
option(ENABLE_PRE_SONAME_BUILD "Build AWS-LC without SONAME configuration for shared library builds" ON)
9797
option(ENABLE_SOURCE_MODIFICATION "Allow the build to update files in the source directory. This is typically done to update versioning." ON)
98+
option(DISABLE_CPU_JITTER_ENTROPY "Disable usage of CPU Jitter Entropy as an entropy source. This option cannot be used with the FIPS build. With this configuration, randomness generation might not use two independent entropy sources." OFF)
99+
98100
include(cmake/go.cmake)
99101

100102
if(NOT ENABLE_PRE_SONAME_BUILD AND BUILD_SHARED_LIBS AND UNIX AND NOT APPLE)
@@ -110,9 +112,20 @@ message(STATUS "PERFORM_SONAME_BUILD: ${PERFORM_SONAME_BUILD}")
110112

111113
enable_language(C)
112114

113-
# The validated entropy source will always be configured to be CPU Jitter
114-
# Entropy by default; because it's the root of the Tree DRBG.
115-
message(STATUS "Entropy source configured: Dynamic (default: CPU Jitter)")
115+
if(DISABLE_CPU_JITTER_ENTROPY)
116+
if(FIPS)
117+
message(FATAL_ERROR "Cannot opt-out of CPU Jitter for the FIPS build")
118+
endif()
119+
add_definitions(-DDISABLE_CPU_JITTER_ENTROPY)
120+
message(STATUS "Entropy source configuration: CPU Jitter opt-out")
121+
message(STATUS "Entropy source configured: Dynamic (default: Operating system)")
122+
else()
123+
# The validated entropy source will always be configured to be CPU Jitter
124+
# Entropy by default; because it's the root of the Tree DRBG.
125+
message(STATUS "Entropy source configured: Dynamic (default: CPU Jitter)")
126+
endif()
127+
128+
116129

117130
if(${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
118131
# OpenBSD by defaults links with --execute-only this is problematic for two reasons:
@@ -1110,7 +1123,11 @@ if(BUILD_TESTING)
11101123
endmacro()
11111124
endif()
11121125

1113-
add_subdirectory(third_party/jitterentropy)
1126+
# By default, include jitter entropy.
1127+
if(NOT DISABLE_CPU_JITTER_ENTROPY)
1128+
add_subdirectory(third_party/jitterentropy)
1129+
endif()
1130+
11141131
add_subdirectory(crypto)
11151132
if(BUILD_LIBSSL)
11161133
add_subdirectory(ssl)

crypto/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,11 @@ function(build_libcrypto)
618618
message(FATAL_ERROR "NAME, MODULE_SOURCE are required arguments to build_libcrypto")
619619
endif()
620620

621-
add_library(${arg_NAME} $<TARGET_OBJECTS:crypto_objects> ${CRYPTO_FIPS_OBJECTS} ${arg_MODULE_SOURCE} $<TARGET_OBJECTS:jitterentropy>)
621+
add_library(${arg_NAME} $<TARGET_OBJECTS:crypto_objects> ${CRYPTO_FIPS_OBJECTS} ${arg_MODULE_SOURCE})
622+
623+
if(NOT DISABLE_CPU_JITTER_ENTROPY)
624+
target_sources(${arg_NAME} PRIVATE $<TARGET_OBJECTS:jitterentropy>)
625+
endif()
622626

623627
if(FIPS_DELOCATE OR FIPS_SHARED)
624628
add_dependencies(${arg_NAME} bcm_o_target)

crypto/crypto_test.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <gtest/gtest.h>
2828
#include "test/test_util.h"
29+
#include "ube/snapsafe_detect.h"
2930

3031
static int AWS_LC_ERROR_return(void) {
3132
GUARD_PTR(NULL);
@@ -74,7 +75,9 @@ TEST(CryptoTest, Strndup) {
7475
}
7576

7677
TEST(CryptoTest, aws_lc_assert_entropy_cpu_jitter) {
77-
ASSERT_EQ(1, FIPS_is_entropy_cpu_jitter());
78+
if (FIPS_mode() == 1 && CRYPTO_get_snapsafe_supported() != 1) {
79+
ASSERT_EQ(1, FIPS_is_entropy_cpu_jitter());
80+
}
7881
}
7982

8083
TEST(CryptoTest, OPENSSL_hexstr2buf) {

crypto/fipsmodule/rand/cpu_jitter_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

4+
#if !defined(DISABLE_CPU_JITTER_ENTROPY)
5+
46
#include <gtest/gtest.h>
57

68
#include "../../test/test_util.h"
@@ -63,3 +65,5 @@ TEST(CPUJitterEntropyTest, Basic) {
6365
unsigned int jitter_version = 3060300;
6466
EXPECT_EQ(jitter_version, jent_version());
6567
}
68+
69+
#endif // !defined(DISABLE_CPU_JITTER_ENTROPY)

crypto/fipsmodule/rand/entropy/entropy_source_test.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <gtest/gtest.h>
55

6+
#include <openssl/crypto.h>
7+
68
#include "internal.h"
79
#include "../../../ube/snapsafe_detect.h"
810

@@ -75,13 +77,24 @@ TEST(EntropySources, Configuration) {
7577
// Snapsafe detection is only defined for Linux. So, only strongly assert on
7678
// that kernel.
7779
#if defined(AWSLC_SNAPSAFE_TESTING) && defined(OPENSSL_LINUX)
78-
EXPECT_EQ(SNAPSAFE_FALLBACK_ENTROPY_SOURCE, get_entropy_source_method_id_FOR_TESTING());
80+
EXPECT_EQ(OPT_OUT_CPU_JITTER_ENTROPY_SOURCE, get_entropy_source_method_id_FOR_TESTING());
81+
82+
// If entropy build configuration choose to explicitly opt-out of CPU Jitter
83+
// Entropy
84+
#elif defined(DISABLE_CPU_JITTER_ENTROPY)
85+
EXPECT_EQ(OPT_OUT_CPU_JITTER_ENTROPY_SOURCE, get_entropy_source_method_id_FOR_TESTING());
86+
7987
#else
8088
int expected_entropy_source_id = TREE_DRBG_JITTER_ENTROPY_SOURCE;
8189
if (CRYPTO_get_snapsafe_supported()) {
82-
expected_entropy_source_id = SNAPSAFE_FALLBACK_ENTROPY_SOURCE;
90+
expected_entropy_source_id = OPT_OUT_CPU_JITTER_ENTROPY_SOURCE;
8391
}
8492

8593
EXPECT_EQ(expected_entropy_source_id, get_entropy_source_method_id_FOR_TESTING());
94+
95+
// For FIPS build we can strongly assert.
96+
if (FIPS_mode() == 1 && CRYPTO_get_snapsafe_supported() != 1) {
97+
EXPECT_NE(OPT_OUT_CPU_JITTER_ENTROPY_SOURCE, get_entropy_source_method_id_FOR_TESTING());
98+
}
8699
#endif
87100
}

crypto/fipsmodule/rand/entropy/entropy_sources.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static int entropy_get_extra_entropy(
3636
return 1;
3737
}
3838

39+
// Tree-DRBG entropy source configuration.
3940
// - Tree DRBG with Jitter Entropy as root for seeding.
4041
// - OS as personalization string source.
4142
// - If run-time is on an x86_64 or Arm64 CPU and it supports rdrand
@@ -56,52 +57,68 @@ DEFINE_LOCAL_DATA(struct entropy_source_methods, tree_jitter_entropy_source_meth
5657
out->id = TREE_DRBG_JITTER_ENTROPY_SOURCE;
5758
}
5859

59-
static int snapsafe_fallback_initialize(
60+
static int opt_out_cpu_jitter_initialize(
6061
struct entropy_source_t *entropy_source) {
6162
return 1;
6263
}
6364

64-
static void snapsafe_fallback_zeroize_thread(struct entropy_source_t *entropy_source) {}
65+
static void opt_out_cpu_jitter_zeroize_thread(struct entropy_source_t *entropy_source) {}
6566

66-
static void snapsafe_fallback_free_thread(struct entropy_source_t *entropy_source) {}
67+
static void opt_out_cpu_jitter_free_thread(struct entropy_source_t *entropy_source) {}
6768

68-
static int snapsafe_fallback_get_seed_wrap(
69+
static int opt_out_cpu_jitter_get_seed_wrap(
6970
const struct entropy_source_t *entropy_source, uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
7071
return snapsafe_fallback_get_seed(seed);
7172
}
7273

73-
static int use_snapsafe_fallback_entropy(void) {
74+
// Define conditions for not using CPU Jitter
75+
static int is_snapsafe_environment(void) {
7476
return CRYPTO_get_snapsafe_supported();
7577
}
7678

77-
// Snapsafe fallback environment configurations
78-
// CPU source required for rule-of-two.
79+
static int has_explicitly_opted_out_of_cpu_jitter(void) {
80+
#if defined(DISABLE_CPU_JITTER_ENTROPY)
81+
return 1;
82+
#else
83+
return 0;
84+
#endif
85+
}
86+
87+
static int use_opt_out_cpu_jitter_entropy(void) {
88+
if (has_explicitly_opted_out_of_cpu_jitter() == 1 ||
89+
is_snapsafe_environment() == 1) {
90+
return 1;
91+
}
92+
return 0;
93+
}
94+
95+
// Out-out CPU Jitter configurations. CPU source required for rule-of-two.
7996
// - OS as seed source source.
8097
// - Uses rdrand or rndr, if supported, for personalization string. Otherwise
8198
// falls back to OS source.
82-
DEFINE_LOCAL_DATA(struct entropy_source_methods, snapsafe_fallback_entropy_source_methods) {
83-
out->initialize = snapsafe_fallback_initialize;
84-
out->zeroize_thread = snapsafe_fallback_zeroize_thread;
85-
out->free_thread = snapsafe_fallback_free_thread;
86-
out->get_seed = snapsafe_fallback_get_seed_wrap;
99+
DEFINE_LOCAL_DATA(struct entropy_source_methods, opt_out_cpu_jitter_entropy_source_methods) {
100+
out->initialize = opt_out_cpu_jitter_initialize;
101+
out->zeroize_thread = opt_out_cpu_jitter_zeroize_thread;
102+
out->free_thread = opt_out_cpu_jitter_free_thread;
103+
out->get_seed = opt_out_cpu_jitter_get_seed_wrap;
87104
if (have_hw_rng_x86_64() == 1 ||
88105
have_hw_rng_aarch64() == 1) {
89106
out->get_extra_entropy = entropy_get_prediction_resistance;
90107
} else {
91108
// Fall back to seed source because a second source must always be present.
92-
out->get_extra_entropy = snapsafe_fallback_get_seed_wrap;
109+
out->get_extra_entropy = opt_out_cpu_jitter_get_seed_wrap;
93110
}
94111
out->get_prediction_resistance = NULL;
95-
out->id = SNAPSAFE_FALLBACK_ENTROPY_SOURCE;
112+
out->id = OPT_OUT_CPU_JITTER_ENTROPY_SOURCE;
96113
}
97114

98115
static const struct entropy_source_methods * get_entropy_source_methods(void) {
99116
if (*allow_entropy_source_methods_override_bss_get() == 1) {
100117
return *entropy_source_methods_override_bss_get();
101118
}
102119

103-
if (use_snapsafe_fallback_entropy()) {
104-
return snapsafe_fallback_entropy_source_methods();
120+
if (use_opt_out_cpu_jitter_entropy()) {
121+
return opt_out_cpu_jitter_entropy_source_methods();
105122
}
106123

107124
return tree_jitter_entropy_source_methods();

crypto/fipsmodule/rand/entropy/internal.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extern "C" {
1515

1616
#define OVERRIDDEN_ENTROPY_SOURCE 0
1717
#define TREE_DRBG_JITTER_ENTROPY_SOURCE 1
18-
#define SNAPSAFE_FALLBACK_ENTROPY_SOURCE 2
18+
#define OPT_OUT_CPU_JITTER_ENTROPY_SOURCE 2
1919

2020
#define ENTROPY_JITTER_MAX_NUM_TRIES (3)
2121

@@ -55,11 +55,20 @@ OPENSSL_EXPORT void override_entropy_source_method_FOR_TESTING(
5555

5656
OPENSSL_EXPORT int get_entropy_source_method_id_FOR_TESTING(void);
5757

58-
OPENSSL_EXPORT int tree_jitter_initialize(struct entropy_source_t *entropy_source);
59-
OPENSSL_EXPORT void tree_jitter_zeroize_thread_drbg(struct entropy_source_t *entropy_source);
60-
OPENSSL_EXPORT void tree_jitter_free_thread_drbg(struct entropy_source_t *entropy_source);
61-
OPENSSL_EXPORT int tree_jitter_get_seed(
62-
const struct entropy_source_t *entropy_source, uint8_t seed[CTR_DRBG_ENTROPY_LEN]);
58+
#if !defined(DISABLE_CPU_JITTER_ENTROPY)
59+
OPENSSL_EXPORT int tree_jitter_initialize(struct entropy_source_t *entropy_source);
60+
OPENSSL_EXPORT void tree_jitter_zeroize_thread_drbg(struct entropy_source_t *entropy_source);
61+
OPENSSL_EXPORT void tree_jitter_free_thread_drbg(struct entropy_source_t *entropy_source);
62+
OPENSSL_EXPORT int tree_jitter_get_seed(
63+
const struct entropy_source_t *entropy_source, uint8_t seed[CTR_DRBG_ENTROPY_LEN]);
64+
#else // !defined(DISABLE_CPU_JITTER_ENTROPY)
65+
// Define stubs for tree-DRBG functions that implements the entropy source
66+
// interface.
67+
static inline int tree_jitter_initialize(struct entropy_source_t *entropy_source) { return 0; }
68+
static inline void tree_jitter_zeroize_thread_drbg(struct entropy_source_t *entropy_source) { abort(); }
69+
static inline void tree_jitter_free_thread_drbg(struct entropy_source_t *entropy_source) { abort(); }
70+
static inline int tree_jitter_get_seed(const struct entropy_source_t *entropy_source, uint8_t seed[CTR_DRBG_ENTROPY_LEN]) { return 0; }
71+
#endif // !defined(DISABLE_CPU_JITTER_ENTROPY)
6372

6473
// rndr_multiple8 writes |len| number of bytes to |buf| generated using the
6574
// rndr instruction. |len| must be a multiple of 8.

crypto/fipsmodule/rand/entropy/tree_drbg_jitter_entropy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

4+
#if !defined(DISABLE_CPU_JITTER_ENTROPY)
5+
46
#include <openssl/ctrdrbg.h>
57
#include <openssl/mem.h>
68
#include <openssl/type_check.h>
@@ -535,3 +537,5 @@ OPENSSL_EXPORT int set_thread_and_global_tree_drbg_reseed_counter_FOR_TESTING(
535537
CRYPTO_STATIC_MUTEX_unlock_write(global_seed_drbg_lock_bss_get());
536538
return ret;
537539
}
540+
541+
#endif // !defined(DISABLE_CPU_JITTER_ENTROPY)

crypto/fipsmodule/rand/entropy/tree_drbg_jitter_entropy_isolated_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

4+
#if !defined(DISABLE_CPU_JITTER_ENTROPY)
5+
46
#include <gtest/gtest.h>
57

68
#include "internal.h"
@@ -551,3 +553,5 @@ TEST(treeDrbgJitterentropyTest, SkippedALL) {
551553
}
552554

553555
#endif
556+
557+
#endif // !defined(DISABLE_CPU_JITTER_ENTROPY)

crypto/fipsmodule/self_check/fips.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "../../internal.h"
2020
#include "../delocate.h"
2121

22+
#include "../rand/entropy/internal.h"
23+
2224

2325
int FIPS_mode(void) {
2426
#if defined(BORINGSSL_FIPS) && !defined(OPENSSL_ASAN)
@@ -29,6 +31,9 @@ int FIPS_mode(void) {
2931
}
3032

3133
int FIPS_is_entropy_cpu_jitter(void) {
34+
if (OPT_OUT_CPU_JITTER_ENTROPY_SOURCE == get_entropy_source_method_id_FOR_TESTING()) {
35+
return 0;
36+
}
3237
return 1;
3338
}
3439

0 commit comments

Comments
 (0)