Skip to content

Commit

Permalink
Register ACCP's SecureRandom by default for all builds
Browse files Browse the repository at this point in the history
+ Register ACCP's SecureRandom by default.
+ This change also updates the AWS-LC's commits are used with FIPS and none-FIPS builds.
  + As part of updating AWS-LC, EC_METHOD_get_field_type, and RAND_poll are removed since they always return constant values.
  • Loading branch information
amirhosv committed Jun 21, 2024
2 parents 0917177 + 959c07e commit a49d084
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 67 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,6 @@ set(COVERAGE_ARGUMENTS
if(FIPS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFIPS_BUILD")
set(TEST_FIPS_PROPERTY "-DFIPS=true")
# ACCP's default behavior in FIPS mode is to not register a SecureRandom implementation.
# However, we explicitly register it here to ensure its coverage under test.
set(REGISTER_RNG_PROPERTY "-Dcom.amazon.corretto.crypto.provider.registerSecureRandom=true")
else()
set(TEST_FIPS_PROPERTY "-DFIPS=false")
endif()
Expand All @@ -635,7 +632,6 @@ set(TEST_RUNNER_ARGUMENTS
${TEST_ADD_OPENS}
${TEST_FIPS_PROPERTY}
${EXTERNAL_LIB_PROPERTY}
${REGISTER_RNG_PROPERTY}
-Dcom.amazon.corretto.crypto.provider.inTestSuite=hunter2
-Dtest.data.dir=${TEST_DATA_DIR}
-Djunit.jupiter.execution.parallel.enabled=true
Expand Down
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,6 @@ The FIPS builds use a different version of AWS-LC along with `FIPS=1` build flag
AWS-LC will have FIPS certification. As a result, ACCP in FIPS mode only uses a version of AWS-LC
that has FIPS certification or it will have in future.

For performance reasons, ACCP does not register a SecureRandom implementation in FIPS mode.
Relevant operations within the FIPS module boundary (e.g. key generation, non-deterministic signing, etc.) will still use AWS-LC's internal DRBG.
Users who require ACCP to provide FIPS-validated pseudo-randomness _outside_ the module boundary via SecureRandom should set `registerSecureRandom=true`.

When changing between FIPS and non-FIPS builds, be sure to do a full `clean` of your build environment.

##### All targets
Expand Down Expand Up @@ -348,13 +344,9 @@ Thus, these should all be set on the JVM command line using `-D`.
Using JCE's impelmentation is generally recommended unless using ACCP as a standalone provider
Callers can choose to register ACCP's implementation at runtime with a call to `AmazonCorrettoCryptoProvider.registerEcParams()`
* `com.amazon.corretto.crypto.provider.registerSecureRandom`
Takes in `true` or `false` (defaults to `false` in FIPS mode, defaults to `true` in non-FIPS).
Takes in `true` or `false` (defaults to `true`).
If `true`, then ACCP will register a SecureRandom implementation (`LibCryptoRng`) backed by AWS-LC
Else, ACCP will not register a SecureRandom implementation, meaning that the JCA will source SecureRandom instances from another registered provider. AWS-LC will still use its internal DRBG for key generation and other operations requiring secure pseudo-randomness.
LibCryptoRng is very fast during steady state operation in all cases. In FIPS mode, however, AWS-LC-FIPS's CPU jitter-based entropy source incurs a ~10ms initialization cost for every new thread.
This means that there is a slight "pause" before ACCP FIPS's SecureRandom can produce pseudo-random bytes in highly threaded environments.
Because, in extreme cases this could present an availability risk, we do not register LibCryptoRng by default in configurations where this initialization cost is incurred (i.e. FIPS mode).
Non-FIPS AWS-LC does not use CPU jitter for its DRBG seed's entropy, and therefore does not incur this initialization cost, therefore we register LibCryptoRng by default when not in FIPS mode.
* `com.amazon.corretto.crypto.provider.nativeContextReleaseStrategy`
Takes in `HYBRID`, `LAZY`, or `EAGER` (defaults ot `HYBRID`). This property only affects
AES-GCM cipher for now. AES-GCM associates a native object of type `EVP_CIPHER_CTX`
Expand Down
2 changes: 1 addition & 1 deletion aws-lc
1 change: 0 additions & 1 deletion benchmarks/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ jmh {
resultFormat.set("JSON")
duplicateClassesStrategy.set(DuplicatesStrategy.WARN)
jvmArgs.add("-DversionStr=${accpVersion}")
jvmArgs.add("-Dcom.amazon.corretto.crypto.provider.registerSecureRandom=true")
if (project.hasProperty("nativeContextReleaseStrategy")) {
jvmArgs.add("-Dcom.amazon.corretto.crypto.provider.nativeContextReleaseStrategy=${nativeContextReleaseStrategy}")
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ group = 'software.amazon.cryptools'
version = '2.4.0'
ext.isFips = Boolean.getBoolean('FIPS')
if (ext.isFips) {
ext.awsLcGitVersionId = 'AWS-LC-FIPS-2.0.2'
ext.awsLcGitVersionId = 'AWS-LC-FIPS-2.0.13'
} else {
ext.awsLcGitVersionId = '32143aae568a64245f9eae54dcbc49043dbf41e4'
ext.awsLcGitVersionId = 'v1.30.1'
}
ext.isLegacyBuild = Boolean.getBoolean('LEGACY_BUILD')

Expand Down
35 changes: 5 additions & 30 deletions csrc/ec_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ JNIEXPORT jint JNICALL Java_com_amazon_corretto_crypto_provider_EcUtils_curveNam
BigNumObj orderBN;

const EC_POINT* generator = NULL;
const EC_METHOD* method = NULL;
int fieldNid = 0;
int m = 0;

// Figure out which type of group this is
method = EC_GROUP_method_of(group);
if (!method) {
throw_openssl("Unable to acquire method");
}
fieldNid = EC_METHOD_get_field_type(method);

if (EC_GROUP_get_cofactor(group, cfBN, NULL) != 1) {
throw_openssl("Unable to get cofactor");
Expand All @@ -118,26 +108,11 @@ JNIEXPORT jint JNICALL Java_com_amazon_corretto_crypto_provider_EcUtils_curveNam
throw_openssl("Unable to get generator");
}

switch (fieldNid) {
case NID_X9_62_prime_field:
if (EC_GROUP_get_curve_GFp(group, pBN, aBN, bBN, NULL) != 1) {
throw_openssl("Unable to get group information");
}
if (EC_POINT_get_affine_coordinates_GFp(group, generator, gxBN, gyBN, NULL) != 1) {
throw_openssl("Unable to get generator coordinates");
}
break;
case NID_X9_62_characteristic_two_field:
if (EC_GROUP_get_curve_GFp(group, pBN, aBN, bBN, NULL) != 1) {
throw_openssl("Unable to get group information");
}
if (EC_POINT_get_affine_coordinates_GFp(group, generator, gxBN, gyBN, NULL) != 1) {
throw_openssl("Unable to get generator coordinates");
}
m = EC_GROUP_get_degree(group);
env->SetIntArrayRegion(mArr, 0, 1, &m);
env.rethrow_java_exception();
break;
if (EC_GROUP_get_curve_GFp(group, pBN, aBN, bBN, NULL) != 1) {
throw_openssl("Unable to get group information");
}
if (EC_POINT_get_affine_coordinates_GFp(group, generator, gxBN, gyBN, NULL) != 1) {
throw_openssl("Unable to get generator coordinates");
}

gxBN.toJavaArray(env, gxArr);
Expand Down
3 changes: 0 additions & 3 deletions csrc/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ void initialize()
CRYPTO_library_init();
ERR_load_crypto_strings();
OpenSSL_add_all_digests();

// seed the PRNG
RAND_poll();
}

}
Expand Down
2 changes: 1 addition & 1 deletion examples/gradle-kt-dsl/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DELAY_BETWEEN_RETRIES=10
for i in $(seq 1 ${NUMBER_OF_RETRIES})
do
echo "Iteration ${i}"
./gradlew lib:test && ./gradlew -Pfips -Dcom.amazon.corretto.crypto.provider.registerSecureRandom=true lib:test
./gradlew lib:test && ./gradlew -Pfips lib:test
result=$?
if [[ $result -eq 0 ]]
then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,8 @@ public AmazonCorrettoCryptoProvider() {
Utils.getBooleanProperty(PROPERTY_CACHE_SELF_TEST_RESULTS, true);
this.shouldRegisterEcParams = Utils.getBooleanProperty(PROPERTY_REGISTER_EC_PARAMS, false);

// AWS-LC-FIPS's DRBG has per-thread initialization latency that can degrade performance in
// highly threaded
// applications. Until this is resolved, we only register an AWS-LC-backed SecureRandom
// implementation
// when we're not operating in FIPS mode.
this.shouldRegisterSecureRandom =
Utils.getBooleanProperty(PROPERTY_REGISTER_SECURE_RANDOM, !isFips());
Utils.getBooleanProperty(PROPERTY_REGISTER_SECURE_RANDOM, true);

this.nativeContextReleaseStrategy = Utils.getNativeContextReleaseStrategyProperty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,9 @@ public static void main(String[] args) throws Exception {
@SuppressWarnings("unused")
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "SunEC");

// Ensure that FIPS mode determines default behavior for registering SecureRandom and "strong"
// random
// Applications should never use getInstanceStrong as it is an anti-pattern.
final SecureRandom strongRng = SecureRandom.getInstanceStrong();
if (NATIVE_PROVIDER.isFips()) {
assertNotEquals(NATIVE_PROVIDER.getName(), new SecureRandom().getProvider().getName());
assertNotEquals(NATIVE_PROVIDER.getName(), strongRng.getProvider().getName());
} else {
assertEquals(NATIVE_PROVIDER.getName(), new SecureRandom().getProvider().getName());
assertEquals(NATIVE_PROVIDER.getName(), strongRng.getProvider().getName());
}
assertEquals(NATIVE_PROVIDER.getName(), new SecureRandom().getProvider().getName());
assertEquals(NATIVE_PROVIDER.getName(), strongRng.getProvider().getName());

// Ensure that we can successfully generate an AES key, regardless of FIPS
// mode or whether ACCP registers a SecureRandom implementation.
Expand Down

0 comments on commit a49d084

Please sign in to comment.