]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/constant_time_test.c
2 * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
13 #include "internal/nelem.h"
14 #include "internal/constant_time.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;
24 static uint32_t CONSTTIME_TRUE_32
= (uint32_t)(~(uint32_t)0);
25 static uint32_t CONSTTIME_FALSE_32
= 0;
26 static uint64_t CONSTTIME_TRUE_64
= (uint64_t)(~(uint64_t)0);
27 static uint64_t CONSTTIME_FALSE_64
= 0;
29 static unsigned int test_values
[] = {
30 0, 1, 1024, 12345, 32000, UINT_MAX
/ 2 - 1,
31 UINT_MAX
/ 2, UINT_MAX
/ 2 + 1, UINT_MAX
- 1,
35 static unsigned char test_values_8
[] = {
36 0, 1, 2, 20, 32, 127, 128, 129, 255
39 static int signed_test_values
[] = {
40 0, 1, -1, 1024, -1024, 12345, -12345,
41 32000, -32000, INT_MAX
, INT_MIN
, INT_MAX
- 1,
45 static size_t test_values_s
[] = {
46 0, 1, 1024, 12345, 32000, SIZE_MAX
/ 2 - 1,
47 SIZE_MAX
/ 2, SIZE_MAX
/ 2 + 1, SIZE_MAX
- 1,
51 static uint32_t test_values_32
[] = {
52 0, 1, 1024, 12345, 32000, UINT32_MAX
/ 2, UINT32_MAX
/ 2 + 1,
53 UINT32_MAX
- 1, UINT32_MAX
56 static uint64_t test_values_64
[] = {
57 0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX
/ 2,
58 UINT64_MAX
/ 2 + 1, UINT64_MAX
- 1, UINT64_MAX
61 static int test_binary_op(unsigned int (*op
) (unsigned int a
, unsigned int b
),
62 const char *op_name
, unsigned int a
, unsigned int b
,
65 if (is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_TRUE
))
67 if (!is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_FALSE
))
72 static int test_binary_op_8(unsigned
73 char (*op
) (unsigned int a
, unsigned int b
),
74 const char *op_name
, unsigned int a
,
75 unsigned int b
, int is_true
)
77 if (is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_TRUE_8
))
79 if (!is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_FALSE_8
))
84 static int test_binary_op_s(size_t (*op
) (size_t a
, size_t b
),
85 const char *op_name
, size_t a
, size_t b
,
88 if (is_true
&& !TEST_size_t_eq(op(a
,b
), CONSTTIME_TRUE_S
))
90 if (!is_true
&& !TEST_uint_eq(op(a
,b
), CONSTTIME_FALSE_S
))
95 static int test_binary_op_64(uint64_t (*op
)(uint64_t a
, uint64_t b
),
96 const char *op_name
, uint64_t a
, uint64_t b
,
99 uint64_t c
= op(a
, b
);
101 if (is_true
&& c
!= CONSTTIME_TRUE_64
) {
102 TEST_error("TRUE %s op failed", op_name
);
103 BIO_printf(bio_err
, "a=%jx b=%jx\n", a
, b
);
105 } else if (!is_true
&& c
!= CONSTTIME_FALSE_64
) {
106 TEST_error("FALSE %s op failed", op_name
);
107 BIO_printf(bio_err
, "a=%jx b=%jx\n", a
, b
);
113 static int test_is_zero(int i
)
115 unsigned int a
= test_values
[i
];
117 if (a
== 0 && !TEST_uint_eq(constant_time_is_zero(a
), CONSTTIME_TRUE
))
119 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero(a
), CONSTTIME_FALSE
))
124 static int test_is_zero_8(int i
)
126 unsigned int a
= test_values_8
[i
];
128 if (a
== 0 && !TEST_uint_eq(constant_time_is_zero_8(a
), CONSTTIME_TRUE_8
))
130 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero_8(a
), CONSTTIME_FALSE_8
))
135 static int test_is_zero_32(int i
)
137 uint32_t a
= test_values_32
[i
];
139 if (a
== 0 && !TEST_true(constant_time_is_zero_32(a
) == CONSTTIME_TRUE_32
))
141 if (a
!= 0 && !TEST_true(constant_time_is_zero_32(a
) == CONSTTIME_FALSE_32
))
146 static int test_is_zero_s(int i
)
148 size_t a
= test_values_s
[i
];
150 if (a
== 0 && !TEST_size_t_eq(constant_time_is_zero_s(a
), CONSTTIME_TRUE_S
))
152 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero_s(a
), CONSTTIME_FALSE_S
))
157 static int test_select(unsigned int a
, unsigned int b
)
159 if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE
, a
, b
), a
))
161 if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE
, a
, b
), b
))
166 static int test_select_8(unsigned char a
, unsigned char b
)
168 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8
, a
, b
), a
))
170 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8
, a
, b
), b
))
175 static int test_select_32(uint32_t a
, uint32_t b
)
177 if (!TEST_true(constant_time_select_32(CONSTTIME_TRUE_32
, a
, b
) == a
))
179 if (!TEST_true(constant_time_select_32(CONSTTIME_FALSE_32
, a
, b
) == b
))
184 static int test_select_s(size_t a
, size_t b
)
186 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S
, a
, b
), a
))
188 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S
, a
, b
), b
))
193 static int test_select_64(uint64_t a
, uint64_t b
)
195 uint64_t selected
= constant_time_select_64(CONSTTIME_TRUE_64
, a
, b
);
198 TEST_error("test_select_64 TRUE failed");
199 BIO_printf(bio_err
, "a=%jx b=%jx got %jx wanted a\n", a
, b
, selected
);
202 selected
= constant_time_select_64(CONSTTIME_FALSE_64
, a
, b
);
204 BIO_printf(bio_err
, "a=%jx b=%jx got %jx wanted b\n", a
, b
, selected
);
210 static int test_select_int(int a
, int b
)
212 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE
, a
, b
), a
))
214 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE
, a
, b
), b
))
219 static int test_eq_int_8(int a
, int b
)
221 if (a
== b
&& !TEST_int_eq(constant_time_eq_int_8(a
, b
), CONSTTIME_TRUE_8
))
223 if (a
!= b
&& !TEST_int_eq(constant_time_eq_int_8(a
, b
), CONSTTIME_FALSE_8
))
228 static int test_eq_s(size_t a
, size_t b
)
230 if (a
== b
&& !TEST_size_t_eq(constant_time_eq_s(a
, b
), CONSTTIME_TRUE_S
))
232 if (a
!= b
&& !TEST_int_eq(constant_time_eq_s(a
, b
), CONSTTIME_FALSE_S
))
237 static int test_eq_int(int a
, int b
)
239 if (a
== b
&& !TEST_uint_eq(constant_time_eq_int(a
, b
), CONSTTIME_TRUE
))
241 if (a
!= b
&& !TEST_uint_eq(constant_time_eq_int(a
, b
), CONSTTIME_FALSE
))
246 static int test_sizeofs(void)
248 if (!TEST_uint_eq(OSSL_NELEM(test_values
), OSSL_NELEM(test_values_s
)))
253 static int test_binops(int i
)
255 unsigned int a
= test_values
[i
];
259 for (j
= 0; j
< (int)OSSL_NELEM(test_values
); ++j
) {
260 unsigned int b
= test_values
[j
];
262 if (!test_select(a
, b
)
263 || !test_binary_op(&constant_time_lt
, "ct_lt",
265 || !test_binary_op(&constant_time_lt
, "constant_time_lt",
267 || !test_binary_op(&constant_time_ge
, "constant_time_ge",
269 || !test_binary_op(&constant_time_ge
, "constant_time_ge",
271 || !test_binary_op(&constant_time_eq
, "constant_time_eq",
273 || !test_binary_op(&constant_time_eq
, "constant_time_eq",
280 static int test_binops_8(int i
)
282 unsigned int a
= test_values_8
[i
];
286 for (j
= 0; j
< (int)OSSL_NELEM(test_values_8
); ++j
) {
287 unsigned int b
= test_values_8
[j
];
289 if (!test_binary_op_8(&constant_time_lt_8
, "constant_time_lt_8",
291 || !test_binary_op_8(&constant_time_lt_8
, "constant_time_lt_8",
293 || !test_binary_op_8(&constant_time_ge_8
, "constant_time_ge_8",
295 || !test_binary_op_8(&constant_time_ge_8
, "constant_time_ge_8",
297 || !test_binary_op_8(&constant_time_eq_8
, "constant_time_eq_8",
299 || !test_binary_op_8(&constant_time_eq_8
, "constant_time_eq_8",
306 static int test_binops_s(int i
)
308 size_t a
= test_values_s
[i
];
312 for (j
= 0; j
< (int)OSSL_NELEM(test_values_s
); ++j
) {
313 size_t b
= test_values_s
[j
];
315 if (!test_select_s(a
, b
)
317 || !test_binary_op_s(&constant_time_lt_s
, "constant_time_lt_s",
319 || !test_binary_op_s(&constant_time_lt_s
, "constant_time_lt_s",
321 || !test_binary_op_s(&constant_time_ge_s
, "constant_time_ge_s",
323 || !test_binary_op_s(&constant_time_ge_s
, "constant_time_ge_s",
325 || !test_binary_op_s(&constant_time_eq_s
, "constant_time_eq_s",
327 || !test_binary_op_s(&constant_time_eq_s
, "constant_time_eq_s",
334 static int test_signed(int i
)
336 int c
= signed_test_values
[i
];
340 for (j
= 0; j
< OSSL_NELEM(signed_test_values
); ++j
) {
341 int d
= signed_test_values
[j
];
343 if (!test_select_int(c
, d
)
344 || !test_eq_int(c
, d
)
345 || !test_eq_int_8(c
, d
))
351 static int test_8values(int i
)
353 unsigned char e
= test_values_8
[i
];
357 for (j
= 0; j
< sizeof(test_values_8
); ++j
) {
358 unsigned char f
= test_values_8
[j
];
360 if (!test_select_8(e
, f
))
366 static int test_32values(int i
)
368 uint32_t e
= test_values_32
[i
];
372 for (j
= 0; j
< OSSL_NELEM(test_values_32
); j
++) {
373 uint32_t f
= test_values_32
[j
];
375 if (!test_select_32(e
, f
))
381 static int test_64values(int i
)
383 uint64_t g
= test_values_64
[i
];
386 for (j
= i
+ 1; j
< (int)OSSL_NELEM(test_values_64
); j
++) {
387 uint64_t h
= test_values_64
[j
];
389 if (!test_binary_op_64(&constant_time_lt_64
, "constant_time_lt_64",
391 || !test_select_64(g
, h
)) {
392 TEST_info("test_64values failed i=%d j=%d", i
, j
);
399 int setup_tests(void)
401 ADD_TEST(test_sizeofs
);
402 ADD_ALL_TESTS(test_is_zero
, OSSL_NELEM(test_values
));
403 ADD_ALL_TESTS(test_is_zero_8
, OSSL_NELEM(test_values_8
));
404 ADD_ALL_TESTS(test_is_zero_32
, OSSL_NELEM(test_values_32
));
405 ADD_ALL_TESTS(test_is_zero_s
, OSSL_NELEM(test_values_s
));
406 ADD_ALL_TESTS(test_binops
, OSSL_NELEM(test_values
));
407 ADD_ALL_TESTS(test_binops_8
, OSSL_NELEM(test_values_8
));
408 ADD_ALL_TESTS(test_binops_s
, OSSL_NELEM(test_values_s
));
409 ADD_ALL_TESTS(test_signed
, OSSL_NELEM(signed_test_values
));
410 ADD_ALL_TESTS(test_8values
, OSSL_NELEM(test_values_8
));
411 ADD_ALL_TESTS(test_32values
, OSSL_NELEM(test_values_32
));
412 ADD_ALL_TESTS(test_64values
, OSSL_NELEM(test_values_64
));