From f48cae2dcd5ff60c678484cd213881cc90722d41 Mon Sep 17 00:00:00 2001 From: Benedict Date: Sat, 13 Aug 2016 00:56:06 +0200 Subject: set3, completed task 23 --- lib/lib3.c | 90 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 24 deletions(-) (limited to 'lib/lib3.c') diff --git a/lib/lib3.c b/lib/lib3.c index adea72e..e976fd1 100644 --- a/lib/lib3.c +++ b/lib/lib3.c @@ -78,69 +78,71 @@ int aes_ctr(char *in, int length_in, char *out, char *string_key, char *nonce) } -void mt_19937_seed(unsigned int seed) +void mt_19937_seed(unsigned int seed, struct mt_19937_state *mt_19937) { int i; - __global_mt_19937.index = MT_19937_N; - __global_mt_19937.mt[0] = seed; - memset(&__global_mt_19937.mt[1], 0, 623); + mt_19937->index = MT_19937_N; + mt_19937->mt[0] = seed; + memset(&mt_19937->mt[1], 0, 623); for(i=1;i<=MT_19937_N;i++) { - __global_mt_19937.mt[i] = (1812433253 * (__global_mt_19937.mt[i-1] - ^ (__global_mt_19937.mt[i-1] >> 30))) + i; + mt_19937->mt[i] = (1812433253 * (mt_19937->mt[i-1] + ^ (mt_19937->mt[i-1] >> 30))) + i; // only the last 32 bit - __global_mt_19937.mt[i] &= 0xFFFFFFFF; + mt_19937->mt[i] &= 0xFFFFFFFF; } } -void mt_19937_generate() +void mt_19937_generate(struct mt_19937_state *mt_19937) { unsigned int i, x; for(i=0;i<=MT_19937_N;i++) { - x = (((__global_mt_19937.mt[i] & MT_19937_UPPER_MASK) + - (__global_mt_19937.mt[(i+1) % 624] & MT_19937_LOWER_MASK) + // x is the first bit of mt[i] plus the last 31 bits of the + // next number + x = (((mt_19937->mt[i] & MT_19937_UPPER_MASK) + + (mt_19937->mt[(i+1) % 624] & MT_19937_LOWER_MASK) )& 0xFFFFFFFF); - __global_mt_19937.mt[i] = __global_mt_19937.mt[(i+397) % MT_19937_N] ^ x >> 1; + mt_19937->mt[i] = mt_19937->mt[(i+397) % MT_19937_N] ^ x >> 1; if (x % 2) - __global_mt_19937.mt[i] ^= 0x9908b0df; + mt_19937->mt[i] ^= 0x9908b0df; } - __global_mt_19937.index = 0; + mt_19937->index = 0; } -int mt_19937() +unsigned int mt_19937(struct mt_19937_state *mt_19937_st) { unsigned int y = 0x0; - if(__global_mt_19937.index >= MT_19937_N) - mt_19937_generate(); + if(mt_19937_st->index >= MT_19937_N) + mt_19937_generate(mt_19937_st); - y = __global_mt_19937.mt[__global_mt_19937.index]; + y = mt_19937_st->mt[mt_19937_st->index]; y = y ^ (y >> 11); y = y ^ ((y << 7) & 0x9D2C5680); y = y ^ ((y << 15) & 0xEFC60000); y = y ^ (y >> 18); - __global_mt_19937.index++; + mt_19937_st->index++; return (y & 0xFFFFFFFF); } -int mt_19937_timestamp_orcale() +unsigned int mt_19937_timestamp_orcale(struct mt_19937_state *mt_19937_st) { sleep(random_number_between(40,1000)); unsigned int seed = time(NULL); printf("timestamp orcale seed: %u\n", seed); - mt_19937_seed(seed); + mt_19937_seed(seed, mt_19937_st); sleep(random_number_between(40,1000)); - return mt_19937(); + return mt_19937(mt_19937_st); } /*** * is there a more clever way to do this than brute force? @@ -148,16 +150,56 @@ int mt_19937_timestamp_orcale() **/ void mt_19937_brute_force_timestamp() { + struct mt_19937_state mt_state; unsigned int start = time(NULL); - int rnd = mt_19937_timestamp_orcale(); + int rnd = mt_19937_timestamp_orcale(&mt_state); unsigned int stop = time(NULL); unsigned int i; // try every seed between start and stop for(i=start;start<=stop;i++) { - mt_19937_seed(i); - if (rnd == mt_19937()) { + mt_19937_seed(i, &mt_state); + if (rnd == mt_19937(&mt_state)) { printf("found seed: %u\n", i); break; } } } + +int unshift_right_xor(int number, int shifts) +{ + /** + * restore shift bits in every round + **/ + int rounds = 0; + int restore = 0; + + while(rounds*shifts < 32) { + // take #shift bit be on, begin on the left and go to the right + unsigned int tmp = (0xFFFFFFFF << (32-shifts)) >> (rounds*shifts); + unsigned int tmp2 = number & tmp; + number ^= tmp2 >> shifts; + restore |= tmp2; + rounds++; + } + return restore; +} + +/*** + * why the fuck is the reverse AND working? + **/ +int unshift_left_xor(int number, int shifts, unsigned int mask) +{ + int rounds = 0; + int restore = 0; + + while(rounds*shifts < 32) { + // take #shift bit be on, begin on the right and shift it to left every round + unsigned int tmp = (0xFFFFFFFF >> (32-shifts)) << (rounds*shifts); + unsigned int tmp2 = number & tmp; + number ^= tmp2 << shifts & mask; + restore |= tmp2; + rounds++; + } + return restore; +} + -- cgit v1.2.3-70-g09d2