123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787 |
- #ifdef __OPTIMIZE__
- #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
- #endif
- typedef unsigned long long __u64;
- typedef long long __s64;
- typedef unsigned int __u32;
- #define NULL ((void *) 0)
- struct rand_data {
-
- __u64 data;
- __u64 old_data;
- __u64 prev_time;
- #define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
- __u64 last_delta;
- __s64 last_delta2;
- unsigned int stuck:1;
- unsigned int osr;
- unsigned int stir:1;
- unsigned int disable_unbias:1;
- #define JENT_MEMORY_BLOCKS 64
- #define JENT_MEMORY_BLOCKSIZE 32
- #define JENT_MEMORY_ACCESSLOOPS 128
- #define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
- unsigned char *mem;
- unsigned int memlocation;
- unsigned int memblocks;
- unsigned int memblocksize;
- unsigned int memaccessloops;
- };
- #define JENT_DISABLE_STIR (1<<0)
- #define JENT_DISABLE_UNBIAS (1<<1)
- #define JENT_DISABLE_MEMORY_ACCESS (1<<2)
- #define JENT_ENOTIME 1
- #define JENT_ECOARSETIME 2
- #define JENT_ENOMONOTONIC 3
- #define JENT_EMINVARIATION 4
- #define JENT_EVARVAR 5
- #define JENT_EMINVARVAR 6
- void jent_get_nstime(__u64 *out);
- __u64 jent_rol64(__u64 word, unsigned int shift);
- void *jent_zalloc(unsigned int len);
- void jent_zfree(void *ptr);
- int jent_fips_enabled(void);
- void jent_panic(char *s);
- void jent_memcpy(void *dest, const void *src, unsigned int n);
- static __u64 jent_loop_shuffle(struct rand_data *ec,
- unsigned int bits, unsigned int min)
- {
- __u64 time = 0;
- __u64 shuffle = 0;
- unsigned int i = 0;
- unsigned int mask = (1<<bits) - 1;
- jent_get_nstime(&time);
-
- if (ec)
- time ^= ec->data;
-
- for (i = 0; (DATA_SIZE_BITS / bits) > i; i++) {
- shuffle ^= time & mask;
- time = time >> bits;
- }
-
- return (shuffle + (1<<min));
- }
- static __u64 jent_fold_time(struct rand_data *ec, __u64 time,
- __u64 *folded, __u64 loop_cnt)
- {
- unsigned int i;
- __u64 j = 0;
- __u64 new = 0;
- #define MAX_FOLD_LOOP_BIT 4
- #define MIN_FOLD_LOOP_BIT 0
- __u64 fold_loop_cnt =
- jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
-
- if (loop_cnt)
- fold_loop_cnt = loop_cnt;
- for (j = 0; j < fold_loop_cnt; j++) {
- new = 0;
- for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
- __u64 tmp = time << (DATA_SIZE_BITS - i);
- tmp = tmp >> (DATA_SIZE_BITS - 1);
- new ^= tmp;
- }
- }
- *folded = new;
- return fold_loop_cnt;
- }
- static unsigned int jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
- {
- unsigned char *tmpval = NULL;
- unsigned int wrap = 0;
- __u64 i = 0;
- #define MAX_ACC_LOOP_BIT 7
- #define MIN_ACC_LOOP_BIT 0
- __u64 acc_loop_cnt =
- jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
- if (NULL == ec || NULL == ec->mem)
- return 0;
- wrap = ec->memblocksize * ec->memblocks;
-
- if (loop_cnt)
- acc_loop_cnt = loop_cnt;
- for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
- tmpval = ec->mem + ec->memlocation;
-
- *tmpval = (*tmpval + 1) & 0xff;
-
- ec->memlocation = ec->memlocation + ec->memblocksize - 1;
- ec->memlocation = ec->memlocation % wrap;
- }
- return i;
- }
- static void jent_stuck(struct rand_data *ec, __u64 current_delta)
- {
- __s64 delta2 = ec->last_delta - current_delta;
- __s64 delta3 = delta2 - ec->last_delta2;
- ec->last_delta = current_delta;
- ec->last_delta2 = delta2;
- if (!current_delta || !delta2 || !delta3)
- ec->stuck = 1;
- }
- static __u64 jent_measure_jitter(struct rand_data *ec)
- {
- __u64 time = 0;
- __u64 data = 0;
- __u64 current_delta = 0;
-
- jent_memaccess(ec, 0);
-
- jent_get_nstime(&time);
- current_delta = time - ec->prev_time;
- ec->prev_time = time;
-
- jent_fold_time(ec, current_delta, &data, 0);
-
- jent_stuck(ec, current_delta);
- return data;
- }
- static __u64 jent_unbiased_bit(struct rand_data *entropy_collector)
- {
- do {
- __u64 a = jent_measure_jitter(entropy_collector);
- __u64 b = jent_measure_jitter(entropy_collector);
- if (a == b)
- continue;
- if (1 == a)
- return 1;
- else
- return 0;
- } while (1);
- }
- static void jent_stir_pool(struct rand_data *entropy_collector)
- {
-
- union c {
- __u64 u64;
- __u32 u32[2];
- };
-
- union c constant;
-
- union c mixer;
- unsigned int i = 0;
-
- constant.u32[1] = 0x67452301;
- constant.u32[0] = 0xefcdab89;
- mixer.u32[1] = 0x98badcfe;
- mixer.u32[0] = 0x10325476;
- for (i = 0; i < DATA_SIZE_BITS; i++) {
-
- if ((entropy_collector->data >> i) & 1)
- mixer.u64 ^= constant.u64;
- mixer.u64 = jent_rol64(mixer.u64, 1);
- }
- entropy_collector->data ^= mixer.u64;
- }
- static void jent_gen_entropy(struct rand_data *ec)
- {
- unsigned int k = 0;
-
- jent_measure_jitter(ec);
- while (1) {
- __u64 data = 0;
- if (ec->disable_unbias == 1)
- data = jent_measure_jitter(ec);
- else
- data = jent_unbiased_bit(ec);
-
- if (ec->stuck) {
-
- ec->data ^= data;
- ec->stuck = 0;
- continue;
- }
-
- ec->data ^= data;
- ec->data ^= ((ec->data >> 63) & 1);
- ec->data ^= ((ec->data >> 60) & 1);
- ec->data ^= ((ec->data >> 55) & 1);
- ec->data ^= ((ec->data >> 30) & 1);
- ec->data ^= ((ec->data >> 27) & 1);
- ec->data ^= ((ec->data >> 22) & 1);
- ec->data = jent_rol64(ec->data, 1);
-
- if (++k >= (DATA_SIZE_BITS * ec->osr))
- break;
- }
- if (ec->stir)
- jent_stir_pool(ec);
- }
- static void jent_fips_test(struct rand_data *ec)
- {
- if (!jent_fips_enabled())
- return;
-
- if (!ec->old_data) {
- ec->old_data = ec->data;
- jent_gen_entropy(ec);
- }
- if (ec->data == ec->old_data)
- jent_panic("jitterentropy: Duplicate output detected\n");
- ec->old_data = ec->data;
- }
- int jent_read_entropy(struct rand_data *ec, unsigned char *data,
- unsigned int len)
- {
- unsigned char *p = data;
- if (!ec)
- return -1;
- while (0 < len) {
- unsigned int tocopy;
- jent_gen_entropy(ec);
- jent_fips_test(ec);
- if ((DATA_SIZE_BITS / 8) < len)
- tocopy = (DATA_SIZE_BITS / 8);
- else
- tocopy = len;
- jent_memcpy(p, &ec->data, tocopy);
- len -= tocopy;
- p += tocopy;
- }
- return 0;
- }
- struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
- unsigned int flags)
- {
- struct rand_data *entropy_collector;
- entropy_collector = jent_zalloc(sizeof(struct rand_data));
- if (!entropy_collector)
- return NULL;
- if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
-
- entropy_collector->mem = jent_zalloc(JENT_MEMORY_SIZE);
- if (!entropy_collector->mem) {
- jent_zfree(entropy_collector);
- return NULL;
- }
- entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
- entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
- entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
- }
-
- if (0 == osr)
- osr = 1;
- entropy_collector->osr = osr;
- entropy_collector->stir = 1;
- if (flags & JENT_DISABLE_STIR)
- entropy_collector->stir = 0;
- if (flags & JENT_DISABLE_UNBIAS)
- entropy_collector->disable_unbias = 1;
-
- jent_gen_entropy(entropy_collector);
- return entropy_collector;
- }
- void jent_entropy_collector_free(struct rand_data *entropy_collector)
- {
- jent_zfree(entropy_collector->mem);
- entropy_collector->mem = NULL;
- jent_zfree(entropy_collector);
- entropy_collector = NULL;
- }
- int jent_entropy_init(void)
- {
- int i;
- __u64 delta_sum = 0;
- __u64 old_delta = 0;
- int time_backwards = 0;
- int count_var = 0;
- int count_mod = 0;
-
-
-
- #define TESTLOOPCOUNT 300
- #define CLEARCACHE 100
- for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
- __u64 time = 0;
- __u64 time2 = 0;
- __u64 folded = 0;
- __u64 delta = 0;
- unsigned int lowdelta = 0;
- jent_get_nstime(&time);
- jent_fold_time(NULL, time, &folded, 1<<MIN_FOLD_LOOP_BIT);
- jent_get_nstime(&time2);
-
- if (!time || !time2)
- return JENT_ENOTIME;
- delta = time2 - time;
-
- if (!delta)
- return JENT_ECOARSETIME;
-
- if (CLEARCACHE > i)
- continue;
-
- if (!(time2 > time))
- time_backwards++;
-
- lowdelta = time2 - time;
- if (!(lowdelta % 100))
- count_mod++;
-
- if (i) {
- if (delta != old_delta)
- count_var++;
- if (delta > old_delta)
- delta_sum += (delta - old_delta);
- else
- delta_sum += (old_delta - delta);
- }
- old_delta = delta;
- }
-
- if (3 < time_backwards)
- return JENT_ENOMONOTONIC;
-
- if (!delta_sum)
- return JENT_EVARVAR;
-
- if (delta_sum <= 1)
- return JENT_EMINVARVAR;
-
- if ((TESTLOOPCOUNT/10 * 9) < count_mod)
- return JENT_ECOARSETIME;
- return 0;
- }
|