Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/constant_time_test.cc

raw
1 // Copyright (c) 2014 The OpenSSL Project. All rights reserved.
2 // Author: Emilia Kasper ([email protected])
3 // Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley (Google).
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 // Utilities for constant-time cryptography.
7
8 #include "internal.h"
9 #include "test/test_util.h"
10
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <limits>
16
17 #include <gtest/gtest.h>
18
19 #include <openssl/mem.h>
20 #include <openssl/rand.h>
21
22
23 static uint8_t FromBool8(bool b) {
24 return b ? CONSTTIME_TRUE_8 : CONSTTIME_FALSE_8;
25 }
26
27 static crypto_word_t FromBoolW(bool b) {
28 return b ? CONSTTIME_TRUE_W : CONSTTIME_FALSE_W;
29 }
30
31 static const uint8_t test_values_8[] = {0, 1, 2, 20, 32, 127, 128, 129, 255};
32
33 static crypto_word_t test_values_w[] = {
34 0,
35 1,
36 1024,
37 12345,
38 32000,
39 #if defined(OPENSSL_64_BIT)
40 0xffffffff / 2 - 1,
41 0xffffffff / 2,
42 0xffffffff / 2 + 1,
43 0xffffffff - 1,
44 0xffffffff,
45 #endif
46 std::numeric_limits<crypto_word_t>::max() / 2 - 1,
47 std::numeric_limits<crypto_word_t>::max() / 2,
48 std::numeric_limits<crypto_word_t>::max() / 2 + 1,
49 std::numeric_limits<crypto_word_t>::max() - 1,
50 std::numeric_limits<crypto_word_t>::max(),
51 };
52
53 static int signed_test_values[] = {
54 0, 1, -1, 1024, -1024, 12345, -12345,
55 32000, -32000, INT_MAX, INT_MIN, INT_MAX - 1, INT_MIN + 1};
56
57 TEST(ConstantTimeTest, Test) {
58 for (crypto_word_t a : test_values_w) {
59 SCOPED_TRACE(a);
60
61 EXPECT_EQ(FromBoolW(a == 0), constant_time_is_zero_w(a));
62 EXPECT_EQ(FromBool8(a == 0), constant_time_is_zero_8(a));
63
64 for (crypto_word_t b : test_values_w) {
65 SCOPED_TRACE(b);
66
67 EXPECT_EQ(FromBoolW(a < b), constant_time_lt_w(a, b));
68 EXPECT_EQ(FromBool8(a < b), constant_time_lt_8(a, b));
69
70 EXPECT_EQ(FromBoolW(a >= b), constant_time_ge_w(a, b));
71 EXPECT_EQ(FromBool8(a >= b), constant_time_ge_8(a, b));
72
73 EXPECT_EQ(FromBoolW(a == b), constant_time_eq_w(a, b));
74 EXPECT_EQ(FromBool8(a == b), constant_time_eq_8(a, b));
75
76 EXPECT_EQ(a, constant_time_select_w(CONSTTIME_TRUE_W, a, b));
77 EXPECT_EQ(b, constant_time_select_w(CONSTTIME_FALSE_W, a, b));
78 }
79 }
80
81 for (int a : signed_test_values) {
82 SCOPED_TRACE(a);
83 for (int b : signed_test_values) {
84 SCOPED_TRACE(b);
85
86 EXPECT_EQ(a, constant_time_select_int(CONSTTIME_TRUE_W, a, b));
87 EXPECT_EQ(b, constant_time_select_int(CONSTTIME_FALSE_W, a, b));
88
89 EXPECT_EQ(FromBoolW(a == b), constant_time_eq_int(a, b));
90 EXPECT_EQ(FromBool8(a == b), constant_time_eq_int_8(a, b));
91 }
92 }
93
94 for (uint8_t a : test_values_8) {
95 SCOPED_TRACE(static_cast<int>(a));
96 for (uint8_t b : test_values_8) {
97 SCOPED_TRACE(static_cast<int>(b));
98 EXPECT_EQ(a, constant_time_select_8(CONSTTIME_TRUE_8, a, b));
99 EXPECT_EQ(b, constant_time_select_8(CONSTTIME_FALSE_8, a, b));
100 }
101 }
102
103 // Test constant_time_select_array_w.
104 crypto_word_t a[256], b[256], c[256];
105 RAND_bytes(reinterpret_cast<uint8_t *>(a), sizeof(a));
106 RAND_bytes(reinterpret_cast<uint8_t *>(b), sizeof(b));
107 RAND_bytes(reinterpret_cast<uint8_t *>(c), sizeof(c));
108
109 constant_time_select_array_w(c, a, b, FromBoolW(true), 256);
110 EXPECT_EQ(0, OPENSSL_memcmp(c, a, sizeof(c)));
111 constant_time_select_array_w(c, a, b, FromBoolW(false), 256);
112 EXPECT_EQ(0, OPENSSL_memcmp(c, b, sizeof(c)));
113
114 // Test constant_time_select_entry_from_table_w.
115 // The table consists of 100 entries, each of size 10 crypto_word_t's.
116 crypto_word_t table[100 * 10];
117 crypto_word_t entry[10];
118 RAND_bytes(reinterpret_cast<uint8_t *>(table), sizeof(table));
119 RAND_bytes(reinterpret_cast<uint8_t *>(entry), sizeof(entry));
120
121 for (size_t i = 0; i < 100; i++) {
122 constant_time_select_entry_from_table_w(entry, table, i, 100, 10);
123 EXPECT_EQ(0, OPENSSL_memcmp(entry, &table[i * 10], sizeof(entry)));
124 }
125
126 // Test that the output remains unchanged when index is out of bounds.
127 crypto_word_t entry_copy[10];
128 OPENSSL_memcpy(entry_copy, entry, sizeof(entry));
129 constant_time_select_entry_from_table_w(entry, table, 101, 100, 10);
130 EXPECT_EQ(0, OPENSSL_memcmp(entry_copy, entry, sizeof(entry)));
131 }
132
133 TEST(ConstantTimeTest, MemCmp) {
134 uint8_t buf[256], copy[256];
135 RAND_bytes(buf, sizeof(buf));
136
137 OPENSSL_memcpy(copy, buf, sizeof(buf));
138 EXPECT_EQ(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
139
140 for (size_t i = 0; i < sizeof(buf); i++) {
141 for (uint8_t bit = 1; bit != 0; bit <<= 1) {
142 OPENSSL_memcpy(copy, buf, sizeof(buf));
143 copy[i] ^= bit;
144 EXPECT_NE(0, CRYPTO_memcmp(buf, copy, sizeof(buf)));
145 }
146 }
147 }
148
149 TEST(ConstantTimeTest, ValueBarrier) {
150 for (int i = 0; i < 10; i++) {
151 crypto_word_t word;
152 RAND_bytes(reinterpret_cast<uint8_t *>(&word), sizeof(word));
153 EXPECT_EQ(word, value_barrier_w(word));
154
155 uint32_t u32;
156 RAND_bytes(reinterpret_cast<uint8_t *>(&u32), sizeof(u32));
157 EXPECT_EQ(u32, value_barrier_u32(u32));
158
159 uint64_t u64;
160 RAND_bytes(reinterpret_cast<uint8_t *>(&u64), sizeof(u64));
161 EXPECT_EQ(u64, value_barrier_u64(u64));
162 }
163 }