Botan  1.11.15
Public Member Functions | Static Public Member Functions
Botan::Intel_Rdrand Class Reference

#include <rdrand.h>

Inheritance diagram for Botan::Intel_Rdrand:
Botan::EntropySource

List of all members.

Public Member Functions

std::string name () const
void poll (Entropy_Accumulator &accum)

Static Public Member Functions

static void poll_available_sources (class Entropy_Accumulator &accum)

Detailed Description

Entropy source using the rdrand instruction first introduced on Intel's Ivy Bridge architecture.

Definition at line 19 of file rdrand.h.


Member Function Documentation

std::string Botan::Intel_Rdrand::name ( ) const [inline, virtual]
Returns:
name identifying this entropy source

Implements Botan::EntropySource.

Definition at line 22 of file rdrand.h.

{ return "Intel Rdrand"; }
void Botan::Intel_Rdrand::poll ( Entropy_Accumulator accum) [virtual]

Perform an entropy gathering poll

Parameters:
accumis an accumulator object that will be given entropy

Implements Botan::EntropySource.

Definition at line 20 of file rdrand.cpp.

References Botan::Entropy_Accumulator::add(), and Botan::CPUID::has_rdrand().

   {
   if(!CPUID::has_rdrand())
      return;

   /*
   * Put an upper bound on the total entropy we're willing to claim
   * for any one polling of rdrand to prevent it from swamping our
   * poll. Internally, the rdrand system is a DRGB that reseeds at a
   * somewhat unpredictable rate (the current conditions are
   * documented, but that might not be true for different
   * implementations, eg on Haswell or a future AMD chip, so I don't
   * want to assume). This limit ensures we're going to poll at least
   * one other source so we have some diversity in our inputs.
   */

   const size_t POLL_UPPER_BOUND = 96;
   const size_t RDRAND_POLLS = 32;
   const double ENTROPY_PER_POLL =
      static_cast<double>(POLL_UPPER_BOUND) / (RDRAND_POLLS * 4);

   for(size_t i = 0; i != RDRAND_POLLS; ++i)
      {
      unsigned int r = 0;

#if BOTAN_USE_GCC_INLINE_ASM
      int cf = 0;

      // Encoding of rdrand %eax
      asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
          "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
#else
      int cf = _rdrand32_step(&r);
#endif

      if(cf == 1)
         accum.add(r, ENTROPY_PER_POLL);
      }
   }
void Botan::EntropySource::poll_available_sources ( class Entropy_Accumulator accum) [static, inherited]

Definition at line 108 of file entropy_srcs.cpp.

References Botan::Entropy_Accumulator::polling_goal_achieved().

Referenced by Botan::HMAC_RNG::reseed().

   {
   static std::vector<std::unique_ptr<EntropySource>> g_sources(get_default_entropy_sources());

   if(g_sources.empty())
      throw std::runtime_error("No entropy sources enabled at build time, poll failed");

   size_t poll_attempt = 0;

   while(!accum.polling_goal_achieved() && poll_attempt < 16)
      {
      const size_t src_idx = poll_attempt % g_sources.size();
      g_sources[src_idx]->poll(accum);
      ++poll_attempt;
      }
   }

The documentation for this class was generated from the following files: