1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2014-2018 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <stdlib.h>
12*e0c4386eSCy Schubert 
13*e0c4386eSCy Schubert #include "internal/nelem.h"
14*e0c4386eSCy Schubert #include "internal/constant_time.h"
15*e0c4386eSCy Schubert #include "testutil.h"
16*e0c4386eSCy Schubert #include "internal/numbers.h"
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert static const unsigned int CONSTTIME_TRUE = (unsigned)(~0);
19*e0c4386eSCy Schubert static const unsigned int CONSTTIME_FALSE = 0;
20*e0c4386eSCy Schubert static const unsigned char CONSTTIME_TRUE_8 = 0xff;
21*e0c4386eSCy Schubert static const unsigned char CONSTTIME_FALSE_8 = 0;
22*e0c4386eSCy Schubert static const size_t CONSTTIME_TRUE_S = ~((size_t)0);
23*e0c4386eSCy Schubert static const size_t CONSTTIME_FALSE_S = 0;
24*e0c4386eSCy Schubert static uint32_t CONSTTIME_TRUE_32 = (uint32_t)(~(uint32_t)0);
25*e0c4386eSCy Schubert static uint32_t CONSTTIME_FALSE_32 = 0;
26*e0c4386eSCy Schubert static uint64_t CONSTTIME_TRUE_64 = (uint64_t)(~(uint64_t)0);
27*e0c4386eSCy Schubert static uint64_t CONSTTIME_FALSE_64 = 0;
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert static unsigned int test_values[] = {
30*e0c4386eSCy Schubert     0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
31*e0c4386eSCy Schubert     UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
32*e0c4386eSCy Schubert     UINT_MAX
33*e0c4386eSCy Schubert };
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert static unsigned char test_values_8[] = {
36*e0c4386eSCy Schubert     0, 1, 2, 20, 32, 127, 128, 129, 255
37*e0c4386eSCy Schubert };
38*e0c4386eSCy Schubert 
39*e0c4386eSCy Schubert static int signed_test_values[] = {
40*e0c4386eSCy Schubert     0, 1, -1, 1024, -1024, 12345, -12345,
41*e0c4386eSCy Schubert     32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
42*e0c4386eSCy Schubert     INT_MIN + 1
43*e0c4386eSCy Schubert };
44*e0c4386eSCy Schubert 
45*e0c4386eSCy Schubert static size_t test_values_s[] = {
46*e0c4386eSCy Schubert     0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
47*e0c4386eSCy Schubert     SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
48*e0c4386eSCy Schubert     SIZE_MAX
49*e0c4386eSCy Schubert };
50*e0c4386eSCy Schubert 
51*e0c4386eSCy Schubert static uint32_t test_values_32[] = {
52*e0c4386eSCy Schubert     0, 1, 1024, 12345, 32000, UINT32_MAX / 2, UINT32_MAX / 2 + 1,
53*e0c4386eSCy Schubert     UINT32_MAX - 1, UINT32_MAX
54*e0c4386eSCy Schubert };
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert static uint64_t test_values_64[] = {
57*e0c4386eSCy Schubert     0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2,
58*e0c4386eSCy Schubert     UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX
59*e0c4386eSCy Schubert };
60*e0c4386eSCy Schubert 
test_binary_op(unsigned int (* op)(unsigned int a,unsigned int b),const char * op_name,unsigned int a,unsigned int b,int is_true)61*e0c4386eSCy Schubert static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b),
62*e0c4386eSCy Schubert                           const char *op_name, unsigned int a, unsigned int b,
63*e0c4386eSCy Schubert                           int is_true)
64*e0c4386eSCy Schubert {
65*e0c4386eSCy Schubert     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE))
66*e0c4386eSCy Schubert         return 0;
67*e0c4386eSCy Schubert     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE))
68*e0c4386eSCy Schubert         return 0;
69*e0c4386eSCy Schubert     return 1;
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert 
test_binary_op_8(unsigned char (* op)(unsigned int a,unsigned int b),const char * op_name,unsigned int a,unsigned int b,int is_true)72*e0c4386eSCy Schubert static int test_binary_op_8(unsigned
73*e0c4386eSCy Schubert                             char (*op) (unsigned int a, unsigned int b),
74*e0c4386eSCy Schubert                             const char *op_name, unsigned int a,
75*e0c4386eSCy Schubert                             unsigned int b, int is_true)
76*e0c4386eSCy Schubert {
77*e0c4386eSCy Schubert     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8))
78*e0c4386eSCy Schubert         return 0;
79*e0c4386eSCy Schubert     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8))
80*e0c4386eSCy Schubert         return 0;
81*e0c4386eSCy Schubert     return 1;
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert 
test_binary_op_s(size_t (* op)(size_t a,size_t b),const char * op_name,size_t a,size_t b,int is_true)84*e0c4386eSCy Schubert static int test_binary_op_s(size_t (*op) (size_t a, size_t b),
85*e0c4386eSCy Schubert                             const char *op_name, size_t a, size_t b,
86*e0c4386eSCy Schubert                             int is_true)
87*e0c4386eSCy Schubert {
88*e0c4386eSCy Schubert     if (is_true && !TEST_size_t_eq(op(a,b), CONSTTIME_TRUE_S))
89*e0c4386eSCy Schubert         return 0;
90*e0c4386eSCy Schubert     if (!is_true && !TEST_uint_eq(op(a,b), CONSTTIME_FALSE_S))
91*e0c4386eSCy Schubert         return 0;
92*e0c4386eSCy Schubert     return 1;
93*e0c4386eSCy Schubert }
94*e0c4386eSCy Schubert 
test_binary_op_64(uint64_t (* op)(uint64_t a,uint64_t b),const char * op_name,uint64_t a,uint64_t b,int is_true)95*e0c4386eSCy Schubert static int test_binary_op_64(uint64_t (*op)(uint64_t a, uint64_t b),
96*e0c4386eSCy Schubert                              const char *op_name, uint64_t a, uint64_t b,
97*e0c4386eSCy Schubert                              int is_true)
98*e0c4386eSCy Schubert {
99*e0c4386eSCy Schubert     uint64_t c = op(a, b);
100*e0c4386eSCy Schubert 
101*e0c4386eSCy Schubert     if (is_true && c != CONSTTIME_TRUE_64) {
102*e0c4386eSCy Schubert         TEST_error("TRUE %s op failed", op_name);
103*e0c4386eSCy Schubert         BIO_printf(bio_err, "a=%jx b=%jx\n", a, b);
104*e0c4386eSCy Schubert         return 0;
105*e0c4386eSCy Schubert     } else if (!is_true && c != CONSTTIME_FALSE_64) {
106*e0c4386eSCy Schubert         TEST_error("FALSE %s op failed", op_name);
107*e0c4386eSCy Schubert         BIO_printf(bio_err, "a=%jx b=%jx\n", a, b);
108*e0c4386eSCy Schubert         return 0;
109*e0c4386eSCy Schubert     }
110*e0c4386eSCy Schubert     return 1;
111*e0c4386eSCy Schubert }
112*e0c4386eSCy Schubert 
test_is_zero(int i)113*e0c4386eSCy Schubert static int test_is_zero(int i)
114*e0c4386eSCy Schubert {
115*e0c4386eSCy Schubert     unsigned int a = test_values[i];
116*e0c4386eSCy Schubert 
117*e0c4386eSCy Schubert     if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE))
118*e0c4386eSCy Schubert         return 0;
119*e0c4386eSCy Schubert     if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE))
120*e0c4386eSCy Schubert         return 0;
121*e0c4386eSCy Schubert     return 1;
122*e0c4386eSCy Schubert }
123*e0c4386eSCy Schubert 
test_is_zero_8(int i)124*e0c4386eSCy Schubert static int test_is_zero_8(int i)
125*e0c4386eSCy Schubert {
126*e0c4386eSCy Schubert     unsigned int a = test_values_8[i];
127*e0c4386eSCy Schubert 
128*e0c4386eSCy Schubert     if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))
129*e0c4386eSCy Schubert         return 0;
130*e0c4386eSCy Schubert     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))
131*e0c4386eSCy Schubert         return 0;
132*e0c4386eSCy Schubert     return 1;
133*e0c4386eSCy Schubert }
134*e0c4386eSCy Schubert 
test_is_zero_32(int i)135*e0c4386eSCy Schubert static int test_is_zero_32(int i)
136*e0c4386eSCy Schubert {
137*e0c4386eSCy Schubert     uint32_t a = test_values_32[i];
138*e0c4386eSCy Schubert 
139*e0c4386eSCy Schubert     if (a == 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_TRUE_32))
140*e0c4386eSCy Schubert         return 0;
141*e0c4386eSCy Schubert     if (a != 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_FALSE_32))
142*e0c4386eSCy Schubert         return 0;
143*e0c4386eSCy Schubert     return 1;
144*e0c4386eSCy Schubert }
145*e0c4386eSCy Schubert 
test_is_zero_s(int i)146*e0c4386eSCy Schubert static int test_is_zero_s(int i)
147*e0c4386eSCy Schubert {
148*e0c4386eSCy Schubert     size_t a = test_values_s[i];
149*e0c4386eSCy Schubert 
150*e0c4386eSCy Schubert     if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S))
151*e0c4386eSCy Schubert         return 0;
152*e0c4386eSCy Schubert     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S))
153*e0c4386eSCy Schubert         return 0;
154*e0c4386eSCy Schubert     return 1;
155*e0c4386eSCy Schubert }
156*e0c4386eSCy Schubert 
test_select(unsigned int a,unsigned int b)157*e0c4386eSCy Schubert static int test_select(unsigned int a, unsigned int b)
158*e0c4386eSCy Schubert {
159*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a))
160*e0c4386eSCy Schubert         return 0;
161*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b))
162*e0c4386eSCy Schubert         return 0;
163*e0c4386eSCy Schubert     return 1;
164*e0c4386eSCy Schubert }
165*e0c4386eSCy Schubert 
test_select_8(unsigned char a,unsigned char b)166*e0c4386eSCy Schubert static int test_select_8(unsigned char a, unsigned char b)
167*e0c4386eSCy Schubert {
168*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a))
169*e0c4386eSCy Schubert         return 0;
170*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b))
171*e0c4386eSCy Schubert         return 0;
172*e0c4386eSCy Schubert     return 1;
173*e0c4386eSCy Schubert }
174*e0c4386eSCy Schubert 
test_select_32(uint32_t a,uint32_t b)175*e0c4386eSCy Schubert static int test_select_32(uint32_t a, uint32_t b)
176*e0c4386eSCy Schubert {
177*e0c4386eSCy Schubert     if (!TEST_true(constant_time_select_32(CONSTTIME_TRUE_32, a, b) == a))
178*e0c4386eSCy Schubert         return 0;
179*e0c4386eSCy Schubert     if (!TEST_true(constant_time_select_32(CONSTTIME_FALSE_32, a, b) == b))
180*e0c4386eSCy Schubert         return 0;
181*e0c4386eSCy Schubert     return 1;
182*e0c4386eSCy Schubert }
183*e0c4386eSCy Schubert 
test_select_s(size_t a,size_t b)184*e0c4386eSCy Schubert static int test_select_s(size_t a, size_t b)
185*e0c4386eSCy Schubert {
186*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a))
187*e0c4386eSCy Schubert         return 0;
188*e0c4386eSCy Schubert     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b))
189*e0c4386eSCy Schubert         return 0;
190*e0c4386eSCy Schubert     return 1;
191*e0c4386eSCy Schubert }
192*e0c4386eSCy Schubert 
test_select_64(uint64_t a,uint64_t b)193*e0c4386eSCy Schubert static int test_select_64(uint64_t a, uint64_t b)
194*e0c4386eSCy Schubert {
195*e0c4386eSCy Schubert     uint64_t selected = constant_time_select_64(CONSTTIME_TRUE_64, a, b);
196*e0c4386eSCy Schubert 
197*e0c4386eSCy Schubert     if (selected != a) {
198*e0c4386eSCy Schubert         TEST_error("test_select_64 TRUE failed");
199*e0c4386eSCy Schubert         BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted a\n", a, b, selected);
200*e0c4386eSCy Schubert         return 0;
201*e0c4386eSCy Schubert     }
202*e0c4386eSCy Schubert     selected = constant_time_select_64(CONSTTIME_FALSE_64, a, b);
203*e0c4386eSCy Schubert     if (selected != b) {
204*e0c4386eSCy Schubert         BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted b\n", a, b, selected);
205*e0c4386eSCy Schubert         return 0;
206*e0c4386eSCy Schubert     }
207*e0c4386eSCy Schubert     return 1;
208*e0c4386eSCy Schubert }
209*e0c4386eSCy Schubert 
test_select_int(int a,int b)210*e0c4386eSCy Schubert static int test_select_int(int a, int b)
211*e0c4386eSCy Schubert {
212*e0c4386eSCy Schubert     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a))
213*e0c4386eSCy Schubert         return 0;
214*e0c4386eSCy Schubert     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b))
215*e0c4386eSCy Schubert         return 0;
216*e0c4386eSCy Schubert     return 1;
217*e0c4386eSCy Schubert }
218*e0c4386eSCy Schubert 
test_eq_int_8(int a,int b)219*e0c4386eSCy Schubert static int test_eq_int_8(int a, int b)
220*e0c4386eSCy Schubert {
221*e0c4386eSCy Schubert     if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8))
222*e0c4386eSCy Schubert         return 0;
223*e0c4386eSCy Schubert     if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8))
224*e0c4386eSCy Schubert         return 0;
225*e0c4386eSCy Schubert     return 1;
226*e0c4386eSCy Schubert }
227*e0c4386eSCy Schubert 
test_eq_s(size_t a,size_t b)228*e0c4386eSCy Schubert static int test_eq_s(size_t a, size_t b)
229*e0c4386eSCy Schubert {
230*e0c4386eSCy Schubert     if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S))
231*e0c4386eSCy Schubert         return 0;
232*e0c4386eSCy Schubert     if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S))
233*e0c4386eSCy Schubert         return 0;
234*e0c4386eSCy Schubert     return 1;
235*e0c4386eSCy Schubert }
236*e0c4386eSCy Schubert 
test_eq_int(int a,int b)237*e0c4386eSCy Schubert static int test_eq_int(int a, int b)
238*e0c4386eSCy Schubert {
239*e0c4386eSCy Schubert     if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE))
240*e0c4386eSCy Schubert         return 0;
241*e0c4386eSCy Schubert     if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE))
242*e0c4386eSCy Schubert         return 0;
243*e0c4386eSCy Schubert     return 1;
244*e0c4386eSCy Schubert }
245*e0c4386eSCy Schubert 
test_sizeofs(void)246*e0c4386eSCy Schubert static int test_sizeofs(void)
247*e0c4386eSCy Schubert {
248*e0c4386eSCy Schubert     if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s)))
249*e0c4386eSCy Schubert         return 0;
250*e0c4386eSCy Schubert     return 1;
251*e0c4386eSCy Schubert }
252*e0c4386eSCy Schubert 
test_binops(int i)253*e0c4386eSCy Schubert static int test_binops(int i)
254*e0c4386eSCy Schubert {
255*e0c4386eSCy Schubert     unsigned int a = test_values[i];
256*e0c4386eSCy Schubert     int j;
257*e0c4386eSCy Schubert     int ret = 1;
258*e0c4386eSCy Schubert 
259*e0c4386eSCy Schubert     for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) {
260*e0c4386eSCy Schubert         unsigned int b = test_values[j];
261*e0c4386eSCy Schubert 
262*e0c4386eSCy Schubert         if (!test_select(a, b)
263*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_lt, "ct_lt",
264*e0c4386eSCy Schubert                                    a, b, a < b)
265*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_lt, "constant_time_lt",
266*e0c4386eSCy Schubert                                    b, a, b < a)
267*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
268*e0c4386eSCy Schubert                                    a, b, a >= b)
269*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
270*e0c4386eSCy Schubert                                    b, a, b >= a)
271*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
272*e0c4386eSCy Schubert                                    a, b, a == b)
273*e0c4386eSCy Schubert                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
274*e0c4386eSCy Schubert                                    b, a, b == a))
275*e0c4386eSCy Schubert             ret = 0;
276*e0c4386eSCy Schubert     }
277*e0c4386eSCy Schubert     return ret;
278*e0c4386eSCy Schubert }
279*e0c4386eSCy Schubert 
test_binops_8(int i)280*e0c4386eSCy Schubert static int test_binops_8(int i)
281*e0c4386eSCy Schubert {
282*e0c4386eSCy Schubert     unsigned int a = test_values_8[i];
283*e0c4386eSCy Schubert     int j;
284*e0c4386eSCy Schubert     int ret = 1;
285*e0c4386eSCy Schubert 
286*e0c4386eSCy Schubert     for (j = 0; j < (int)OSSL_NELEM(test_values_8); ++j) {
287*e0c4386eSCy Schubert         unsigned int b = test_values_8[j];
288*e0c4386eSCy Schubert 
289*e0c4386eSCy Schubert         if (!test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
290*e0c4386eSCy Schubert                                      a, b, a < b)
291*e0c4386eSCy Schubert                 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
292*e0c4386eSCy Schubert                                      b, a, b < a)
293*e0c4386eSCy Schubert                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
294*e0c4386eSCy Schubert                                      a, b, a >= b)
295*e0c4386eSCy Schubert                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
296*e0c4386eSCy Schubert                                      b, a, b >= a)
297*e0c4386eSCy Schubert                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
298*e0c4386eSCy Schubert                                      a, b, a == b)
299*e0c4386eSCy Schubert                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
300*e0c4386eSCy Schubert                                      b, a, b == a))
301*e0c4386eSCy Schubert             ret = 0;
302*e0c4386eSCy Schubert     }
303*e0c4386eSCy Schubert     return ret;
304*e0c4386eSCy Schubert }
305*e0c4386eSCy Schubert 
test_binops_s(int i)306*e0c4386eSCy Schubert static int test_binops_s(int i)
307*e0c4386eSCy Schubert {
308*e0c4386eSCy Schubert     size_t a = test_values_s[i];
309*e0c4386eSCy Schubert     int j;
310*e0c4386eSCy Schubert     int ret = 1;
311*e0c4386eSCy Schubert 
312*e0c4386eSCy Schubert     for (j = 0; j < (int)OSSL_NELEM(test_values_s); ++j) {
313*e0c4386eSCy Schubert         size_t b = test_values_s[j];
314*e0c4386eSCy Schubert 
315*e0c4386eSCy Schubert         if (!test_select_s(a, b)
316*e0c4386eSCy Schubert                 || !test_eq_s(a, b)
317*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
318*e0c4386eSCy Schubert                                      a, b, a < b)
319*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
320*e0c4386eSCy Schubert                                      b, a, b < a)
321*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
322*e0c4386eSCy Schubert                                      a, b, a >= b)
323*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
324*e0c4386eSCy Schubert                                      b, a, b >= a)
325*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
326*e0c4386eSCy Schubert                                      a, b, a == b)
327*e0c4386eSCy Schubert                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
328*e0c4386eSCy Schubert                                      b, a, b == a))
329*e0c4386eSCy Schubert             ret = 0;
330*e0c4386eSCy Schubert     }
331*e0c4386eSCy Schubert     return ret;
332*e0c4386eSCy Schubert }
333*e0c4386eSCy Schubert 
test_signed(int i)334*e0c4386eSCy Schubert static int test_signed(int i)
335*e0c4386eSCy Schubert {
336*e0c4386eSCy Schubert     int c = signed_test_values[i];
337*e0c4386eSCy Schubert     unsigned int j;
338*e0c4386eSCy Schubert     int ret = 1;
339*e0c4386eSCy Schubert 
340*e0c4386eSCy Schubert     for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) {
341*e0c4386eSCy Schubert         int d = signed_test_values[j];
342*e0c4386eSCy Schubert 
343*e0c4386eSCy Schubert         if (!test_select_int(c, d)
344*e0c4386eSCy Schubert                 || !test_eq_int(c, d)
345*e0c4386eSCy Schubert                 || !test_eq_int_8(c, d))
346*e0c4386eSCy Schubert             ret = 0;
347*e0c4386eSCy Schubert     }
348*e0c4386eSCy Schubert     return ret;
349*e0c4386eSCy Schubert }
350*e0c4386eSCy Schubert 
test_8values(int i)351*e0c4386eSCy Schubert static int test_8values(int i)
352*e0c4386eSCy Schubert {
353*e0c4386eSCy Schubert     unsigned char e = test_values_8[i];
354*e0c4386eSCy Schubert     unsigned int j;
355*e0c4386eSCy Schubert     int ret = 1;
356*e0c4386eSCy Schubert 
357*e0c4386eSCy Schubert     for (j = 0; j < sizeof(test_values_8); ++j) {
358*e0c4386eSCy Schubert         unsigned char f = test_values_8[j];
359*e0c4386eSCy Schubert 
360*e0c4386eSCy Schubert         if (!test_select_8(e, f))
361*e0c4386eSCy Schubert             ret = 0;
362*e0c4386eSCy Schubert     }
363*e0c4386eSCy Schubert     return ret;
364*e0c4386eSCy Schubert }
365*e0c4386eSCy Schubert 
test_32values(int i)366*e0c4386eSCy Schubert static int test_32values(int i)
367*e0c4386eSCy Schubert {
368*e0c4386eSCy Schubert     uint32_t e = test_values_32[i];
369*e0c4386eSCy Schubert     size_t j;
370*e0c4386eSCy Schubert     int ret = 1;
371*e0c4386eSCy Schubert 
372*e0c4386eSCy Schubert     for (j = 0; j < OSSL_NELEM(test_values_32); j++) {
373*e0c4386eSCy Schubert         uint32_t f = test_values_32[j];
374*e0c4386eSCy Schubert 
375*e0c4386eSCy Schubert         if (!test_select_32(e, f))
376*e0c4386eSCy Schubert             ret = 0;
377*e0c4386eSCy Schubert     }
378*e0c4386eSCy Schubert     return ret;
379*e0c4386eSCy Schubert }
380*e0c4386eSCy Schubert 
test_64values(int i)381*e0c4386eSCy Schubert static int test_64values(int i)
382*e0c4386eSCy Schubert {
383*e0c4386eSCy Schubert     uint64_t g = test_values_64[i];
384*e0c4386eSCy Schubert     int j, ret = 1;
385*e0c4386eSCy Schubert 
386*e0c4386eSCy Schubert     for (j = i + 1; j < (int)OSSL_NELEM(test_values_64); j++) {
387*e0c4386eSCy Schubert         uint64_t h = test_values_64[j];
388*e0c4386eSCy Schubert 
389*e0c4386eSCy Schubert         if (!test_binary_op_64(&constant_time_lt_64, "constant_time_lt_64",
390*e0c4386eSCy Schubert                                g, h, g < h)
391*e0c4386eSCy Schubert                 || !test_select_64(g, h)) {
392*e0c4386eSCy Schubert             TEST_info("test_64values failed i=%d j=%d", i, j);
393*e0c4386eSCy Schubert             ret = 0;
394*e0c4386eSCy Schubert         }
395*e0c4386eSCy Schubert     }
396*e0c4386eSCy Schubert     return ret;
397*e0c4386eSCy Schubert }
398*e0c4386eSCy Schubert 
setup_tests(void)399*e0c4386eSCy Schubert int setup_tests(void)
400*e0c4386eSCy Schubert {
401*e0c4386eSCy Schubert     ADD_TEST(test_sizeofs);
402*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_is_zero, OSSL_NELEM(test_values));
403*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_is_zero_8, OSSL_NELEM(test_values_8));
404*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_is_zero_32, OSSL_NELEM(test_values_32));
405*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_is_zero_s, OSSL_NELEM(test_values_s));
406*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values));
407*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_binops_8, OSSL_NELEM(test_values_8));
408*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_binops_s, OSSL_NELEM(test_values_s));
409*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values));
410*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_8values, OSSL_NELEM(test_values_8));
411*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_32values, OSSL_NELEM(test_values_32));
412*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_64values, OSSL_NELEM(test_values_64));
413*e0c4386eSCy Schubert     return 1;
414*e0c4386eSCy Schubert }
415