Add some casts for %j
[oweals/openssl.git] / test / constant_time_test.c
1 /*
2  * Copyright 2014-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "e_os.h"
14 #include "internal/constant_time_locl.h"
15 #include "testutil.h"
16 #include "internal/numbers.h"
17
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;
26
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,
29                           int is_true)
30 {
31     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE))
32         return 0;
33     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE))
34         return 0;
35     return 1;
36 }
37
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)
42 {
43     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8))
44         return 0;
45     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8))
46         return 0;
47     return 1;
48 }
49
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,
52                             int is_true)
53 {
54     if (is_true && !TEST_size_t_eq(op(a,b), CONSTTIME_TRUE_S))
55         return 0;
56     if (!is_true && !TEST_uint_eq(op(a,b), CONSTTIME_FALSE_S))
57         return 0;
58     return 1;
59 }
60
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,
63                              int is_true)
64 {
65     uint64_t c = op(a, b);
66
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", (uintmax_t)a, (uintmax_t)b);
70         return 0;
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", (uintmax_t)a, (uintmax_t)b);
74         return 0;
75     }
76     return 1;
77 }
78
79
80 static int test_is_zero(unsigned int a)
81 {
82     if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE))
83         return 0;
84     if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE))
85         return 0;
86     return 1;
87 }
88
89 static int test_is_zero_8(unsigned int a)
90 {
91     if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))
92         return 0;
93     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))
94         return 0;
95     return 1;
96 }
97
98 static int test_is_zero_s(unsigned int a)
99 {
100     if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S))
101         return 0;
102     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S))
103         return 0;
104     return 1;
105 }
106
107 static int test_select(unsigned int a, unsigned int b)
108 {
109     if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a))
110         return 0;
111     if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b))
112         return 0;
113     return 1;
114 }
115
116 static int test_select_8(unsigned char a, unsigned char b)
117 {
118     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a))
119         return 0;
120     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b))
121         return 0;
122     return 1;
123 }
124
125 static int test_select_s(unsigned char a, unsigned char b)
126 {
127     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a))
128         return 0;
129     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b))
130         return 0;
131     return 1;
132 }
133
134 static int test_select_64(uint64_t a, uint64_t b)
135 {
136     uint64_t selected = constant_time_select_64(CONSTTIME_TRUE_64, a, b);
137
138     if (selected != a) {
139         TEST_error("test_select_64 TRUE failed");
140         BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted a\n",
141                    (uintmax_t)a, (uintmax_t)b, (uintmax_t)selected);
142         return 0;
143     }
144     selected = constant_time_select_64(CONSTTIME_FALSE_64, a, b);
145     if (selected != b) {
146         BIO_printf(bio_err, "a=%jx b=%jx got %jx wanted b\n",
147                    (uintmax_t)a, (uintmax_t)b, (uintmax_t)selected);
148         return 0;
149     }
150     return 1;
151 }
152
153 static int test_select_int(int a, int b)
154 {
155     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a))
156         return 0;
157     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b))
158         return 0;
159     return 1;
160 }
161
162 static int test_eq_int_8(int a, int b)
163 {
164     if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8))
165         return 0;
166     if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8))
167         return 0;
168     return 1;
169 }
170
171 static int test_eq_s(size_t a, size_t b)
172 {
173     if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S))
174         return 0;
175     if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S))
176         return 0;
177     return 1;
178 }
179
180 static int test_eq_int(int a, int b)
181 {
182     if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE))
183         return 0;
184     if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE))
185         return 0;
186     return 1;
187 }
188
189 static unsigned int test_values[] = {
190     0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
191     UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
192     UINT_MAX
193 };
194
195 static unsigned char test_values_8[] = {
196     0, 1, 2, 20, 32, 127, 128, 129, 255
197 };
198
199 static int signed_test_values[] = {
200     0, 1, -1, 1024, -1024, 12345, -12345,
201     32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
202     INT_MIN + 1
203 };
204
205 static size_t test_values_s[] = {
206     0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
207     SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
208     SIZE_MAX
209 };
210
211 static uint64_t test_values_64[] = {
212     0, 1, 1024, 12345, 32000, 32000000, 32000000001, UINT64_MAX / 2,
213     UINT64_MAX / 2 + 1, UINT64_MAX - 1, UINT64_MAX
214 };
215
216 static int test_sizeofs(void)
217 {
218     if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s)))
219         return 0;
220     return 1;
221 }
222
223 static int test_binops(int i)
224 {
225     unsigned int a = test_values[i];
226     unsigned int g = test_values_s[i];
227     int j;
228     int ret = 1;
229
230     if (!test_is_zero(a) || !test_is_zero_8(a) || !test_is_zero_s(g))
231         ret = 0;
232
233     for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) {
234         unsigned int b = test_values[j];
235         unsigned int h = test_values[j];
236
237         if (!test_select(a, b)
238                 || !test_select_s(g, h)
239                 || !test_eq_s(g, h)
240                 || !test_binary_op(&constant_time_lt, "ct_lt",
241                                    a, b, a < b)
242                 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
243                                      a, b, a < b)
244                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
245                                      g, h, g < h)
246                 || !test_binary_op(&constant_time_lt, "constant_time_lt",
247                                    b, a, b < a)
248                 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
249                                      b, a, b < a)
250                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
251                                      h, g, h < g)
252                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
253                                    a, b, a >= b)
254                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
255                                      a, b, a >= b)
256                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
257                                      g, h, g >= h)
258                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
259                                    b, a, b >= a)
260                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
261                                      b, a, b >= a)
262                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
263                                      h, g, h >= g)
264                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
265                                    a, b, a == b)
266                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
267                                      a, b, a == b)
268                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
269                                      g, h, g == h)
270                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
271                                    b, a, b == a)
272                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
273                                      b, a, b == a)
274                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
275                                      h, g, h == g)) {
276             ret = 0;
277         }
278     }
279     return ret;
280 }
281
282 static int test_signed(int i)
283 {
284     int c = signed_test_values[i];
285     unsigned int j;
286     int ret = 1;
287
288     for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) {
289         int d = signed_test_values[j];
290
291         if (!test_select_int(c, d)
292                 || !test_eq_int(c, d)
293                 || !test_eq_int_8(c, d))
294             ret = 0;
295     }
296     return ret;
297 }
298
299 static int test_8values(int i)
300 {
301     unsigned char e = test_values_8[i];
302     unsigned int j;
303     int ret = 1;
304
305     for (j = 0; j < sizeof(test_values_8); ++j) {
306         unsigned char f = test_values_8[j];
307
308         if (!test_select_8(e, f))
309             ret = 0;
310     }
311     return ret;
312 }
313
314 static int test_64values(int i)
315 {
316     uint64_t g = test_values_64[i];
317     int j, ret = 1;
318
319     for (j = i + 1; j < (int)OSSL_NELEM(test_values_64); j++) {
320         uint64_t h = test_values_64[j];
321
322         if (!test_binary_op_64(&constant_time_lt_64, "constant_time_lt_64",
323                                g, h, g < h)
324                 || !test_select_64(g, h)) {
325             TEST_info("test_64values failed i=%d j=%d", i, j);
326             ret = 0;
327         }
328     }
329     return ret;
330 }
331
332 int setup_tests(void)
333 {
334     ADD_TEST(test_sizeofs);
335     ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values));
336     ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values));
337     ADD_ALL_TESTS(test_8values, OSSL_NELEM(test_values_8));
338     ADD_ALL_TESTS(test_64values, OSSL_NELEM(test_values_64));
339     return 1;
340 }