27 #include "crypto/chacha/chacha.h"
67 struct NoiseRandState_s
77 uint8_t chacha_k[crypto_stream_chacha20_KEYBYTES];
78 uint8_t chacha_n[crypto_stream_chacha20_IETF_NONCEBYTES];
85 #define NOISE_RAND_RESEED_COUNT 1600000
88 #define NOISE_RAND_REKEY_COUNT 16
93 static uint8_t
const starting_key[32] = {
94 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85,
95 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A,
96 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C,
97 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19
128 memcpy((*state)->chacha_k, starting_key, crypto_stream_chacha20_KEYBYTES);
129 memset((*state)->chacha_n, 0, crypto_stream_chacha20_IETF_NONCEBYTES);
131 chacha_keysetup(&((*state)->chacha), starting_key, 256);
180 uint8_t data[crypto_stream_chacha20_KEYBYTES + crypto_stream_chacha20_IETF_NONCEBYTES];
193 crypto_stream_chacha20_ietf_xor(data, data,
sizeof(data), state->chacha_n, state->chacha_k);
194 memcpy(state->chacha_k, data, crypto_stream_chacha20_KEYBYTES);
195 memcpy(state->chacha_n, data + crypto_stream_chacha20_KEYBYTES, crypto_stream_chacha20_IETF_NONCEBYTES);
197 chacha_encrypt_bytes(&(state->chacha), data, data,
sizeof(data));
198 chacha_keysetup(&(state->chacha), data, 256);
199 chacha_ivsetup(&(state->chacha), data + 32, 0);
201 state->left = NOISE_RAND_RESEED_COUNT;
204 memset(data, 0,
sizeof(data));
206 crypto_stream_chacha20_ietf_xor(data, data,
sizeof(data), state->chacha_n, state->chacha_k);
207 memcpy(state->chacha_k, data, crypto_stream_chacha20_KEYBYTES);
208 memcpy(state->chacha_n, data + crypto_stream_chacha20_KEYBYTES, crypto_stream_chacha20_IETF_NONCEBYTES);
210 chacha_encrypt_bytes(&(state->chacha), data, data,
sizeof(data));
211 chacha_keysetup(&(state->chacha), data, 256);
212 chacha_ivsetup(&(state->chacha), data + 32, 0);
228 uint8_t data[crypto_stream_chacha20_KEYBYTES + crypto_stream_chacha20_IETF_NONCEBYTES];
232 memset(data, 0,
sizeof(data));
234 crypto_stream_chacha20_ietf_xor(data, data,
sizeof(data), state->chacha_n, state->chacha_k);
235 memcpy(state->chacha_k, data, crypto_stream_chacha20_KEYBYTES);
236 memcpy(state->chacha_n, data + crypto_stream_chacha20_KEYBYTES, crypto_stream_chacha20_IETF_NONCEBYTES);
238 chacha_encrypt_bytes(&(state->chacha), data, data,
sizeof(data));
239 chacha_keysetup(&(state->chacha), data, 256);
240 chacha_ivsetup(&(state->chacha), data + 32, 0);
275 memset(buffer, 0, len);
280 if (state->left < len)
289 if (state->left >= 64) {
297 if (blocks++ >= NOISE_RAND_REKEY_COUNT) {
299 noise_randstate_rekey(state);
303 crypto_stream_chacha20_ietf_xor_ic(buffer, buffer, temp_len, state->chacha_n, blocks + 1, state->chacha_k);
305 chacha_encrypt_bytes(&(state->chacha), buffer, buffer, temp_len);
314 noise_randstate_rekey(state);
350 size_t padded_len,
int padding_mode)
358 if (padded_len > orig_len)
359 memset(payload + orig_len, 0, padded_len - orig_len);
364 if (padded_len <= orig_len)
369 memset(payload + orig_len, 0, padded_len - orig_len);
373 (state, payload + orig_len, padded_len - orig_len);
400 memset(&state, 0,
sizeof(state));
402 memcpy(state.chacha_k, starting_key, crypto_stream_chacha20_KEYBYTES);
403 memset(state.chacha_n, 0, crypto_stream_chacha20_IETF_NONCEBYTES);
405 chacha_keysetup(&(state.chacha), starting_key, 256);
#define NOISE_PADDING_ZERO
Pads messages with zero bytes.
void noise_clean(void *data, size_t size)
Cleans a block of memory to destroy its contents.
#define NOISE_ERROR_INVALID_PARAM
Invalid parameter to function; e.g. a NULL value.
#define NOISE_ERROR_NONE
Success, no error.
int noise_randstate_pad(NoiseRandState *state, uint8_t *payload, size_t orig_len, size_t padded_len, int padding_mode)
Adds padding bytes to the end of a message payload.
#define NOISE_ERROR_NO_MEMORY
Insufficient memory to complete the operation.
int noise_randstate_generate(NoiseRandState *state, uint8_t *buffer, size_t len)
Generates random bytes for use by the application.
int noise_randstate_new(NoiseRandState **state)
Creates a new random number generator.
struct NoiseRandState_s NoiseRandState
Opaque object that represents a random number generator.
int noise_randstate_generate_simple(uint8_t *buffer, size_t len)
Generates random data without first creating a RandState object.
Internal definitions for the library.
void noise_free(void *ptr, size_t size)
Destroys the contents of a block of memory and free it.
int noise_randstate_reseed(NoiseRandState *state)
Reseeds the random number generator from operating system entropy.
#define noise_new(type)
Allocates an object from the system and initializes it.
int noise_randstate_free(NoiseRandState *state)
Frees a RandState object after destroying all sensitive material.
void noise_rand_bytes(void *bytes, size_t size)
Gets cryptographically-strong random bytes from the operating system.