Merge Nokia copyright notice into standard
[openssl.git] / test / constant_time_test.c
1 /*
2  * Copyright 2014-2016 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
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,
27                           int is_true)
28 {
29     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE))
30         return 0;
31     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE))
32         return 0;
33     return 1;
34 }
35
36 static int test_binary_op_8(unsigned
37                             char (*op) (unsigned int a, unsigned int b),
38                             const char *op_name, unsigned int a,
39                             unsigned int b, int is_true)
40 {
41     if (is_true && !TEST_uint_eq(op(a, b), CONSTTIME_TRUE_8))
42         return 0;
43     if (!is_true && !TEST_uint_eq(op(a, b), CONSTTIME_FALSE_8))
44         return 0;
45     return 1;
46 }
47
48 static int test_binary_op_s(size_t (*op) (size_t a, size_t b),
49                             const char *op_name, size_t a, size_t b,
50                             int is_true)
51 {
52     if (is_true && !TEST_size_t_eq(op(a,b), CONSTTIME_TRUE_S))
53         return 0;
54     if (!is_true && !TEST_uint_eq(op(a,b), CONSTTIME_FALSE_S))
55         return 0;
56     return 1;
57 }
58
59 static int test_is_zero(unsigned int a)
60 {
61     if (a == 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_TRUE))
62         return 0;
63     if (a != 0 && !TEST_uint_eq(constant_time_is_zero(a), CONSTTIME_FALSE))
64         return 0;
65     return 1;
66 }
67
68 static int test_is_zero_8(unsigned int a)
69 {
70     if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))
71         return 0;
72     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))
73         return 0;
74     return 1;
75 }
76
77 static int test_is_zero_s(unsigned int a)
78 {
79     if (a == 0 && !TEST_size_t_eq(constant_time_is_zero_s(a), CONSTTIME_TRUE_S))
80         return 0;
81     if (a != 0 && !TEST_uint_eq(constant_time_is_zero_s(a), CONSTTIME_FALSE_S))
82         return 0;
83     return 1;
84 }
85
86 static int test_select(unsigned int a, unsigned int b)
87 {
88     if (!TEST_uint_eq(constant_time_select(CONSTTIME_TRUE, a, b), a))
89         return 0;
90     if (!TEST_uint_eq(constant_time_select(CONSTTIME_FALSE, a, b), b))
91         return 0;
92     return 1;
93 }
94
95 static int test_select_8(unsigned char a, unsigned char b)
96 {
97     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_TRUE_8, a, b), a))
98         return 0;
99     if (!TEST_uint_eq(constant_time_select_8(CONSTTIME_FALSE_8, a, b), b))
100         return 0;
101     return 1;
102 }
103
104 static int test_select_s(unsigned char a, unsigned char b)
105 {
106     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_TRUE_S, a, b), a))
107         return 0;
108     if (!TEST_uint_eq(constant_time_select_s(CONSTTIME_FALSE_S, a, b), b))
109         return 0;
110     return 1;
111 }
112
113 static int test_select_int(int a, int b)
114 {
115     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_TRUE, a, b), a))
116         return 0;
117     if (!TEST_int_eq(constant_time_select_int(CONSTTIME_FALSE, a, b), b))
118         return 0;
119     return 1;
120 }
121
122 static int test_eq_int_8(int a, int b)
123 {
124     if (a == b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_TRUE_8))
125         return 0;
126     if (a != b && !TEST_int_eq(constant_time_eq_int_8(a, b), CONSTTIME_FALSE_8))
127         return 0;
128     return 1;
129 }
130
131 static int test_eq_s(size_t a, size_t b)
132 {
133     if (a == b && !TEST_size_t_eq(constant_time_eq_s(a, b), CONSTTIME_TRUE_S))
134         return 0;
135     if (a != b && !TEST_int_eq(constant_time_eq_s(a, b), CONSTTIME_FALSE_S))
136         return 0;
137     return 1;
138 }
139
140 static int test_eq_int(int a, int b)
141 {
142     if (a == b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_TRUE))
143         return 0;
144     if (a != b && !TEST_uint_eq(constant_time_eq_int(a, b), CONSTTIME_FALSE))
145         return 0;
146     return 1;
147 }
148
149 static unsigned int test_values[] =
150     { 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
151     UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
152     UINT_MAX
153 };
154
155 static unsigned char test_values_8[] =
156     { 0, 1, 2, 20, 32, 127, 128, 129, 255 };
157
158 static int signed_test_values[] = { 0, 1, -1, 1024, -1024, 12345, -12345,
159     32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
160     INT_MIN + 1
161 };
162
163 static size_t test_values_s[] =
164     { 0, 1, 1024, 12345, 32000, SIZE_MAX / 2 - 1,
165     SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX - 1,
166     SIZE_MAX
167 };
168
169 static int test_sizeofs(void)
170 {
171     if (!TEST_uint_eq(OSSL_NELEM(test_values), OSSL_NELEM(test_values_s)))
172         return 0;
173     return 1;
174 }
175
176 static int test_binops(int i)
177 {
178     unsigned int a = test_values[i];
179     unsigned int g = test_values_s[i];
180     int j;
181     int ret = 1;
182
183     if (!test_is_zero(a) || !test_is_zero_8(a) || !test_is_zero_s(g))
184         ret = 0;
185
186     for (j = 0; j < (int)OSSL_NELEM(test_values); ++j) {
187         unsigned int b = test_values[j];
188         unsigned int h = test_values[j];
189
190         if (!test_select(a, b)
191                 || !test_select_s(g, h)
192                 || !test_eq_s(g, h)
193                 || !test_binary_op(&constant_time_lt, "ct_lt",
194                                    a, b, a < b)
195                 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
196                                      a, b, a < b)
197                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
198                                      g, h, g < h)
199                 || !test_binary_op(&constant_time_lt, "constant_time_lt",
200                                    b, a, b < a)
201                 || !test_binary_op_8(&constant_time_lt_8, "constant_time_lt_8",
202                                      b, a, b < a)
203                 || !test_binary_op_s(&constant_time_lt_s, "constant_time_lt_s",
204                                      h, g, h < g)
205                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
206                                    a, b, a >= b)
207                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
208                                      a, b, a >= b)
209                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
210                                      g, h, g >= h)
211                 || !test_binary_op(&constant_time_ge, "constant_time_ge",
212                                    b, a, b >= a)
213                 || !test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8",
214                                      b, a, b >= a)
215                 || !test_binary_op_s(&constant_time_ge_s, "constant_time_ge_s",
216                                      h, g, h >= g)
217                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
218                                    a, b, a == b)
219                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
220                                      a, b, a == b)
221                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
222                                      g, h, g == h)
223                 || !test_binary_op(&constant_time_eq, "constant_time_eq",
224                                    b, a, b == a)
225                 || !test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8",
226                                      b, a, b == a)
227                 || !test_binary_op_s(&constant_time_eq_s, "constant_time_eq_s",
228                                      h, g, h == g)) {
229             ret = 0;
230         }
231     }
232     return ret;
233 }
234
235 static int test_signed(int i)
236 {
237     int c = signed_test_values[i];
238     unsigned int j;
239     int ret = 1;
240
241     for (j = 0; j < OSSL_NELEM(signed_test_values); ++j) {
242         int d = signed_test_values[j];
243
244         if (!test_select_int(c, d)
245                 || !test_eq_int(c, d)
246                 || !test_eq_int_8(c, d))
247             ret = 0;
248     }
249     return ret;
250 }
251
252 static int test_8values(int i)
253 {
254     unsigned char e = test_values_8[i];
255     unsigned int j;
256     int ret = 1;
257
258     for (j = 0; j < sizeof(test_values_8); ++j) {
259         unsigned char f = test_values_8[j];
260
261         if (!test_select_8(e, f))
262             ret = 0;
263     }
264     return ret;
265 }
266
267
268 void register_tests(void)
269 {
270     ADD_TEST(test_sizeofs);
271     ADD_ALL_TESTS(test_binops, OSSL_NELEM(test_values));
272     ADD_ALL_TESTS(test_signed, OSSL_NELEM(signed_test_values));
273     ADD_ALL_TESTS(test_8values, sizeof(test_values_8));
274 }