-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to define a callback for FIPS test failures before aborting the process #2162
Draft
andrewhop
wants to merge
3
commits into
aws:main
Choose a base branch
from
andrewhop:fips_callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
#if defined(BORINGSSL_FIPS) | ||
#include <gtest/gtest.h> | ||
#include <openssl/crypto.h> | ||
#include <openssl/curve25519.h> | ||
#include <openssl/dh.h> | ||
#include <openssl/ec_key.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/nid.h> | ||
#include <openssl/rsa.h> | ||
|
||
#include "internal.h" | ||
|
||
extern "C" { | ||
OPENSSL_EXPORT void AWS_LC_fips_failure_callback(const char* message); | ||
} | ||
|
||
void AWS_LC_fips_failure_callback(const char* message) { | ||
ASSERT_EQ(1, FIPS_mode()); | ||
|
||
// TODO update the self test to report the actual test that failed | ||
const std::map<std::string, std::string> kat_failure_messages = { | ||
{"HMAC-SHA-256", "Integrity test failed"}, | ||
{"AES-CBC-encrypt", "Self-tests failed"}, | ||
{"AES-CBC-decrypt", "Self-tests failed"}, | ||
{"AES-GCM-encrypt", "Self-tests failed"}, | ||
{"AES-GCM-decrypt", "Self-tests failed"}, | ||
{"DRBG", "Self-tests failed"}, | ||
{"DRBG-reseed", "Self-tests failed"}, | ||
{"SHA-1", "Self-tests failed"}, | ||
{"SHA-256", "Integrity test failed"}, | ||
{"SHA-512", "Self-tests failed"}, | ||
{"TLS-KDF", "Self-tests failed"}, | ||
{"RSA-sign", "RSA self-tests failed"}, | ||
{"RSA-verify", "RSA self-tests failed"}, | ||
{"ECDSA-sign", "ECC self-tests failed"}, | ||
{"ECDSA-verify", "ECC self-tests failed"}, | ||
{"Z-computation", "ECC self-tests failed"}, | ||
{"FFDH", "FFDH self-tests failed"}, | ||
{"RSA_PWCT", "RSA PCT failed"}, | ||
{"ECDSA_PWCT", "EC PCT failed"} | ||
}; | ||
|
||
char* broken_kat = getenv("FIPS_CALLBACK_TEST_EXPECTED_FAILURE"); | ||
if (broken_kat != nullptr) { | ||
auto expected_message = kat_failure_messages.find(broken_kat); | ||
if (expected_message != kat_failure_messages.end()) { | ||
ASSERT_STREQ(expected_message->second.c_str(), message); | ||
} else { | ||
FAIL() << "Failed to find expected message for FIPS_CALLBACK_TEST_POWER_ON_TEST_FAILURE=" << broken_kat; | ||
} | ||
|
||
} else { | ||
FAIL() << "AWS_LC_fips_failure_callback called when no KAT was expected to be broken"; | ||
} | ||
fprintf(stderr, "AWS-LC FIPS callback passed\n"); | ||
} | ||
|
||
TEST(FIPSCallback, PowerOnSelfTests) { | ||
// Some KATs are lazy and run on first use | ||
bssl::UniquePtr<RSA> rsa(RSA_new()); | ||
EXPECT_TRUE(RSA_generate_key_fips(rsa.get(), 2048, nullptr)); | ||
|
||
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); | ||
EXPECT_TRUE(EC_KEY_generate_key_fips(key.get())); | ||
|
||
bssl::UniquePtr<DH> dh(DH_new()); | ||
ASSERT_TRUE(DH_generate_parameters_ex(dh.get(), 64, DH_GENERATOR_5, nullptr)); | ||
EXPECT_TRUE(DH_generate_key(dh.get())); | ||
|
||
bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, nullptr)); | ||
ASSERT_TRUE(ctx); | ||
ASSERT_TRUE(EVP_PKEY_CTX_kem_set_params(ctx.get(), NID_MLKEM512)); | ||
ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); | ||
EVP_PKEY *raw = nullptr; | ||
ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw)); | ||
OPENSSL_free(raw); | ||
|
||
uint8_t public_key[ED25519_PUBLIC_KEY_LEN]; | ||
uint8_t private_key[ED25519_PRIVATE_KEY_LEN]; | ||
ED25519_keypair(public_key, private_key); | ||
|
||
uint8_t message[2]; | ||
uint8_t context[2]; | ||
uint8_t signature[ED25519_SIGNATURE_LEN]; | ||
ED25519ph_sign(signature, message, sizeof(message), private_key, context, sizeof(context)); | ||
|
||
char* broken_kat = getenv("FIPS_CALLBACK_TEST_POWER_ON_TEST_FAILURE"); | ||
if (broken_kat != nullptr) { | ||
// A kat was supposed to be broken and the self tests should have failed by now | ||
FAIL() << "FIPS_CALLBACK_TEST_POWER_ON_TEST_FAILURE=" << broken_kat | ||
<< " and should have failed the self tests and aborted before this" << | ||
"function can run"; | ||
} else { | ||
// break-kat.go has not run and corrupted this test yet, everything should work | ||
ASSERT_TRUE(BORINGSSL_self_test()); | ||
} | ||
} | ||
|
||
TEST(FIPSCallback, RSARuntimeTest) { | ||
// At this point the library has loaded, if a self test was broken | ||
// the process would have aborted already | ||
ASSERT_EQ(1, FIPS_mode()); | ||
ASSERT_EQ(1, BORINGSSL_self_test()); | ||
|
||
char*broken_runtime_test = getenv("BORINGSSL_FIPS_BREAK_TEST"); | ||
bssl::UniquePtr<RSA> rsa(RSA_new()); | ||
// This should either pass or abort | ||
EXPECT_TRUE(RSA_generate_key_fips(rsa.get(), 2048, nullptr)); | ||
if (broken_runtime_test != nullptr && strcmp(broken_runtime_test, "RSA_PWCT" ) == 0) { | ||
FAIL() << "FIPS_CALLBACK_TEST_POWER_ON_TEST_FAILURE=RSA_PWCT and should have" | ||
" failed the self tests and aborted before here"; | ||
} | ||
} | ||
|
||
TEST(FIPSCallback, ECDSARuntimeTest) { | ||
// At this point the library has loaded, if a self test was broken | ||
// the process would have aborted already | ||
ASSERT_EQ(1, FIPS_mode()); | ||
ASSERT_EQ(1, BORINGSSL_self_test()); | ||
|
||
char*broken_runtime_test = getenv("BORINGSSL_FIPS_BREAK_TEST"); | ||
// This should either pass or abort | ||
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); | ||
EXPECT_TRUE(EC_KEY_generate_key_fips(key.get())); | ||
if (broken_runtime_test != nullptr && (strcmp(broken_runtime_test, "ECDSA_PWCT" ) == 0 || | ||
strcmp(broken_runtime_test, "CRNG" ) == 0)) { | ||
FAIL() << "FIPS_CALLBACK_TEST_POWER_ON_TEST_FAILURE=ECDSA_PWCT and should have" | ||
" failed the self tests and aborted before here"; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please correct me if I'm wrong, but this will break our next FIPS validation. If we allow someone outside the boundary to register a callback our lab will not be able to prove that we stop processing events when a PCT or self-tests fails. When we detect a failure we need to abort immediately as defined in ISO 19790 section 7.3.3, b), 7.3.3 c), and 7.5. This needs to be wrapped in a #ifdef so we don't expose it when we are building as a 140-3 level 1 module.