Skip to content

Instantly share code, notes, and snippets.

@munro
Created January 24, 2013 02:16
Show Gist options
  • Save munro/4617095 to your computer and use it in GitHub Desktop.
Save munro/4617095 to your computer and use it in GitHub Desktop.
Timing Attack
#include <stdio.h>
#include <sys/time.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
// http://www.kernel.org/doc/man-pages/online/pages/man2/clock_gettime.2.html
uint64_t microtime() {
struct timeval start;
struct timezone tz;
gettimeofday(&start, &tz);
return (((uint64_t) start.tv_sec) * 1000000) + (uint64_t) start.tv_usec;
}
int main(int argc, char** argv) {
unsigned int i, temp;
uint64_t start, end;
start = microtime();
start = microtime();
for (i = 0; i < 0xfffffff; i += 1) {
strncmp("HELLO", "WORLD", 5);
}
end = microtime() - start;
printf("Time: %llu\n", end);
start = microtime();
for (i = 0; i < 0xfffffff; i += 1) {
strncmp("HELLO", "HELLO", 5);
}
end = microtime() - start;
printf("Time: %llu\n", end);
return 0;
}
var iters = 0xffffff,
trials = 100,
test = '5ca2c015b57a74e28d4aae6a154b489368922827';
function attack(str) {
var start, i, temp;
start = new Date().getTime();
for (i = 0; i < iters; i += 1) {
temp = (test === str);
}
return new Date().getTime() - start;
}
var testers = [
'516bee79283014470271832d34d4db7990283466', // good
'6e18e5a4a7dee589bce20ff39a87ecbd53db55d9',
'9fd1911878d91e4835b402c75ab62f7360162359'
];
var tests = (function () {
var i, tests = [];
for (i = 0; i < trials; i += 1) {
tests.push(testers.map(attack));
}
return tests;
}()).reduce(function (a, b) {
return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
});
console.log('match time ', tests[0]);
console.log('no match #1 time', tests[1]);
console.log('no match #2 time', tests[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment