|
rlib
Convenience library for useful things
|
OS-backed entropy sources plus refcounted PRNG objects. More...
Macros | |
| #define | r_rand_std_srand(seed) srand (seed) |
Seed the C-library rand() with seed. | |
| #define | r_rand_std_rand() rand () |
Single sample from the C-library rand(). | |
| #define | r_rand_prng_new r_prng_new_kiss |
| Default PRNG constructor (currently KISS). | |
| #define | r_prng_ref r_ref_ref |
| Take a reference (alias for r_ref_ref). | |
| #define | r_prng_unref r_ref_unref |
| Drop a reference (alias for r_ref_unref). | |
Typedefs | |
| typedef struct RPrng | RPrng |
| Opaque, refcounted PRNG handle. | |
Functions | |
| rboolean | r_rand_entropy_fill (ruint8 *buf, rsize size) |
Fill buf with size bytes of cryptographic-grade OS entropy. | |
| ruint64 | r_rand_entropy_u64 (void) |
| Read a 64-bit value from the OS entropy source. | |
| ruint32 | r_rand_entropy_u32 (void) |
| Read a 32-bit value from the OS entropy source. | |
| ruint64 | r_prng_get_u64 (RPrng *prng) |
Draw a 64-bit sample from prng. | |
| rboolean | r_prng_fill (RPrng *prng, ruint8 *buf, rsize size) |
Fill buf with size pseudo-random bytes from prng. | |
| rboolean | r_prng_fill_nonzero (RPrng *prng, ruint8 *buf, rsize size) |
| Like r_prng_fill, but every byte is guaranteed non-zero. | |
| rboolean | r_prng_fill_base64 (RPrng *prng, rchar *buf, rsize size) |
Fill buf with size base64-alphabet characters; useful for token / nonce generation. | |
PRNG constructors | |
| RPrng * | r_prng_new_crypto (void) |
| Cryptographically-secure PRNG: a ChaCha20 DRBG seeded from the OS entropy source and periodically reseeded. | |
| RPrng * | r_prng_new_system (void) |
| Cryptographically-secure PRNG that reads every output directly from the OS entropy source (see r_rand_entropy_fill). | |
| RPrng * | r_prng_new_kiss (void) |
| KISS PRNG seeded from OS entropy. | |
| RPrng * | r_prng_new_kiss_with_seed (ruint64 x, ruint64 y, ruint64 z, ruint64 c) |
| KISS PRNG with explicit four-component seed. | |
| RPrng * | r_prng_new_mt (void) |
| Mersenne Twister PRNG seeded from OS entropy. | |
| RPrng * | r_prng_new_mt_with_seed (ruint64 seed) |
| Mersenne Twister PRNG seeded from a single 64-bit value. | |
| RPrng * | r_prng_new_mt_with_seed_array (const ruint64 *array, rsize size) |
| Mersenne Twister PRNG seeded from an array of 64-bit values (recommended for reproducing a specific state). | |
OS-backed entropy sources plus refcounted PRNG objects.
Three tiers, by intended use:
getrandom / getentropy, CryptGenRandom on Windows, or /dev/urandom as a fallback).| RPrng * r_prng_new_crypto | ( | void | ) |
Cryptographically-secure PRNG: a ChaCha20 DRBG seeded from the OS entropy source and periodically reseeded.
This is the recommended source for cryptographic keys and nonces. It applies fast key erasure (forward secrecy) and reseeds from the OS after a bounded number of output bytes, combining the throughput of a userspace stream cipher with the unpredictability of the OS entropy pool.
NULL on allocation / entropy failure. | RPrng * r_prng_new_kiss | ( | void | ) |
KISS PRNG seeded from OS entropy.
| RPrng * r_prng_new_mt | ( | void | ) |
Mersenne Twister PRNG seeded from OS entropy.
| RPrng * r_prng_new_system | ( | void | ) |
Cryptographically-secure PRNG that reads every output directly from the OS entropy source (see r_rand_entropy_fill).
Slower than r_prng_new_crypto but holds no userspace state. Aborts the process if the OS source ever fails to deliver entropy, so callers never silently receive predictable output.
NULL on allocation failure. Fill buf with size bytes of cryptographic-grade OS entropy.
Reads from the OS CSPRNG, preferring the getrandom / getentropy syscalls (or CryptGenRandom on Windows) and falling back to /dev/urandom.
Unlike r_rand_entropy_u32 / r_rand_entropy_u64, this makes no fallback: it returns FALSE if the OS source cannot deliver the full request, so it is safe to use for keys and nonces.
| buf | Destination buffer. |
| size | Number of bytes to fill. |
TRUE if all size bytes were filled from the OS source. | ruint32 r_rand_entropy_u32 | ( | void | ) |
Read a 32-bit value from the OS entropy source.
| ruint64 r_rand_entropy_u64 | ( | void | ) |
Read a 64-bit value from the OS entropy source.