Botan  1.11.15
src/lib/entropy/entropy_srcs.cpp
Go to the documentation of this file.
00001 /*
00002 * Entropy Source Polling
00003 * (C) 2008-2010,2015 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/entropy_src.h>
00009 
00010 #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
00011   #include <botan/internal/hres_timer.h>
00012 #endif
00013 
00014 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
00015   #include <botan/internal/rdrand.h>
00016 #endif
00017 
00018 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
00019   #include <botan/internal/dev_random.h>
00020 #endif
00021 
00022 #if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
00023   #include <botan/internal/es_egd.h>
00024 #endif
00025 
00026 #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
00027   #include <botan/internal/unix_procs.h>
00028 #endif
00029 
00030 #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
00031   #include <botan/internal/es_beos.h>
00032 #endif
00033 
00034 #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
00035   #include <botan/internal/es_capi.h>
00036 #endif
00037 
00038 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
00039   #include <botan/internal/es_win32.h>
00040 #endif
00041 
00042 #if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
00043   #include <botan/internal/proc_walk.h>
00044 #endif
00045 
00046 namespace Botan {
00047 
00048 namespace {
00049 
00050 std::vector<std::unique_ptr<EntropySource>> get_default_entropy_sources()
00051    {
00052    std::vector<std::unique_ptr<EntropySource>> sources;
00053 
00054 #if defined(BOTAN_HAS_ENTROPY_SRC_HIGH_RESOLUTION_TIMER)
00055    sources.push_back(std::unique_ptr<EntropySource>(new High_Resolution_Timestamp));
00056 #endif
00057 
00058 #if defined(BOTAN_HAS_ENTROPY_SRC_RDRAND)
00059    sources.push_back(std::unique_ptr<EntropySource>(new Intel_Rdrand));
00060 #endif
00061 
00062 #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
00063    sources.push_back(std::unique_ptr<EntropySource>(new UnixProcessInfo_EntropySource));
00064 #endif
00065 
00066 #if defined(BOTAN_HAS_ENTROPY_SRC_DEV_RANDOM)
00067    sources.push_back(std::unique_ptr<EntropySource>(new Device_EntropySource(
00068       { "/dev/random", "/dev/srandom", "/dev/urandom" }
00069    )));
00070 #endif
00071 
00072 #if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
00073    sources.push_back(std::unique_ptr<EntropySource>(new Win32_CAPI_EntropySource));
00074 #endif
00075 
00076 #if defined(BOTAN_HAS_ENTROPY_SRC_PROC_WALKER)
00077    sources.push_back(std::unique_ptr<EntropySource>(
00078       new ProcWalking_EntropySource("/proc")));
00079 #endif
00080 
00081 #if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
00082    sources.push_back(std::unique_ptr<EntropySource>(new Win32_EntropySource));
00083 #endif
00084 
00085 #if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
00086    sources.push_back(std::unique_ptr<EntropySource>(new BeOS_EntropySource));
00087 #endif
00088 
00089 #if defined(BOTAN_HAS_ENTROPY_SRC_UNIX_PROCESS_RUNNER)
00090    sources.push_back(std::unique_ptr<EntropySource>(
00091       new Unix_EntropySource(
00092          { "/bin", "/sbin", "/usr/bin", "/usr/sbin" }
00093       )));
00094 #endif
00095 
00096 #if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
00097    sources.push_back(std::unique_ptr<EntropySource>(
00098       new EGD_EntropySource({ "/var/run/egd-pool", "/dev/egd-pool" })
00099       ));
00100 #endif
00101 
00102    return sources;
00103    }
00104 
00105 }
00106 
00107 //static
00108 void EntropySource::poll_available_sources(class Entropy_Accumulator& accum)
00109    {
00110    static std::vector<std::unique_ptr<EntropySource>> g_sources(get_default_entropy_sources());
00111 
00112    if(g_sources.empty())
00113       throw std::runtime_error("No entropy sources enabled at build time, poll failed");
00114 
00115    size_t poll_attempt = 0;
00116 
00117    while(!accum.polling_goal_achieved() && poll_attempt < 16)
00118       {
00119       const size_t src_idx = poll_attempt % g_sources.size();
00120       g_sources[src_idx]->poll(accum);
00121       ++poll_attempt;
00122       }
00123    }
00124 
00125 }
00126