#include #include #include #include #include #include #include #include #include #include //#include #ifdef __linux__ #include #include #include #include #include #endif #include "scryptenc.h" #include "warn.h" #include #include #include #include #include #include #include #include #include #include #include int do_scrypt=1; int gap; FILE *outfile = NULL; SHA256_CTX hashctx; int timevaldiff(struct timeval *starttime, struct timeval *finishtime); unsigned char count_ones(unsigned int num); int debug=0; int increment_until_gettimeofday(int us){ // see how many rounds we can do in a fixed amount of time struct timeval now, then; int cycles=0; if(us>100) us/=100; // XXX should be documented better // this is *intentionally* a slow clock gettimeofday(&then, NULL); while(1){ cycles++; gettimeofday(&now, NULL); if(timevaldiff(&then, &now)>us) break; } //cycles&=7; return cycles; } int measure_usleep_with_clock_monotonic(int us){ struct timespec now, then; int x; clock_gettime(CLOCK_MONOTONIC, &then); usleep(us); clock_gettime(CLOCK_MONOTONIC, &now); x= now.tv_nsec - then.tv_nsec; //x&=7; return x; } int measure_usleep_with_clock_realtime(int us){ struct timespec now, then; int x; clock_gettime(CLOCK_REALTIME, &then); usleep(us); clock_gettime(CLOCK_REALTIME, &now); x= now.tv_nsec - then.tv_nsec; //x&=7; return x; } #if defined(__i386__) || defined(_amd64_) || defined(__x86_64__) typedef unsigned long long ticks; static __inline__ ticks getticks(void) { unsigned a, d; asm("cpuid"); asm volatile("rdtsc" : "=a" (a), "=d" (d)); return (((ticks)a) | (((ticks)d) << 32)); } int measure_usleep_with_rdtsc(int us){ ticks now, then; long long ret; then = getticks(); usleep(us); now = getticks(); ret = now-then; //ret&=7; // intentionally casting back down to an int return (int) ret; } #endif typedef struct { int *num; int sleep; int diff; int started; int stop; } thread_rng_arg; void *thread_twiddle(void *arg){ thread_rng_arg *p = (thread_rng_arg *)arg; //struct timeval then, now; while(1){ *(p->num) += p->diff; if(p->sleep) usleep(p->sleep); p->started=1; if(p->stop) { p->stop=2; return; } } } int read_thread_tugowar(unsigned char *buf, int len, int mode){ pthread_t t1, t2; thread_rng_arg p1, p2; int i, bits=0; int raw_a,raw_b,a,b,merge; int swap=0; char zero=0; char one=1; memset(buf, 0, len); memset(&p1, 0, sizeof(thread_rng_arg)); memset(&p2, 0, sizeof(thread_rng_arg)); if(mode==0 || mode==2) { p1.num = p2.num = calloc(4, 1);} if(mode==1) { p1.num = calloc(4,1); p2.num = calloc(4,1); } if(mode>2) { exit(255); } if(mode==2) { p2.started=1; p2.stop=2; } p1.diff=1; p2.diff=-1; pthread_create(&t1, NULL, thread_twiddle, &p1); if(mode==0 || mode==1) pthread_create(&t2, NULL, thread_twiddle, &p2); // don't start reading until both threads are spinning while(!p1.started || !p2.started) { usleep(10); } for(i=0; isizeof(AES_KEY)) { exit(255); } memcpy(&key->rd_key, dk, dklen); key->rounds=14; aes_ctx = crypto_aesctr_init(key, 0); } #define WIDTH 1024 int cs_read(unsigned char *dst, unsigned int len){ unsigned int count=len; unsigned int i=0; unsigned char *tmp; int to_copy=0; if(stream_old==NULL) stream_old = calloc(WIDTH, 1); if(stream_new==NULL) stream_new = calloc(WIDTH, 1); while(i8){ exit(254); } if(random_generator){ for(i=0; i<32; i++){ // This explicitly means we want only 256 bits of entropy while(bits<8){ raw_a = random_generator(gap); raw_b = random_generator(gap); SHA256_Update(&hashctx, &raw_a, sizeof(int)); if(outfile) fwrite(&raw_a, sizeof(int), 1, outfile); if(debug) fprintf(stderr, "bits=%i x=%u\n", bits+i*8, raw_a); SHA256_Update(&hashctx, &raw_b, sizeof(int)); if(outfile) fwrite(&raw_b, sizeof(int), 1, outfile); if(debug) fprintf(stderr, "bits=%i x=%u\n", bits+i*8, raw_b); a = count_ones(raw_a) & 1; b = count_ones(raw_b) & 1; if(a!=b) { a ? SHA256_Update(&hashctx, &zero, sizeof(zero)) : SHA256_Update(&hashctx, &one, sizeof(one)); bits++; } if(outfile && a!=b) { a ? fwrite(&zero, sizeof(zero), 1, outfile) : fwrite(&one, sizeof(one), 1, outfile); } } bits=0; } SHA256_Final(kesha, &hashctx); } N = 1<<15; r = 8; p = 1; // fixed salt is fine since the key is never (presumably) repeated memset(salt, 0, sizeof(salt)); if(do_scrypt==1) { crypto_scrypt(kesha, sizeof(kesha), salt, sizeof(salt), N, r, p, scrypt_aes, sizeof(scrypt_aes)); cs_init(scrypt_aes, sizeof(scrypt_aes)); } else { cs_init(kesha, sizeof(kesha)); } cs_read(buf, len); return len; //len; } // utility functions int timevaldiff(struct timeval *starttime, struct timeval *finishtime) { int msec; msec=(finishtime->tv_sec-starttime->tv_sec)*1000; msec+=(finishtime->tv_usec-starttime->tv_usec)/1000; return msec; } unsigned char count_ones(unsigned int num){ unsigned char i; unsigned char sum=0; for(i=0; i>=1; } return sum; } #define VERSION "1.0" void help(){ fprintf(stdout, "dakarand %s: Highly Experimental Entropy Generator For Commodity Hardware\n\ Dan Kaminsky, Chief Scientist, DKH / dankaminsky.com / dan@doxpara.com \n\ Summary:\n\ Any system with two clocks is not deterministic. Even one part per\n\ million of jitter equals a bit per megahertz per second. Dakarand\n\ is a framework to test whether these bits might be useful after all.\n\ THIS CODE IS DESIGNED TO BE ATTACKED AND IS EXPECTED TO FAIL. DO NOT\n\ USE IT FOR ANYTHING EVEN REMOTELY IMPORTANT...yet.\n\ Options:\n\ -v: Verbose -- show intermediate values, from which 1 bit is extracted\n\ -m mode: Select entropy source.\n\ 0: Two Threads, One Int\n\ 1: Two Threads, Two Ints\n\ 2: One Thread, One Int\n\ 3: Increment Until Gettimeofday\n\ 4: Measure usleep with CLOCK_MONOTONIC [default]\n\ 5: Measure usleep with CLOCK_REALTIME\n\ 6: Measure usleep with RDTSC\n\ 7: Measure RTC (128hz) with CLOCK_MONOTONIC [LINUX ONLY]\n\ 8: Measure RTC (8192hz) with CLOCK_MONOTONIC [LINUX ONLY]\n\ -r: Update system entropy [LINUX ONLY]\n\ -f format: Select output format\n\ 0: 16 hexadecimal bytes, comma delimited [default]\n\ 1: 4 integers from 0 to 2**32-1\n\ 2: Raw bytes\n\ -o filename: Select output file\n\ -l length: Select approximate number of output bytes\n\ -g us: Microseconds between probes (default: 1000)\n\ -d debugfile: File which will receive raw bits being hashed --\n\ attack this!\n\ -s: Disable scrypt'ing of hashed entropy\n\ Credit: This is based on Truerand from Matt Blaze and DP Mitchell '95-'96.\n\ ", VERSION); } int main(int argc, char **argv){ //unsigned char buf[32]; unsigned char *buf; int i,j,c; int mode=4; int format=0; int len=32; int *ic; int update_entropy=0; int err; #ifdef __linux__ int rndfd; struct rand_pool_info *output; #endif FILE *out = stdout; SHA256_Init(&hashctx); gap=1000; outfile=NULL; while((c=getopt(argc, argv, "m:f:o:vl:rg:d:s")) != -1){ switch(c){ case 'm': mode = atoi(optarg); break; case 'f': format = atoi(optarg); break; case 'o': if(strncmp(optarg, "-", 2)==0) { out = stdout; } else { out = fopen(optarg, "w"); } break; case 'v': debug=1; break; case 'l': len = atoi(optarg); if(lenbuf_size = len; output->entropy_count = output->buf_size * 8; memcpy(output->buf, buf, len); err=ioctl(rndfd, RNDADDENTROPY, output); fprintf(stdout, "%u\n", err); free(output); close(rndfd); } #endif if(format==0) { for(j=0; j