diff options
author | Jack Sangdahl <[email protected]> | 2024-06-15 22:27:25 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-06-15 22:27:25 -0600 |
commit | a59a5d144cc31cc8386e60dcc97be45a0dfd5256 (patch) | |
tree | 076d0adc9bc53538e7283e0248adef57ce2a9a51 /crack_mt19937_seed_s3.c | |
download | cryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.tar.gz cryptopals-a59a5d144cc31cc8386e60dcc97be45a0dfd5256.zip |
Diffstat (limited to 'crack_mt19937_seed_s3.c')
-rw-r--r-- | crack_mt19937_seed_s3.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/crack_mt19937_seed_s3.c b/crack_mt19937_seed_s3.c new file mode 100644 index 0000000..4c4c4fb --- /dev/null +++ b/crack_mt19937_seed_s3.c @@ -0,0 +1,49 @@ +// https://cryptopals.com/sets/3/challenges/22 +#include "util/mersenne_twister.h" +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> + +/* + * wait a random number of seconds between 40-1000 + * seeds RNG with current unix timestamp + * wait a random number of seconds again + * returns first 32-bit output of RNG + * profit + */ + +#define LO 40 +#define HI 1000 + +static uint32_t real_seed; + +static uint32_t simulation() { + uint32_t wait_time = arc4random_uniform(HI - LO + 1) + LO; + sleep((int)wait_time); + real_seed = (uint32_t)time(NULL); + seed(real_seed); + wait_time = arc4random_uniform(HI - LO + 1) + LO; + sleep((int)wait_time); + return extract(); +} + +static uint32_t detect_seed(uint32_t first_val) { + uint32_t now = (uint32_t)time(NULL); + for (uint32_t s = now; s > now - 3 * HI; s--) { + seed(s); + if (extract() == first_val) + return s; + } + printf("could not find seed D:\n"); + return 0; +} + +int main() { + uint32_t rand_val = simulation(); + uint32_t detected_seed = detect_seed(rand_val); + printf("real_seed = %" PRIu32 ", detected seed = %" PRIu32 "\n", real_seed, + detected_seed); + return 0; +} |