Chacha20 v0.3.1 Chacha20

Chacha20 symmetric stream cipher

https://tools.ietf.org/html/rfc7539

The calling semantics are still sub-optimal and no performance tuning has been done.

Summary

Types

The parameters and state of the current session

The shared encryption key

The shared per-session nonce

Functions

Return an arbitrary block

The crypt function suitable for a complete message

The crypt function suitable for streaming

Types

chacha_parameters :: {key, nonce, non_neg_integer, binary}

The parameters and state of the current session

  • The shared key
  • The session nonce
  • The next block number
  • The unused portion of the current block

To start from block 0, the initial state is {k,n,0,""}

key :: <<_::32 * 8>>

The shared encryption key.

nonce :: <<_::12 * 8>> | <<_::8 * 8>>

The shared per-session nonce.

By spec, this nonce may be used to encrypt a stream of up to 256GiB

An eight-byte nonce is compatible with the original reference implementation.

Functions

block(k, n, b)

Specs

block(key, nonce, non_neg_integer) :: binary

Return an arbitrary block

This is probably most useful in fast-forwarding a stream.

crypt(m, k, n, c \\ 0)

Specs

crypt(binary, key, nonce, non_neg_integer) :: binary

The crypt function suitable for a complete message.

This is a convenience wrapper when the full message is ready for processing.

The operations are symmetric, so if crypt(m,k,n) = c, then crypt(c,k,n) = m

crypt_bytes(m, p, acc)

Specs

crypt_bytes(binary, chacha_parameters, [binary]) :: {binary, chacha_parameters}

The crypt function suitable for streaming

Use an initial state of {k,n,0,""} The returned parameters can be used for the next available bytes. Any previous emitted binary can be included in the acc, if desired.