chacha.h

ChaCha stream cipher.

int chacha_init(chacha_ctx * ctx, unsigned rounds, const uint8_t * key, uint32_t keylen, const uint8_t nonce)

Initialize a ChaCha context.

Parameters

ctx:The context to initialize
rounds:Number of rounds. Recommended: 20. Also in use: 8 and 12.
key:The key to use.
keylen:Length (in bytes) of key. Must be 16 or 32.
nonce:IV / nonce to use.

Return values

  • == 0 on success.
  • < 0 if an illegal value for rounds or keylen was suppplied.
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.

Parameters

ctx:The ChaCha context
x:The block of the keystream (sizeof(x) == 64).

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.

Parameters

ctx:The ChaCha context.
m:The input.
c:The output.

void chacha_decrypt_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.

Parameters

ctx:The ChaCha context.
m:The input.
c:The output.

void chacha_prng_seed(const void * data, msp430_types.h::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".

Parameters

data:Some random data.
bytes:Length of data in bytes where 0 < bytes <= 64.

uint32_t chacha_prng_next(void)

Extract a number from the pseudo-random number generator.

Return values

  • The random value
struct chacha_ctx

A ChaCha cipher stream context.

Initialize with chacha.h::chacha_init().

uint32_t state()

The current state of the stream.

uint8_t rounds

Number of iterations.