libdecaf
spongerng.hxx
Go to the documentation of this file.
1 
14 #ifndef __DECAF_SPONGERNG_HXX__
15 #define __DECAF_SPONGERNG_HXX__
16 
17 #include <decaf/spongerng.h>
18 
19 #include <string>
20 #include <sys/types.h>
21 #include <errno.h>
22 
24 #if __cplusplus >= 201103L
25 #define DECAF_NOEXCEPT noexcept
26 #define DECAF_DELETE = delete
27 #else
28 #define DECAF_NOEXCEPT throw()
29 #define DECAF_DELETE
30 #endif
31 
33 namespace decaf {
34 
36 class SpongeRng : public Rng {
37 private:
40 
41 public:
46  enum Deterministic { RANDOM = 0, DETERMINISTIC = 1 };
47 
49  class RngException : public std::exception {
50  private:
52  const char *const what_;
54  public:
55  const int err_code;
56  const char *what() const DECAF_NOEXCEPT { return what_; }
57  RngException(int err_code, const char *what_) DECAF_NOEXCEPT : what_(what_), err_code(err_code) {}
58  };
59 
61  inline SpongeRng( const Block &in, Deterministic det ) {
62  decaf_spongerng_init_from_buffer(sp,in.data(),in.size(),(int)det);
63  }
64 
66  inline SpongeRng( const std::string &in = "/dev/urandom", size_t len = 32, Deterministic det = RANDOM )
67  /*throw(RngException)*/ {
68  decaf_error_t ret = decaf_spongerng_init_from_file(sp,in.c_str(),len,det);
69  if (!decaf_successful(ret)) {
70  throw RngException(errno, "Couldn't load from file");
71  }
72  }
73 
75  inline void stir( const Block &data ) DECAF_NOEXCEPT {
76  decaf_spongerng_stir(sp,data.data(),data.size());
77  }
78 
80  inline ~SpongeRng() DECAF_NOEXCEPT { decaf_spongerng_destroy(sp); }
81 
82  using Rng::read;
83 
85  virtual inline void read(Buffer buffer) DECAF_NOEXCEPT
86 #if __cplusplus >= 201103L
87  final
88 #endif
89  { decaf_spongerng_next(sp,buffer.data(),buffer.size()); }
90 
91 private:
92  SpongeRng(const SpongeRng &) DECAF_DELETE;
93  SpongeRng &operator=(const SpongeRng &) DECAF_DELETE;
94 };
97 } /* namespace decaf */
98 
99 #undef DECAF_NOEXCEPT
100 #undef DECAF_DELETE
101 
102 #endif /* __DECAF_SPONGERNG_HXX__ */
decaf
Namespace for all libdecaf C++ objects.
Definition: ed255.hxx:41
decaf::SpongeRng
Sponge-based random-number generator.
Definition: spongerng.hxx:36
decaf::SpongeRng::Deterministic
Deterministic
Deterministic flag.
Definition: spongerng.hxx:46
decaf::Block::size
size_t size() const DECAF_NOEXCEPT
Get the size.
Definition: secure_buffer.hxx:202
decaf::SpongeRng::read
virtual void read(Buffer buffer) DECAF_NOEXCEPT
Read data to a buffer.
Definition: spongerng.hxx:85
decaf::SpongeRng::SpongeRng
SpongeRng(const std::string &in="/dev/urandom", size_t len=32, Deterministic det=RANDOM)
Initialize, non-deterministically by default, from C/C++ filename.
Definition: spongerng.hxx:66
decaf::SpongeRng::~SpongeRng
~SpongeRng() DECAF_NOEXCEPT
Securely destroy by overwriting state.
Definition: spongerng.hxx:80
decaf::Rng::read
virtual void read(Buffer buffer) DECAF_NOEXCEPT=0
Read into a Buffer.
decaf::SpongeRng::stir
void stir(const Block &data) DECAF_NOEXCEPT
Stir in new data.
Definition: spongerng.hxx:75
decaf_spongerng_init_from_buffer
void DECAF_API_VIS decaf_spongerng_init_from_buffer(decaf_keccak_prng_t prng, const uint8_t *__restrict__ in, size_t len, int deterministic) DECAF_NONNULL
Initialize a sponge-based CSPRNG from a buffer.
decaf_spongerng_next
void DECAF_API_VIS decaf_spongerng_next(decaf_keccak_prng_t prng, uint8_t *__restrict__ out, size_t len)
Output bytes from a sponge-based CSPRNG.
decaf_spongerng_stir
void DECAF_API_VIS decaf_spongerng_stir(decaf_keccak_prng_t prng, const uint8_t *__restrict__ in, size_t len) DECAF_NONNULL
Stir entropy data into a sponge-based CSPRNG from a buffer.
decaf_error_t
decaf_error_t
Another boolean type used to indicate success or failure.
Definition: common.h:120
decaf::SpongeRng::RngException
Exception thrown when The RNG fails (to seed itself)
Definition: spongerng.hxx:49
decaf::Rng
Prototype of a random number generator.
Definition: secure_buffer.hxx:138
decaf::Block
A reference to a block of data, which (when accessed through this base class) is const.
Definition: secure_buffer.hxx:159
decaf::SpongeRng::RngException::err_code
const int err_code
errno that caused the reseed to fail.
Definition: spongerng.hxx:55
decaf_spongerng_init_from_file
decaf_error_t DECAF_API_VIS decaf_spongerng_init_from_file(decaf_keccak_prng_t prng, const char *file, size_t len, int deterministic) DECAF_NONNULL DECAF_WARN_UNUSED
Initialize a sponge-based CSPRNG from a file.
decaf::Buffer
A reference to a writable block of data.
Definition: secure_buffer.hxx:264
decaf::Block::data
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition: secure_buffer.hxx:193
spongerng.h
Sponge-based RNGs.
decaf::SpongeRng::SpongeRng
SpongeRng(const Block &in, Deterministic det)
Initialize, deterministically by default, from block.
Definition: spongerng.hxx:61
decaf_keccak_prng_t
decaf_keccak_prng_s decaf_keccak_prng_t[1]
Keccak CSPRNG structure as one-element array.
Definition: spongerng.h:27