-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.c
296 lines (240 loc) · 10.1 KB
/
checker.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <immintrin.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "combinations.h"
#include "checker_helper.h"
#include "checker.h"
#include "gray.h"
#ifdef VECT
int check_support(const struct comb_t comb_struct, const int nb_internal, __m256i probes_a_curr,
__m256i probes_b_curr, uint64_t probes_r_curr, __m256i **probes_a_all,
__m256i **probes_b_all, uint64_t *probes_r_all[NB_PR], int check_sni)
{
#ifdef GLITCH
uint64_t *counters = malloc(comb_struct.k*sizeof(uint64_t));
int *local_radices = malloc(comb_struct.k*sizeof(int));
int *corres = malloc(comb_struct.k*sizeof(int));
int nb_nontrivials = 0;
uint64_t c = 1;
uint64_t i;
int to_incr, to_incr_bin, p;
int attack = 0;
int mod = 1;
int *mods;
for (i = 0; i < comb_struct.k; i++) {
if (radices[comb_struct.combination[i]] > 1) {
//printf("nb: %d\n", nb_nontrivials);
counters[nb_nontrivials] = 1;
// There are $2^{radix}$ possible linear combination
local_radices[nb_nontrivials] = 1 << radices[comb_struct.combination[i]];
corres[nb_nontrivials] = i;
nb_nontrivials++;
}
}
mods = calloc(nb_nontrivials, sizeof(int));
for (int k = 0; k < nb_nontrivials; k++) {
mod *= local_radices[k];
mods[k] = mod;
}
// Check_initial expression
if (check_sni) {
attack |= check_attack_sni(comb_struct.k, nb_internal, probes_r_curr, probes_a_curr, probes_b_curr);
} else {
attack |= check_attack_ni(comb_struct.k, probes_r_curr, probes_a_curr, probes_b_curr);
}
// Which probe to increment
while ((to_incr = next_increment(&c, mods, nb_nontrivials)) != -1) {
// Increment lower-level gray code : which expression to increment
to_incr_bin = next_increment_bin(&(counters[to_incr]),
radices[comb_struct.combination[corres[to_incr]]]);
p = comb_struct.combination[corres[to_incr]];
probes_a_curr = _mm256_xor_si256(probes_a_curr, probes_a_all[p][to_incr_bin]);
probes_b_curr = _mm256_xor_si256(probes_b_curr, probes_b_all[p][to_incr_bin]);
probes_r_curr ^= probes_r_all[p][to_incr_bin];
if (counters[to_incr] == 0) {
// Avoid testing empty linear combination
to_incr_bin = next_increment_bin(&(counters[to_incr]),
radices[comb_struct.combination[corres[to_incr]]]);
p = comb_struct.combination[corres[to_incr]];
probes_a_curr = _mm256_xor_si256(probes_a_curr, probes_a_all[p][to_incr_bin]);
probes_b_curr = _mm256_xor_si256(probes_b_curr, probes_b_all[p][to_incr_bin]);
probes_r_curr ^= probes_r_all[p][to_incr_bin];
}
if (check_sni) {
attack |= check_attack_sni(comb_struct.k, nb_internal, probes_r_curr, probes_a_curr, probes_b_curr);
} else {
attack |= check_attack_ni(comb_struct.k, probes_r_curr, probes_a_curr, probes_b_curr);
}
}
free(counters);
free(local_radices);
free(corres);
free(mods);
return attack;
#endif
if (check_sni) {
return check_attack_sni(comb_struct.k, nb_internal, probes_r_curr, probes_a_curr, probes_b_curr);
} else {
return check_attack_ni(comb_struct.k, probes_r_curr, probes_a_curr, probes_b_curr);
}
}
int next_support(struct comb_t *comb_struct, struct comb_diff_t *comb_diff,
__m256i *probes_a_curr, __m256i *probes_b_curr, uint64_t *probes_r_curr,
__m256i *probes_a_all[NB_PR], __m256i *probes_b_all[NB_PR], uint64_t *probes_r_all[NB_PR],
uint64_t *nb_internal, int check_sni)
{
next_combination(comb_struct, comb_diff);
if (comb_struct->done) return -1;
// Adjust the number of internal probes
if (check_sni) {
if (comb_diff->to_del < NB_INT) (*nb_internal)--;
if (comb_diff->to_add < NB_INT) (*nb_internal)++;
}
*probes_a_curr = _mm256_xor_si256(*probes_a_curr, probes_a_all[comb_diff->to_del][0]);
*probes_a_curr = _mm256_xor_si256(*probes_a_curr, probes_a_all[comb_diff->to_add][0]);
*probes_b_curr = _mm256_xor_si256(*probes_b_curr, probes_b_all[comb_diff->to_del][0]);
*probes_b_curr = _mm256_xor_si256(*probes_b_curr, probes_b_all[comb_diff->to_add][0]);
*probes_r_curr ^= probes_r_all[comb_diff->to_del][0] ^ probes_r_all[comb_diff->to_add][0];
return 0;
}
int check_partial(struct comb_t comb_struct, uint64_t nb, int check_sni)
{
uint64_t nb_internal;
struct comb_diff_t comb_diff;
uint64_t probes_r_curr;
uint64_t *probes_r_all[NB_PR];
int attack = 0;
uint64_t c = 0;
__m256i probes_a_curr;
__m256i probes_b_curr;
__m256i *probes_a_all[NB_PR];
__m256i *probes_b_all[NB_PR];
init_all(probes_a_all, probes_b_all, probes_r_all);
init_sh_curr(&probes_a_curr, probes_a_all, comb_struct.combination, comb_struct.k);
init_sh_curr(&probes_b_curr, probes_b_all, comb_struct.combination, comb_struct.k);
init_r_curr(&probes_r_curr, probes_r_all, comb_struct.combination, comb_struct.k);
c = 0;
nb_internal = 0;
for (uint64_t i = 0; i < comb_struct.k; i++)
nb_internal += comb_struct.combination[i] < NB_INT ? 1 : 0;
while (!attack && c < nb) {
c++;
attack = check_support(comb_struct, nb_internal, probes_a_curr,
probes_b_curr, probes_r_curr, probes_a_all, probes_b_all,
probes_r_all, check_sni);
if (next_support(&comb_struct, &comb_diff, &probes_a_curr, &probes_b_curr,
&probes_r_curr, probes_a_all, probes_b_all, probes_r_all,
&nb_internal, check_sni)) {
break;
}
}
free_all(probes_a_all, probes_b_all, probes_r_all);
if (attack) {
printf("\n");
print_combination(comb_struct);
printf("to_del=%lu\n", comb_diff.to_del);
printf("to_add=%lu\n", comb_diff.to_add);
printf("\n------- UNSAFE -------\n");
if (check_sni) {
printf("ATTACK SNI FOUND\n");
} else {
printf("ATTACK NI FOUND\n");
}
return 0;
}
return 1;
}
#else /* Non-vectorised */
// if (check_sni) {
// attack_sni = check_attack_sni(comb_struct.k, nb_internal, probes_r_curr, probes_a_curr, probes_b_curr);
// } else {
// attack_ni = check_attack_ni(comb_struct.k, probes_r_curr, probes_a_curr, probes_b_curr);
// }
//int next_support(struct comb_t *comb_struct, struct comb_diff_t *comb_diff,
// __m256i *probes_a_curr, __m256i *probes_b_curr, uint64_t *probes_r_curr,
// __m256i *probes_a_all[NB_PR], __m256i *probes_b_all[NB_PR], uint64_t *probes_r_all[NB_PR],
// uint64_t *nb_internal, int check_sni)
//{
// next_combination(comb_struct, comb_diff);
// if (comb_struct->done) return -1;
//
// // Adjust the number of internal probes
// if (check_sni) {
// if (comb_diff->to_del < NB_INT) *nb_internal--;
// if (comb_diff->to_add < NB_INT) *nb_internal++;
// }
//
// probes_sh_xor(probes_a_curr, probes_sh_a[comb_diff.to_del]);
// probes_sh_xor(probes_a_curr, probes_sh_a[comb_diff.to_add]);
//
// probes_sh_xor(probes_b_curr, probes_sh_b[comb_diff.to_del]);
// probes_sh_xor(probes_b_curr, probes_sh_b[comb_diff.to_add]);
//
// probes_r_xor(probes_r_curr, probes_r[comb_diff.to_del]);
// probes_r_xor(probes_r_curr, probes_r[comb_diff.to_add]);
//
// }
int check_partial(struct comb_t comb_struct, uint64_t nb, int check_sni)
{
#ifdef GLITCH
fprintf(stderr, "\n/!\\ Non-vectorised verification of glitched gadgets is not implemented yet! /!\\\n");
exit(-1);
#endif /* GLITCH */
uint64_t nb_internal;
struct comb_diff_t comb_diff;
uint64_t probes_r_curr[SIZE_R];
int attack_sni = 0;
int attack_ni = 0;
uint64_t c = 0;
uint64_t probes_a_curr[NB_SH][SIZE_SH];
uint64_t probes_b_curr[NB_SH][SIZE_SH];
init_sh_curr(probes_a_curr, probes_sh_a, comb_struct.combination, comb_struct.k);
init_sh_curr(probes_b_curr, probes_sh_b, comb_struct.combination, comb_struct.k);
init_r_curr(probes_r_curr, comb_struct.combination, comb_struct.k);
c = 0;
nb_internal = 0;
for (uint64_t i = 0; i < comb_struct.k; i++)
nb_internal += comb_struct.combination[i] < NB_INT ? 1 : 0;
while (!attack_ni && !attack_sni && c < nb) {
c++;
if (check_sni) {
attack_sni = check_attack_sni(comb_struct.k, nb_internal, probes_r_curr, probes_a_curr, probes_b_curr);
} else {
attack_ni = check_attack_ni(comb_struct.k, probes_r_curr, probes_a_curr, probes_b_curr);
}
next_combination(&comb_struct, &comb_diff);
if (comb_struct.done) break;
// Adjust the number of internal probes
if (check_sni) {
if (comb_diff.to_del < NB_INT) nb_internal--;
if (comb_diff.to_add < NB_INT) nb_internal++;
}
probes_sh_xor(probes_a_curr, probes_sh_a[comb_diff.to_del]);
probes_sh_xor(probes_a_curr, probes_sh_a[comb_diff.to_add]);
probes_sh_xor(probes_b_curr, probes_sh_b[comb_diff.to_del]);
probes_sh_xor(probes_b_curr, probes_sh_b[comb_diff.to_add]);
probes_r_xor(probes_r_curr, probes_r[comb_diff.to_del]);
probes_r_xor(probes_r_curr, probes_r[comb_diff.to_add]);
// attack = check_support(comb_struct, nb_internal, probes_a_curr,
// probes_b_curr, probes_r_curr, probes_a_all, probes_b_all,
// probes_r_all, check_sni);
// if (next_support(&comb_struct, &comb_diff, &probes_a_curr, &probes_b_curr,
// &probes_r_curr, probes_a_all, probes_b_all, probes_r_all,
// &nb_internal, check_sni)) {
// break;
// }
}
if (attack_ni || attack_sni) {
printf("\n");
print_combination(comb_struct);
printf("to_del=%lu\n", comb_diff.to_del);
printf("to_add=%lu\n", comb_diff.to_add);
printf("\n------- UNSAFE -------\n");
if (attack_ni) printf("ATTACK NI FOUND\n");
if (attack_sni) printf("ATTACK SNI FOUND\n");
return 0;
}
return 1;
}
#endif /* VECT */