crypto/chacha20
lib/crypto/chacha20.vklib/crypto/chacha20.vk Pure-Veko RFC 8439 ChaCha20 stream cipher, Poly1305 MAC, and ChaCha20-Poly1305 AEAD.
chacha20-block = fn [key bytes, counter i64, nonce bytes] any
Produces one 64-byte ChaCha20 keystream block for key (32 bytes), counter, nonce (12 bytes).
chacha20-xor = fn [key bytes, counter i64, nonce bytes, data bytes] any
Encrypts/decrypts data with ChaCha20 starting at counter.
poly1305-mac = fn [otk bytes, msg bytes] any
RFC 8439 Poly1305 130-bit MAC using 5 x 26-bit positive limbs (< 2^56 max intermediate).
aead-encrypt = fn [key bytes, nonce bytes, aad bytes, plaintext bytes] any
RFC 8439 ChaCha20-Poly1305 AEAD encryption returning ciphertext || tag(16).
aead-decrypt = fn [key bytes, nonce bytes, aad bytes, ct_and_tag bytes] any
RFC 8439 ChaCha20-Poly1305 AEAD decryption verifying 16-byte trailing tag in constant time.
hchacha20 = fn [key bytes, nonce16 bytes] bytes
Computes the 32-byte HChaCha20 subkey from key (32 bytes) and nonce16 (16 bytes).
use [cc = crypto/chacha20, hex = codec/hex]
k = hex/decode '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
n = hex/decode '000000090000004a0000000031415927'
subk = hex/encode (cc/hchacha20 k n)
if subk != '82413b4227b27bfed30e42508a877d73a0f9e4d58a74a853c12ec41326d3ecdc' [
err 'hchacha20 RFC vector mismatch'
] else [ .ok ]
xchacha20-poly1305-encrypt = fn [key bytes, nonce24 bytes, aad bytes, plaintext bytes] any
XChaCha20-Poly1305 AEAD encryption with 32-byte key and 24-byte nonce24, returning ciphertext || tag(16).
use [cc = crypto/chacha20]
k = bytes/fill 32 0x11
n = bytes/fill 24 0x22
aad = bytes/from-str 'hdr'
pt = bytes/from-str 'hello xchacha20'
ct = cc/xchacha20-poly1305-encrypt k n aad pt
back = cc/xchacha20-poly1305-decrypt k n aad ct
if back != pt [ err 'xchacha20 roundtrip' ] else [ .ok ]
xchacha20-poly1305-decrypt = fn [key bytes, nonce24 bytes, aad bytes, ct_and_tag bytes] any
XChaCha20-Poly1305 AEAD decryption verifying 16-byte trailing tag in constant time.