chromium/third_party/boringssl/src/crypto/hrss/hrss_test.cc

/* Copyright (c) 2018, 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 <gtest/gtest.h>

#include <openssl/hrss.h>
#include <openssl/rand.h>

#include "../internal.h"
#include "../test/abi_test.h"
#include "../test/test_util.h"
#include "internal.h"


// poly3_rand sets |r| to a random value (albeit with bias).
static void poly3_rand(poly3 *p) {}

// poly3_word_add sets (|s1|, |a1|) += (|s2|, |a2|).
static void poly3_word_add(crypto_word_t *s1, crypto_word_t *a1,
                           const crypto_word_t s2, const crypto_word_t a2) {}

TEST(HRSS, Poly3Invert) {}

TEST(HRSS, Poly3UnreducedInput) {}

TEST(HRSS, Basic) {}

TEST(HRSS, Random) {}

TEST(HRSS, NoWritesToConstData) {}

TEST(HRSS, Golden) {}

#if defined(POLY_RQ_MUL_ASM) && defined(SUPPORTS_ABI_TEST)
TEST(HRSS, ABI) {
  if (!CRYPTO_is_AVX2_capable()) {
    fprintf(stderr, "Skipping ABI test due to lack of AVX2 support.\n");
    return;
  }

  alignas(16) uint16_t r[N + 3];
  alignas(16) uint16_t a[N + 3] = {0};
  alignas(16) uint16_t b[N + 3] = {0};

  uint8_t kCanary[256];
  static_assert(sizeof(kCanary) % 32 == 0, "needed for alignment");
  memset(kCanary, 42, sizeof(kCanary));

  auto scratch_buf = std::make_unique<uint8_t[]>(
      32 + sizeof(kCanary) + POLY_MUL_RQ_SCRATCH_SPACE + sizeof(kCanary));
  uint8_t *scratch =
      static_cast<uint8_t *>(align_pointer(scratch_buf.get(), 32));
  OPENSSL_memcpy(scratch, kCanary, sizeof(kCanary));
  OPENSSL_memcpy(scratch + sizeof(kCanary) + POLY_MUL_RQ_SCRATCH_SPACE, kCanary,
                 sizeof(kCanary));

  // The function should not touch more than |POLY_MUL_RQ_SCRATCH_SPACE| bytes
  // of |scratch|.
  CHECK_ABI(poly_Rq_mul, r, a, b, &scratch[sizeof(kCanary)]);

  EXPECT_EQ(Bytes(scratch, sizeof(kCanary)), Bytes(kCanary));
  EXPECT_EQ(Bytes(scratch + sizeof(kCanary) + POLY_MUL_RQ_SCRATCH_SPACE,
                  sizeof(kCanary)),
            Bytes(kCanary));
}
#endif  // POLY_RQ_MUL_ASM && SUPPORTS_ABI_TEST