libdecaf
Loading...
Searching...
No Matches
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
33namespace decaf {
34
36class SpongeRng : public Rng {
37private:
40
41public:
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_; }
58 };
59
64
66 inline SpongeRng( const std::string &in = "/dev/urandom", size_t len = 32, Deterministic det = RANDOM )
67 /*throw(RngException)*/ {
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
90
91private:
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__ */
A reference to a block of data, which (when accessed through this base class) is const.
Definition secure_buffer.hxx:159
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition secure_buffer.hxx:199
size_t size() const DECAF_NOEXCEPT
Get the size.
Definition secure_buffer.hxx:208
A reference to a writable block of data.
Definition secure_buffer.hxx:270
A fixed-size block.
Definition secure_buffer.hxx:253
Prototype of a random number generator.
Definition secure_buffer.hxx:138
virtual void read(Buffer buffer) DECAF_NOEXCEPT=0
Read into a Buffer.
Exception thrown when The RNG fails (to seed itself)
Definition spongerng.hxx:49
RngException(int err_code, const char *what_) DECAF_NOEXCEPT
Construct.
Definition spongerng.hxx:57
const int err_code
errno that caused the reseed to fail.
Definition spongerng.hxx:55
const char * what() const DECAF_NOEXCEPT
Description of exception.
Definition spongerng.hxx:56
Sponge-based random-number generator.
Definition spongerng.hxx:36
SpongeRng(const Block &in, Deterministic det)
Initialize, deterministically by default, from block.
Definition spongerng.hxx:61
void stir(const Block &data) DECAF_NOEXCEPT
Stir in new data.
Definition spongerng.hxx:75
~SpongeRng() DECAF_NOEXCEPT
Securely destroy by overwriting state.
Definition spongerng.hxx:80
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
virtual void read(Buffer buffer) DECAF_NOEXCEPT
Read data to a buffer.
Definition spongerng.hxx:85
Deterministic
Deterministic flag.
Definition spongerng.hxx:46
decaf_error_t
Another boolean type used to indicate success or failure.
Definition common.h:120
Namespace for all C++ decaf objects.
Definition decaf.hxx:22
Sponge-based RNGs.
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.
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_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.
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_s decaf_keccak_prng_t[1]
Keccak CSPRNG structure as one-element array.
Definition spongerng.h:27