Remove unused constant-time utilities.

This commit is contained in:
Brian Smith 2016-03-16 23:11:58 -10:00
parent f4851f4322
commit 7c8e9fe403
2 changed files with 0 additions and 148 deletions

View File

@ -109,24 +109,6 @@ static int test_is_zero(unsigned int a) {
return 0;
}
static int test_is_zero_8(unsigned int a) {
uint8_t c = constant_time_is_zero_8(a);
if (a == 0 && c != CONSTTIME_TRUE_8) {
fprintf(stderr,
"Test failed for constant_time_is_zero(%du): expected %u (TRUE), "
"got %u\n",
a, CONSTTIME_TRUE_8, c);
return 1;
} else if (a != 0 && c != CONSTTIME_FALSE) {
fprintf(stderr,
"Test failed for constant_time_is_zero(%du): expected %u (FALSE), "
"got %u\n",
a, CONSTTIME_FALSE_8, c);
return 1;
}
return 0;
}
static int test_select(unsigned int a, unsigned int b) {
unsigned int selected = constant_time_select(CONSTTIME_TRUE, a, b);
if (selected != a) {
@ -147,46 +129,6 @@ static int test_select(unsigned int a, unsigned int b) {
return 0;
}
static int test_select_8(uint8_t a, uint8_t b) {
uint8_t selected = constant_time_select_8(CONSTTIME_TRUE_8, a, b);
if (selected != a) {
fprintf(stderr,
"Test failed for constant_time_select(%u, %u,"
"%u): expected %u(first value), got %u\n",
CONSTTIME_TRUE, a, b, a, selected);
return 1;
}
selected = constant_time_select_8(CONSTTIME_FALSE_8, a, b);
if (selected != b) {
fprintf(stderr,
"Test failed for constant_time_select(%u, %u,"
"%u): expected %u(second value), got %u\n",
CONSTTIME_FALSE, a, b, b, selected);
return 1;
}
return 0;
}
static int test_select_int(int a, int b) {
int selected = constant_time_select_int(CONSTTIME_TRUE, a, b);
if (selected != a) {
fprintf(stderr,
"Test failed for constant_time_select(%du, %d,"
"%d): expected %d(first value), got %d\n",
CONSTTIME_TRUE, a, b, a, selected);
return 1;
}
selected = constant_time_select_int(CONSTTIME_FALSE, a, b);
if (selected != b) {
fprintf(stderr,
"Test failed for constant_time_select(%du, %d,"
"%d): expected %d(second value), got %d\n",
CONSTTIME_FALSE, a, b, b, selected);
return 1;
}
return 0;
}
static int test_eq_int(int a, int b) {
unsigned int equal = constant_time_eq_int(a, b);
if (a == b && equal != CONSTTIME_TRUE) {
@ -205,24 +147,6 @@ static int test_eq_int(int a, int b) {
return 0;
}
static int test_eq_int_8(int a, int b) {
uint8_t equal = constant_time_eq_int_8(a, b);
if (a == b && equal != CONSTTIME_TRUE_8) {
fprintf(stderr,
"Test failed for constant_time_eq_int_8(%d, %d): expected "
"%u(TRUE), got %u\n",
a, b, CONSTTIME_TRUE_8, equal);
return 1;
} else if (a != b && equal != CONSTTIME_FALSE_8) {
fprintf(stderr,
"Test failed for constant_time_eq_int_8(%d, %d): expected "
"%u(FALSE), got %u\n",
a, b, CONSTTIME_FALSE_8, equal);
return 1;
}
return 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};
@ -236,40 +160,18 @@ static int signed_test_values[] = {
int main(void) {
unsigned int a, b, i, j;
int c, d;
uint8_t e, f;
int num_failed = 0, num_all = 0;
for (i = 0; i < sizeof(test_values) / sizeof(int); ++i) {
a = test_values[i];
num_failed += test_is_zero(a);
num_failed += test_is_zero_8(a);
num_all += 2;
for (j = 0; j < sizeof(test_values) / sizeof(int); ++j) {
b = test_values[j];
num_failed +=
test_binary_op(&constant_time_lt, "constant_time_lt", a, b, a < b);
num_failed += test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
a, b, a < b);
num_failed +=
test_binary_op(&constant_time_lt, "constant_time_lt_8", b, a, b < a);
num_failed += test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
b, a, b < a);
num_failed +=
test_binary_op(&constant_time_ge, "constant_time_ge", a, b, a >= b);
num_failed += test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
a, b, a >= b);
num_failed +=
test_binary_op(&constant_time_ge, "constant_time_ge", b, a, b >= a);
num_failed += test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
b, a, b >= a);
num_failed +=
test_binary_op(&constant_time_eq, "constant_time_eq", a, b, a == b);
num_failed += test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
a, b, a == b);
num_failed +=
test_binary_op(&constant_time_eq, "constant_time_eq", b, a, b == a);
num_failed += test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
b, a, b == a);
num_failed += test_select(a, b);
num_all += 13;
}
@ -279,22 +181,11 @@ int main(void) {
c = signed_test_values[i];
for (j = 0; j < sizeof(signed_test_values) / sizeof(int); ++j) {
d = signed_test_values[j];
num_failed += test_select_int(c, d);
num_failed += test_eq_int(c, d);
num_failed += test_eq_int_8(c, d);
num_all += 3;
}
}
for (i = 0; i < sizeof(test_values_8); ++i) {
e = test_values_8[i];
for (j = 0; j < sizeof(test_values_8); ++j) {
f = test_values_8[j];
num_failed += test_select_8(e, f);
num_all += 1;
}
}
if (!num_failed) {
return EXIT_SUCCESS;
} else {

View File

@ -248,21 +248,12 @@ static inline unsigned int constant_time_lt(unsigned int a, unsigned int b) {
return constant_time_msb(a^((a^b)|((a-b)^a)));
}
/* constant_time_lt_8 acts like |constant_time_lt| but returns an 8-bit mask. */
static inline uint8_t constant_time_lt_8(unsigned int a, unsigned int b) {
return (uint8_t)(constant_time_lt(a, b));
}
/* constant_time_gt returns 0xff..f if a >= b and 0 otherwise. */
static inline unsigned int constant_time_ge(unsigned int a, unsigned int b) {
return ~constant_time_lt(a, b);
}
/* constant_time_ge_8 acts like |constant_time_ge| but returns an 8-bit mask. */
static inline uint8_t constant_time_ge_8(unsigned int a, unsigned int b) {
return (uint8_t)(constant_time_ge(a, b));
}
/* constant_time_is_zero returns 0xff..f if a == 0 and 0 otherwise. */
static inline unsigned int constant_time_is_zero(unsigned int a) {
/* Here is an SMT-LIB verification of this formula:
@ -280,33 +271,16 @@ static inline unsigned int constant_time_is_zero(unsigned int a) {
return constant_time_msb(~a & (a - 1));
}
/* constant_time_is_zero_8 acts like constant_time_is_zero but returns an 8-bit
* mask. */
static inline uint8_t constant_time_is_zero_8(unsigned int a) {
return (uint8_t)(constant_time_is_zero(a));
}
/* constant_time_eq returns 0xff..f if a == b and 0 otherwise. */
static inline unsigned int constant_time_eq(unsigned int a, unsigned int b) {
return constant_time_is_zero(a ^ b);
}
/* constant_time_eq_8 acts like |constant_time_eq| but returns an 8-bit mask. */
static inline uint8_t constant_time_eq_8(unsigned int a, unsigned int b) {
return (uint8_t)(constant_time_eq(a, b));
}
/* constant_time_eq_int acts like |constant_time_eq| but works on int values. */
static inline unsigned int constant_time_eq_int(int a, int b) {
return constant_time_eq((unsigned)(a), (unsigned)(b));
}
/* constant_time_eq_int_8 acts like |constant_time_eq_int| but returns an 8-bit
* mask. */
static inline uint8_t constant_time_eq_int_8(int a, int b) {
return constant_time_eq_8((unsigned)(a), (unsigned)(b));
}
/* constant_time_select returns (mask & a) | (~mask & b). When |mask| is all 1s
* or all 0s (as returned by the methods above), the select methods return
* either |a| (if |mask| is nonzero) or |b| (if |mask| is zero). */
@ -315,19 +289,6 @@ static inline unsigned int constant_time_select(unsigned int mask,
return (mask & a) | (~mask & b);
}
/* constant_time_select_8 acts like |constant_time_select| but operates on
* 8-bit values. */
static inline uint8_t constant_time_select_8(uint8_t mask, uint8_t a,
uint8_t b) {
return (uint8_t)(constant_time_select(mask, a, b));
}
/* constant_time_select_int acts like |constant_time_select| but operates on
* ints. */
static inline int constant_time_select_int(unsigned int mask, int a, int b) {
return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
}
/* Thread-safe initialisation. */