2016-10-25 01:02:26 +01:00
|
|
|
/* 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. */
|
|
|
|
|
|
|
|
#if !defined(__STDC_FORMAT_MACROS)
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <openssl/base.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-10-25 01:02:26 +01:00
|
|
|
#include <openssl/bn.h>
|
2018-11-06 23:18:56 +00:00
|
|
|
#include <openssl/cpu.h>
|
|
|
|
#include <openssl/ec.h>
|
2016-10-25 01:02:26 +01:00
|
|
|
#include <openssl/mem.h>
|
2018-11-06 23:18:56 +00:00
|
|
|
#include <openssl/nid.h>
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2018-11-06 23:18:56 +00:00
|
|
|
#include "internal.h"
|
2017-05-02 22:25:39 +01:00
|
|
|
#include "../bn/internal.h"
|
2017-08-08 23:19:07 +01:00
|
|
|
#include "../../internal.h"
|
2017-05-02 22:25:39 +01:00
|
|
|
#include "../../test/file_test.h"
|
2017-06-09 01:04:08 +01:00
|
|
|
#include "../../test/test_util.h"
|
2016-10-25 01:02:26 +01:00
|
|
|
#include "p256-x86_64.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Disable tests if BORINGSSL_SHARED_LIBRARY is defined. These tests need access
|
|
|
|
// to internal functions.
|
|
|
|
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
|
|
|
|
!defined(OPENSSL_SMALL) && !defined(BORINGSSL_SHARED_LIBRARY)
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
TEST(P256_X86_64Test, SelectW5) {
|
2016-10-25 01:02:26 +01:00
|
|
|
// Fill a table with some garbage input.
|
2017-08-08 23:19:07 +01:00
|
|
|
alignas(64) P256_POINT table[16];
|
2016-10-25 01:02:26 +01:00
|
|
|
for (size_t i = 0; i < 16; i++) {
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(table[i].X, 3 * i, sizeof(table[i].X));
|
|
|
|
OPENSSL_memset(table[i].Y, 3 * i + 1, sizeof(table[i].Y));
|
|
|
|
OPENSSL_memset(table[i].Z, 3 * i + 2, sizeof(table[i].Z));
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 16; i++) {
|
|
|
|
P256_POINT val;
|
|
|
|
ecp_nistz256_select_w5(&val, table, i);
|
|
|
|
|
|
|
|
P256_POINT expected;
|
|
|
|
if (i == 0) {
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(&expected, 0, sizeof(expected));
|
2016-10-25 01:02:26 +01:00
|
|
|
} else {
|
|
|
|
expected = table[i-1];
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_EQ(Bytes(reinterpret_cast<const char *>(&expected), sizeof(expected)),
|
|
|
|
Bytes(reinterpret_cast<const char *>(&val), sizeof(val)));
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
TEST(P256_X86_64Test, SelectW7) {
|
2016-10-25 01:02:26 +01:00
|
|
|
// Fill a table with some garbage input.
|
2017-08-08 23:19:07 +01:00
|
|
|
alignas(64) P256_POINT_AFFINE table[64];
|
2016-10-25 01:02:26 +01:00
|
|
|
for (size_t i = 0; i < 64; i++) {
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(table[i].X, 2 * i, sizeof(table[i].X));
|
|
|
|
OPENSSL_memset(table[i].Y, 2 * i + 1, sizeof(table[i].Y));
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 64; i++) {
|
|
|
|
P256_POINT_AFFINE val;
|
|
|
|
ecp_nistz256_select_w7(&val, table, i);
|
|
|
|
|
|
|
|
P256_POINT_AFFINE expected;
|
|
|
|
if (i == 0) {
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(&expected, 0, sizeof(expected));
|
2016-10-25 01:02:26 +01:00
|
|
|
} else {
|
|
|
|
expected = table[i-1];
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_EQ(Bytes(reinterpret_cast<const char *>(&expected), sizeof(expected)),
|
|
|
|
Bytes(reinterpret_cast<const char *>(&val), sizeof(val)));
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 23:18:56 +00:00
|
|
|
TEST(P256_X86_64Test, BEEU) {
|
|
|
|
if ((OPENSSL_ia32cap_P[1] & (1 << 28)) == 0) {
|
|
|
|
// No AVX support; cannot run the BEEU code.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bssl::UniquePtr<EC_GROUP> group(
|
|
|
|
EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
|
|
|
|
ASSERT_TRUE(group);
|
|
|
|
|
|
|
|
BN_ULONG order_words[P256_LIMBS];
|
|
|
|
ASSERT_TRUE(
|
|
|
|
bn_copy_words(order_words, P256_LIMBS, EC_GROUP_get0_order(group.get())));
|
|
|
|
|
|
|
|
BN_ULONG in[P256_LIMBS], out[P256_LIMBS];
|
|
|
|
EC_SCALAR in_scalar, out_scalar, result;
|
|
|
|
OPENSSL_memset(in, 0, sizeof(in));
|
|
|
|
|
|
|
|
// Trying to find the inverse of zero should fail.
|
|
|
|
ASSERT_FALSE(beeu_mod_inverse_vartime(out, in, order_words));
|
|
|
|
|
|
|
|
// kOneMont is 1, in Montgomery form.
|
|
|
|
static const BN_ULONG kOneMont[P256_LIMBS] = {
|
|
|
|
TOBN(0xc46353d, 0x039cdaaf), TOBN(0x43190552, 0x58e8617b),
|
|
|
|
0, 0xffffffff,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (BN_ULONG i = 1; i < 2000; i++) {
|
|
|
|
SCOPED_TRACE(i);
|
|
|
|
|
|
|
|
in[0] = i;
|
|
|
|
if (i >= 1000) {
|
|
|
|
in[1] = i << 8;
|
|
|
|
in[2] = i << 32;
|
|
|
|
in[3] = i << 48;
|
|
|
|
} else {
|
|
|
|
in[1] = in[2] = in[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(bn_less_than_words(in, order_words, P256_LIMBS));
|
|
|
|
ASSERT_TRUE(beeu_mod_inverse_vartime(out, in, order_words));
|
|
|
|
EXPECT_TRUE(bn_less_than_words(out, order_words, P256_LIMBS));
|
|
|
|
|
|
|
|
// Calculate out*in and confirm that it equals one, modulo the order.
|
|
|
|
OPENSSL_memcpy(in_scalar.bytes, in, sizeof(in));
|
|
|
|
OPENSSL_memcpy(out_scalar.bytes, out, sizeof(out));
|
|
|
|
ec_scalar_to_montgomery(group.get(), &in_scalar, &in_scalar);
|
|
|
|
ec_scalar_to_montgomery(group.get(), &out_scalar, &out_scalar);
|
|
|
|
ec_scalar_mul_montgomery(group.get(), &result, &in_scalar, &out_scalar);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, OPENSSL_memcmp(kOneMont, &result, sizeof(kOneMont)));
|
|
|
|
|
|
|
|
// Invert the result and expect to get back to the original value.
|
|
|
|
ASSERT_TRUE(beeu_mod_inverse_vartime(out, out, order_words));
|
|
|
|
EXPECT_EQ(0, OPENSSL_memcmp(in, out, sizeof(in)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 01:02:26 +01:00
|
|
|
static bool GetFieldElement(FileTest *t, BN_ULONG out[P256_LIMBS],
|
|
|
|
const char *name) {
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
if (!t->GetBytes(&bytes, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes.size() != BN_BYTES * P256_LIMBS) {
|
2017-06-09 01:04:08 +01:00
|
|
|
ADD_FAILURE() << "Invalid length: " << name;
|
2016-10-25 01:02:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |byte| contains bytes in big-endian while |out| should contain |BN_ULONG|s
|
|
|
|
// in little-endian.
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(out, 0, P256_LIMBS * sizeof(BN_ULONG));
|
2016-10-25 01:02:26 +01:00
|
|
|
for (size_t i = 0; i < bytes.size(); i++) {
|
|
|
|
out[P256_LIMBS - 1 - (i / BN_BYTES)] <<= 8;
|
|
|
|
out[P256_LIMBS - 1 - (i / BN_BYTES)] |= bytes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string FieldElementToString(const BN_ULONG a[P256_LIMBS]) {
|
|
|
|
std::string ret;
|
|
|
|
for (size_t i = P256_LIMBS-1; i < P256_LIMBS; i--) {
|
|
|
|
char buf[2 * BN_BYTES + 1];
|
|
|
|
BIO_snprintf(buf, sizeof(buf), BN_HEX_FMT2, a[i]);
|
|
|
|
ret += buf;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
static testing::AssertionResult ExpectFieldElementsEqual(
|
|
|
|
const char *expected_expr, const char *actual_expr,
|
|
|
|
const BN_ULONG expected[P256_LIMBS], const BN_ULONG actual[P256_LIMBS]) {
|
2016-12-13 06:07:13 +00:00
|
|
|
if (OPENSSL_memcmp(expected, actual, sizeof(BN_ULONG) * P256_LIMBS) == 0) {
|
2017-06-09 01:04:08 +01:00
|
|
|
return testing::AssertionSuccess();
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Expected: " << FieldElementToString(expected) << " ("
|
|
|
|
<< expected_expr << ")\n"
|
|
|
|
<< "Actual: " << FieldElementToString(actual) << " (" << actual_expr
|
|
|
|
<< ")";
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
#define EXPECT_FIELD_ELEMENTS_EQUAL(a, b) \
|
|
|
|
EXPECT_PRED_FORMAT2(ExpectFieldElementsEqual, a, b)
|
|
|
|
|
2016-10-25 01:02:26 +01:00
|
|
|
static bool PointToAffine(P256_POINT_AFFINE *out, const P256_POINT *in) {
|
|
|
|
static const uint8_t kP[] = {
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
};
|
|
|
|
|
|
|
|
bssl::UniquePtr<BIGNUM> x(BN_new()), y(BN_new()), z(BN_new());
|
|
|
|
bssl::UniquePtr<BIGNUM> p(BN_bin2bn(kP, sizeof(kP), nullptr));
|
|
|
|
if (!x || !y || !z || !p ||
|
|
|
|
!bn_set_words(x.get(), in->X, P256_LIMBS) ||
|
|
|
|
!bn_set_words(y.get(), in->Y, P256_LIMBS) ||
|
|
|
|
!bn_set_words(z.get(), in->Z, P256_LIMBS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coordinates must be fully-reduced.
|
2016-11-15 22:34:53 +00:00
|
|
|
if (BN_cmp(x.get(), p.get()) >= 0 ||
|
2016-10-25 01:02:26 +01:00
|
|
|
BN_cmp(y.get(), p.get()) >= 0 ||
|
|
|
|
BN_cmp(z.get(), p.get()) >= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_is_zero(z.get())) {
|
|
|
|
// The point at infinity is represented as (0, 0).
|
2018-01-20 21:51:54 +00:00
|
|
|
OPENSSL_memset(out, 0, sizeof(P256_POINT_AFFINE));
|
2016-10-25 01:02:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
|
2018-01-23 22:03:26 +00:00
|
|
|
bssl::UniquePtr<BN_MONT_CTX> mont(
|
|
|
|
BN_MONT_CTX_new_for_modulus(p.get(), ctx.get()));
|
2016-10-25 01:02:26 +01:00
|
|
|
if (!ctx || !mont ||
|
|
|
|
// Invert Z.
|
|
|
|
!BN_from_montgomery(z.get(), z.get(), mont.get(), ctx.get()) ||
|
|
|
|
!BN_mod_inverse(z.get(), z.get(), p.get(), ctx.get()) ||
|
|
|
|
!BN_to_montgomery(z.get(), z.get(), mont.get(), ctx.get()) ||
|
|
|
|
// Convert (X, Y, Z) to (X/Z^2, Y/Z^3).
|
|
|
|
!BN_mod_mul_montgomery(x.get(), x.get(), z.get(), mont.get(),
|
|
|
|
ctx.get()) ||
|
|
|
|
!BN_mod_mul_montgomery(x.get(), x.get(), z.get(), mont.get(),
|
|
|
|
ctx.get()) ||
|
|
|
|
!BN_mod_mul_montgomery(y.get(), y.get(), z.get(), mont.get(),
|
|
|
|
ctx.get()) ||
|
|
|
|
!BN_mod_mul_montgomery(y.get(), y.get(), z.get(), mont.get(),
|
|
|
|
ctx.get()) ||
|
|
|
|
!BN_mod_mul_montgomery(y.get(), y.get(), z.get(), mont.get(),
|
2018-01-20 21:51:54 +00:00
|
|
|
ctx.get()) ||
|
|
|
|
!bn_copy_words(out->X, P256_LIMBS, x.get()) ||
|
|
|
|
!bn_copy_words(out->Y, P256_LIMBS, y.get())) {
|
2016-10-25 01:02:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
static testing::AssertionResult ExpectPointsEqual(
|
|
|
|
const char *expected_expr, const char *actual_expr,
|
|
|
|
const P256_POINT_AFFINE *expected, const P256_POINT *actual) {
|
2016-10-25 01:02:26 +01:00
|
|
|
// There are multiple representations of the same |P256_POINT|, so convert to
|
|
|
|
// |P256_POINT_AFFINE| and compare.
|
|
|
|
P256_POINT_AFFINE affine;
|
2017-06-09 01:04:08 +01:00
|
|
|
if (!PointToAffine(&affine, actual)) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Could not convert " << actual_expr << " to affine: ("
|
|
|
|
<< FieldElementToString(actual->X) << ", "
|
|
|
|
<< FieldElementToString(actual->Y) << ", "
|
|
|
|
<< FieldElementToString(actual->Z) << ")";
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
if (OPENSSL_memcmp(expected, &affine, sizeof(P256_POINT_AFFINE)) != 0) {
|
2017-06-09 01:04:08 +01:00
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Expected: (" << FieldElementToString(expected->X) << ", "
|
|
|
|
<< FieldElementToString(expected->Y) << ") (" << expected_expr
|
|
|
|
<< "; affine)\n"
|
|
|
|
<< "Actual: (" << FieldElementToString(affine.X) << ", "
|
|
|
|
<< FieldElementToString(affine.Y) << ") (" << actual_expr << ")";
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
return testing::AssertionSuccess();
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
#define EXPECT_POINTS_EQUAL(a, b) EXPECT_PRED_FORMAT2(ExpectPointsEqual, a, b)
|
|
|
|
|
|
|
|
static void TestNegate(FileTest *t) {
|
2016-10-25 01:02:26 +01:00
|
|
|
BN_ULONG a[P256_LIMBS], b[P256_LIMBS];
|
2017-06-09 01:04:08 +01:00
|
|
|
ASSERT_TRUE(GetFieldElement(t, a, "A"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b, "B"));
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
// Test that -A = B.
|
|
|
|
BN_ULONG ret[P256_LIMBS];
|
|
|
|
ecp_nistz256_neg(ret, a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(b, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_neg(ret, ret /* a */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(b, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
// Test that -B = A.
|
|
|
|
ecp_nistz256_neg(ret, b);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(a, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_neg(ret, ret /* b */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(a, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
static void TestMulMont(FileTest *t) {
|
2016-10-25 01:02:26 +01:00
|
|
|
BN_ULONG a[P256_LIMBS], b[P256_LIMBS], result[P256_LIMBS];
|
2017-06-09 01:04:08 +01:00
|
|
|
ASSERT_TRUE(GetFieldElement(t, a, "A"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b, "B"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, result, "Result"));
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
BN_ULONG ret[P256_LIMBS];
|
|
|
|
ecp_nistz256_mul_mont(ret, a, b);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
ecp_nistz256_mul_mont(ret, b, a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_mul_mont(ret, ret /* a */, b);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
2016-10-25 01:02:26 +01:00
|
|
|
ecp_nistz256_mul_mont(ret, b, ret);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_mul_mont(ret, a, ret /* b */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_mul_mont(ret, ret /* b */, a);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
if (OPENSSL_memcmp(a, b, sizeof(a)) == 0) {
|
2016-10-25 01:02:26 +01:00
|
|
|
ecp_nistz256_sqr_mont(ret, a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_sqr_mont(ret, ret /* a */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
static void TestFromMont(FileTest *t) {
|
2016-10-25 01:02:26 +01:00
|
|
|
BN_ULONG a[P256_LIMBS], result[P256_LIMBS];
|
2017-06-09 01:04:08 +01:00
|
|
|
ASSERT_TRUE(GetFieldElement(t, a, "A"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, result, "Result"));
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
BN_ULONG ret[P256_LIMBS];
|
|
|
|
ecp_nistz256_from_mont(ret, a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_from_mont(ret, ret /* a */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
static void TestPointAdd(FileTest *t) {
|
2016-10-25 01:02:26 +01:00
|
|
|
P256_POINT a, b;
|
|
|
|
P256_POINT_AFFINE result;
|
2017-06-09 01:04:08 +01:00
|
|
|
ASSERT_TRUE(GetFieldElement(t, a.X, "A.X"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, a.Y, "A.Y"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, a.Z, "A.Z"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b.X, "B.X"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b.Y, "B.Y"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b.Z, "B.Z"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, result.X, "Result.X"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, result.Y, "Result.Y"));
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
P256_POINT ret;
|
|
|
|
ecp_nistz256_point_add(&ret, &a, &b);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
ecp_nistz256_point_add(&ret, &b, &a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add(&ret, &ret /* a */, &b);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add(&ret, &b, &ret /* a */);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add(&ret, &a, &ret /* b */);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add(&ret, &ret /* b */, &a);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
P256_POINT_AFFINE a_affine, b_affine, infinity;
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(&infinity, 0, sizeof(infinity));
|
2017-06-09 01:04:08 +01:00
|
|
|
ASSERT_TRUE(PointToAffine(&a_affine, &a));
|
|
|
|
ASSERT_TRUE(PointToAffine(&b_affine, &b));
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
// ecp_nistz256_point_add_affine does not work when a == b unless doubling the
|
|
|
|
// point at infinity.
|
2016-12-13 06:07:13 +00:00
|
|
|
if (OPENSSL_memcmp(&a_affine, &b_affine, sizeof(a_affine)) != 0 ||
|
|
|
|
OPENSSL_memcmp(&a_affine, &infinity, sizeof(a_affine)) == 0) {
|
2016-10-25 01:02:26 +01:00
|
|
|
ecp_nistz256_point_add_affine(&ret, &a, &b_affine);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &a, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add_affine(&ret, &ret /* a */, &b_affine);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
ecp_nistz256_point_add_affine(&ret, &b, &a_affine);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(&ret, &b, sizeof(ret));
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_add_affine(&ret, &ret /* b */, &a_affine);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
if (OPENSSL_memcmp(&a, &b, sizeof(a)) == 0) {
|
2016-10-25 01:02:26 +01:00
|
|
|
ecp_nistz256_point_double(&ret, &a);
|
2017-06-09 01:04:08 +01:00
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
|
|
|
|
ret = a;
|
2017-06-09 01:04:08 +01:00
|
|
|
ecp_nistz256_point_double(&ret, &ret /* a */);
|
|
|
|
EXPECT_POINTS_EQUAL(&result, &ret);
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 06:20:15 +01:00
|
|
|
static void TestOrdMulMont(FileTest *t) {
|
|
|
|
// This test works on scalars rather than field elements, but the
|
|
|
|
// representation is the same.
|
|
|
|
BN_ULONG a[P256_LIMBS], b[P256_LIMBS], result[P256_LIMBS];
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, a, "A"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, b, "B"));
|
|
|
|
ASSERT_TRUE(GetFieldElement(t, result, "Result"));
|
|
|
|
|
|
|
|
BN_ULONG ret[P256_LIMBS];
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, a, b);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, b, a);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, ret /* a */, b);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, b, ret);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
OPENSSL_memcpy(ret, b, sizeof(ret));
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, a, ret /* b */);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
OPENSSL_memcpy(ret, b, sizeof(ret));
|
|
|
|
ecp_nistz256_ord_mul_mont(ret, ret /* b */, a);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
if (OPENSSL_memcmp(a, b, sizeof(a)) == 0) {
|
|
|
|
ecp_nistz256_ord_sqr_mont(ret, a, 1);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
|
|
|
|
OPENSSL_memcpy(ret, a, sizeof(ret));
|
|
|
|
ecp_nistz256_ord_sqr_mont(ret, ret /* a */, 1);
|
|
|
|
EXPECT_FIELD_ELEMENTS_EQUAL(result, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:04:08 +01:00
|
|
|
TEST(P256_X86_64Test, TestVectors) {
|
|
|
|
return FileTestGTest("crypto/fipsmodule/ec/p256-x86_64_tests.txt",
|
|
|
|
[](FileTest *t) {
|
2016-10-25 01:02:26 +01:00
|
|
|
if (t->GetParameter() == "Negate") {
|
2017-06-09 01:04:08 +01:00
|
|
|
TestNegate(t);
|
|
|
|
} else if (t->GetParameter() == "MulMont") {
|
|
|
|
TestMulMont(t);
|
|
|
|
} else if (t->GetParameter() == "FromMont") {
|
|
|
|
TestFromMont(t);
|
|
|
|
} else if (t->GetParameter() == "PointAdd") {
|
|
|
|
TestPointAdd(t);
|
2018-04-21 06:20:15 +01:00
|
|
|
} else if (t->GetParameter() == "OrdMulMont") {
|
|
|
|
TestOrdMulMont(t);
|
2017-06-09 01:04:08 +01:00
|
|
|
} else {
|
|
|
|
FAIL() << "Unknown test type:" << t->GetParameter();
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
2017-06-09 01:04:08 +01:00
|
|
|
});
|
2016-10-25 01:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|