ChaCha stream cipher. More...
ChaCha stream cipher.
Definition in file chacha.h.
#include <stdint.h>
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | chacha_ctx |
A ChaCha cipher stream context. More... | |
int | chacha_init (chacha_ctx *ctx, unsigned rounds, const uint8_t *key, uint32_t keylen, const uint8_t nonce[8]) |
Initialize a ChaCha context. | |
void | chacha_keystream_bytes (chacha_ctx *ctx, void *x) |
Generate next block in the keystream. | |
void | chacha_encrypt_bytes (chacha_ctx *ctx, const uint8_t *m, uint8_t *c) |
Encode or decode a block of data. | |
static void | chacha_decrypt_bytes (chacha_ctx *ctx, const uint8_t *m, uint8_t *c) |
Encode or decode a block of data. | |
void | chacha_prng_seed (const void *data, size_t bytes) |
Seed the pseudo-random number generator. | |
uint32_t | chacha_prng_next (void) |
Extract a number from the pseudo-random number generator. | |
|
inlinestatic |
Encode or decode a block of data.
m
is always the input regardless if it is the plaintext or ciphertext, and c
vice verse.
[in,out] | ctx | The ChaCha context. |
[in] | m | The input. |
[out] | c | The output. |
void chacha_encrypt_bytes | ( | chacha_ctx * | ctx, |
const uint8_t * | m, | ||
uint8_t * | c | ||
) |
Encode or decode a block of data.
m
is always the input regardless if it is the plaintext or ciphertext, and c
vice verse.
[in,out] | ctx | The ChaCha context. |
[in] | m | The input. |
[out] | c | The output. |
int chacha_init | ( | chacha_ctx * | ctx, |
unsigned | rounds, | ||
const uint8_t * | key, | ||
uint32_t | keylen, | ||
const uint8_t | nonce[8] | ||
) |
Initialize a ChaCha context.
[out] | ctx | The context to initialize |
[in] | rounds | Number of rounds. Recommended: 20. Also in use: 8 and 12. |
[in] | key | The key to use. |
[in] | keylen | Length (in bytes) of key . Must be 16 or 32. |
[in] | nonce | IV / nonce to use. |
== 0
on success. < 0
if an illegal value for rounds
or keylen
was supplied. void chacha_keystream_bytes | ( | chacha_ctx * | ctx, |
void * | x | ||
) |
Generate next block in the keystream.
If you want to seek inside the cipher steam, then you have to update the clock in ctx->state[13]:ctx->state[12]
manually.
[in,out] | ctx | The ChaCha context |
[out] | x | The block of the keystream (sizeof(x) == 64 ). |
uint32_t chacha_prng_next | ( | void | ) |
Extract a number from the pseudo-random number generator.
void chacha_prng_seed | ( | const void * | data, |
size_t | bytes | ||
) |
Seed the pseudo-random number generator.
You can seed the random number generator with up to 64 bytes of data. If you feed less than 64 bytes of data, then the privous state gets only partially overwritten.
If you want to supply multiple information, then you have to concatenate them manually before invoking the function.
The PRNG gets a random seed in the build process. You can set a deterministic value by supplying a comma separated argument to make
like RIOT_CHACHA_PRNG_DEFAULT="0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
.
[in] | data | Some random data. |
[in] | bytes | Length of data in bytes where 0 < bytes <= 64 . |