Skip to content
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

Enable UBE backends and move out of module #2022

Merged
Merged
28 changes: 3 additions & 25 deletions crypto/fipsmodule/rand/new_rand_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "new_rand_internal.h"
#include "../../ube/internal.h"

#include "../../test/ube_test.h"
#include "../../test/test_util.h"

#include <thread>
Expand All @@ -17,36 +18,13 @@
// Remove when promoting to default
#if !defined(BORINGSSL_PREFIX)

class newRandTest : public::testing::Test {
public:
void SetUp() override {
uint64_t current_generation_number = 0;
if (CRYPTO_get_ube_generation_number(&current_generation_number) == 1) {
ube_detection_supported_ = true;
}
}

void TearDown() override {
disable_mocked_ube_detection_FOR_TESTING();
}

protected:
bool UbeIsSupported(void) const {
return ube_detection_supported_;
}

void allowMockedUbe(void) const {
allow_mocked_ube_detection_FOR_TESTING();
}

bool ube_detection_supported_ = false;
};

#define COMPILATION_UNIT_NR_PREFIX
#include "new_rand_prefix.h"

#define MAX_REQUEST_SIZE (CTR_DRBG_MAX_GENERATE_LENGTH * 2 + 1)

class newRandTest : public ubeTest {};

static void randBasicTests(bool *returnFlag) {
// Do not use stack arrays for these. For example, Alpine OS has too low
// default thread stack size limit to accommodate.
Expand Down
203 changes: 53 additions & 150 deletions crypto/ube/ube_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,15 @@

#include <gtest/gtest.h>

#include <openssl/rand.h>

#include "internal.h"
#include "../test/ube_test.h"
#include "../test/test_util.h"

class ubeTest : public::testing::Test {
public:
void SetUp() override {
uint64_t current_generation_number = 0;
if (CRYPTO_get_ube_generation_number(&current_generation_number) == 1) {
ube_detection_supported_ = true;
}
}

void TearDown() override {
disable_mocked_ube_detection_FOR_TESTING();
}
class ubeGenerationNumberTest : public ubeTest {} ;

protected:
bool UbeIsSupported(void) const {
return ube_detection_supported_;
}

void allowMockedUbe(void) const {
allow_mocked_ube_detection_FOR_TESTING();
}

bool ube_detection_supported_ = false;
};

TEST_F(ubeTest, BasicTests) {
TEST_F(ubeGenerationNumberTest, BasicTests) {
uint64_t generation_number = 0;
if (CRYPTO_get_ube_generation_number(&generation_number) == 0) {
// In this case, UBE detection is disabled, so just return
Expand All @@ -53,92 +33,39 @@ TEST_F(ubeTest, BasicTests) {
ASSERT_EQ(current_generation_number, generation_number);
}

TEST_F(ubeTest, MockedMethodTestsSnapsafe) {

allowMockedUbe();
static void MockedDetectionMethodTest(
std::function<void(uint32_t)> set_method_generation_number) {

uint64_t generation_number = 0;
uint64_t cached_generation_number = 0;
uint32_t mocked_generation_number = 0;
torben-hansen marked this conversation as resolved.
Show resolved Hide resolved

// First test incrementing the fork generation number. Pick a starting point
// and get initial UBE generation number
set_fork_generation_number_FOR_TESTING(5);
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock a snapsafe event.
set_snapsafe_generation_number_FOR_TESTING(6);

// Generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number + 1);

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock another snapsafe event with higher increment.
set_snapsafe_generation_number_FOR_TESTING(8);

// Generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number + 1);

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock another snapsafe event but with a strictly smaller value.
set_snapsafe_generation_number_FOR_TESTING(1);

// Generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number + 1);

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);
}

TEST_F(ubeTest, MockedMethodTestsFork) {

allowMockedUbe();

uint64_t generation_number = 0;
uint64_t cached_generation_number = 0;
uint8_t initial_mocked_generation_number[4] = {0};
ASSERT_TRUE(RAND_bytes(initial_mocked_generation_number, 4));
mocked_generation_number =
((uint32_t)initial_mocked_generation_number[0] << 24) |
((uint32_t)initial_mocked_generation_number[1] << 16) |
((uint32_t)initial_mocked_generation_number[2] << 8) |
((uint32_t)initial_mocked_generation_number[3]);
torben-hansen marked this conversation as resolved.
Show resolved Hide resolved

// First test incrementing the fork generation number. Pick a starting point
// and get initial UBE generation number
set_fork_generation_number_FOR_TESTING(10);
// Testing that UBE generation number is incremented when:
// mocked_generation_number + 1
// mocked_generation_number + 3
// mocked_generation_number - 1
torben-hansen marked this conversation as resolved.
Show resolved Hide resolved
// Set our starting point and get initial UBE generation number
set_method_generation_number(mocked_generation_number);
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));

// Should be stable again.
// Should be stable.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock a process fork. We used 10 before. Hence, 11 should work.
set_fork_generation_number_FOR_TESTING(11);
// Mock a UBE.
set_method_generation_number(mocked_generation_number + 1);

// Generation number should have incremented once.
// UBE generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
Expand All @@ -150,8 +77,8 @@ TEST_F(ubeTest, MockedMethodTestsFork) {
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock another process fork with higher increment.
set_fork_generation_number_FOR_TESTING(13);
// Mock another UBE with higher increment.
set_method_generation_number(mocked_generation_number + 3);

// Generation number should have incremented once.
cached_generation_number = generation_number;
Expand All @@ -165,8 +92,8 @@ TEST_F(ubeTest, MockedMethodTestsFork) {
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Mock another fork event but with a strictly smaller value.
set_snapsafe_generation_number_FOR_TESTING(5);
// Mock another UBE but with a strictly smaller value.
set_method_generation_number(mocked_generation_number - 2);

// Generation number should have incremented once.
cached_generation_number = generation_number;
Expand All @@ -181,62 +108,38 @@ TEST_F(ubeTest, MockedMethodTestsFork) {
ASSERT_EQ(generation_number, cached_generation_number);
}

TEST_F(ubeTest, MockedMethodTestsForkAndSnapsafe) {
TEST_F(ubeGenerationNumberTest, MockedDetectionMethodTests) {

allowMockedUbe();

uint64_t generation_number = 0;
uint64_t cached_generation_number = 0;

// Try to increment both fork and snapsafe generation numbers. We expect
// to see one increment in the ube generation number and then stability.
set_fork_generation_number_FOR_TESTING(20);
set_snapsafe_generation_number_FOR_TESTING(20);
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));

// And that it's now stable.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);

// Increment both, this time using higher increments than 1 and different
// increment values.
set_fork_generation_number_FOR_TESTING(24);
set_snapsafe_generation_number_FOR_TESTING(23);

// Generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number + 1);

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);
MockedDetectionMethodTest(
[](uint32_t gn) {
set_fork_generation_number_FOR_TESTING(static_cast<uint64_t>(gn));
}
);

// Try strictly smaller values. These are values previously used as fork and
// snapsafe generation number. It should still result in a UBE generation
// number increment.
set_fork_generation_number_FOR_TESTING(1);
set_snapsafe_generation_number_FOR_TESTING(10);
MockedDetectionMethodTest(
[](uint32_t gn) {
set_snapsafe_generation_number_FOR_TESTING(gn);
}
);

// Generation number should have incremented once.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number + 1);
MockedDetectionMethodTest(
[](uint32_t gn) {
set_fork_generation_number_FOR_TESTING(static_cast<uint64_t>(gn));
set_snapsafe_generation_number_FOR_TESTING(gn);
}
);

// Should be stable again.
cached_generation_number = generation_number;
generation_number = 0;
ASSERT_TRUE(CRYPTO_get_ube_generation_number(&generation_number));
ASSERT_EQ(generation_number, cached_generation_number);
MockedDetectionMethodTest(
[](uint32_t gn) {
set_fork_generation_number_FOR_TESTING(static_cast<uint64_t>(gn));
set_snapsafe_generation_number_FOR_TESTING(gn + 1);
}
);
}

TEST_F(ubeTest, ExpectedSupportTests) {
TEST_F(ubeGenerationNumberTest, ExpectedSupportTests) {
uint64_t generation_number = 0;
// Operating systems where we expect UBE detection to be enabled.
if (osIsAmazonLinux()) {
Expand Down
Loading