A look at the ChaCha stream cipher.
Introduction
ChaCha is a stream cipher designed by Daniel J. Bernstein after Salsa20, ChaCha is a variant of Salsa20.
We will be looking at ChaCha as it is described in RFC 8439.
Adoption
ChaCha20 is used in WireGuard and TLSv1.3 (where it’s also used alongside AES-128-GCM and AES-256-GCM). ChaCha20 is in favor when a stream cipher is acceptable and there may not be AES hardware acceleration or when the cipher has to be written in software.
Design
ChaCha uses an ARX (add-rotate-xor) design, it works on sixteen 32 bit little endian integers in a grid-like pattern.
ChaCha has a key size of 256 bits (8 words) and a nonce size of 96 bits (3 words).
For these reasons ChaCha is really easy to implement in software.
State
The ChaCha state can be represented as follows.
| CONSTANT | CONSTANT | CONSTANT | CONSTANT |
| KEY | KEY | KEY | KEY |
| KEY | KEY | KEY | KEY |
| CTR | NONCE | NONCE | NONCE |
The constant is defined as expand 32-byte k.
0x61707865; // "expa"
0x3320646e; // "nd 3"
0x79622d32; // "2-by"
0x6b206574; // "te k"
The ChaCha state is 512 bits in size, with 2^32 counters per key/nonce pair, and therefore because 4294967296 * 64, we have ~256 GiB to encrypt with a single key/nonce pair, but to be conservative I’d rekey much before that.
Rounds
ChaCha uses 20 rounds in total. a column round made out of 4 quarter rounds, and a diagonal round made of 4 quarter rounds.
The quarter round goes as follows:
#define CHACHA_QR(a, b, c, d) { a += b; d ^= a; d = ROTL32(d,16);\
c += d; b ^= c; b = ROTL32(b, 12);\
a += b; d ^= a; d = ROTL32(d, 8);\
c += d; b ^= c; b = ROTL32(b, 7); }
Where ROTL32 is defined as
#define ROTL32(x, n) ((x << n) | (x >> (32 - n)))
ChaCha20 rounds
// 20 / 2 = 10. we have both a column and diagonal round.
for(int i = 0; i < 10; i++) {
// column round
CHACHA_QR(state[0], state[4], state[8] , state[12]);
CHACHA_QR(state[1], state[5], state[9] , state[13]);
CHACHA_QR(state[2], state[6], state[10], state[14]);
CHACHA_QR(state[3], state[7], state[11], state[15]);
// diagonal round
CHACHA_QR(state[0], state[5], state[10], state[15]);
CHACHA_QR(state[1], state[6], state[11], state[12]);
CHACHA_QR(state[2], state[7], state[8] , state[13]);
CHACHA_QR(state[3], state[4], state[9] , state[14]);
}
Note there are other variants of ChaCha with altered rounds, for example ChaCha12 and ChaCha8.
Addition with original state
In ChaCha the state that’s had its rounds is added with the original state. This is so that an attacker would need the original state, otherwise a known plaintext would reveal the rounded state, and an attacker could invert the state getting the key.
Doing this makes an attacker need to know the original state.
So at the very beginning, before any rounds, we may do something as following:
uint32_t initial_state[16];
mymemcpy(initial_state, state, 64);
Then after the rounds:
for(int i = 0; i < 16; i++) {
state[i] += initial_state[i];
}
Then after this we can xor the plaintext with our chacha state and encrypt data.
References / See Also
- RFC 8439, you should use this if you want to implement ChaCha.
- my full chacha source code