#include <gtest/gtest.h>
#include "internal.h"
#include "../../../ube/snapsafe_detect.h"
#define MAX_MULTIPLE_FROM_RNG (16)
TEST(EntropySourceHw, Aarch64) {
uint8_t buf[MAX_MULTIPLE_FROM_RNG*8] = { 0 } ;
#if !defined(OPENSSL_AARCH64) || defined(OPENSSL_NO_ASM)
ASSERT_FALSE(have_hw_rng_aarch64_for_testing());
ASSERT_FALSE(rndr_multiple8(buf, 0));
ASSERT_FALSE(rndr_multiple8(buf, 8));
#else
if (have_hw_rng_aarch64_for_testing() != 1) {
GTEST_SKIP() << "Compiled for Arm64, but Aarch64 hw rng is not available in run-time";
}
ASSERT_FALSE(rndr_multiple8(buf, 0));
for (size_t i = 8; i < MAX_MULTIPLE_FROM_RNG; i += 8) {
ASSERT_TRUE(rndr_multiple8(buf, i));
}
for (size_t i : {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15}) {
ASSERT_FALSE(rndr_multiple8(buf, i));
}
#endif
}
TEST(EntropySourceHw, x86_64) {
uint8_t buf[MAX_MULTIPLE_FROM_RNG*8] = { 0 } ;
#if !defined(OPENSSL_X86_64) || defined(OPENSSL_NO_ASM)
ASSERT_FALSE(have_hw_rng_x86_64_for_testing());
ASSERT_FALSE(rdrand_multiple8(buf, 0));
ASSERT_FALSE(rdrand_multiple8(buf, 8));
#else
if (have_hw_rng_x86_64_for_testing() != 1) {
GTEST_SKIP() << "Compiled for x86_64, but x86_64 hw rng is not available in run-time";
}
ASSERT_FALSE(rdrand_multiple8(buf, 0));
for (size_t i = 8; i < MAX_MULTIPLE_FROM_RNG; i += 8) {
ASSERT_TRUE(rdrand_multiple8(buf, i));
}
for (size_t i : {1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15}) {
ASSERT_FALSE(rdrand_multiple8(buf, i));
}
#endif
}
TEST(EntropySources, Configuration) {
uint8_t buf[1];
ASSERT_TRUE(RAND_bytes(buf, sizeof(buf)));
#if defined(AWSLC_SNAPSAFE_TESTING) && defined(OPENSSL_LINUX)
EXPECT_EQ(SNAPSAFE_FALLBACK_ENTROPY_SOURCE, get_entropy_source_method_id_FOR_TESTING());
#else
int expected_entropy_source_id = TREE_DRBG_JITTER_ENTROPY_SOURCE;
if (CRYPTO_get_snapsafe_supported()) {
expected_entropy_source_id = SNAPSAFE_FALLBACK_ENTROPY_SOURCE;
}
EXPECT_EQ(expected_entropy_source_id, get_entropy_source_method_id_FOR_TESTING());
#endif
}