static void do_init(void) { uint8_t sha1[SHA1_DIGEST_SIZE]; struct sha1_ctx sha1_ctx; uint8_t random_seed[16]; struct timeval now; /* Get seed data. */ get_entropy_or_die(random_seed, sizeof random_seed); xgettimeofday(&now); /* Convert seed into key. */ sha1_init(&sha1_ctx); sha1_update(&sha1_ctx, random_seed, sizeof random_seed); sha1_update(&sha1_ctx, &now, sizeof now); sha1_update_int(&sha1_ctx, getpid()); #ifndef _WIN32 sha1_update_int(&sha1_ctx, getppid()); sha1_update_int(&sha1_ctx, getuid()); sha1_update_int(&sha1_ctx, getgid()); #endif sha1_final(&sha1_ctx, sha1); /* Generate key. */ BUILD_ASSERT(sizeof sha1 >= 16); aes128_schedule(&key, sha1); /* Generate initial counter. */ get_entropy_or_die(counter, sizeof counter); }
void random_init(void) { uint32_t *seedp = seed_get(); while (!*seedp) { struct timeval tv; uint32_t entropy; pthread_t self; xgettimeofday(&tv); get_entropy_or_die(&entropy, 4); self = pthread_self(); *seedp = (tv.tv_sec ^ tv.tv_usec ^ entropy ^ hash_bytes(&self, sizeof self, 0)); } }