| 1 |
// Copyright (c) 2020, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include <string> |
| 8 |
|
| 9 |
#include <openssl/base.h> |
| 10 |
#include <openssl/aead.h> |
| 11 |
#include <openssl/crypto.h> |
| 12 |
#include <openssl/cipher.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
#include <openssl/service_indicator.h> |
| 15 |
|
| 16 |
#include <gtest/gtest.h> |
| 17 |
#include "test/test_util.h" |
| 18 |
#include "ube/vm_ube_detect.h" |
| 19 |
|
| 20 |
static int AWS_LC_ERROR_return(void) { |
| 21 |
GUARD_PTR(NULL); |
| 22 |
return 1; |
| 23 |
} |
| 24 |
|
| 25 |
static int AWS_LC_SUCCESS_return(void) { |
| 26 |
char non_null_ptr[1]; |
| 27 |
GUARD_PTR(non_null_ptr); |
| 28 |
return 1; |
| 29 |
} |
| 30 |
|
| 31 |
TEST(CryptoTest, SafetyMacro) { |
| 32 |
// It is assumed that |GUARD_PTR| returns 0 for fail/false and 1 for |
| 33 |
// success/true. Change these default values with care because code might not |
| 34 |
// use the related macros |AWS_LC_ERROR| or |AWS_LC_SUCCESS|. |
| 35 |
EXPECT_EQ(AWS_LC_ERROR_return(), 0); |
| 36 |
EXPECT_EQ(AWS_LC_SUCCESS_return(), 1); |
| 37 |
} |
| 38 |
|
| 39 |
// Test that OPENSSL_VERSION_NUMBER and OPENSSL_VERSION_TEXT are consistent. |
| 40 |
// Node.js parses the version out of OPENSSL_VERSION_TEXT instead of using |
| 41 |
// OPENSSL_VERSION_NUMBER. |
| 42 |
TEST(CryptoTest, Version) { |
| 43 |
char expected[512]; |
| 44 |
snprintf(expected, sizeof(expected), "OpenSSL %d.%d.%d ", |
| 45 |
OPENSSL_VERSION_NUMBER >> 28, (OPENSSL_VERSION_NUMBER >> 20) & 0xff, |
| 46 |
(OPENSSL_VERSION_NUMBER >> 12) & 0xff); |
| 47 |
EXPECT_EQ(expected, |
| 48 |
std::string(OPENSSL_VERSION_TEXT).substr(0, strlen(expected))); |
| 49 |
|
| 50 |
std::string full_expected = "OpenSSL 1.1.1 (compatible; AWS-LC "; |
| 51 |
full_expected += AWSLC_VERSION_NUMBER_STRING; |
| 52 |
full_expected += ")"; |
| 53 |
EXPECT_EQ(OPENSSL_VERSION_TEXT, full_expected); |
| 54 |
|
| 55 |
full_expected = AWSLC_VERSION_STRING; |
| 56 |
std::string actual = std::string(OpenSSL_version(OPENSSL_VERSION)); |
| 57 |
EXPECT_EQ(actual, full_expected); |
| 58 |
} |
| 59 |
|
| 60 |
TEST(CryptoTest, Strndup) { |
| 61 |
bssl::UniquePtr<char> str(OPENSSL_strndup(nullptr, 0)); |
| 62 |
EXPECT_TRUE(str); |
| 63 |
EXPECT_STREQ("", str.get()); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(CryptoTest, aws_lc_assert_entropy_cpu_jitter) { |
| 67 |
if (FIPS_mode() == 1 && CRYPTO_get_vm_ube_supported() != 1) { |
| 68 |
ASSERT_EQ(1, FIPS_is_entropy_cpu_jitter()); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
TEST(CryptoTest, OPENSSL_hexstr2buf) { |
| 73 |
const char *test_cases[][2] = {{"a2", "\xa2"}, |
| 74 |
{"a213", "\xa2\x13"}, |
| 75 |
{"ffeedd", "\xff\xee\xdd"}, |
| 76 |
{"10aab1c2", "\x10\xaa\xb1\xc2"}}; |
| 77 |
|
| 78 |
for (auto test_case : test_cases) { |
| 79 |
const char *test_value = test_case[0]; |
| 80 |
const char *expected_answer = test_case[1]; |
| 81 |
size_t actual_answer_len = 0; |
| 82 |
// The longest test case we have is currently 4 bytes long |
| 83 |
size_t expected_answer_len = OPENSSL_strnlen(test_case[1], 5); |
| 84 |
unsigned char *buf = OPENSSL_hexstr2buf(test_value, &actual_answer_len); |
| 85 |
ASSERT_TRUE(buf != nullptr); |
| 86 |
EXPECT_EQ(expected_answer_len, actual_answer_len); |
| 87 |
EXPECT_EQ(0, OPENSSL_memcmp(buf, expected_answer, expected_answer_len)); |
| 88 |
OPENSSL_free(buf); |
| 89 |
} |
| 90 |
|
| 91 |
// Test failure modes |
| 92 |
size_t actual_answer_len = 0; |
| 93 |
EXPECT_FALSE(OPENSSL_hexstr2buf("a", &actual_answer_len)); |
| 94 |
EXPECT_FALSE(OPENSSL_hexstr2buf(NULL, &actual_answer_len)); |
| 95 |
EXPECT_FALSE(OPENSSL_hexstr2buf("ab", nullptr)); |
| 96 |
EXPECT_FALSE(OPENSSL_hexstr2buf("ag", &actual_answer_len)); |
| 97 |
} |
| 98 |
|
| 99 |
#if defined(BORINGSSL_FIPS) |
| 100 |
TEST(CryptoTest, FIPSdownstreamPrecompilationFlag) { |
| 101 |
#if defined(AWSLC_FIPS) |
| 102 |
ASSERT_TRUE(1); |
| 103 |
#else |
| 104 |
ASSERT_TRUE(0); |
| 105 |
#endif |
| 106 |
} |
| 107 |
#endif // defined(BORINGSSL_FIPS) |
| 108 |
|
| 109 |
#if defined(BORINGSSL_FIPS) && !defined(OPENSSL_ASAN) && !defined(OPENSSL_MSAN) |
| 110 |
TEST(Crypto, OnDemandIntegrityTest) { |
| 111 |
BORINGSSL_integrity_test(); |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
OPENSSL_DEPRECATED static void DeprecatedFunction() {} |
| 116 |
|
| 117 |
OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 118 |
TEST(CryptoTest, DeprecatedFunction) { |
| 119 |
// This is deprecated, but should not trigger any warnings. |
| 120 |
DeprecatedFunction(); |
| 121 |
} |
| 122 |
OPENSSL_END_ALLOW_DEPRECATED |