Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 1 | /* |
| 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 Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 52 | #include <limits> |
| 53 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 54 | #include <gtest/gtest.h> |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 55 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 56 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 57 | static unsigned FromBool(bool b) { |
| 58 | return b ? CONSTTIME_TRUE : CONSTTIME_FALSE; |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 59 | } |
| 60 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 61 | static uint8_t FromBool8(bool b) { |
| 62 | return b ? CONSTTIME_TRUE_8 : CONSTTIME_FALSE_8; |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 63 | } |
| 64 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 65 | static size_t FromBoolS(bool b) { |
| 66 | return b ? CONSTTIME_TRUE_S : CONSTTIME_FALSE_S; |
| 67 | } |
| 68 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 69 | static unsigned test_values[] = { |
| 70 | 0, |
| 71 | 1, |
| 72 | 1024, |
| 73 | 12345, |
| 74 | 32000, |
| 75 | UINT_MAX / 2 - 1, |
| 76 | UINT_MAX / 2, |
| 77 | UINT_MAX / 2 + 1, |
| 78 | UINT_MAX - 1, |
| 79 | UINT_MAX, |
| 80 | }; |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 81 | |
| 82 | static uint8_t test_values_8[] = {0, 1, 2, 20, 32, 127, 128, 129, 255}; |
| 83 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 84 | static size_t test_values_s[] = { |
| 85 | 0, |
| 86 | 1, |
| 87 | 1024, |
| 88 | 12345, |
| 89 | 32000, |
| 90 | #if defined(OPENSSL_64_BIT) |
| 91 | 0xffffffff / 2 - 1, |
| 92 | 0xffffffff / 2, |
| 93 | 0xffffffff / 2 + 1, |
| 94 | 0xffffffff - 1, |
| 95 | 0xffffffff, |
| 96 | #endif |
| 97 | std::numeric_limits<size_t>::max() / 2 - 1, |
| 98 | std::numeric_limits<size_t>::max() / 2, |
| 99 | std::numeric_limits<size_t>::max() / 2 + 1, |
| 100 | std::numeric_limits<size_t>::max() - 1, |
| 101 | std::numeric_limits<size_t>::max(), |
| 102 | }; |
| 103 | |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 104 | static int signed_test_values[] = { |
| 105 | 0, 1, -1, 1024, -1024, 12345, -12345, |
| 106 | 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1}; |
| 107 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 108 | TEST(ConstantTimeTest, Test) { |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 109 | for (size_t a : test_values_s) { |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 110 | SCOPED_TRACE(a); |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 111 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 112 | EXPECT_EQ(FromBoolS(a == 0), constant_time_is_zero_s(a)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 113 | EXPECT_EQ(FromBool8(a == 0), constant_time_is_zero_8(a)); |
| 114 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 115 | for (size_t b : test_values_s) { |
| 116 | SCOPED_TRACE(b); |
| 117 | |
| 118 | EXPECT_EQ(FromBoolS(a < b), constant_time_lt_s(a, b)); |
| 119 | EXPECT_EQ(FromBool8(a < b), constant_time_lt_8(a, b)); |
| 120 | |
| 121 | EXPECT_EQ(FromBoolS(a >= b), constant_time_ge_s(a, b)); |
| 122 | EXPECT_EQ(FromBool8(a >= b), constant_time_ge_8(a, b)); |
| 123 | |
| 124 | EXPECT_EQ(FromBoolS(a == b), constant_time_eq_s(a, b)); |
| 125 | EXPECT_EQ(FromBool8(a == b), constant_time_eq_8(a, b)); |
| 126 | |
| 127 | EXPECT_EQ(a, constant_time_select_s(CONSTTIME_TRUE_S, a, b)); |
| 128 | EXPECT_EQ(b, constant_time_select_s(CONSTTIME_FALSE_S, a, b)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | for (unsigned a : test_values) { |
| 133 | SCOPED_TRACE(a); |
| 134 | EXPECT_EQ(FromBool(a == 0), constant_time_is_zero(a)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 135 | for (unsigned b : test_values) { |
| 136 | SCOPED_TRACE(b); |
| 137 | |
| 138 | EXPECT_EQ(FromBool(a < b), constant_time_lt(a, b)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 139 | EXPECT_EQ(FromBool(a >= b), constant_time_ge(a, b)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 140 | EXPECT_EQ(FromBool(a == b), constant_time_eq(a, b)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 141 | |
| 142 | EXPECT_EQ(a, constant_time_select(CONSTTIME_TRUE, a, b)); |
| 143 | EXPECT_EQ(b, constant_time_select(CONSTTIME_FALSE, a, b)); |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 147 | for (int a : signed_test_values) { |
| 148 | SCOPED_TRACE(a); |
| 149 | for (int b : signed_test_values) { |
| 150 | SCOPED_TRACE(b); |
| 151 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 152 | // constant_time_select_int accepts both size_t and unsigned masks. |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 153 | EXPECT_EQ(a, constant_time_select_int(CONSTTIME_TRUE, a, b)); |
| 154 | EXPECT_EQ(b, constant_time_select_int(CONSTTIME_FALSE, a, b)); |
| 155 | |
David Benjamin | d1c0de6 | 2017-03-16 11:38:18 -0400 | [diff] [blame^] | 156 | EXPECT_EQ(a, constant_time_select_int(CONSTTIME_TRUE_S, a, b)); |
| 157 | EXPECT_EQ(b, constant_time_select_int(CONSTTIME_FALSE_S, a, b)); |
| 158 | |
| 159 | EXPECT_EQ(FromBoolS(a == b), constant_time_eq_int(a, b)); |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 160 | EXPECT_EQ(FromBool8(a == b), constant_time_eq_int_8(a, b)); |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
David Benjamin | 81a191d | 2017-03-16 11:25:20 -0400 | [diff] [blame] | 164 | for (uint8_t a : test_values_8) { |
| 165 | SCOPED_TRACE(static_cast<int>(a)); |
| 166 | for (uint8_t b : test_values_8) { |
| 167 | SCOPED_TRACE(static_cast<int>(b)); |
| 168 | EXPECT_EQ(a, constant_time_select_8(CONSTTIME_TRUE_8, a, b)); |
| 169 | EXPECT_EQ(b, constant_time_select_8(CONSTTIME_FALSE_8, a, b)); |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
Adam Langley | 9ed9dae | 2014-11-04 11:22:01 -0800 | [diff] [blame] | 172 | } |