int main(int argc, char **argv) { (void) argc; (void) argv; #ifndef NO_OPENSSL RAND_poll(); #endif struct ottery_config cfg_chacha8; struct ottery_config cfg_chacha12; struct ottery_config cfg_chacha20; ottery_config_init(&cfg_chacha8); ottery_config_force_implementation(&cfg_chacha8, OTTERY_PRF_CHACHA8); ottery_config_init(&cfg_chacha12); ottery_config_force_implementation(&cfg_chacha12, OTTERY_PRF_CHACHA12); ottery_config_init(&cfg_chacha20); ottery_config_force_implementation(&cfg_chacha20, OTTERY_PRF_CHACHA20); ottery_st_init(&s8, &cfg_chacha8); ottery_st_init(&s12, &cfg_chacha12); ottery_st_init(&s20, &cfg_chacha20); ottery_st_init_nolock(&s8nl, &cfg_chacha8); ottery_st_init_nolock(&s12nl, &cfg_chacha12); ottery_st_init_nolock(&s20nl, &cfg_chacha20); time_chacharand8(); time_chacharand8_u64(); time_chacharand8_onebyte(); time_chacharand8_buf16(); time_chacharand8_buf1024(); time_chacharand12(); time_chacharand12_u64(); time_chacharand12_onebyte(); time_chacharand12_buf16(); time_chacharand12_buf1024(); time_chacharand20(); time_chacharand20_u64(); time_chacharand20_onebyte(); time_chacharand20_buf16(); time_chacharand20_buf1024(); time_chacharand8nl(); time_chacharand8nl_u64(); time_chacharand8nl_onebyte(); time_chacharand8nl_buf16(); time_chacharand8nl_buf1024(); time_chacharand12nl(); time_chacharand12nl_u64(); time_chacharand12nl_onebyte(); time_chacharand12nl_buf16(); time_chacharand12nl_buf1024(); time_chacharand20nl(); time_chacharand20nl_u64(); time_chacharand20nl_onebyte(); time_chacharand20nl_buf16(); time_chacharand20nl_buf1024(); time_arc4random(); time_arc4random_u64(); time_arc4random_onebyte(); time_arc4random_buf16(); time_arc4random_buf1024(); if (ottery_get_cpu_capabilities_() & OTTERY_CPUCAP_RAND) { time_rdrandom(); time_rdrandom_buf16(); time_rdrandom_buf1024(); } #ifndef NO_URANDOM urandom_fd = open("/dev/urandom", O_RDONLY); time_urandom(); time_urandom_u64(); time_urandom_buf16(); time_urandom_buf1024(); #endif time_libc_random(); time_libc_random_u64(); time_libc_onebyte(); time_libcrandom_buf16(); time_libcrandom_buf1024(); time_openssl_random(); time_opensslrandom_buf16(); time_opensslrandom_buf1024(); return 0; }
/** * Initialize or reinitialize a PRNG state. * * @param st The state to initialize or reinitialize. * @param prf The configuration to use. (Ignored for reinit) * @return An OTTERY_ERR_* value (zero on success, nonzero on failure). */ static int ottery_st_initialize(struct ottery_state *st, const struct ottery_config *config, int locked) { const struct ottery_prf *prf = NULL; struct ottery_config cfg_tmp; int err; /* We really need our state to be aligned. If it isn't, let's give an * error now, and not a crash when the SIMD instructions start to fail. */ if (((uintptr_t)st) & 0xf) return OTTERY_ERR_STATE_ALIGNMENT; if (!config) { ottery_config_init(&cfg_tmp); config = &cfg_tmp; } prf = config->impl; if (!prf) prf = ottery_get_impl(NULL); memset(st, 0, sizeof(*st)); if (locked) { /* Now set up the spinlock or mutex or hybrid thing. */ if (INIT_LOCK(&st->mutex)) return OTTERY_ERR_LOCK_INIT; } /* Check invariants for PRF, in case we wrote some bad code. */ if ((prf->state_len > MAX_STATE_LEN) || (prf->state_bytes > MAX_STATE_BYTES) || (prf->state_bytes > prf->output_len) || (prf->output_len > MAX_OUTPUT_LEN)) return OTTERY_ERR_INTERNAL; /* Check whether some of our structure size assumptions are right. */ if ((sizeof(struct ottery_state) > OTTERY_STATE_DUMMY_SIZE_) || (sizeof(struct ottery_config) > OTTERY_CONFIG_DUMMY_SIZE_)) return OTTERY_ERR_INTERNAL; memcpy(&st->entropy_config, &config->entropy_config, sizeof(struct ottery_entropy_config)); /* Copy the PRF into place. */ memcpy(&st->prf, prf, sizeof(*prf)); if ((err = ottery_st_reseed(st))) return err; /* Set the magic number last, or else we might look like we succeeded * when we didn't */ st->magic = MAGIC(st); st->pid = getpid(); return 0; }