blob: 3cb5866aa8a7e99aace10b91825a15081e5d3eaf [file] [log] [blame]
Adam Langley9ed9dae2014-11-04 11:22:01 -08001/*
2 * Utilities for constant-time cryptography.
3 *
4 * Author: Emilia Kasper (emilia@openssl.org)
5 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
6 * (Google).
7 * ====================================================================
8 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * "This product includes cryptographic software written by
21 * Eric Young (eay@cryptsoft.com)"
22 * The word 'cryptographic' can be left out if the rouines from the library
23 * being used are not cryptographic related :-).
24 * 4. If you include any Windows specific code (or a derivative thereof) from
25 * the apps directory (application code) you must include an acknowledgement:
26 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
27 *
28 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * The licence and distribution terms for any publically available version or
41 * derivative of this code cannot be changed. i.e. this code cannot simply be
42 * copied and put under another distribution licence
43 * [including the GNU Public Licence.]
44 */
45
46#include "internal.h"
47
48#include <limits.h>
49#include <stdio.h>
50#include <stdlib.h>
51
David Benjamind1c0de62017-03-16 11:38:18 -040052#include <limits>
53
David Benjamin81a191d2017-03-16 11:25:20 -040054#include <gtest/gtest.h>
Adam Langley9ed9dae2014-11-04 11:22:01 -080055
David Benjamin81a191d2017-03-16 11:25:20 -040056
David Benjamin81a191d2017-03-16 11:25:20 -040057static uint8_t FromBool8(bool b) {
58 return b ? CONSTTIME_TRUE_8 : CONSTTIME_FALSE_8;
Adam Langley9ed9dae2014-11-04 11:22:01 -080059}
60
Adam Langley518ba072017-04-20 13:51:11 -070061static crypto_word_t FromBoolW(bool b) {
62 return b ? CONSTTIME_TRUE_W : CONSTTIME_FALSE_W;
David Benjamind1c0de62017-03-16 11:38:18 -040063}
64
Adam Langleyfaa539f2017-03-30 10:06:42 -070065static const uint8_t test_values_8[] = {0, 1, 2, 20, 32, 127, 128, 129, 255};
Adam Langley9ed9dae2014-11-04 11:22:01 -080066
Adam Langley518ba072017-04-20 13:51:11 -070067static crypto_word_t test_values_w[] = {
David Benjamind1c0de62017-03-16 11:38:18 -040068 0,
69 1,
70 1024,
71 12345,
72 32000,
73#if defined(OPENSSL_64_BIT)
74 0xffffffff / 2 - 1,
75 0xffffffff / 2,
76 0xffffffff / 2 + 1,
77 0xffffffff - 1,
78 0xffffffff,
79#endif
Adam Langley518ba072017-04-20 13:51:11 -070080 std::numeric_limits<crypto_word_t>::max() / 2 - 1,
81 std::numeric_limits<crypto_word_t>::max() / 2,
82 std::numeric_limits<crypto_word_t>::max() / 2 + 1,
83 std::numeric_limits<crypto_word_t>::max() - 1,
84 std::numeric_limits<crypto_word_t>::max(),
David Benjamind1c0de62017-03-16 11:38:18 -040085};
86
Adam Langley9ed9dae2014-11-04 11:22:01 -080087static int signed_test_values[] = {
88 0, 1, -1, 1024, -1024, 12345, -12345,
89 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1};
90
David Benjamin81a191d2017-03-16 11:25:20 -040091TEST(ConstantTimeTest, Test) {
Adam Langley518ba072017-04-20 13:51:11 -070092 for (crypto_word_t a : test_values_w) {
David Benjamin81a191d2017-03-16 11:25:20 -040093 SCOPED_TRACE(a);
Adam Langley9ed9dae2014-11-04 11:22:01 -080094
Adam Langley518ba072017-04-20 13:51:11 -070095 EXPECT_EQ(FromBoolW(a == 0), constant_time_is_zero_w(a));
David Benjamin81a191d2017-03-16 11:25:20 -040096 EXPECT_EQ(FromBool8(a == 0), constant_time_is_zero_8(a));
97
Adam Langley518ba072017-04-20 13:51:11 -070098 for (crypto_word_t b : test_values_w) {
David Benjamind1c0de62017-03-16 11:38:18 -040099 SCOPED_TRACE(b);
100
Adam Langley518ba072017-04-20 13:51:11 -0700101 EXPECT_EQ(FromBoolW(a < b), constant_time_lt_w(a, b));
David Benjamind1c0de62017-03-16 11:38:18 -0400102 EXPECT_EQ(FromBool8(a < b), constant_time_lt_8(a, b));
103
Adam Langley518ba072017-04-20 13:51:11 -0700104 EXPECT_EQ(FromBoolW(a >= b), constant_time_ge_w(a, b));
David Benjamind1c0de62017-03-16 11:38:18 -0400105 EXPECT_EQ(FromBool8(a >= b), constant_time_ge_8(a, b));
106
Adam Langley518ba072017-04-20 13:51:11 -0700107 EXPECT_EQ(FromBoolW(a == b), constant_time_eq_w(a, b));
David Benjamind1c0de62017-03-16 11:38:18 -0400108 EXPECT_EQ(FromBool8(a == b), constant_time_eq_8(a, b));
109
Adam Langley518ba072017-04-20 13:51:11 -0700110 EXPECT_EQ(a, constant_time_select_w(CONSTTIME_TRUE_W, a, b));
111 EXPECT_EQ(b, constant_time_select_w(CONSTTIME_FALSE_W, a, b));
David Benjamind1c0de62017-03-16 11:38:18 -0400112 }
113 }
114
David Benjamin81a191d2017-03-16 11:25:20 -0400115 for (int a : signed_test_values) {
116 SCOPED_TRACE(a);
117 for (int b : signed_test_values) {
118 SCOPED_TRACE(b);
119
Adam Langley518ba072017-04-20 13:51:11 -0700120 EXPECT_EQ(a, constant_time_select_int(CONSTTIME_TRUE_W, a, b));
121 EXPECT_EQ(b, constant_time_select_int(CONSTTIME_FALSE_W, a, b));
David Benjamind1c0de62017-03-16 11:38:18 -0400122
Adam Langley518ba072017-04-20 13:51:11 -0700123 EXPECT_EQ(FromBoolW(a == b), constant_time_eq_int(a, b));
David Benjamin81a191d2017-03-16 11:25:20 -0400124 EXPECT_EQ(FromBool8(a == b), constant_time_eq_int_8(a, b));
Adam Langley9ed9dae2014-11-04 11:22:01 -0800125 }
126 }
127
David Benjamin81a191d2017-03-16 11:25:20 -0400128 for (uint8_t a : test_values_8) {
129 SCOPED_TRACE(static_cast<int>(a));
130 for (uint8_t b : test_values_8) {
131 SCOPED_TRACE(static_cast<int>(b));
132 EXPECT_EQ(a, constant_time_select_8(CONSTTIME_TRUE_8, a, b));
133 EXPECT_EQ(b, constant_time_select_8(CONSTTIME_FALSE_8, a, b));
Adam Langley9ed9dae2014-11-04 11:22:01 -0800134 }
135 }
Adam Langley9ed9dae2014-11-04 11:22:01 -0800136}