#include "client_test_helpers.h"
#include "sa.h"
#include "sa_crypto_cipher_common.h"
#include "gtest/gtest.h"
using namespace client_test_helpers;
namespace {
TEST_P(SaCryptoCipherWithSvpTest, processChacha20EncryptResumePartialBlock) {
sa_buffer_type const buffer_type = std::get<0>(GetParam());
cipher_parameters parameters;
parameters.cipher_algorithm = SA_CIPHER_ALGORITHM_CHACHA20;
parameters.svp_required = false;
sa_key_type const key_type = SA_KEY_TYPE_SYMMETRIC;
size_t const key_size = SYM_256_KEY_SIZE;
auto cipher = initialize_cipher(SA_CIPHER_MODE_ENCRYPT, key_type, key_size, parameters);
ASSERT_NE(cipher, nullptr);
if (*cipher == UNSUPPORTED_CIPHER)
GTEST_SKIP() << "Cipher algorithm not supported";
auto clear = random(34);
auto in_buffer = buffer_alloc(buffer_type, clear);
ASSERT_NE(in_buffer, nullptr);
auto out_buffer = buffer_alloc(buffer_type, clear.size());
ASSERT_NE(out_buffer, nullptr);
size_t bytes_to_process = clear.size() / 2;
sa_status status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_OK);
ASSERT_EQ(bytes_to_process, clear.size() / 2);
bytes_to_process = clear.size() / 2;
status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_OK);
ASSERT_EQ(bytes_to_process, clear.size() / 2);
ASSERT_TRUE(verify_encrypt(out_buffer.get(), clear, parameters, false));
}
TEST_P(SaCryptoCipherWithSvpTest, processChacha20DecryptResumePartialBlock) {
sa_buffer_type const buffer_type = std::get<0>(GetParam());
cipher_parameters parameters;
parameters.cipher_algorithm = SA_CIPHER_ALGORITHM_CHACHA20;
parameters.svp_required = false;
sa_key_type const key_type = SA_KEY_TYPE_SYMMETRIC;
size_t const key_size = SYM_256_KEY_SIZE;
auto cipher = initialize_cipher(SA_CIPHER_MODE_DECRYPT, key_type, key_size, parameters);
ASSERT_NE(cipher, nullptr);
if (*cipher == UNSUPPORTED_CIPHER)
GTEST_SKIP() << "Cipher algorithm not supported";
auto clear = random(34);
auto encrypted = encrypt_openssl(clear, parameters);
ASSERT_FALSE(encrypted.empty());
auto in_buffer = buffer_alloc(buffer_type, encrypted);
ASSERT_NE(in_buffer, nullptr);
auto out_buffer = buffer_alloc(buffer_type, clear.size());
ASSERT_NE(out_buffer, nullptr);
size_t bytes_to_process = clear.size() / 2;
sa_status status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_OK);
ASSERT_EQ(bytes_to_process, clear.size() / 2);
bytes_to_process = clear.size() / 2;
status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_OK);
ASSERT_EQ(bytes_to_process, clear.size() / 2);
ASSERT_TRUE(verify_decrypt(out_buffer.get(), clear));
}
TEST_P(SaCryptoCipherWithSvpTest, processChacha20FailsInvalidOutLength) {
sa_buffer_type const buffer_type = std::get<0>(GetParam());
sa_cipher_mode const cipher_mode = std::get<1>(GetParam());
auto clear_key = random(SYM_256_KEY_SIZE);
sa_rights rights;
sa_rights_set_allow_all(&rights);
auto key = create_sa_key_symmetric(&rights, clear_key);
ASSERT_NE(key, nullptr);
auto cipher = create_uninitialized_sa_crypto_cipher_context();
ASSERT_NE(cipher, nullptr);
std::vector<uint8_t> counter = {1, 0, 0, 0};
auto nonce = random(CHACHA20_NONCE_LENGTH);
sa_cipher_parameters_chacha20 parameters = {counter.data(), counter.size(), nonce.data(), nonce.size()};
sa_status status = sa_crypto_cipher_init(cipher.get(), SA_CIPHER_ALGORITHM_CHACHA20, cipher_mode, *key,
¶meters);
if (status == SA_STATUS_OPERATION_NOT_SUPPORTED)
GTEST_SKIP() << "Cipher algorithm not supported";
ASSERT_EQ(status, SA_STATUS_OK);
auto clear = random(static_cast<size_t>(AES_BLOCK_SIZE) * 2);
auto in_buffer = buffer_alloc(buffer_type, clear);
ASSERT_NE(in_buffer, nullptr);
auto out_buffer = buffer_alloc(SA_BUFFER_TYPE_CLEAR, clear.size() - 1);
ASSERT_NE(out_buffer, nullptr);
size_t bytes_to_process = clear.size();
status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_INVALID_PARAMETER);
}
TEST_F(SaCryptoCipherSvpOnlyTest, initChacha20FailsDecryptInvalidRightsSvpOptionalNotSet) {
auto clear_key = random(SYM_256_KEY_SIZE);
sa_rights rights;
sa_rights_set_allow_all(&rights);
SA_USAGE_BIT_CLEAR(rights.usage_flags, SA_USAGE_FLAG_SVP_OPTIONAL);
auto key = create_sa_key_symmetric(&rights, clear_key);
ASSERT_NE(key, nullptr);
auto cipher = create_uninitialized_sa_crypto_cipher_context();
ASSERT_NE(cipher, nullptr);
std::vector<uint8_t> counter = {1, 0, 0, 0};
auto nonce = random(CHACHA20_NONCE_LENGTH);
sa_cipher_parameters_chacha20 parameters = {counter.data(), counter.size(), nonce.data(), nonce.size()};
sa_status status = sa_crypto_cipher_init(cipher.get(), SA_CIPHER_ALGORITHM_CHACHA20, SA_CIPHER_MODE_DECRYPT,
*key, ¶meters);
if (status == SA_STATUS_OPERATION_NOT_SUPPORTED)
GTEST_SKIP() << "Cipher algorithm not supported";
ASSERT_EQ(status, SA_STATUS_OK);
auto clear = random(static_cast<size_t>(AES_BLOCK_SIZE) * 2);
auto in_buffer = buffer_alloc(SA_BUFFER_TYPE_CLEAR, clear);
ASSERT_NE(in_buffer, nullptr);
auto out_buffer = buffer_alloc(SA_BUFFER_TYPE_CLEAR, clear.size() - 1);
ASSERT_NE(out_buffer, nullptr);
size_t bytes_to_process = clear.size();
status = sa_crypto_cipher_process(out_buffer.get(), *cipher, in_buffer.get(), &bytes_to_process);
ASSERT_EQ(status, SA_STATUS_OPERATION_NOT_ALLOWED);
}
}