utils: Add a constant time memeq() variant for cryptographic purposes
[strongswan.git] / scripts / timeattack.c
1 #include <stdio.h>
2 #include <time.h>
3
4 #include <library.h>
5
6 typedef bool (*attackfn_t)(void *subj, u_char *data, size_t len);
7
8 static void start_timing(struct timespec *start)
9 {
10 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, start);
11 }
12
13 static u_int64_t end_timing(struct timespec *start)
14 {
15 struct timespec end;
16
17 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
18 return (end.tv_nsec - start->tv_nsec) +
19 (end.tv_sec - start->tv_sec) * 1000000000;
20 }
21
22 static int intcmp(const void *a, const void *b)
23 {
24 return *(u_int64_t*)a - *(u_int64_t*)b;
25 }
26
27 static u_int64_t median(u_int64_t *m, int count)
28 {
29 qsort(m, count, sizeof(u_int64_t), intcmp);
30 return m[count / 2];
31 }
32
33 static bool timeattack(attackfn_t attackfn, void *subj, size_t dlen,
34 u_int iterations, u_int distance)
35 {
36 struct timespec start;
37 u_char test[dlen];
38 u_int64_t mini, maxi, t[256], m[256][10];
39 float fastdist = 0, slowdist = 0;
40 int i, j, k, l, byte, limit, retry = 0;
41 int fastest = 0, slowest = 0;
42
43 memset(test, 0, dlen);
44
45 /* do some iterations to fill caches */
46 for (i = 0; i < iterations; i++)
47 {
48 attackfn(subj, test, dlen);
49 }
50
51 for (byte = 0; byte < dlen;)
52 {
53 memset(t, 0, sizeof(t));
54 memset(m, 0, sizeof(m));
55
56 limit = iterations * (retry + 1);
57
58 /* measure timing for all patterns in next byte */
59 for (k = 0; k < 10; k++)
60 {
61 for (j = 0; j < 256; j++)
62 {
63 for (l = 0; l < 100; l++)
64 {
65 test[byte] = j;
66 start_timing(&start);
67 for (i = 0; i < limit; i++)
68 {
69 attackfn(subj, test, dlen);
70 }
71 m[j][k] += end_timing(&start);
72 }
73 }
74 }
75
76 for (j = 0; j < 256; j++)
77 {
78 t[j] = median(m[j], countof(m[j]));
79 }
80
81 /* find fastest/slowest runs */
82 mini = ~0;
83 maxi = 0;
84 for (j = 0; j < 256; j++)
85 {
86 if (t[j] < mini)
87 {
88 mini = min(t[j], mini);
89 fastest = j;
90 }
91 if (t[j] > maxi)
92 {
93 maxi = max(t[j], maxi);
94 slowest = j;
95 }
96 }
97 /* calculate distance to next result */
98 mini = ~0;
99 maxi = 0;
100 for (j = 0; j < 256; j++)
101 {
102 if (fastest != j && t[j] < mini)
103 {
104 mini = min(t[j], mini);
105 fastdist = (float)(t[j] - t[fastest]) / distance;
106 }
107 if (slowest != j && t[j] > maxi)
108 {
109 maxi = max(t[j], maxi);
110 slowdist = (float)(t[slowest] - t[j]) / distance;
111 }
112 }
113 if (fastdist > 1.0f)
114 {
115 fprintf(stderr, "byte %02d: %02x (fastest, dist %02.2f)\n",
116 byte, fastest, fastdist);
117 test[byte] = fastest;
118 retry = 0;
119 byte++;
120 }
121 else if (slowdist > 1.0f)
122 {
123 fprintf(stderr, "byte %02d: %02x (slowest, dist %02.2f)\n",
124 byte, slowest, slowdist);
125 test[byte] = slowest;
126 retry = 0;
127 byte++;
128 }
129 else
130 {
131 if (retry++ > 5 && byte > 0)
132 {
133 fprintf(stderr, "distance fastest %02.2f (%02x), "
134 "slowest %02.2f (%02x), stepping back\n",
135 fastdist, fastest, slowdist, slowest);
136 test[byte--] = 0;
137 }
138 else if (retry < 10)
139 {
140 fprintf(stderr, "distance fastest %02.2f (%02x), "
141 "slowest %02.2f (%02x), retrying (%d)\n",
142 fastdist, fastest, slowdist, slowest, retry);
143 }
144 else
145 {
146 printf("attack failed, giving up\n");
147 return FALSE;
148 }
149 }
150 }
151 if (attackfn(subj, test, dlen))
152 {
153 printf("attack successful with %b\n", test, dlen);
154 return TRUE;
155 }
156 printf("attack failed with %b\n", test, dlen);
157 return FALSE;
158 }
159
160 CALLBACK(attack_memeq1, bool,
161 u_char *subj, u_char *data, size_t len)
162 {
163 return memeq(data, subj, len);
164 }
165
166 CALLBACK(attack_memeq2, bool,
167 u_char *subj, u_char *data, size_t len)
168 {
169 return memeq(subj, data, len);
170 }
171
172 CALLBACK(attack_memeq3, bool,
173 u_char *subj, u_char *data, size_t len)
174 {
175 int i;
176
177 for (i = 0; i < len; i++)
178 {
179 if (subj[i] != data[i])
180 {
181 return FALSE;
182 }
183 }
184 return TRUE;
185 }
186
187 CALLBACK(attack_memeq4, bool,
188 u_char *subj, u_char *data, size_t len)
189 {
190 int i, m = 0;
191
192 for (i = 0; i < len; i++)
193 {
194 m |= subj[i] != data[i];
195 }
196 return !m;
197 }
198
199 CALLBACK(attack_memeq5, bool,
200 u_char *subj, u_char *data, size_t len)
201 {
202 return memeq_const(subj, data, len);
203 }
204
205 static bool attack_memeq(char *name, u_int iterations, u_int distance)
206 {
207 struct {
208 char *name;
209 attackfn_t fn;
210 } attacks[] = {
211 { "memeq1", attack_memeq1 },
212 { "memeq2", attack_memeq2 },
213 { "memeq3", attack_memeq3 },
214 { "memeq4", attack_memeq4 },
215 { "memeq5", attack_memeq5 },
216 };
217 u_char exp[16];
218 int i;
219
220 srandom(time(NULL));
221 for (i = 0; i < sizeof(exp); i++)
222 {
223 exp[i] = random();
224 }
225 fprintf(stderr, "attacking %b\n", exp, sizeof(exp));
226
227 for (i = 0; i < countof(attacks); i++)
228 {
229 if (streq(name, attacks[i].name))
230 {
231 return timeattack(attacks[i].fn, exp, sizeof(exp),
232 iterations, distance);
233 }
234 }
235 return FALSE;
236 }
237
238 CALLBACK(attack_aead, bool,
239 aead_t *aead, u_char *data, size_t len)
240 {
241 u_char iv[aead->get_iv_size(aead)];
242
243 memset(iv, 0, sizeof(iv));
244 return aead->decrypt(aead, chunk_create(data, len), chunk_empty,
245 chunk_from_thing(iv), NULL);
246 }
247
248 static bool attack_aeads(encryption_algorithm_t alg, size_t key_size,
249 u_int iterations, u_int distance)
250 {
251 u_char buf[64];
252 aead_t *aead;
253 bool res;
254
255 aead = lib->crypto->create_aead(lib->crypto, alg, key_size, 0);
256 if (!aead)
257 {
258 fprintf(stderr, "creating AEAD %N failed\n",
259 encryption_algorithm_names, alg);
260 return FALSE;
261 }
262 memset(buf, 0xe3, sizeof(buf));
263 if (!aead->set_key(aead, chunk_create(buf, aead->get_key_size(aead))))
264 {
265 aead->destroy(aead);
266 return FALSE;
267 }
268 memset(buf, 0, aead->get_iv_size(aead));
269 if (!aead->encrypt(aead, chunk_create(buf, 0), chunk_empty,
270 chunk_create(buf, aead->get_iv_size(aead)), NULL))
271 {
272 aead->destroy(aead);
273 return FALSE;
274 }
275 fprintf(stderr, "attacking %b\n", buf, aead->get_icv_size(aead));
276
277 res = timeattack(attack_aead, aead, aead->get_icv_size(aead),
278 iterations, distance);
279 aead->destroy(aead);
280 return res;
281 }
282
283 CALLBACK(attack_signer, bool,
284 signer_t *signer, u_char *data, size_t len)
285 {
286 return signer->verify_signature(signer, chunk_empty, chunk_create(data, len));
287 }
288
289 static bool attack_signers(integrity_algorithm_t alg,
290 u_int iterations, u_int distance)
291 {
292 u_char buf[64];
293 signer_t *signer;
294 bool res;
295
296 signer = lib->crypto->create_signer(lib->crypto, alg);
297 if (!signer)
298 {
299 fprintf(stderr, "creating signer %N failed\n",
300 integrity_algorithm_names, alg);
301 return FALSE;
302 }
303 memset(buf, 0xe3, sizeof(buf));
304 if (!signer->set_key(signer, chunk_create(buf, signer->get_key_size(signer))))
305 {
306 signer->destroy(signer);
307 return FALSE;
308 }
309 if (!signer->get_signature(signer, chunk_empty, buf))
310 {
311 signer->destroy(signer);
312 return FALSE;
313 }
314 fprintf(stderr, "attacking %b\n", buf, signer->get_block_size(signer));
315
316 res = timeattack(attack_signer, signer, signer->get_block_size(signer),
317 iterations, distance);
318 signer->destroy(signer);
319 return res;
320 }
321
322 static bool attack_transform(char *name, u_int iterations, u_int distance)
323 {
324 const proposal_token_t *token;
325
326 token = lib->proposal->get_token(lib->proposal, name);
327 if (!token)
328 {
329 fprintf(stderr, "algorithm '%s' unknown\n", name);
330 return FALSE;
331 }
332
333 switch (token->type)
334 {
335 case ENCRYPTION_ALGORITHM:
336 if (encryption_algorithm_is_aead(token->algorithm))
337 {
338 return attack_aeads(token->algorithm, token->keysize / 8,
339 iterations, distance);
340 }
341 fprintf(stderr, "can't attack a crypter\n");
342 return FALSE;
343 case INTEGRITY_ALGORITHM:
344 return attack_signers(token->algorithm, iterations, distance);
345 default:
346 fprintf(stderr, "can't attack a %N\n", transform_type_names, token->type);
347 return FALSE;
348 }
349 }
350
351 int main(int argc, char *argv[])
352 {
353 library_init(NULL, "timeattack");
354 atexit(library_deinit);
355 lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
356
357 if (argc < 3)
358 {
359 fprintf(stderr, "usage: %s <attack> <iterations> <distance>\n", argv[0]);
360 fprintf(stderr, " <attack>: memeq[1-5] / aead / signer\n");
361 fprintf(stderr, " <iterations>: number of invocations * 1000\n");
362 fprintf(stderr, " <distance>: time difference in ns for a hit\n");
363 fprintf(stderr, " example: %s memeq1 100 500\n", argv[0]);
364 fprintf(stderr, " example: %s aes128gcm16 100 4000\n", argv[0]);
365 return 1;
366 }
367 if (strpfx(argv[1], "memeq"))
368 {
369 return !attack_memeq(argv[1], atoi(argv[2]), atoi(argv[3]));
370 }
371 return !attack_transform(argv[1], atoi(argv[2]), atoi(argv[3]));
372 }