]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/constant_time_test.c
2 * Copyright 2014-2017 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
13 #include <internal/nelem.h>
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;
24 static uint64_t CONSTTIME_TRUE_64
= (uint64_t)(~(uint64_t)0);
25 static uint64_t CONSTTIME_FALSE_64
= 0;
27 static int test_binary_op(unsigned int (*op
) (unsigned int a
, unsigned int b
),
28 const char *op_name
, unsigned int a
, unsigned int b
,
31 if (is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_TRUE
))
33 if (!is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_FALSE
))
38 static int test_binary_op_8(unsigned
39 char (*op
) (unsigned int a
, unsigned int b
),
40 const char *op_name
, unsigned int a
,
41 unsigned int b
, int is_true
)
43 if (is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_TRUE_8
))
45 if (!is_true
&& !TEST_uint_eq(op(a
, b
), CONSTTIME_FALSE_8
))
50 static int test_binary_op_s(size_t (*op
) (size_t a
, size_t b
),
51 const char *op_name
, size_t a
, size_t b
,
54 if (is_true
&& !TEST_size_t_eq(op(a
,b
), CONSTTIME_TRUE_S
))
56 if (!is_true
&& !TEST_uint_eq(op(a
,b
), CONSTTIME_FALSE_S
))
61 static int test_binary_op_64(uint64_t (*op
)(uint64_t a
, uint64_t b
),
62 const char *op_name
, uint64_t a
, uint64_t b
,
65 uint64_t c
= op(a
, b
);
67 if (is_true
&& c
!= CONSTTIME_TRUE_64
) {
68 TEST_error("TRUE %s op failed", op_name
);
69 BIO_printf(bio_err
, "a=%jx b=%jx\n", a
, b
);
71 } else if (!is_true
&& c
!= CONSTTIME_FALSE_64
) {
72 TEST_error("FALSE %s op failed", op_name
);
73 BIO_printf(bio_err
, "a=%jx b=%jx\n", a
, b
);
80 static int test_is_zero(unsigned int a
)
82 if (a
== 0 && !TEST_uint_eq(constant_time_is_zero(a
), CONSTTIME_TRUE
))
84 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero(a
), CONSTTIME_FALSE
))
89 static int test_is_zero_8(unsigned int a
)
91 if (a
== 0 && !TEST_uint_eq(constant_time_is_zero_8(a
), CONSTTIME_TRUE_8
))
93 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero_8(a
), CONSTTIME_FALSE_8
))
98 static int test_is_zero_s(unsigned int a
)
100 if (a
== 0 && !TEST_size_t_eq(constant_time_is_zero_s(a
), CONSTTIME_TRUE_S
))
102 if (a
!= 0 && !TEST_uint_eq(constant_time_is_zero_s(a
), CONSTTIME_FALSE_S
))
107 static int test_select(unsigned int a
, unsigned int b
)
109 if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE
, a
, b
), a
))
111 if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE
, a
, b
), b
))
116 static int test_select_8(unsigned char a
, unsigned char b
)
118 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8
, a
, b
), a
))
120 if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8
, a
, b
), b
))
125 static int test_select_s(unsigned char a
, unsigned char b
)
127 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S
, a
, b
), a
))
129 if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S
, a
, b
), b
))
134 static int test_select_64(uint64_t a
, uint64_t b
)
136 uint64_t selected
= constant_time_select_64(CONSTTIME_TRUE_64
, a
, b
);
139 TEST_error("test_select_64 TRUE failed");
140 BIO_printf(bio_err
, "a=%jx b=%jx got %jx wanted a\n", a
, b
, selected
);
143 selected
= constant_time_select_64(CONSTTIME_FALSE_64
, a
, b
);
145 BIO_printf(bio_err
, "a=%jx b=%jx got %jx wanted b\n", a
, b
, selected
);
151 static int test_select_int(int a
, int b
)
153 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE
, a
, b
), a
))
155 if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE
, a
, b
), b
))
160 static int test_eq_int_8(int a
, int b
)
162 if (a
== b
&& !TEST_int_eq(constant_time_eq_int_8(a
, b
), CONSTTIME_TRUE_8
))
164 if (a
!= b
&& !TEST_int_eq(constant_time_eq_int_8(a
, b
), CONSTTIME_FALSE_8
))
169 static int test_eq_s(size_t a
, size_t b
)
171 if (a
== b
&& !TEST_size_t_eq(constant_time_eq_s(a
, b
), CONSTTIME_TRUE_S
))
173 if (a
!= b
&& !TEST_int_eq(constant_time_eq_s(a
, b
), CONSTTIME_FALSE_S
))
178 static int test_eq_int(int a
, int b
)
180 if (a
== b
&& !TEST_uint_eq(constant_time_eq_int(a
, b
), CONSTTIME_TRUE
))
182 if (a
!= b
&& !TEST_uint_eq(constant_time_eq_int(a
, b
), CONSTTIME_FALSE
))
187 static unsigned int test_values
[] = {
188 0, 1, 1024, 12345, 32000, UINT_MAX
/ 2 - 1,
189 UINT_MAX
/ 2, UINT_MAX
/ 2 + 1, UINT_MAX
- 1,
193 static unsigned char test_values_8
[] = {
194 0, 1, 2, 20, 32, 127, 128, 129, 255
197 static int signed_test_values
[] = {
198 0, 1, -1, 1024, -1024, 12345, -12345,
199 32000, -32000, INT_MAX
, INT_MIN
, INT_MAX
- 1,
203 static size_t test_values_s
[] = {
204 0, 1, 1024, 12345, 32000, SIZE_MAX
/ 2 - 1,
205 SIZE_MAX
/ 2, SIZE_MAX
/ 2 + 1, SIZE_MAX
- 1,
209 static uint64_t test_values_64
[] = {
210 0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX
/ 2,
211 UINT64_MAX
/ 2 + 1, UINT64_MAX
- 1, UINT64_MAX
214 static int test_sizeofs(void)
216 if (!TEST_uint_eq(OSSL_NELEM(test_values
), OSSL_NELEM(test_values_s
)))
221 static int test_binops(int i
)
223 unsigned int a
= test_values
[i
];
224 unsigned int g
= test_values_s
[i
];
228 if (!test_is_zero(a
) || !test_is_zero_8(a
) || !test_is_zero_s(g
))
231 for (j
= 0; j
< (int)OSSL_NELEM(test_values
); ++j
) {
232 unsigned int b
= test_values
[j
];
233 unsigned int h
= test_values
[j
];
235 if (!test_select(a
, b
)
236 || !test_select_s(g
, h
)
238 || !test_binary_op(&constant_time_lt
, "ct_lt",
240 || !test_binary_op_8(&constant_time_lt_8
, "constant_time_lt_8",
242 || !test_binary_op_s(&constant_time_lt_s
, "constant_time_lt_s",
244 || !test_binary_op(&constant_time_lt
, "constant_time_lt",
246 || !test_binary_op_8(&constant_time_lt_8
, "constant_time_lt_8",
248 || !test_binary_op_s(&constant_time_lt_s
, "constant_time_lt_s",
250 || !test_binary_op(&constant_time_ge
, "constant_time_ge",
252 || !test_binary_op_8(&constant_time_ge_8
, "constant_time_ge_8",
254 || !test_binary_op_s(&constant_time_ge_s
, "constant_time_ge_s",
256 || !test_binary_op(&constant_time_ge
, "constant_time_ge",
258 || !test_binary_op_8(&constant_time_ge_8
, "constant_time_ge_8",
260 || !test_binary_op_s(&constant_time_ge_s
, "constant_time_ge_s",
262 || !test_binary_op(&constant_time_eq
, "constant_time_eq",
264 || !test_binary_op_8(&constant_time_eq_8
, "constant_time_eq_8",
266 || !test_binary_op_s(&constant_time_eq_s
, "constant_time_eq_s",
268 || !test_binary_op(&constant_time_eq
, "constant_time_eq",
270 || !test_binary_op_8(&constant_time_eq_8
, "constant_time_eq_8",
272 || !test_binary_op_s(&constant_time_eq_s
, "constant_time_eq_s",
280 static int test_signed(int i
)
282 int c
= signed_test_values
[i
];
286 for (j
= 0; j
< OSSL_NELEM(signed_test_values
); ++j
) {
287 int d
= signed_test_values
[j
];
289 if (!test_select_int(c
, d
)
290 || !test_eq_int(c
, d
)
291 || !test_eq_int_8(c
, d
))
297 static int test_8values(int i
)
299 unsigned char e
= test_values_8
[i
];
303 for (j
= 0; j
< sizeof(test_values_8
); ++j
) {
304 unsigned char f
= test_values_8
[j
];
306 if (!test_select_8(e
, f
))
312 static int test_64values(int i
)
314 uint64_t g
= test_values_64
[i
];
317 for (j
= i
+ 1; j
< (int)OSSL_NELEM(test_values_64
); j
++) {
318 uint64_t h
= test_values_64
[j
];
320 if (!test_binary_op_64(&constant_time_lt_64
, "constant_time_lt_64",
322 || !test_select_64(g
, h
)) {
323 TEST_info("test_64values failed i=%d j=%d", i
, j
);
330 int setup_tests(void)
332 ADD_TEST(test_sizeofs
);
333 ADD_ALL_TESTS(test_binops
, OSSL_NELEM(test_values
));
334 ADD_ALL_TESTS(test_signed
, OSSL_NELEM(signed_test_values
));
335 ADD_ALL_TESTS(test_8values
, OSSL_NELEM(test_values_8
));
336 ADD_ALL_TESTS(test_64values
, OSSL_NELEM(test_values_64
));