Skip to Content
oxcrypto

ChaCha

Encrypt with ChaCha8, ChaCha12, ChaCha20, XChaCha or the original 64-bit-nonce ChaCha20.

from oxcrypto.stream.chacha20 import CHACHA

key = bytes(32)
nonce = bytes(12)
ciphertext = CHACHA.CHACHA20.apply_keystream(key, nonce, b"hello", counter=1)
assert CHACHA.CHACHA20.apply_keystream(key, nonce, ciphertext, counter=1) == b"hello"

cipher = CHACHA.CHACHA20.new(key, nonce)
first = cipher.apply_keystream(b"hel")
cipher.seek(64)
assert cipher.position == 64
VariantKeyNonceUsable keystream
CHACHA8, CHACHA12, CHACHA2032 bytes12 bytes2³² − 1 blocks
XCHACHA8, XCHACHA12, XCHACHA2032 bytes24 bytes2³² − 1 blocks
CHACHA20_LEGACY32 bytes8 bytes2⁶⁴ − 1 blocks

CHACHA20 is the RFC 8439 cipher. XCHACHA20 follows draft-irtf-cfrg-xchacha, which is an expired Internet-Draft. CHACHA8 and CHACHA12 are reduced-round variants with the RFC 8439 layout. CHACHA20_LEGACY is the original design with a 64-bit nonce and counter.

counter is the starting 64-byte block. seek and position use byte offsets. The upstream crate does not produce the final block of the counter range, so processing past the usable keystream raises ValueError.