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

Improve cryptography tests. #3979

Merged
merged 32 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5abe57d
Move the crypto unit tests to a dedicated project.
teo-tsirpanis Mar 20, 2023
85966b3
Test the cryptographic random number generator.
teo-tsirpanis Mar 20, 2023
9dfef2b
Add SHA256 tests from the NIST test vectors.
teo-tsirpanis Mar 20, 2023
ae89383
Add MD5 tests from RFC1321 test vectors.
teo-tsirpanis Mar 20, 2023
0ade268
Fix compile errors around mismatched loop counter types.
teo-tsirpanis Mar 20, 2023
7cbd02f
Remove the previous hash tests.
teo-tsirpanis Mar 20, 2023
7083a61
Make sure formatting does not put `bcrypt.h` before `windows.h`.
teo-tsirpanis Mar 20, 2023
453f5c6
Put `Win32CNG::md5` and `sha256` in the header.
teo-tsirpanis Apr 6, 2023
f39f026
Change `get_random_bytes` to not require a buffer and work with raw p…
teo-tsirpanis Apr 6, 2023
e4289cd
Use an alias to choose the platform-specific crypto class in `crypto.…
teo-tsirpanis Apr 6, 2023
8305fb6
Add `Crypto::get_random_bytes`, removing the platform-specific code f…
teo-tsirpanis Apr 6, 2023
488c4a0
Refactor `test_hash` to use template parameters for the hash instead …
teo-tsirpanis Apr 6, 2023
fb741c1
Use a vector of test cases.
teo-tsirpanis Apr 6, 2023
a2837c8
Improve documentation and naming.
teo-tsirpanis Apr 6, 2023
e8dd34d
Fix formatting.
teo-tsirpanis Apr 7, 2023
3c06639
Use spans in `Crypto::get_random`.
teo-tsirpanis Apr 28, 2023
da7e47b
Add documentation for the AES-GCM tests.
teo-tsirpanis Apr 28, 2023
9e0fa00
Fix compile errors.
teo-tsirpanis May 5, 2023
535d352
Fix CI.
teo-tsirpanis May 8, 2023
1c0c9f4
Merge branch 'dev' into crypto-tests
teo-tsirpanis Jul 12, 2024
ca102fb
Use `string_view`.
teo-tsirpanis Jul 12, 2024
2e68386
Refactor the AES-GCM `TestCase` class to use spans and constexpr.
teo-tsirpanis Jul 12, 2024
10ab36d
Update `to_hex` to accept a span.
teo-tsirpanis Jul 12, 2024
bf805d3
Fix infinite loop.
teo-tsirpanis Jul 15, 2024
9073c89
Fix indentation.
teo-tsirpanis Jul 17, 2024
4dd0359
Make `get_random_bytes` private and remove its test.
teo-tsirpanis Jul 17, 2024
100ce2b
Remove `tiledb/sm/crypto/test/main.cc`.
teo-tsirpanis Jul 17, 2024
fdc9aad
Update template definition in the hash test.
teo-tsirpanis Jul 17, 2024
8b08750
Revert changing the size parameter in the hash function template.
teo-tsirpanis Jul 17, 2024
ff4d989
Merge branch 'dev' into crypto-tests
teo-tsirpanis Jul 18, 2024
c67b6f7
Add comment about linking to OpenSSL target.
teo-tsirpanis Jul 22, 2024
fb5c7e6
Merge branch 'dev' into crypto-tests
teo-tsirpanis Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ set(TILEDB_UNIT_TEST_SOURCES
src/unit-compression-dd.cc
src/unit-compression-delta.cc
src/unit-compression-rle.cc
src/unit-crypto.cc
src/unit-ctx.cc
src/unit-current-domain-rest.cc
src/unit-dense-reader.cc
Expand Down
7 changes: 5 additions & 2 deletions tiledb/sm/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ commence(object_library tiledb_crypto)
this_target_link_libraries(bcrypt)
else()
find_package(OpenSSL REQUIRED)
#this_target_link_libraries(OpenSSL::Crypto)
target_link_libraries(tiledb_crypto PRIVATE OpenSSL::Crypto)
# We cannot use this_target_link_libraries, because it links with PUBLIC
# visibility, and we use OpenSSL only as an internal implementation detail.
target_link_libraries(tiledb_crypto PRIVATE OpenSSL::Crypto)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the resolution on this? It hasn't been addressed from last time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is that this_target_link_libraries links with PUBLIC visibility, which is undesirable in this case, because we use OpenSSL as an internal implementation detail. Changing it right now does not break anything, but it will have undesirable effects once the tiledb_crypto object library is incorporated to the main build (and the OpenSSL::Crypto target would be a requirement even for shared libraries).

I am going to remove the commented line, and add a comment on why we can't use this_target_link_libraries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

endif()
# OpenSSL-3 deprecates MD5
if(MSVC)
Expand All @@ -47,3 +48,5 @@ commence(object_library tiledb_crypto)
set_source_files_properties(crypto_openssl.cc PROPERTIES COMPILE_OPTIONS "-Wno-deprecated-declarations")
endif()
conclude(object_library)

add_test_subdirectory()
20 changes: 5 additions & 15 deletions tiledb/sm/crypto/crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@

#ifdef _WIN32
#include "tiledb/sm/crypto/crypto_win32.h"
using PlatformCrypto = tiledb::sm::Win32CNG;
#else
#include "tiledb/sm/crypto/crypto_openssl.h"
using PlatformCrypto = tiledb::sm::OpenSSL;
#endif

using namespace tiledb::common;
Expand Down Expand Up @@ -89,11 +91,7 @@ Status Crypto::decrypt_aes256gcm(
return LOG_STATUS(
Status_EncryptionError("AES-256-GCM error; invalid tag."));

#ifdef _WIN32
return Win32CNG::decrypt_aes256gcm(key, iv, tag, input, output);
#else
return OpenSSL::decrypt_aes256gcm(key, iv, tag, input, output);
#endif
return PlatformCrypto::decrypt_aes256gcm(key, iv, tag, input, output);
}

Status Crypto::md5(ConstBuffer* input, Buffer* output) {
Expand All @@ -107,11 +105,7 @@ Status Crypto::md5(

Status Crypto::md5(
const void* input, uint64_t input_read_size, Buffer* output) {
#ifdef _WIN32
return Win32CNG::md5(input, input_read_size, output);
#else
return OpenSSL::md5(input, input_read_size, output);
#endif
return PlatformCrypto::md5(input, input_read_size, output);
}

Status Crypto::sha256(ConstBuffer* input, Buffer* output) {
Expand All @@ -125,11 +119,7 @@ Status Crypto::sha256(

Status Crypto::sha256(
const void* input, uint64_t input_read_size, Buffer* output) {
#ifdef _WIN32
return Win32CNG::sha256(input, input_read_size, output);
#else
return OpenSSL::sha256(input, input_read_size, output);
#endif
return PlatformCrypto::sha256(input, input_read_size, output);
}

} // namespace sm
Expand Down
29 changes: 14 additions & 15 deletions tiledb/sm/crypto/crypto_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ using namespace tiledb::common;
namespace tiledb {
namespace sm {

Status OpenSSL::get_random_bytes(unsigned num_bytes, Buffer* output) {
if (output->free_space() < num_bytes)
RETURN_NOT_OK(output->realloc(output->alloced_size() + num_bytes));

int rc = RAND_bytes((unsigned char*)output->cur_data(), num_bytes);
if (rc < 1) {
char err_msg[256];
ERR_error_string_n(ERR_get_error(), err_msg, sizeof(err_msg));
return Status_EncryptionError(
"Cannot generate random bytes with OpenSSL: " + std::string(err_msg));
static Status get_random_bytes(span<uint8_t> buffer) {
while (!buffer.empty()) {
int num_bytes =
int(std::min(buffer.size(), size_t(std::numeric_limits<int>::max())));
int rc = RAND_bytes(buffer.data(), num_bytes);
if (rc < 1) {
char err_msg[256];
ERR_error_string_n(ERR_get_error(), err_msg, sizeof(err_msg));
return Status_EncryptionError(
"Cannot generate random bytes with OpenSSL: " + std::string(err_msg));
}
buffer = buffer.subspan(num_bytes);
}
output->advance_size(num_bytes);
output->advance_offset(num_bytes);

return Status::Ok();
}

Expand All @@ -83,11 +82,11 @@ Status OpenSSL::encrypt_aes256gcm(
RETURN_NOT_OK(output->realloc(output->alloced_size() + required_space));

// Generate IV if the given IV buffer is null.
Buffer generated_iv;
std::array<unsigned char, Crypto::AES256GCM_IV_BYTES> generated_iv;
int iv_len;
unsigned char* iv_buf;
if (iv == nullptr || iv->data() == nullptr) {
RETURN_NOT_OK(get_random_bytes(Crypto::AES256GCM_IV_BYTES, &generated_iv));
RETURN_NOT_OK(get_random_bytes(generated_iv));
iv_len = (int)generated_iv.size();
iv_buf = (unsigned char*)generated_iv.data();
} else {
Expand Down
10 changes: 0 additions & 10 deletions tiledb/sm/crypto/crypto_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ class OpenSSL {
*/
static Status sha256(
const void* input, uint64_t input_read_size, Buffer* output);

private:
/**
* Generates a number of cryptographically random bytes.
*
* @param num_bytes Number of bytes to generate.
* @param output Buffer to store random bytes.
* @return Status
*/
static Status get_random_bytes(unsigned num_bytes, Buffer* output);
teo-tsirpanis marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace sm
Expand Down
40 changes: 15 additions & 25 deletions tiledb/sm/crypto/crypto_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#ifdef _WIN32

#include "tiledb/sm/crypto/crypto_win32.h"

#include <algorithm>
#include "tiledb/common/heap_memory.h"
#include "tiledb/common/logger.h"
#include "tiledb/sm/buffer/buffer.h"
Expand All @@ -47,28 +49,26 @@ using namespace tiledb::common;
namespace tiledb {
namespace sm {

Status Win32CNG::get_random_bytes(unsigned num_bytes, Buffer* output) {
if (output->free_space() < num_bytes)
RETURN_NOT_OK(output->realloc(output->alloced_size() + num_bytes));

static Status get_random_bytes(span<uint8_t> buffer) {
BCRYPT_ALG_HANDLE alg_handle;
if (!NT_SUCCESS(BCryptOpenAlgorithmProvider(
&alg_handle, BCRYPT_RNG_ALGORITHM, nullptr, 0)))
return Status_EncryptionError(
"Win32CNG error; generating random bytes: error opening algorithm.");

if (!NT_SUCCESS(BCryptGenRandom(
alg_handle, (unsigned char*)output->cur_data(), num_bytes, 0))) {
BCryptCloseAlgorithmProvider(alg_handle, 0);
return Status_EncryptionError(
"Win32CNG error; generating random bytes: error generating bytes.");
while (!buffer.empty()) {
ULONG num_bytes = ULONG(
std::min(buffer.size(), size_t(std::numeric_limits<ULONG>::max())));
if (!NT_SUCCESS(BCryptGenRandom(alg_handle, buffer.data(), num_bytes, 0))) {
BCryptCloseAlgorithmProvider(alg_handle, 0);
return Status_EncryptionError(
"Win32CNG error; generating random bytes: error generating bytes.");
}
buffer = buffer.subspan(num_bytes);
}

BCryptCloseAlgorithmProvider(alg_handle, 0);

output->advance_size(num_bytes);
output->advance_offset(num_bytes);

return Status::Ok();
}

Expand All @@ -87,11 +87,11 @@ Status Win32CNG::encrypt_aes256gcm(
// Generate IV if the given IV buffer is null.
ULONG iv_len;
unsigned char* iv_buf;
Buffer generated_iv;
std::array<unsigned char, Crypto::AES256GCM_IV_BYTES> generated_iv;
if (iv == nullptr || iv->data() == nullptr) {
RETURN_NOT_OK(get_random_bytes(Crypto::AES256GCM_IV_BYTES, &generated_iv));
RETURN_NOT_OK(get_random_bytes(generated_iv));
iv_len = (ULONG)generated_iv.size();
iv_buf = (unsigned char*)generated_iv.data();
iv_buf = generated_iv.data();
} else {
iv_len = (ULONG)iv->size();
iv_buf = (unsigned char*)iv->data();
Expand Down Expand Up @@ -294,16 +294,6 @@ Status Win32CNG::decrypt_aes256gcm(
return Status::Ok();
}

Status Win32CNG::md5(
const void* input, uint64_t input_read_size, Buffer* output) {
return hash_bytes(input, input_read_size, output, BCRYPT_MD5_ALGORITHM);
}

Status Win32CNG::sha256(
const void* input, uint64_t input_read_size, Buffer* output) {
return hash_bytes(input, input_read_size, output, BCRYPT_SHA256_ALGORITHM);
}

Status Win32CNG::hash_bytes(
const void* input,
uint64_t input_read_size,
Expand Down
22 changes: 9 additions & 13 deletions tiledb/sm/crypto/crypto_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <windows.h>

#include <bcrypt.h>

#include "tiledb/common/status.h"

using namespace tiledb::common;
Expand Down Expand Up @@ -101,7 +102,9 @@ class Win32CNG {
* @return Status
*/
static Status md5(
const void* input, uint64_t input_read_size, Buffer* output);
const void* input, uint64_t input_read_size, Buffer* output) {
return hash_bytes(input, input_read_size, output, BCRYPT_MD5_ALGORITHM);
}

/**
* Compute sha256 checksum of data
Expand All @@ -112,11 +115,14 @@ class Win32CNG {
* @return Status
*/
static Status sha256(
const void* input, uint64_t input_read_size, Buffer* output);
const void* input, uint64_t input_read_size, Buffer* output) {
return hash_bytes(input, input_read_size, output, BCRYPT_SHA256_ALGORITHM);
}

private:
/**
*
* Compute a has using Win32CNG functions
* Compute a hash using Win32CNG functions
*
* @param input Plaintext to compute hash of
* @param input_read_size size of input to read for hash
Expand All @@ -129,16 +135,6 @@ class Win32CNG {
uint64_t input_read_size,
Buffer* output,
LPCWSTR hash_algorithm);

private:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it private. Or better yet, since it's static, take it out of the class and define it only in the .cc file.

/**
* Generates a number of cryptographically random bytes.
*
* @param num_bytes Number of bytes to generate.
* @param output Buffer to store random bytes.
* @return Status
*/
static Status get_random_bytes(unsigned num_bytes, Buffer* output);
};

} // namespace sm
Expand Down
32 changes: 32 additions & 0 deletions tiledb/sm/crypto/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# tiledb/sm/crypto/test/CMakeLists.txt
#
# The MIT License
#
# Copyright (c) 2024 TileDB, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

include(unit_test)

commence(unit_test tiledb_crypto)
this_target_sources(unit_tiledb_crypto.cc)
this_target_object_libraries(tiledb_crypto)
conclude(unit_test)
34 changes: 0 additions & 34 deletions tiledb/sm/crypto/test/compile_crypto_main.cc

This file was deleted.

Loading
Loading