1 /* crypto/constant_time_test.c */
3 * Utilities for constant-time cryptography.
5 * Author: Emilia Kasper (emilia@openssl.org)
6 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
8 * ====================================================================
9 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * "This product includes cryptographic software written by
22 * Eric Young (eay@cryptsoft.com)"
23 * The word 'cryptographic' can be left out if the rouines from the library
24 * being used are not cryptographic related :-).
25 * 4. If you include any Windows specific code (or a derivative thereof) from
26 * the apps directory (application code) you must include an acknowledgement:
27 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
29 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * The licence and distribution terms for any publically available version or
42 * derivative of this code cannot be changed. i.e. this code cannot simply be
43 * copied and put under another distribution licence
44 * [including the GNU Public Licence.]
47 #include "../crypto/constant_time_locl.h"
53 static const unsigned int CONSTTIME_TRUE
= (unsigned)(~0);
54 static const unsigned int CONSTTIME_FALSE
= 0;
55 static const unsigned char CONSTTIME_TRUE_8
= 0xff;
56 static const unsigned char CONSTTIME_FALSE_8
= 0;
58 static int test_binary_op(unsigned int (*op
)(unsigned int a
, unsigned int b
),
59 const char* op_name
, unsigned int a
, unsigned int b
, int is_true
)
61 unsigned c
= op(a
, b
);
62 if (is_true
&& c
!= CONSTTIME_TRUE
)
64 fprintf(stderr
, "Test failed for %s(%du, %du): expected %du "
65 "(TRUE), got %du\n", op_name
, a
, b
, CONSTTIME_TRUE
, c
);
68 else if (!is_true
&& c
!= CONSTTIME_FALSE
)
70 fprintf(stderr
, "Test failed for %s(%du, %du): expected %du "
71 "(FALSE), got %du\n", op_name
, a
, b
, CONSTTIME_FALSE
,
78 static int test_binary_op_8(unsigned char (*op
)(unsigned int a
, unsigned int b
),
79 const char* op_name
, unsigned int a
, unsigned int b
, int is_true
)
81 unsigned char c
= op(a
, b
);
82 if (is_true
&& c
!= CONSTTIME_TRUE_8
)
84 fprintf(stderr
, "Test failed for %s(%du, %du): expected %u "
85 "(TRUE), got %u\n", op_name
, a
, b
, CONSTTIME_TRUE_8
, c
);
88 else if (!is_true
&& c
!= CONSTTIME_FALSE_8
)
90 fprintf(stderr
, "Test failed for %s(%du, %du): expected %u "
91 "(FALSE), got %u\n", op_name
, a
, b
, CONSTTIME_FALSE_8
,
98 static int test_is_zero(unsigned int a
)
100 unsigned int c
= constant_time_is_zero(a
);
101 if (a
== 0 && c
!= CONSTTIME_TRUE
)
103 fprintf(stderr
, "Test failed for constant_time_is_zero(%du): "
104 "expected %du (TRUE), got %du\n", a
, CONSTTIME_TRUE
, c
);
107 else if (a
!= 0 && c
!= CONSTTIME_FALSE
)
109 fprintf(stderr
, "Test failed for constant_time_is_zero(%du): "
110 "expected %du (FALSE), got %du\n", a
, CONSTTIME_FALSE
,
117 static int test_is_zero_8(unsigned int a
)
119 unsigned char c
= constant_time_is_zero_8(a
);
120 if (a
== 0 && c
!= CONSTTIME_TRUE_8
)
122 fprintf(stderr
, "Test failed for constant_time_is_zero(%du): "
123 "expected %u (TRUE), got %u\n", a
, CONSTTIME_TRUE_8
, c
);
126 else if (a
!= 0 && c
!= CONSTTIME_FALSE
)
128 fprintf(stderr
, "Test failed for constant_time_is_zero(%du): "
129 "expected %u (FALSE), got %u\n", a
, CONSTTIME_FALSE_8
,
136 static int test_select(unsigned int a
, unsigned int b
)
138 unsigned int selected
= constant_time_select(CONSTTIME_TRUE
, a
, b
);
141 fprintf(stderr
, "Test failed for constant_time_select(%du, %du,"
142 "%du): expected %du(first value), got %du\n",
143 CONSTTIME_TRUE
, a
, b
, a
, selected
);
146 selected
= constant_time_select(CONSTTIME_FALSE
, a
, b
);
149 fprintf(stderr
, "Test failed for constant_time_select(%du, %du,"
150 "%du): expected %du(second value), got %du\n",
151 CONSTTIME_FALSE
, a
, b
, b
, selected
);
157 static int test_select_8(unsigned char a
, unsigned char b
)
159 unsigned char selected
= constant_time_select_8(CONSTTIME_TRUE_8
, a
, b
);
162 fprintf(stderr
, "Test failed for constant_time_select(%u, %u,"
163 "%u): expected %u(first value), got %u\n",
164 CONSTTIME_TRUE
, a
, b
, a
, selected
);
167 selected
= constant_time_select_8(CONSTTIME_FALSE_8
, a
, b
);
170 fprintf(stderr
, "Test failed for constant_time_select(%u, %u,"
171 "%u): expected %u(second value), got %u\n",
172 CONSTTIME_FALSE
, a
, b
, b
, selected
);
178 static int test_select_int(int a
, int b
)
180 int selected
= constant_time_select_int(CONSTTIME_TRUE
, a
, b
);
183 fprintf(stderr
, "Test failed for constant_time_select(%du, %d,"
184 "%d): expected %d(first value), got %d\n",
185 CONSTTIME_TRUE
, a
, b
, a
, selected
);
188 selected
= constant_time_select_int(CONSTTIME_FALSE
, a
, b
);
191 fprintf(stderr
, "Test failed for constant_time_select(%du, %d,"
192 "%d): expected %d(second value), got %d\n",
193 CONSTTIME_FALSE
, a
, b
, b
, selected
);
199 static int test_eq_int(int a
, int b
)
201 unsigned int equal
= constant_time_eq_int(a
, b
);
202 if (a
== b
&& equal
!= CONSTTIME_TRUE
)
204 fprintf(stderr
, "Test failed for constant_time_eq_int(%d, %d): "
205 "expected %du(TRUE), got %du\n",
206 a
, b
, CONSTTIME_TRUE
, equal
);
209 else if (a
!= b
&& equal
!= CONSTTIME_FALSE
)
211 fprintf(stderr
, "Test failed for constant_time_eq_int(%d, %d): "
212 "expected %du(FALSE), got %du\n",
213 a
, b
, CONSTTIME_FALSE
, equal
);
219 static int test_eq_int_8(int a
, int b
)
221 unsigned char equal
= constant_time_eq_int_8(a
, b
);
222 if (a
== b
&& equal
!= CONSTTIME_TRUE_8
)
224 fprintf(stderr
, "Test failed for constant_time_eq_int_8(%d, %d): "
225 "expected %u(TRUE), got %u\n",
226 a
, b
, CONSTTIME_TRUE_8
, equal
);
229 else if (a
!= b
&& equal
!= CONSTTIME_FALSE_8
)
231 fprintf(stderr
, "Test failed for constant_time_eq_int_8(%d, %d): "
232 "expected %u(FALSE), got %u\n",
233 a
, b
, CONSTTIME_FALSE_8
, equal
);
239 static unsigned int test_values
[] = {0, 1, 1024, 12345, 32000, UINT_MAX
/2-1,
240 UINT_MAX
/2, UINT_MAX
/2+1, UINT_MAX
-1,
243 static unsigned char test_values_8
[] = {0, 1, 2, 20, 32, 127, 128, 129, 255};
245 static int signed_test_values
[] = {0, 1, -1, 1024, -1024, 12345, -12345,
246 32000, -32000, INT_MAX
, INT_MIN
, INT_MAX
-1,
250 int main(int argc
, char *argv
[])
252 unsigned int a
, b
, i
, j
;
255 int num_failed
= 0, num_all
= 0;
256 fprintf(stdout
, "Testing constant time operations...\n");
258 for (i
= 0; i
< sizeof(test_values
)/sizeof(int); ++i
)
261 num_failed
+= test_is_zero(a
);
262 num_failed
+= test_is_zero_8(a
);
264 for (j
= 0; j
< sizeof(test_values
)/sizeof(int); ++j
)
267 num_failed
+= test_binary_op(&constant_time_lt
,
268 "constant_time_lt", a
, b
, a
< b
);
269 num_failed
+= test_binary_op_8(&constant_time_lt_8
,
270 "constant_time_lt_8", a
, b
, a
< b
);
271 num_failed
+= test_binary_op(&constant_time_lt
,
272 "constant_time_lt_8", b
, a
, b
< a
);
273 num_failed
+= test_binary_op_8(&constant_time_lt_8
,
274 "constant_time_lt_8", b
, a
, b
< a
);
275 num_failed
+= test_binary_op(&constant_time_ge
,
276 "constant_time_ge", a
, b
, a
>= b
);
277 num_failed
+= test_binary_op_8(&constant_time_ge_8
,
278 "constant_time_ge_8", a
, b
, a
>= b
);
279 num_failed
+= test_binary_op(&constant_time_ge
,
280 "constant_time_ge", b
, a
, b
>= a
);
281 num_failed
+= test_binary_op_8(&constant_time_ge_8
,
282 "constant_time_ge_8", b
, a
, b
>= a
);
283 num_failed
+= test_binary_op(&constant_time_eq
,
284 "constant_time_eq", a
, b
, a
== b
);
285 num_failed
+= test_binary_op_8(&constant_time_eq_8
,
286 "constant_time_eq_8", a
, b
, a
== b
);
287 num_failed
+= test_binary_op(&constant_time_eq
,
288 "constant_time_eq", b
, a
, b
== a
);
289 num_failed
+= test_binary_op_8(&constant_time_eq_8
,
290 "constant_time_eq_8", b
, a
, b
== a
);
291 num_failed
+= test_select(a
, b
);
296 for (i
= 0; i
< sizeof(signed_test_values
)/sizeof(int); ++i
)
298 c
= signed_test_values
[i
];
299 for (j
= 0; j
< sizeof(signed_test_values
)/sizeof(int); ++j
)
301 d
= signed_test_values
[j
];
302 num_failed
+= test_select_int(c
, d
);
303 num_failed
+= test_eq_int(c
, d
);
304 num_failed
+= test_eq_int_8(c
, d
);
309 for (i
= 0; i
< sizeof(test_values_8
); ++i
)
311 e
= test_values_8
[i
];
312 for (j
= 0; j
< sizeof(test_values_8
); ++j
)
314 f
= test_values_8
[j
];
315 num_failed
+= test_select_8(e
, f
);
322 fprintf(stdout
, "ok (ran %d tests)\n", num_all
);
327 fprintf(stdout
, "%d of %d tests failed!\n", num_failed
, num_all
);