#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <memory>
#include <gtest/gtest.h>
#include <openssl/crypto.h>
#include <openssl/chacha.h>
#include "internal.h"
#include "../internal.h"
#include "../test/abi_test.h"
#include "../test/test_util.h"
static const uint8_t kKey[32] = …;
static const uint8_t kNonce[12] = …;
static uint32_t kCounter = …;
static const uint8_t kInput[] = …;
static const uint8_t kOutput[] = …;
static uint32_t kOverflowCounter = …;
static const uint8_t kOverflowOutput[] = …;
static_assert …;
static_assert …;
TEST(ChaChaTest, TestVector) { … }
TEST(ChaChaTest, CounterOverflow) { … }
#if defined(SUPPORTS_ABI_TEST)
static void check_abi(uint8_t *out, const uint8_t *in, size_t in_len,
const uint32_t key[8], const uint32_t counter[4]) {
#if defined(CHACHA20_ASM_NEON)
if (ChaCha20_ctr32_neon_capable(in_len)) {
CHECK_ABI(ChaCha20_ctr32_neon, out, in, in_len, key, counter);
}
#endif
#if defined(CHACHA20_ASM_AVX2)
if (ChaCha20_ctr32_avx2_capable(in_len)) {
CHECK_ABI(ChaCha20_ctr32_avx2, out, in, in_len, key, counter);
}
#endif
#if defined(CHACHA20_ASM_SSSE3_4X)
if (ChaCha20_ctr32_ssse3_4x_capable(in_len)) {
CHECK_ABI(ChaCha20_ctr32_ssse3_4x, out, in, in_len, key, counter);
}
#endif
#if defined(CHACHA20_ASM_SSSE3)
if (ChaCha20_ctr32_ssse3_capable(in_len)) {
CHECK_ABI(ChaCha20_ctr32_ssse3, out, in, in_len, key, counter);
}
#endif
#if defined(CHACHA20_ASM_NOHW)
if (in_len > 0) {
CHECK_ABI(ChaCha20_ctr32_nohw, out, in, in_len, key, counter);
}
#endif
}
TEST(ChaChaTest, ABI) {
uint32_t key[8];
OPENSSL_memcpy(key, kKey, sizeof(key));
static const uint32_t kCounterNonce[4] = {0};
auto buf = std::make_unique<uint8_t[]>(sizeof(kInput));
for (size_t len = 0; len <= 32; len++) {
SCOPED_TRACE(len);
check_abi(buf.get(), kInput, len, key, kCounterNonce);
}
for (size_t len : {32 * 2, 32 * 4, 32 * 8, 32 * 16, 32 * 24}) {
SCOPED_TRACE(len);
check_abi(buf.get(), kInput, len, key, kCounterNonce);
check_abi(buf.get(), kInput, len + 15, key, kCounterNonce);
}
}
#endif