提交 59bf467c 编写于 作者: M Matt Caswell

Add tests for newly added constant time functions

Reviewed-by: NBernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/5105)
上级 dd8796c5
......@@ -21,9 +21,43 @@ static const unsigned char CONSTTIME_TRUE_8 = 0xff;
static const unsigned char CONSTTIME_FALSE_8 = 0;
static const size_t CONSTTIME_TRUE_S = ~((size_t)0);
static const size_t CONSTTIME_FALSE_S = 0;
static uint32_t CONSTTIME_TRUE_32 = (uint32_t)(~(uint32_t)0);
static uint32_t CONSTTIME_FALSE_32 = 0;
static uint64_t CONSTTIME_TRUE_64 = (uint64_t)(~(uint64_t)0);
static uint64_t CONSTTIME_FALSE_64 = 0;
static unsigned int test_values[] = {
0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
UINT_MAX
};
static unsigned char test_values_8[] = {
0, 1, 2, 20, 32, 127, 128, 129, 255
};
static int signed_test_values[] = {
0, 1, -1, 1024, -1024, 12345, -12345,
32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
INT_MIN + 1
};
static size_t test_values_s[] = {
0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
SIZE_MAX
};
static uint32_t test_values_32[] = {
0, 1, 1024, 12345, 32000, UINT32_MAX / 2, UINT32_MAX / 2 + 1,
UINT32_MAX - 1, UINT32_MAX
};
static uint64_t test_values_64[] = {
0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2,
UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX
};
static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b),
const char *op_name, unsigned int a, unsigned int b,
int is_true)
......@@ -76,9 +110,10 @@ static int test_binary_op_64(uint64_t (*op)(uint64_t a, uint64_t b),
return 1;
}
static int test_is_zero(unsigned int a)
static int test_is_zero(int i)
{
unsigned int a = test_values[i];
if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE))
return 0;
if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE))
......@@ -86,8 +121,10 @@ static int test_is_zero(unsigned int a)
return 1;
}
static int test_is_zero_8(unsigned int a)
static int test_is_zero_8(int i)
{
unsigned int a = test_values_8[i];
if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))
return 0;
if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))
......@@ -95,8 +132,21 @@ static int test_is_zero_8(unsigned int a)
return 1;
}
static int test_is_zero_s(unsigned int a)
static int test_is_zero_32(int i)
{
uint32_t a = test_values_32[i];
if (a == 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_TRUE_32))
return 0;
if (a != 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_FALSE_32))
return 0;
return 1;
}
static int test_is_zero_s(int i)
{
size_t a = test_values_s[i];
if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S))
return 0;
if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S))
......@@ -122,7 +172,16 @@ static int test_select_8(unsigned char a, unsigned char b)
return 1;
}
static int test_select_s(unsigned char a, unsigned char b)
static int test_select_32(uint32_t a, uint32_t b)
{
if (!TEST_true(constant_time_select_32(CONSTTIME_TRUE_32, a, b) == a))
return 0;
if (!TEST_true(constant_time_select_32(CONSTTIME_FALSE_32, a, b) == b))
return 0;
return 1;
}
static int test_select_s(size_t a, size_t b)
{
if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a))
return 0;
......@@ -184,33 +243,6 @@ static int test_eq_int(int a, int b)
return 1;
}
static unsigned int test_values[] = {
0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
UINT_MAX
};
static unsigned char test_values_8[] = {
0, 1, 2, 20, 32, 127, 128, 129, 255
};
static int signed_test_values[] = {
0, 1, -1, 1024, -1024, 12345, -12345,
32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
INT_MIN + 1
};
static size_t test_values_s[] = {
0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
SIZE_MAX
};
static uint64_t test_values_64[] = {
0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2,
UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX
};
static int test_sizeofs(void)
{
if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s)))
......@@ -221,58 +253,80 @@ static int test_sizeofs(void)
static int test_binops(int i)
{
unsigned int a = test_values[i];
unsigned int g = test_values_s[i];
int j;
int ret = 1;
if (!test_is_zero(a) || !test_is_zero_8(a) || !test_is_zero_s(g))
ret = 0;
for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) {
unsigned int b = test_values[j];
unsigned int h = test_values[j];
if (!test_select(a, b)
|| !test_select_s(g, h)
|| !test_eq_s(g, h)
|| !test_binary_op(&constant_time_lt, "ct_lt",
a, b, a < b)
|| !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
a, b, a < b)
|| !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
g, h, g < h)
|| !test_binary_op(&constant_time_lt, "constant_time_lt",
b, a, b < a)
|| !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
b, a, b < a)
|| !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
h, g, h < g)
|| !test_binary_op(&constant_time_ge, "constant_time_ge",
a, b, a >= b)
|| !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
a, b, a >= b)
|| !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
g, h, g >= h)
|| !test_binary_op(&constant_time_ge, "constant_time_ge",
b, a, b >= a)
|| !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
b, a, b >= a)
|| !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
h, g, h >= g)
|| !test_binary_op(&constant_time_eq, "constant_time_eq",
a, b, a == b)
|| !test_binary_op(&constant_time_eq, "constant_time_eq",
b, a, b == a))
ret = 0;
}
return ret;
}
static int test_binops_8(int i)
{
unsigned int a = test_values_8[i];
int j;
int ret = 1;
for (j = 0; j < (int)OSSL_NELEM(test_values_8); ++j) {
unsigned int b = test_values_8[j];
if (!test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
a, b, a < b)
|| !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
b, a, b < a)
|| !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
a, b, a >= b)
|| !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
b, a, b >= a)
|| !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
a, b, a == b)
|| !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
g, h, g == h)
|| !test_binary_op(&constant_time_eq, "constant_time_eq",
b, a, b == a)
|| !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
b, a, b == a)
b, a, b == a))
ret = 0;
}
return ret;
}
static int test_binops_s(int i)
{
size_t a = test_values_s[i];
int j;
int ret = 1;
for (j = 0; j < (int)OSSL_NELEM(test_values_s); ++j) {
size_t b = test_values_s[j];
if (!test_select_s(a, b)
|| !test_eq_s(a, b)
|| !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
a, b, a < b)
|| !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
b, a, b < a)
|| !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
a, b, a >= b)
|| !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
b, a, b >= a)
|| !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
h, g, h == g)) {
a, b, a == b)
|| !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
b, a, b == a))
ret = 0;
}
}
return ret;
}
......@@ -309,6 +363,21 @@ static int test_8values(int i)
return ret;
}
static int test_32values(int i)
{
uint32_t e = test_values_32[i];
size_t j;
int ret = 1;
for (j = 0; j < OSSL_NELEM(test_values_32); j++) {
uint32_t f = test_values_32[j];
if (!test_select_32(e, f))
ret = 0;
}
return ret;
}
static int test_64values(int i)
{
uint64_t g = test_values_64[i];
......@@ -330,9 +399,16 @@ static int test_64values(int i)
int setup_tests(void)
{
ADD_TEST(test_sizeofs);
ADD_ALL_TESTS(test_is_zero, OSSL_NELEM(test_values));
ADD_ALL_TESTS(test_is_zero_8, OSSL_NELEM(test_values_8));
ADD_ALL_TESTS(test_is_zero_32, OSSL_NELEM(test_values_32));
ADD_ALL_TESTS(test_is_zero_s, OSSL_NELEM(test_values_s));
ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values));
ADD_ALL_TESTS(test_binops_8, OSSL_NELEM(test_values_8));
ADD_ALL_TESTS(test_binops_s, OSSL_NELEM(test_values_s));
ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values));
ADD_ALL_TESTS(test_8values, OSSL_NELEM(test_values_8));
ADD_ALL_TESTS(test_32values, OSSL_NELEM(test_values_32));
ADD_ALL_TESTS(test_64values, OSSL_NELEM(test_values_64));
return 1;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册