scripts: Add a tool that tries to guess MAC/ICV values using validation times
[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 static bool attack_memeq(char *name, u_int iterations, u_int distance)
200 {
201 struct {
202 char *name;
203 attackfn_t fn;
204 } attacks[] = {
205 { "memeq1", attack_memeq1 },
206 { "memeq2", attack_memeq2 },
207 { "memeq3", attack_memeq3 },
208 { "memeq4", attack_memeq4 },
209 };
210 u_char exp[16];
211 int i;
212
213 srandom(time(NULL));
214 for (i = 0; i < sizeof(exp); i++)
215 {
216 exp[i] = random();
217 }
218 fprintf(stderr, "attacking %b\n", exp, sizeof(exp));
219
220 for (i = 0; i < countof(attacks); i++)
221 {
222 if (streq(name, attacks[i].name))
223 {
224 return timeattack(attacks[i].fn, exp, sizeof(exp),
225 iterations, distance);
226 }
227 }
228 return FALSE;
229 }
230
231 CALLBACK(attack_aead, bool,
232 aead_t *aead, u_char *data, size_t len)
233 {
234 u_char iv[aead->get_iv_size(aead)];
235
236 memset(iv, 0, sizeof(iv));
237 return aead->decrypt(aead, chunk_create(data, len), chunk_empty,
238 chunk_from_thing(iv), NULL);
239 }
240
241 static bool attack_aeads(encryption_algorithm_t alg, size_t key_size,
242 u_int iterations, u_int distance)
243 {
244 u_char buf[64];
245 aead_t *aead;
246 bool res;
247
248 aead = lib->crypto->create_aead(lib->crypto, alg, key_size, 0);
249 if (!aead)
250 {
251 fprintf(stderr, "creating AEAD %N failed\n",
252 encryption_algorithm_names, alg);
253 return FALSE;
254 }
255 memset(buf, 0xe3, sizeof(buf));
256 if (!aead->set_key(aead, chunk_create(buf, aead->get_key_size(aead))))
257 {
258 aead->destroy(aead);
259 return FALSE;
260 }
261 memset(buf, 0, aead->get_iv_size(aead));
262 if (!aead->encrypt(aead, chunk_create(buf, 0), chunk_empty,
263 chunk_create(buf, aead->get_iv_size(aead)), NULL))
264 {
265 aead->destroy(aead);
266 return FALSE;
267 }
268 fprintf(stderr, "attacking %b\n", buf, aead->get_icv_size(aead));
269
270 res = timeattack(attack_aead, aead, aead->get_icv_size(aead),
271 iterations, distance);
272 aead->destroy(aead);
273 return res;
274 }
275
276 CALLBACK(attack_signer, bool,
277 signer_t *signer, u_char *data, size_t len)
278 {
279 return signer->verify_signature(signer, chunk_empty, chunk_create(data, len));
280 }
281
282 static bool attack_signers(integrity_algorithm_t alg,
283 u_int iterations, u_int distance)
284 {
285 u_char buf[64];
286 signer_t *signer;
287 bool res;
288
289 signer = lib->crypto->create_signer(lib->crypto, alg);
290 if (!signer)
291 {
292 fprintf(stderr, "creating signer %N failed\n",
293 integrity_algorithm_names, alg);
294 return FALSE;
295 }
296 memset(buf, 0xe3, sizeof(buf));
297 if (!signer->set_key(signer, chunk_create(buf, signer->get_key_size(signer))))
298 {
299 signer->destroy(signer);
300 return FALSE;
301 }
302 if (!signer->get_signature(signer, chunk_empty, buf))
303 {
304 signer->destroy(signer);
305 return FALSE;
306 }
307 fprintf(stderr, "attacking %b\n", buf, signer->get_block_size(signer));
308
309 res = timeattack(attack_signer, signer, signer->get_block_size(signer),
310 iterations, distance);
311 signer->destroy(signer);
312 return res;
313 }
314
315 static bool attack_transform(char *name, u_int iterations, u_int distance)
316 {
317 const proposal_token_t *token;
318
319 token = lib->proposal->get_token(lib->proposal, name);
320 if (!token)
321 {
322 fprintf(stderr, "algorithm '%s' unknown\n", name);
323 return FALSE;
324 }
325
326 switch (token->type)
327 {
328 case ENCRYPTION_ALGORITHM:
329 if (encryption_algorithm_is_aead(token->algorithm))
330 {
331 return attack_aeads(token->algorithm, token->keysize / 8,
332 iterations, distance);
333 }
334 fprintf(stderr, "can't attack a crypter\n");
335 return FALSE;
336 case INTEGRITY_ALGORITHM:
337 return attack_signers(token->algorithm, iterations, distance);
338 default:
339 fprintf(stderr, "can't attack a %N\n", transform_type_names, token->type);
340 return FALSE;
341 }
342 }
343
344 int main(int argc, char *argv[])
345 {
346 library_init(NULL, "timeattack");
347 atexit(library_deinit);
348 lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
349
350 if (argc < 3)
351 {
352 fprintf(stderr, "usage: %s <attack> <iterations> <distance>\n", argv[0]);
353 fprintf(stderr, " <attack>: memeq[1-4] / aead / signer\n");
354 fprintf(stderr, " <iterations>: number of invocations * 1000\n");
355 fprintf(stderr, " <distance>: time difference in ns for a hit\n");
356 fprintf(stderr, " example: %s memeq1 100 500\n", argv[0]);
357 fprintf(stderr, " example: %s aes128gcm16 100 4000\n", argv[0]);
358 return 1;
359 }
360 if (strpfx(argv[1], "memeq"))
361 {
362 return !attack_memeq(argv[1], atoi(argv[2]), atoi(argv[3]));
363 }
364 return !attack_transform(argv[1], atoi(argv[2]), atoi(argv[3]));
365 }