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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
#include <stdio.h>
#include <time.h>
#include <library.h>
typedef bool (*attackfn_t)(void *subj, u_char *data, size_t len);
static void start_timing(struct timespec *start)
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, start);
}
static uint64_t end_timing(struct timespec *start)
{
struct timespec end;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
return (end.tv_nsec - start->tv_nsec) +
(end.tv_sec - start->tv_sec) * 1000000000;
}
static int intcmp(const void *a, const void *b)
{
return *(uint64_t*)a - *(uint64_t*)b;
}
static uint64_t median(uint64_t *m, int count)
{
qsort(m, count, sizeof(uint64_t), intcmp);
return m[count / 2];
}
static bool timeattack(attackfn_t attackfn, void *subj, size_t dlen,
u_int iterations, u_int distance)
{
struct timespec start;
u_char test[dlen];
uint64_t mini, maxi, t[256], m[256][10];
float fastdist = 0, slowdist = 0;
int i, j, k, l, byte, limit, retry = 0;
int fastest = 0, slowest = 0;
memset(test, 0, dlen);
/* do some iterations to fill caches */
for (i = 0; i < iterations; i++)
{
attackfn(subj, test, dlen);
}
for (byte = 0; byte < dlen;)
{
memset(t, 0, sizeof(t));
memset(m, 0, sizeof(m));
limit = iterations * (retry + 1);
/* measure timing for all patterns in next byte */
for (k = 0; k < 10; k++)
{
for (j = 0; j < 256; j++)
{
for (l = 0; l < 100; l++)
{
test[byte] = j;
start_timing(&start);
for (i = 0; i < limit; i++)
{
attackfn(subj, test, dlen);
}
m[j][k] += end_timing(&start);
}
}
}
for (j = 0; j < 256; j++)
{
t[j] = median(m[j], countof(m[j]));
}
/* find fastest/slowest runs */
mini = ~0;
maxi = 0;
for (j = 0; j < 256; j++)
{
if (t[j] < mini)
{
mini = min(t[j], mini);
fastest = j;
}
if (t[j] > maxi)
{
maxi = max(t[j], maxi);
slowest = j;
}
}
/* calculate distance to next result */
mini = ~0;
maxi = 0;
for (j = 0; j < 256; j++)
{
if (fastest != j && t[j] < mini)
{
mini = min(t[j], mini);
fastdist = (float)(t[j] - t[fastest]) / distance;
}
if (slowest != j && t[j] > maxi)
{
maxi = max(t[j], maxi);
slowdist = (float)(t[slowest] - t[j]) / distance;
}
}
if (fastdist > 1.0f)
{
fprintf(stderr, "byte %02d: %02x (fastest, dist %02.2f)\n",
byte, fastest, fastdist);
test[byte] = fastest;
retry = 0;
byte++;
}
else if (slowdist > 1.0f)
{
fprintf(stderr, "byte %02d: %02x (slowest, dist %02.2f)\n",
byte, slowest, slowdist);
test[byte] = slowest;
retry = 0;
byte++;
}
else
{
if (retry++ > 5 && byte > 0)
{
fprintf(stderr, "distance fastest %02.2f (%02x), "
"slowest %02.2f (%02x), stepping back\n",
fastdist, fastest, slowdist, slowest);
test[byte--] = 0;
}
else if (retry < 10)
{
fprintf(stderr, "distance fastest %02.2f (%02x), "
"slowest %02.2f (%02x), retrying (%d)\n",
fastdist, fastest, slowdist, slowest, retry);
}
else
{
printf("attack failed, giving up\n");
return FALSE;
}
}
}
if (attackfn(subj, test, dlen))
{
printf("attack successful with %b\n", test, dlen);
return TRUE;
}
printf("attack failed with %b\n", test, dlen);
return FALSE;
}
CALLBACK(attack_memeq1, bool,
u_char *subj, u_char *data, size_t len)
{
return memeq(data, subj, len);
}
CALLBACK(attack_memeq2, bool,
u_char *subj, u_char *data, size_t len)
{
return memeq(subj, data, len);
}
CALLBACK(attack_memeq3, bool,
u_char *subj, u_char *data, size_t len)
{
int i;
for (i = 0; i < len; i++)
{
if (subj[i] != data[i])
{
return FALSE;
}
}
return TRUE;
}
CALLBACK(attack_memeq4, bool,
u_char *subj, u_char *data, size_t len)
{
int i, m = 0;
for (i = 0; i < len; i++)
{
m |= subj[i] != data[i];
}
return !m;
}
CALLBACK(attack_memeq5, bool,
u_char *subj, u_char *data, size_t len)
{
return memeq_const(subj, data, len);
}
static bool attack_memeq(char *name, u_int iterations, u_int distance)
{
struct {
char *name;
attackfn_t fn;
} attacks[] = {
{ "memeq1", attack_memeq1 },
{ "memeq2", attack_memeq2 },
{ "memeq3", attack_memeq3 },
{ "memeq4", attack_memeq4 },
{ "memeq5", attack_memeq5 },
};
u_char exp[16];
int i;
srandom(time(NULL));
for (i = 0; i < sizeof(exp); i++)
{
exp[i] = random();
}
fprintf(stderr, "attacking %b\n", exp, sizeof(exp));
for (i = 0; i < countof(attacks); i++)
{
if (streq(name, attacks[i].name))
{
return timeattack(attacks[i].fn, exp, sizeof(exp),
iterations, distance);
}
}
return FALSE;
}
CALLBACK(attack_chunk1, bool,
u_char *subj, u_char *data, size_t len)
{
return chunk_equals(chunk_create(subj, len), chunk_create(data, len));
}
CALLBACK(attack_chunk2, bool,
u_char *subj, u_char *data, size_t len)
{
return chunk_equals_const(chunk_create(subj, len), chunk_create(data, len));
}
static bool attack_chunk(char *name, u_int iterations, u_int distance)
{
struct {
char *name;
attackfn_t fn;
} attacks[] = {
{ "chunk1", attack_chunk1 },
{ "chunk2", attack_chunk2 },
};
u_char exp[16];
int i;
srandom(time(NULL));
for (i = 0; i < sizeof(exp); i++)
{
exp[i] = random();
}
fprintf(stderr, "attacking %b\n", exp, sizeof(exp));
for (i = 0; i < countof(attacks); i++)
{
if (streq(name, attacks[i].name))
{
return timeattack(attacks[i].fn, exp, sizeof(exp),
iterations, distance);
}
}
return FALSE;
}
CALLBACK(attack_aead, bool,
aead_t *aead, u_char *data, size_t len)
{
u_char iv[aead->get_iv_size(aead)];
memset(iv, 0, sizeof(iv));
return aead->decrypt(aead, chunk_create(data, len), chunk_empty,
chunk_from_thing(iv), NULL);
}
static bool attack_aeads(encryption_algorithm_t alg, size_t key_size,
u_int iterations, u_int distance)
{
u_char buf[64];
aead_t *aead;
bool res;
aead = lib->crypto->create_aead(lib->crypto, alg, key_size, 0);
if (!aead)
{
fprintf(stderr, "creating AEAD %N failed\n",
encryption_algorithm_names, alg);
return FALSE;
}
memset(buf, 0xe3, sizeof(buf));
if (!aead->set_key(aead, chunk_create(buf, aead->get_key_size(aead))))
{
aead->destroy(aead);
return FALSE;
}
memset(buf, 0, aead->get_iv_size(aead));
if (!aead->encrypt(aead, chunk_create(buf, 0), chunk_empty,
chunk_create(buf, aead->get_iv_size(aead)), NULL))
{
aead->destroy(aead);
return FALSE;
}
fprintf(stderr, "attacking %b\n", buf, aead->get_icv_size(aead));
res = timeattack(attack_aead, aead, aead->get_icv_size(aead),
iterations, distance);
aead->destroy(aead);
return res;
}
CALLBACK(attack_signer, bool,
signer_t *signer, u_char *data, size_t len)
{
return signer->verify_signature(signer, chunk_empty, chunk_create(data, len));
}
static bool attack_signers(integrity_algorithm_t alg,
u_int iterations, u_int distance)
{
u_char buf[64];
signer_t *signer;
bool res;
signer = lib->crypto->create_signer(lib->crypto, alg);
if (!signer)
{
fprintf(stderr, "creating signer %N failed\n",
integrity_algorithm_names, alg);
return FALSE;
}
memset(buf, 0xe3, sizeof(buf));
if (!signer->set_key(signer, chunk_create(buf, signer->get_key_size(signer))))
{
signer->destroy(signer);
return FALSE;
}
if (!signer->get_signature(signer, chunk_empty, buf))
{
signer->destroy(signer);
return FALSE;
}
fprintf(stderr, "attacking %b\n", buf, signer->get_block_size(signer));
res = timeattack(attack_signer, signer, signer->get_block_size(signer),
iterations, distance);
signer->destroy(signer);
return res;
}
static bool attack_transform(char *name, u_int iterations, u_int distance)
{
const proposal_token_t *token;
token = lib->proposal->get_token(lib->proposal, name);
if (!token)
{
fprintf(stderr, "algorithm '%s' unknown\n", name);
return FALSE;
}
switch (token->type)
{
case ENCRYPTION_ALGORITHM:
if (encryption_algorithm_is_aead(token->algorithm))
{
return attack_aeads(token->algorithm, token->keysize / 8,
iterations, distance);
}
fprintf(stderr, "can't attack a crypter\n");
return FALSE;
case INTEGRITY_ALGORITHM:
return attack_signers(token->algorithm, iterations, distance);
default:
fprintf(stderr, "can't attack a %N\n", transform_type_names, token->type);
return FALSE;
}
}
int main(int argc, char *argv[])
{
library_init(NULL, "timeattack");
atexit(library_deinit);
lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
if (argc < 3)
{
fprintf(stderr, "usage: %s <attack> <iterations> <distance>\n", argv[0]);
fprintf(stderr, " <attack>: memeq[1-5] / chunk[1-2] / aead / signer\n");
fprintf(stderr, " <iterations>: number of invocations * 1000\n");
fprintf(stderr, " <distance>: time difference in ns for a hit\n");
fprintf(stderr, " example: %s memeq1 100 500\n", argv[0]);
fprintf(stderr, " example: %s aes128gcm16 100 4000\n", argv[0]);
return 1;
}
if (strpfx(argv[1], "memeq"))
{
return !attack_memeq(argv[1], atoi(argv[2]), atoi(argv[3]));
}
if (strpfx(argv[1], "chunk"))
{
return !attack_chunk(argv[1], atoi(argv[2]), atoi(argv[3]));
}
return !attack_transform(argv[1], atoi(argv[2]), atoi(argv[3]));
}
|