/* * Auto Seeded RNG * (C) 2008 Jack Lloyd * * Distributed under the terms of the Botan license */ #include #include #include #include #include #include #include #if defined(BOTAN_HAS_RANDPOOL) #include #endif #if defined(BOTAN_HAS_HMAC_RNG) #include #endif #if defined(BOTAN_HAS_X931_RNG) #include #endif #if defined(BOTAN_HAS_TIMER_HARDWARE) #include #endif #if defined(BOTAN_HAS_TIMER_POSIX) #include #endif #if defined(BOTAN_HAS_TIMER_UNIX) #include #endif #if defined(BOTAN_HAS_TIMER_WIN32) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_EGD) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) #include #endif #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) #include #endif namespace Botan { namespace { /** * Add any known entropy sources to this RNG */ void add_entropy_sources(RandomNumberGenerator* rng) { // Add a high resolution timer, if available #if defined(BOTAN_HAS_TIMER_HARDWARE) rng->add_entropy_source(new Hardware_Timer); #elif defined(BOTAN_HAS_TIMER_POSIX) rng->add_entropy_source(new POSIX_Timer); #elif defined(BOTAN_HAS_TIMER_UNIX) rng->add_entropy_source(new Unix_Timer); #elif defined(BOTAN_HAS_TIMER_WIN32) rng->add_entropy_source(new Win32_Timer); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE) rng->add_entropy_source( new Device_EntropySource( split_on("/dev/urandom:/dev/random:/dev/srandom", ':') ) ); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_EGD) rng->add_entropy_source( new EGD_EntropySource(split_on("/var/run/egd-pool:/dev/egd-pool", ':')) ); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI) rng->add_entropy_source(new Win32_CAPI_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) rng->add_entropy_source(new FTW_EntropySource("/proc")); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32) rng->add_entropy_source(new Win32_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS) rng->add_entropy_source(new BeOS_EntropySource); #endif #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX) rng->add_entropy_source( new Unix_EntropySource(split_on("/bin:/sbin:/usr/bin:/usr/sbin", ':')) ); #endif } } AutoSeeded_RNG::AutoSeeded_RNG(u32bit poll_bits) { rng = 0; #if defined(BOTAN_HAS_HMAC_RNG) rng = new HMAC_RNG(new HMAC(new SHA_512), new HMAC(new SHA_256)); #elif defined(BOTAN_HAS_RANDPOOL) rng = new Randpool(new AES_256, new HMAC(new SHA_256)); #endif if(!rng) throw Internal_Error("No usable RNG found enabled in build"); /* If X9.31 is available, use it to wrap the other RNG as a failsafe */ #if defined(BOTAN_HAS_X931_RNG) rng = new ANSI_X931_RNG(new AES_256, rng); #endif add_entropy_sources(rng); rng->reseed(poll_bits); } }