This repository was archived by the owner on Aug 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathct.cpp
More file actions
115 lines (93 loc) · 2.53 KB
/
Copy pathct.cpp
File metadata and controls
115 lines (93 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Those tests work only with Clang and Memory Sanitizer
#include <gtest/gtest.h>
#include <common/ct_check.h>
#include <common/utils.h>
TEST(ConstantTime, CtCheck_Negative) {
unsigned char a[16], b[16];
unsigned i;
memset(a, 42, 16);
memset(b, 42, 16);
ct_poison(a, 16);
for (i = 0; i < 16; i++) {
ct_expect_uum();
if (a[i] != b[i]) {
break;
}
ct_require_uum();
}
ct_purify(a, 16);
// Ensure buffers are not optimized-out
ASSERT_EQ(a[0], b[0]);
}
TEST(ConstantTime, CtCheck_Positive_NoAccess) {
unsigned i;
char result = 0;
unsigned char a[16], b[16];
memset(a, 42, sizeof(a));
memset(b, 42, sizeof(b));
ct_poison(a, 16);
for (i = 0; i < 16; i++) {
result |= a[i] ^ b[i];
}
ct_purify(a, 16);
// Purify result, to allow check that otherwise
// would be not constant-time.
ct_purify(&result, 1);
ASSERT_EQ(result, 0);
}
TEST(ConstantTime, CtCheck_Negative_UseSecretAsIndex) {
static const unsigned char tab[2] = {1, 0};
unsigned char a[16];
unsigned char result;
memset(a, 42, sizeof(a));
ct_poison(a, 16);
ct_expect_uum();
result = tab[a[0] & 1];
ct_require_uum();
ct_purify(a, 16);
// Ensure variables are not optimized-out
ct_purify(&result, 1);
ASSERT_EQ(result, 1);
}
TEST(ConstantTime, CtCheck_memcmp) {
unsigned char a[16], b[16];
memset(a, 42, sizeof(a));
memset(b, 42, sizeof(b));
uint8_t ret;
ct_poison(a, 16);
ret = ct_memcmp(a,b,16);
ct_expect_uum();
// Doesn't matter what we check. It's just to
// enusre UMR is triggered.
if (!ret) ASSERT_EQ(ret, 0);
ct_require_uum();
ct_purify(&ret, 1);
b[1] = 0;
ct_expect_uum();
ret = ct_memcmp(a,b,16);
if (ret) ASSERT_EQ(ret,1);
ct_require_uum();
ct_purify(&ret, 1);
}
TEST(ConstantTime, CtCheck_memcmp_chained) {
unsigned char a[16], b[16], c[16], d[16];
memset(a, 42, sizeof(a));
memset(b, 42, sizeof(b));
memset(d, 42, sizeof(b));
memset(c, 41, sizeof(c));
uint8_t ret;
ct_poison(a, 16);
ct_expect_uum();
// obviously must generate UMR if first check fails
// and second is not done
ret = (ct_memcmp(a,c,16)==0) && (ct_memcmp(a,b,16)==0);
ct_require_uum();
ct_purify(&ret, 1);
ASSERT_EQ(ret,0);
ct_expect_uum();
// it's still UMR even if both checks are OK
ret = (ct_memcmp(a,d,16)==0) && (ct_memcmp(a,b,16)==0);
ct_require_uum();
ct_purify(&ret, 1);
ASSERT_EQ(ret,1);
}