constant_time_test.c revision 296465
1/* crypto/constant_time_test.c */
2/*-
3 * Utilities for constant-time cryptography.
4 *
5 * Author: Emilia Kasper (emilia@openssl.org)
6 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
7 * (Google).
8 * ====================================================================
9 * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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)"
28 *
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
39 * SUCH DAMAGE.
40 *
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.]
45 */
46
47#include "../crypto/constant_time_locl.h"
48
49#include <limits.h>
50#include <stdio.h>
51#include <stdlib.h>
52
53static const unsigned int CONSTTIME_TRUE = (unsigned)(~0);
54static const unsigned int CONSTTIME_FALSE = 0;
55static const unsigned char CONSTTIME_TRUE_8 = 0xff;
56static const unsigned char CONSTTIME_FALSE_8 = 0;
57
58static int test_binary_op(unsigned int (*op) (unsigned int a, unsigned int b),
59                          const char *op_name, unsigned int a, unsigned int b,
60                          int is_true)
61{
62    unsigned c = op(a, b);
63    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);
66        return 1;
67    } else if (!is_true && c != CONSTTIME_FALSE) {
68        fprintf(stderr, "Test failed for  %s(%du, %du): expected %du "
69                "(FALSE), got %du\n", op_name, a, b, CONSTTIME_FALSE, c);
70        return 1;
71    }
72    return 0;
73}
74
75static int test_binary_op_8(unsigned
76                            char (*op) (unsigned int a, unsigned int b),
77                            const char *op_name, unsigned int a,
78                            unsigned int b, int is_true)
79{
80    unsigned char c = op(a, b);
81    if (is_true && c != CONSTTIME_TRUE_8) {
82        fprintf(stderr, "Test failed for %s(%du, %du): expected %u "
83                "(TRUE), got %u\n", op_name, a, b, CONSTTIME_TRUE_8, c);
84        return 1;
85    } else if (!is_true && c != CONSTTIME_FALSE_8) {
86        fprintf(stderr, "Test failed for  %s(%du, %du): expected %u "
87                "(FALSE), got %u\n", op_name, a, b, CONSTTIME_FALSE_8, c);
88        return 1;
89    }
90    return 0;
91}
92
93static int test_is_zero(unsigned int a)
94{
95    unsigned int c = constant_time_is_zero(a);
96    if (a == 0 && c != CONSTTIME_TRUE) {
97        fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
98                "expected %du (TRUE), got %du\n", a, CONSTTIME_TRUE, c);
99        return 1;
100    } else if (a != 0 && c != CONSTTIME_FALSE) {
101        fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
102                "expected %du (FALSE), got %du\n", a, CONSTTIME_FALSE, c);
103        return 1;
104    }
105    return 0;
106}
107
108static int test_is_zero_8(unsigned int a)
109{
110    unsigned char c = constant_time_is_zero_8(a);
111    if (a == 0 && c != CONSTTIME_TRUE_8) {
112        fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
113                "expected %u (TRUE), got %u\n", a, CONSTTIME_TRUE_8, c);
114        return 1;
115    } else if (a != 0 && c != CONSTTIME_FALSE) {
116        fprintf(stderr, "Test failed for constant_time_is_zero(%du): "
117                "expected %u (FALSE), got %u\n", a, CONSTTIME_FALSE_8, c);
118        return 1;
119    }
120    return 0;
121}
122
123static int test_select(unsigned int a, unsigned int b)
124{
125    unsigned int selected = constant_time_select(CONSTTIME_TRUE, a, b);
126    if (selected != a) {
127        fprintf(stderr, "Test failed for constant_time_select(%du, %du,"
128                "%du): expected %du(first value), got %du\n",
129                CONSTTIME_TRUE, a, b, a, selected);
130        return 1;
131    }
132    selected = constant_time_select(CONSTTIME_FALSE, a, b);
133    if (selected != b) {
134        fprintf(stderr, "Test failed for constant_time_select(%du, %du,"
135                "%du): expected %du(second value), got %du\n",
136                CONSTTIME_FALSE, a, b, b, selected);
137        return 1;
138    }
139    return 0;
140}
141
142static int test_select_8(unsigned char a, unsigned char b)
143{
144    unsigned char selected = constant_time_select_8(CONSTTIME_TRUE_8, a, b);
145    if (selected != a) {
146        fprintf(stderr, "Test failed for constant_time_select(%u, %u,"
147                "%u): expected %u(first value), got %u\n",
148                CONSTTIME_TRUE, a, b, a, selected);
149        return 1;
150    }
151    selected = constant_time_select_8(CONSTTIME_FALSE_8, a, b);
152    if (selected != b) {
153        fprintf(stderr, "Test failed for constant_time_select(%u, %u,"
154                "%u): expected %u(second value), got %u\n",
155                CONSTTIME_FALSE, a, b, b, selected);
156        return 1;
157    }
158    return 0;
159}
160
161static int test_select_int(int a, int b)
162{
163    int selected = constant_time_select_int(CONSTTIME_TRUE, a, b);
164    if (selected != a) {
165        fprintf(stderr, "Test failed for constant_time_select(%du, %d,"
166                "%d): expected %d(first value), got %d\n",
167                CONSTTIME_TRUE, a, b, a, selected);
168        return 1;
169    }
170    selected = constant_time_select_int(CONSTTIME_FALSE, a, b);
171    if (selected != b) {
172        fprintf(stderr, "Test failed for constant_time_select(%du, %d,"
173                "%d): expected %d(second value), got %d\n",
174                CONSTTIME_FALSE, a, b, b, selected);
175        return 1;
176    }
177    return 0;
178}
179
180static int test_eq_int(int a, int b)
181{
182    unsigned int equal = constant_time_eq_int(a, b);
183    if (a == b && equal != CONSTTIME_TRUE) {
184        fprintf(stderr, "Test failed for constant_time_eq_int(%d, %d): "
185                "expected %du(TRUE), got %du\n", a, b, CONSTTIME_TRUE, equal);
186        return 1;
187    } else if (a != b && equal != CONSTTIME_FALSE) {
188        fprintf(stderr, "Test failed for constant_time_eq_int(%d, %d): "
189                "expected %du(FALSE), got %du\n",
190                a, b, CONSTTIME_FALSE, equal);
191        return 1;
192    }
193    return 0;
194}
195
196static int test_eq_int_8(int a, int b)
197{
198    unsigned char equal = constant_time_eq_int_8(a, b);
199    if (a == b && equal != CONSTTIME_TRUE_8) {
200        fprintf(stderr, "Test failed for constant_time_eq_int_8(%d, %d): "
201                "expected %u(TRUE), got %u\n", a, b, CONSTTIME_TRUE_8, equal);
202        return 1;
203    } else if (a != b && equal != CONSTTIME_FALSE_8) {
204        fprintf(stderr, "Test failed for constant_time_eq_int_8(%d, %d): "
205                "expected %u(FALSE), got %u\n",
206                a, b, CONSTTIME_FALSE_8, equal);
207        return 1;
208    }
209    return 0;
210}
211
212static unsigned int test_values[] =
213    { 0, 1, 1024, 12345, 32000, UINT_MAX / 2 - 1,
214    UINT_MAX / 2, UINT_MAX / 2 + 1, UINT_MAX - 1,
215    UINT_MAX
216};
217
218static unsigned char test_values_8[] =
219    { 0, 1, 2, 20, 32, 127, 128, 129, 255 };
220
221static int signed_test_values[] = { 0, 1, -1, 1024, -1024, 12345, -12345,
222    32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1,
223    INT_MIN + 1
224};
225
226int main(int argc, char *argv[])
227{
228    unsigned int a, b, i, j;
229    int c, d;
230    unsigned char e, f;
231    int num_failed = 0, num_all = 0;
232    fprintf(stdout, "Testing constant time operations...\n");
233
234    for (i = 0; i < sizeof(test_values) / sizeof(int); ++i) {
235        a = test_values[i];
236        num_failed += test_is_zero(a);
237        num_failed += test_is_zero_8(a);
238        num_all += 2;
239        for (j = 0; j < sizeof(test_values) / sizeof(int); ++j) {
240            b = test_values[j];
241            num_failed += test_binary_op(&constant_time_lt,
242                                         "constant_time_lt", a, b, a < b);
243            num_failed += test_binary_op_8(&constant_time_lt_8,
244                                           "constant_time_lt_8", a, b, a < b);
245            num_failed += test_binary_op(&constant_time_lt,
246                                         "constant_time_lt_8", b, a, b < a);
247            num_failed += test_binary_op_8(&constant_time_lt_8,
248                                           "constant_time_lt_8", b, a, b < a);
249            num_failed += test_binary_op(&constant_time_ge,
250                                         "constant_time_ge", a, b, a >= b);
251            num_failed += test_binary_op_8(&constant_time_ge_8,
252                                           "constant_time_ge_8", a, b,
253                                           a >= b);
254            num_failed +=
255                test_binary_op(&constant_time_ge, "constant_time_ge", b, a,
256                               b >= a);
257            num_failed +=
258                test_binary_op_8(&constant_time_ge_8, "constant_time_ge_8", b,
259                                 a, b >= a);
260            num_failed +=
261                test_binary_op(&constant_time_eq, "constant_time_eq", a, b,
262                               a == b);
263            num_failed +=
264                test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", a,
265                                 b, a == b);
266            num_failed +=
267                test_binary_op(&constant_time_eq, "constant_time_eq", b, a,
268                               b == a);
269            num_failed +=
270                test_binary_op_8(&constant_time_eq_8, "constant_time_eq_8", b,
271                                 a, b == a);
272            num_failed += test_select(a, b);
273            num_all += 13;
274        }
275    }
276
277    for (i = 0; i < sizeof(signed_test_values) / sizeof(int); ++i) {
278        c = signed_test_values[i];
279        for (j = 0; j < sizeof(signed_test_values) / sizeof(int); ++j) {
280            d = signed_test_values[j];
281            num_failed += test_select_int(c, d);
282            num_failed += test_eq_int(c, d);
283            num_failed += test_eq_int_8(c, d);
284            num_all += 3;
285        }
286    }
287
288    for (i = 0; i < sizeof(test_values_8); ++i) {
289        e = test_values_8[i];
290        for (j = 0; j < sizeof(test_values_8); ++j) {
291            f = test_values_8[j];
292            num_failed += test_select_8(e, f);
293            num_all += 1;
294        }
295    }
296
297    if (!num_failed) {
298        fprintf(stdout, "ok (ran %d tests)\n", num_all);
299        return EXIT_SUCCESS;
300    } else {
301        fprintf(stdout, "%d of %d tests failed!\n", num_failed, num_all);
302        return EXIT_FAILURE;
303    }
304}
305