49#define LOG_MODULE "CSPRNG"
50#define LOG_LEVEL LOG_LEVEL_NONE
53static size_t read_state_bytes;
66 for(i = 0; i < CSPRNG_SEED_LEN; i++) {
67 seed.
u8[i] ^= new_seed->
u8[i];
71 LOG_DBG_BYTES(seed.
key, CSPRNG_KEY_LEN);
74 LOG_DBG_BYTES(seed.
state, CSPRNG_STATE_LEN);
89 pos = MIN(len, CSPRNG_STATE_LEN - read_state_bytes);
90 memcpy(result, seed.
state + read_state_bytes, pos);
91 read_state_bytes += pos;
96 AES_128.set_key(seed.
key);
97 for(; pos < len; pos += CSPRNG_STATE_LEN) {
98 AES_128.encrypt(seed.
state);
99 read_state_bytes = MIN(len - pos, CSPRNG_STATE_LEN);
100 memcpy(result + pos, seed.
state, read_state_bytes);
Default definitions of C compiler quirk work-arounds.
An OFB-AES-128-based CSPRNG.
bool csprng_rand(uint8_t *result, size_t len)
Generates a cryptographic random number.
void csprng_feed(struct csprng_seed *new_seed)
Mixes a new seed with the current one.
Header file for the logging system.
This is the structure of a seed.
uint8_t state[AES_128_BLOCK_SIZE]
internal state of the CSPRNG
uint8_t u8[(AES_128_KEY_LENGTH+AES_128_BLOCK_SIZE)]
for convenience
uint8_t key[AES_128_KEY_LENGTH]
AES-128 key of the CSPRNG.