The Salsa20/Chacha20 stream ciphers

These are similar stream ciphers by djb.

A reduced round variant of Salsa20 (Salsa20/12) was selected as a finalist of the eSTREAM stream cipher competition. This implementation does the full 20 rounds.

ChaCha20 is fundamentally like Salsa20, but has a tweaked round function to improve security margin without damaging performance.

Types

cf_salsa20_ctx

Incremental interface to Salsa20.

cf_salsa20_ctx.key0

Half of key material.

cf_salsa20_ctx.key1

Half of key material.

cf_salsa20_ctx.nonce

Nonce and counter block.

cf_salsa20_ctx.constant

Per-key-length constants.

cf_salsa20_ctx.block

Buffer for unused key stream material.

cf_salsa20_ctx.nblock

Number of bytes at end of block that can be used as key stream.

cf_chacha20_ctx

Incremental interface to Chacha20. This structure is identical to cf_salsa20_ctx.

Functions

void cf_salsa20_init(cf_salsa20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[8])

Salsa20 initialisation function.

Parameters:
  • ctx – salsa20 context.
  • key – key material.
  • nkey – length of key in bytes, either 16 or 32.
  • nonce – per-message nonce.
void cf_chacha20_init(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[8])

Chacha20 initialisation function.

Parameters:
  • ctx – chacha20 context (written).
  • key – key material.
  • nkey – length of key in bytes, either 16 or 32.
  • nonce – per-message nonce.
void cf_chacha20_init_custom(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[16], size_t ncounter)

Chacha20 initialisation function. This version gives full control over the whole initial nonce value, and the size of the counter. The counter is always at the front of the nonce.

Parameters:
  • ctx – chacha20 context (written).
  • key – key material.
  • nkey – length of key in bytes, either 16 or 32.
  • nonce – per-message nonce. ncounter bytes at the start are the block counter.
  • ncounter – length, in bytes, of the counter portion of the nonce.
void cf_salsa20_cipher(cf_salsa20_ctx *ctx, const uint8_t *input, uint8_t *output, size_t count)

Salsa20 encryption/decryption function.

Parameters:
  • ctx – salsa20 context.
  • input – input data buffer (read), count bytes long.
  • output – output data buffer (written), count bytes long.
void cf_chacha20_cipher(cf_chacha20_ctx *ctx, const uint8_t *input, uint8_t *output, size_t count)

Chacha20 encryption/decryption function.

Parameters:
  • ctx – chacha20 context.
  • input – input data buffer (read), count bytes long.
  • output – output data buffer (written), count bytes long.