constant_time_test.cc
Go to the documentation of this file.
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 
52 #include <limits>
53 
54 #include <gtest/gtest.h>
55 
56 #include <openssl/mem.h>
57 #include <openssl/rand.h>
58 
59 
60 static uint8_t FromBool8(bool b) {
62 }
63 
64 static crypto_word_t FromBoolW(bool b) {
66 }
67 
68 static const uint8_t test_values_8[] = {0, 1, 2, 20, 32, 127, 128, 129, 255};
69 
70 static crypto_word_t test_values_w[] = {
71  0,
72  1,
73  1024,
74  12345,
75  32000,
76 #if defined(OPENSSL_64_BIT)
77  0xffffffff / 2 - 1,
78  0xffffffff / 2,
79  0xffffffff / 2 + 1,
80  0xffffffff - 1,
81  0xffffffff,
82 #endif
88 };
89 
90 static int signed_test_values[] = {
91  0, 1, -1, 1024, -1024, 12345, -12345,
92  32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1};
93 
94 TEST(ConstantTimeTest, Test) {
95  for (crypto_word_t a : test_values_w) {
96  SCOPED_TRACE(a);
97 
100 
101  for (crypto_word_t b : test_values_w) {
102  SCOPED_TRACE(b);
103 
106 
109 
112 
115  }
116  }
117 
118  for (int a : signed_test_values) {
119  SCOPED_TRACE(a);
120  for (int b : signed_test_values) {
121  SCOPED_TRACE(b);
122 
125 
128  }
129  }
130 
131  for (uint8_t a : test_values_8) {
132  SCOPED_TRACE(static_cast<int>(a));
133  for (uint8_t b : test_values_8) {
134  SCOPED_TRACE(static_cast<int>(b));
137  }
138  }
139 }
140 
141 TEST(ConstantTimeTest, MemCmp) {
142  uint8_t buf[256], copy[256];
143  RAND_bytes(buf, sizeof(buf));
144 
145  OPENSSL_memcpy(copy, buf, sizeof(buf));
146  EXPECT_EQ(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
147 
148  for (size_t i = 0; i < sizeof(buf); i++) {
149  for (uint8_t bit = 1; bit != 0; bit <<= 1) {
150  OPENSSL_memcpy(copy, buf, sizeof(buf));
151  copy[i] ^= bit;
152  EXPECT_NE(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
153  }
154  }
155 }
156 
157 TEST(ConstantTimeTest, ValueBarrier) {
158  for (int i = 0; i < 10; i++) {
159  crypto_word_t word;
160  RAND_bytes(reinterpret_cast<uint8_t *>(&word), sizeof(word));
161  EXPECT_EQ(word, value_barrier_w(word));
162 
163  uint32_t u32;
164  RAND_bytes(reinterpret_cast<uint8_t *>(&u32), sizeof(u32));
165  EXPECT_EQ(u32, value_barrier_u32(u32));
166 
167  uint64_t u64;
168  RAND_bytes(reinterpret_cast<uint8_t *>(&u64), sizeof(u64));
169  EXPECT_EQ(u64, value_barrier_u64(u64));
170  }
171 }
RAND_bytes
#define RAND_bytes
Definition: boringssl_prefix_symbols.h:2060
value_barrier_u32
static uint32_t value_barrier_u32(uint32_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:296
FromBoolW
static crypto_word_t FromBoolW(bool b)
Definition: constant_time_test.cc:64
CONSTTIME_TRUE_8
#define CONSTTIME_TRUE_8
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:278
signed_test_values
static int signed_test_values[]
Definition: constant_time_test.cc:90
CONSTTIME_TRUE_W
#define CONSTTIME_TRUE_W
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:276
internal.h
test_values_8
static const uint8_t test_values_8[]
Definition: constant_time_test.cc:68
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
constant_time_is_zero_w
static crypto_word_t constant_time_is_zero_w(crypto_word_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:372
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
constant_time_ge_w
static crypto_word_t constant_time_ge_w(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:360
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
constant_time_lt_8
static uint8_t constant_time_lt_8(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:355
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
CONSTTIME_FALSE_W
#define CONSTTIME_FALSE_W
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:277
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
constant_time_select_int
static int constant_time_select_int(crypto_word_t mask, int a, int b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:441
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
test_values_w
static crypto_word_t test_values_w[]
Definition: constant_time_test.cc:70
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
TEST
TEST(ConstantTimeTest, Test)
Definition: constant_time_test.cc:94
constant_time_eq_int_8
static uint8_t constant_time_eq_int_8(int a, int b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:413
constant_time_ge_8
static uint8_t constant_time_ge_8(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:367
constant_time_eq_8
static uint8_t constant_time_eq_8(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:401
value_barrier_w
static crypto_word_t value_barrier_w(crypto_word_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:288
FromBool8
static uint8_t FromBool8(bool b)
Definition: constant_time_test.cc:60
rand.h
value_barrier_u64
static uint64_t value_barrier_u64(uint64_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:304
CONSTTIME_FALSE_8
#define CONSTTIME_FALSE_8
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:279
constant_time_eq_int
static crypto_word_t constant_time_eq_int(int a, int b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:407
constant_time_select_w
static crypto_word_t constant_time_select_w(crypto_word_t mask, crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:420
constant_time_is_zero_8
static uint8_t constant_time_is_zero_8(crypto_word_t a)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:389
mem.h
constant_time_select_8
static uint8_t constant_time_select_8(uint8_t mask, uint8_t a, uint8_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:434
constant_time_eq_w
static crypto_word_t constant_time_eq_w(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:394
Test
Definition: hpack_parser_test.cc:43
CRYPTO_memcmp
#define CRYPTO_memcmp
Definition: boringssl_prefix_symbols.h:1178
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
constant_time_lt_w
static crypto_word_t constant_time_lt_w(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:318


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01