2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include "internal/constant_time_locl.h"
16 #include "internal/numbers.h"
18 static const unsigned int CONSTTIME_TRUE = (unsigned)(~0);
19 static const unsigned int CONSTTIME_FALSE = 0;
20 static const unsigned char CONSTTIME_TRUE_8 = 0xff;
21 static const unsigned char CONSTTIME_FALSE_8 = 0;
22 static const size_t CONSTTIME_TRUE_S = ~((size_t)0);
23 static const size_t CONSTTIME_FALSE_S = 0;
25 static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b),
26 const char *op_name, unsigned int a, unsigned int b,
29 if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE))
31 if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE))
36 static int test_binary_op_8(unsigned
37 char (*op) (unsigned int a, unsigned int b),
38 const char *op_name, unsigned int a,
39 unsigned int b, int is_true)
41 if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8))
43 if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8))
48 static int test_binary_op_s(size_t (*op) (size_t a, size_t b),
49 const char *op_name, size_t a, size_t b,
52 if (is_true && !TEST_size_t_eq(op(a,b), CONSTTIME_TRUE_S))
54 if (!is_true && !TEST_uint_eq(op(a,b), CONSTTIME_FALSE_S))
59 static int test_is_zero(unsigned int a)
61 if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE))
63 if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE))
68 static int test_is_zero_8(unsigned int a)
70 if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))
72 if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))
77 static int test_is_zero_s(unsigned int a)
79 if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S))
81 if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S))
86 static int test_select(unsigned int a, unsigned int b)
88 if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a))
90 if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b))
95 static int test_select_8(unsigned char a, unsigned char b)
97 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a))
99 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b))
104 static int test_select_s(unsigned char a, unsigned char b)
106 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a))
108 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b))
113 static int test_select_int(int a, int b)
115 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a))
117 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b))
122 static int test_eq_int_8(int a, int b)
124 if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8))
126 if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8))
131 static int test_eq_s(size_t a, size_t b)
133 if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S))
135 if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S))
140 static int test_eq_int(int a, int b)
142 if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE))
144 if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE))
149 static unsigned int test_values[] =
150 { 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
151 UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
155 static unsigned char test_values_8[] =
156 { 0, 1, 2, 20, 32, 127, 128, 129, 255 };
158 static int signed_test_values[] = { 0, 1, -1, 1024, -1024, 12345, -12345,
159 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
163 static size_t test_values_s[] =
164 { 0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
165 SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
169 static int test_sizeofs(void)
171 if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s)))
176 static int test_binops(int i)
178 unsigned int a = test_values[i];
179 unsigned int g = test_values_s[i];
183 if (!test_is_zero(a) || !test_is_zero_8(a) || !test_is_zero_s(g))
186 for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) {
187 unsigned int b = test_values[j];
188 unsigned int h = test_values[j];
190 if (!test_select(a, b)
191 || !test_select_s(g, h)
193 || !test_binary_op(&constant_time_lt, "ct_lt",
195 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
197 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
199 || !test_binary_op(&constant_time_lt, "constant_time_lt",
201 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
203 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
205 || !test_binary_op(&constant_time_ge, "constant_time_ge",
207 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
209 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
211 || !test_binary_op(&constant_time_ge, "constant_time_ge",
213 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
215 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
217 || !test_binary_op(&constant_time_eq, "constant_time_eq",
219 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
221 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
223 || !test_binary_op(&constant_time_eq, "constant_time_eq",
225 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
227 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
235 static int test_signed(int i)
237 int c = signed_test_values[i];
241 for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) {
242 int d = signed_test_values[j];
244 if (!test_select_int(c, d)
245 || !test_eq_int(c, d)
246 || !test_eq_int_8(c, d))
252 static int test_8values(int i)
254 unsigned char e = test_values_8[i];
258 for (j = 0; j < sizeof(test_values_8); ++j) {
259 unsigned char f = test_values_8[j];
261 if (!test_select_8(e, f))
268 void register_tests(void)
270 ADD_TEST(test_sizeofs);
271 ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values));
272 ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values));
273 ADD_ALL_TESTS(test_8values, sizeof(test_values_8));