Skip to content

Commit

Permalink
test: Add a test for encrypting 100MB of data.
Browse files Browse the repository at this point in the history
Both in C and C++, because the C++ tests don't currently run on FreeBSD,
and we want to know how the FreeBSD VM performs (answer: poorly).
  • Loading branch information
iphydf committed Nov 22, 2023
1 parent 28f3904 commit a927183
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,30 @@ static void test_large_data_symmetric(void)
free(m1);
}

static void test_very_large_data(void)
{
const Random *rng = system_random();
ck_assert(rng != nullptr);

uint8_t nonce[CRYPTO_NONCE_SIZE] = {0};

Check notice on line 277 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L277

Variable 'nonce' can be declared as const array
uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t sk[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(rng, pk, sk);

// 100 MiB of data (all zeroes, doesn't matter what's inside).
const uint32_t plain_size = 100 * 1024 * 1024;
uint8_t *plain = (uint8_t *)malloc(plain_size);

Check warning on line 284 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L284

MISRA 21.3 rule
uint8_t *encrypted = (uint8_t *)malloc(plain_size + CRYPTO_MAC_SIZE);

Check warning on line 285 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L285

MISRA 21.3 rule

ck_assert(plain != nullptr);

Check warning on line 287 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L287

MISRA 21.8 rule
ck_assert(encrypted != nullptr);

Check warning on line 288 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L288

MISRA 21.8 rule

encrypt_data(pk, sk, nonce, plain, plain_size, encrypted);

free(encrypted);

Check warning on line 292 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L292

MISRA 21.3 rule
free(plain);

Check warning on line 293 in auto_tests/crypto_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/crypto_test.c#L293

MISRA 21.3 rule
}

static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num)
{
uint32_t num1 = 0;
Expand Down Expand Up @@ -340,6 +364,7 @@ int main(void)
test_endtoend(); /* waiting up to 15 seconds */
test_large_data();
test_large_data_symmetric();
test_very_large_data();
test_increment_nonce();
test_memzero();

Expand Down
19 changes: 19 additions & 0 deletions toxcore/crypto_core_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@ namespace {

using HmacKey = std::array<uint8_t, CRYPTO_HMAC_KEY_SIZE>;
using Hmac = std::array<uint8_t, CRYPTO_HMAC_SIZE>;
using PublicKey = std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>;
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
using Signature = std::array<uint8_t, CRYPTO_SIGNATURE_SIZE>;
using Nonce = std::array<uint8_t, CRYPTO_NONCE_SIZE>;

TEST(CryptoCore, EncryptLargeData)
{
const Random *rng = system_random();
ASSERT_NE(rng, nullptr);

Nonce nonce{};
PublicKey pk;
SecretKey sk;
crypto_new_keypair(rng, pk.data(), sk.data());

// 100 MiB of data (all zeroes, doesn't matter what's inside).
std::vector<uint8_t> plain(100 * 1024 * 1024);
std::vector<uint8_t> encrypted(plain.size() + CRYPTO_MAC_SIZE);

encrypt_data(pk.data(), sk.data(), nonce.data(), plain.data(), plain.size(), encrypted.data());
}

TEST(CryptoCore, IncrementNonce)
{
Nonce nonce{};
Expand Down

0 comments on commit a927183

Please sign in to comment.