crypto: Implement RFC8439-compatible variant of ChaCha20

There are two variants of ChaCha20 in use. The original one uses a 64-bit
nonce and a 64-bit block counter, while the one used in RFC8439 uses a
96-bit nonce and 32-bit block counter. This commit changes the interface
to use the 96/32 split (but automatically incrementing the first 32-bit
part of the nonce when the 32-bit block counter overflows, so to retain
compatibility with >256 GiB output).

Simultaneously, also merge the SetIV and Seek64 functions, as we almost
always call both anyway.

Co-authored-by: dhruv <856960+dhruv@users.noreply.github.com>
This commit is contained in:
Pieter Wuille
2023-06-27 16:24:02 -04:00
parent cf4da5ec29
commit 511a8d406e
7 changed files with 89 additions and 71 deletions

View File

@@ -47,16 +47,12 @@ ChaCha20Aligned::ChaCha20Aligned(const unsigned char* key32)
SetKey32(key32);
}
void ChaCha20Aligned::SetIV(uint64_t iv)
void ChaCha20Aligned::Seek64(Nonce96 nonce, uint32_t block_counter)
{
input[10] = iv;
input[11] = iv >> 32;
}
void ChaCha20Aligned::Seek64(uint64_t pos)
{
input[8] = pos;
input[9] = pos >> 32;
input[8] = block_counter;
input[9] = nonce.first;
input[10] = nonce.second;
input[11] = nonce.second >> 32;
}
inline void ChaCha20Aligned::Keystream64(unsigned char* c, size_t blocks)