Loading...
Searching...
No Matches
spongerng.hxx
Go to the documentation of this file.
#ifndef __DECAF_SPONGERNG_HXX__
#define __DECAF_SPONGERNG_HXX__
#include <decaf/spongerng.h>
#include <string>
#include <sys/types.h>
#include <errno.h>
#if __cplusplus >= 201103L
#define DECAF_NOEXCEPT noexcept
#define DECAF_DELETE = delete
#else
#define DECAF_NOEXCEPT throw()
#define DECAF_DELETE
#endif
namespace decaf {
class SpongeRng : public Rng {
private:
decaf_keccak_prng_t sp;
public:
enum Deterministic { RANDOM = 0, DETERMINISTIC = 1 };
class RngException : public std::exception {
private:
const char *const what_;
public:
const int err_code;
const char *what() const DECAF_NOEXCEPT { return what_; }
RngException(int err_code, const char *what_) DECAF_NOEXCEPT : what_(what_), err_code(err_code) {}
};
inline SpongeRng( const Block &in, Deterministic det ) {
decaf_spongerng_init_from_buffer(sp,in.data(),in.size(),(int)det);
}
inline SpongeRng( const std::string &in = "/dev/urandom", size_t len = 32, Deterministic det = RANDOM )
/*throw(RngException)*/ {
decaf_error_t ret = decaf_spongerng_init_from_file(sp,in.c_str(),len,det);
if (!decaf_successful(ret)) {
throw RngException(errno, "Couldn't load from file");
}
}
inline void stir( const Block &data ) DECAF_NOEXCEPT {
decaf_spongerng_stir(sp,data.data(),data.size());
}
inline ~SpongeRng() DECAF_NOEXCEPT { decaf_spongerng_destroy(sp); }
using Rng::read;
virtual inline void read(Buffer buffer) DECAF_NOEXCEPT
#if __cplusplus >= 201103L
final
#endif
{ decaf_spongerng_next(sp,buffer.data(),buffer.size()); }
private:
SpongeRng(const SpongeRng &) DECAF_DELETE;
SpongeRng &operator=(const SpongeRng &) DECAF_DELETE;
};
} /* namespace decaf */
#undef DECAF_NOEXCEPT
#undef DECAF_DELETE
#endif /* __DECAF_SPONGERNG_HXX__ */
decaf::Block
A reference to a block of data, which (when accessed through this base class) is const.
Definition secure_buffer.hxx:159
decaf::Block::data
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition secure_buffer.hxx:199
decaf::Block::size
size_t size() const DECAF_NOEXCEPT
Get the size.
Definition secure_buffer.hxx:208
decaf::Buffer
A reference to a writable block of data.
Definition secure_buffer.hxx:270
decaf::FixedBlock
A fixed-size block.
Definition secure_buffer.hxx:253
decaf::Rng
Prototype of a random number generator.
Definition secure_buffer.hxx:138
decaf::Rng::read
virtual void read(Buffer buffer) DECAF_NOEXCEPT=0
Read into a Buffer.
decaf::SpongeRng::RngException
Exception thrown when The RNG fails (to seed itself)
Definition spongerng.hxx:49
decaf::SpongeRng::RngException::RngException
RngException(int err_code, const char *what_) DECAF_NOEXCEPT
Construct.
Definition spongerng.hxx:57
decaf::SpongeRng::RngException::err_code
const int err_code
errno that caused the reseed to fail.
Definition spongerng.hxx:55
decaf::SpongeRng::RngException::what
const char * what() const DECAF_NOEXCEPT
Description of exception.
Definition spongerng.hxx:56
decaf::SpongeRng
Sponge-based random-number generator.
Definition spongerng.hxx:36
decaf::SpongeRng::SpongeRng
SpongeRng(const Block &in, Deterministic det)
Initialize, deterministically by default, from block.
Definition spongerng.hxx:61
decaf::SpongeRng::stir
void stir(const Block &data) DECAF_NOEXCEPT
Stir in new data.
Definition spongerng.hxx:75
decaf::SpongeRng::~SpongeRng
~SpongeRng() DECAF_NOEXCEPT
Securely destroy by overwriting state.
Definition spongerng.hxx:80
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::read
virtual void read(Buffer buffer) DECAF_NOEXCEPT
Read data to a buffer.
Definition spongerng.hxx:85
decaf::SpongeRng::Deterministic
Deterministic
Deterministic flag.
Definition spongerng.hxx:46
decaf_error_t
decaf_error_t
Another boolean type used to indicate success or failure.
Definition common.h:120
decaf
Namespace for all C++ decaf objects.
Definition decaf.hxx:22
spongerng.h
Sponge-based RNGs.
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_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_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_keccak_prng_t
decaf_keccak_prng_s decaf_keccak_prng_t[1]
Keccak CSPRNG structure as one-element array.
Definition spongerng.h:27