chromium/third_party/boringssl/src/crypto/chacha/chacha_test.cc

/* Copyright (c) 2016, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#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);
    // Cover the partial block paths.
    check_abi(buf.get(), kInput, len + 15, key, kCounterNonce);
  }
}

#endif  // SUPPORTS_ABI_TEST