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
10 #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 unsigned c = op(a, b);
30 if (is_true && c != CONSTTIME_TRUE) {
31 fprintf(stderr, "Test failed for %s(%du, %du): expected %du "
32 "(TRUE), got %du\n", op_name, a, b, CONSTTIME_TRUE, c);
34 } else if (!is_true && c != CONSTTIME_FALSE) {
35 fprintf(stderr, "Test failed for %s(%du, %du): expected %du "
36 "(FALSE), got %du\n", op_name, a, b, CONSTTIME_FALSE, c);
42 static int test_binary_op_8(unsigned
43 char (*op) (unsigned int a, unsigned int b),
44 const char *op_name, unsigned int a,
45 unsigned int b, int is_true)
47 unsigned char c = op(a, b);
48 if (is_true && c != CONSTTIME_TRUE_8) {
49 fprintf(stderr, "Test failed for %s(%du, %du): expected %u "
50 "(TRUE), got %u\n", op_name, a, b, CONSTTIME_TRUE_8, c);
52 } else if (!is_true && c != CONSTTIME_FALSE_8) {
53 fprintf(stderr, "Test failed for %s(%du, %du): expected %u "
54 "(FALSE), got %u\n", op_name, a, b, CONSTTIME_FALSE_8, c);
60 static int test_binary_op_s(size_t (*op) (size_t a, size_t b),
61 const char *op_name, size_t a, size_t b,
65 if (is_true && c != CONSTTIME_TRUE_S) {
66 fprintf(stderr, "Test failed for %s(%"OSSLzu", %"OSSLzu
67 "): expected %"OSSLzu" (TRUE), got %"OSSLzu"\n",
68 op_name, a, b, CONSTTIME_TRUE_S, c);
70 } else if (!is_true && c != CONSTTIME_FALSE_S) {
71 fprintf(stderr, "Test failed for %s(%"OSSLzu", %"OSSLzu
72 "): expected %" OSSLzu " (FALSE), got %"OSSLzu"\n",
73 op_name, a, b, CONSTTIME_FALSE_S, c);
79 static int test_is_zero(unsigned int a)
81 unsigned int c = constant_time_is_zero(a);
82 if (a == 0 && c != CONSTTIME_TRUE) {
83 fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
84 "expected %du (TRUE), got %du\n", a, CONSTTIME_TRUE, c);
86 } else if (a != 0 && c != CONSTTIME_FALSE) {
87 fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
88 "expected %du (FALSE), got %du\n", a, CONSTTIME_FALSE, c);
94 static int test_is_zero_8(unsigned int a)
96 unsigned char c = constant_time_is_zero_8(a);
97 if (a == 0 && c != CONSTTIME_TRUE_8) {
98 fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
99 "expected %u (TRUE), got %u\n", a, CONSTTIME_TRUE_8, c);
101 } else if (a != 0 && c != CONSTTIME_FALSE) {
102 fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
103 "expected %u (FALSE), got %u\n", a, CONSTTIME_FALSE_8, c);
109 static int test_is_zero_s(size_t a)
111 size_t c = constant_time_is_zero_s(a);
112 if (a == 0 && c != CONSTTIME_TRUE_S) {
113 fprintf(stderr, "Test failed for constant_time_is_zero_s(%"OSSLzu"): "
114 "expected %"OSSLzu" (TRUE), got %"OSSLzu"\n",
115 a, CONSTTIME_TRUE_S, c);
117 } else if (a != 0 && c != CONSTTIME_FALSE) {
118 fprintf(stderr, "Test failed for constant_time_is_zero_s(%"OSSLzu"): "
119 "expected %"OSSLzu" (FALSE), got %"OSSLzu"\n",
120 a, CONSTTIME_FALSE_S, c);
125 static int test_select(unsigned int a, unsigned int b)
127 unsigned int selected = constant_time_select(CONSTTIME_TRUE, a, b);
129 fprintf(stderr, "Test failed for constant_time_select(%du, %du,"
130 "%du): expected %du(first value), got %du\n",
131 CONSTTIME_TRUE, a, b, a, selected);
134 selected = constant_time_select(CONSTTIME_FALSE, a, b);
136 fprintf(stderr, "Test failed for constant_time_select(%du, %du,"
137 "%du): expected %du(second value), got %du\n",
138 CONSTTIME_FALSE, a, b, b, selected);
144 static int test_select_8(unsigned char a, unsigned char b)
146 unsigned char selected = constant_time_select_8(CONSTTIME_TRUE_8, a, b);
148 fprintf(stderr, "Test failed for constant_time_select(%u, %u,"
149 "%u): expected %u(first value), got %u\n",
150 CONSTTIME_TRUE, a, b, a, selected);
153 selected = constant_time_select_8(CONSTTIME_FALSE_8, a, b);
155 fprintf(stderr, "Test failed for constant_time_select(%u, %u,"
156 "%u): expected %u(second value), got %u\n",
157 CONSTTIME_FALSE, a, b, b, selected);
163 static int test_select_int(int a, int b)
165 int selected = constant_time_select_int(CONSTTIME_TRUE, a, b);
167 fprintf(stderr, "Test failed for constant_time_select(%du, %d,"
168 "%d): expected %d(first value), got %d\n",
169 CONSTTIME_TRUE, a, b, a, selected);
172 selected = constant_time_select_int(CONSTTIME_FALSE, a, b);
174 fprintf(stderr, "Test failed for constant_time_select(%du, %d,"
175 "%d): expected %d(second value), got %d\n",
176 CONSTTIME_FALSE, a, b, b, selected);
183 static int test_select_s(size_t a, size_t b)
185 size_t selected = constant_time_select_s(CONSTTIME_TRUE_S, a, b);
187 fprintf(stderr, "Test failed for constant_time_select_s(%"OSSLzu
188 ", %"OSSLzu",%"OSSLzu"): expected %"OSSLzu
189 "(first value), got %"OSSLzu"\n",
190 CONSTTIME_TRUE_S, a, b, a, selected);
193 selected = constant_time_select_s(CONSTTIME_FALSE_S, a, b);
195 fprintf(stderr, "Test failed for constant_time_select_s(%"OSSLzu
196 ", %"OSSLzu",%"OSSLzu"): expected %"OSSLzu
197 "(second value), got %"OSSLzu"\n",
198 CONSTTIME_FALSE_S, a, b, b, selected);
204 static int test_eq_int(int a, int b)
206 unsigned int equal = constant_time_eq_int(a, b);
207 if (a == b && equal != CONSTTIME_TRUE) {
208 fprintf(stderr, "Test failed for constant_time_eq_int(%d, %d): "
209 "expected %du(TRUE), got %du\n", a, b, CONSTTIME_TRUE, equal);
211 } else if (a != b && equal != CONSTTIME_FALSE) {
212 fprintf(stderr, "Test failed for constant_time_eq_int(%d, %d): "
213 "expected %du(FALSE), got %du\n",
214 a, b, CONSTTIME_FALSE, equal);
220 static int test_eq_int_8(int a, int b)
222 unsigned char equal = constant_time_eq_int_8(a, b);
223 if (a == b && equal != CONSTTIME_TRUE_8) {
224 fprintf(stderr, "Test failed for constant_time_eq_int_8(%d, %d): "
225 "expected %u(TRUE), got %u\n", a, b, CONSTTIME_TRUE_8, equal);
227 } else if (a != b && equal != CONSTTIME_FALSE_8) {
228 fprintf(stderr, "Test failed for constant_time_eq_int_8(%d, %d): "
229 "expected %u(FALSE), got %u\n",
230 a, b, CONSTTIME_FALSE_8, equal);
236 static int test_eq_s(size_t a, size_t b)
238 size_t equal = constant_time_eq_s(a, b);
239 if (a == b && equal != CONSTTIME_TRUE_S) {
240 fprintf(stderr, "Test failed for constant_time_eq_int(%"OSSLzu
241 ", %"OSSLzu"): expected %"OSSLzu"(TRUE), got %"OSSLzu"\n",
242 a, b, CONSTTIME_TRUE_S, equal);
244 } else if (a != b && equal != CONSTTIME_FALSE_S) {
245 fprintf(stderr, "Test failed for constant_time_eq_int(%"OSSLzu", %"
246 OSSLzu"): expected %"OSSLzu"(FALSE), got %"OSSLzu"\n",
247 a, b, CONSTTIME_FALSE_S, equal);
253 static unsigned int test_values[] =
254 { 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
255 UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
259 static unsigned char test_values_8[] =
260 { 0, 1, 2, 20, 32, 127, 128, 129, 255 };
262 static int signed_test_values[] = { 0, 1, -1, 1024, -1024, 12345, -12345,
263 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
267 static size_t test_values_s[] =
268 { 0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
269 SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
273 int main(int argc, char *argv[])
275 unsigned int a, b, i, j;
279 int num_failed = 0, num_all = 0;
280 fprintf(stdout, "Testing constant time operations...\n");
282 if (OSSL_NELEM(test_values) != OSSL_NELEM(test_values_s)) {
283 fprintf(stdout, "Unexpected number of tests\n");
287 for (i = 0; i < OSSL_NELEM(test_values); ++i) {
289 g = test_values_s[i];
290 num_failed += test_is_zero(a);
291 num_failed += test_is_zero_8(a);
292 num_failed += test_is_zero_s(g);
294 for (j = 0; j < OSSL_NELEM(test_values); ++j) {
297 num_failed += test_binary_op(&constant_time_lt,
298 "constant_time_lt", a, b, a < b);
299 num_failed += test_binary_op_8(&constant_time_lt_8,
300 "constant_time_lt_8", a, b, a < b);
301 num_failed += test_binary_op_s(&constant_time_lt_s,
302 "constant_time_lt_s", g, h, g < h);
303 num_failed += test_binary_op(&constant_time_lt,
304 "constant_time_lt", b, a, b < a);
305 num_failed += test_binary_op_8(&constant_time_lt_8,
306 "constant_time_lt_8", b, a, b < a);
307 num_failed += test_binary_op_s(&constant_time_lt_s,
308 "constant_time_lt_s", h, g, h < g);
309 num_failed += test_binary_op(&constant_time_ge,
310 "constant_time_ge", a, b, a >= b);
311 num_failed += test_binary_op_8(&constant_time_ge_8,
312 "constant_time_ge_8", a, b,
314 num_failed += test_binary_op_s(&constant_time_ge_s,
315 "constant_time_ge_s", g, h, g >= h);
317 test_binary_op(&constant_time_ge, "constant_time_ge", b, a,
320 test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", b,
323 test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s", h, g,
326 test_binary_op(&constant_time_eq, "constant_time_eq", a, b,
329 test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", a,
332 test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", g, h,
335 test_binary_op(&constant_time_eq, "constant_time_eq", b, a,
338 test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", b,
341 test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s", h, g,
343 num_failed += test_select(a, b);
344 num_failed += test_select_s(g, h);
345 num_failed += test_eq_s(g, h);
350 for (i = 0; i < OSSL_NELEM(signed_test_values); ++i) {
351 c = signed_test_values[i];
352 for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) {
353 d = signed_test_values[j];
354 num_failed += test_select_int(c, d);
355 num_failed += test_eq_int(c, d);
356 num_failed += test_eq_int_8(c, d);
361 for (i = 0; i < sizeof(test_values_8); ++i) {
362 e = test_values_8[i];
363 for (j = 0; j < sizeof(test_values_8); ++j) {
364 f = test_values_8[j];
365 num_failed += test_select_8(e, f);
371 fprintf(stdout, "success (ran %d tests)\n", num_all);
374 fprintf(stdout, "%d of %d tests failed!\n", num_failed, num_all);