libsodium_crypto_stream_xchacha20 (libsodium v3.0.1)
View SourceThe XChaCha20 stream cipher: ChaCha20 extended with a 192-bit nonce, which is long enough to be chosen at random for every message.
crypto_stream_xchacha20/3 returns raw keystream, 'xor'/3 encrypts or
decrypts by combining a message with it, and xor_ic/4 starts at a given
64-byte block counter.
This is not authenticated encryption: ciphertexts can be modified without
detection, so pair it with a MAC or use libsodium_crypto_aead_xchacha20poly1305
instead. A nonce must never be reused with the same key.
K = libsodium_crypto_stream_xchacha20:keygen(),
N = libsodium_randombytes:buf(libsodium_crypto_stream_xchacha20:noncebytes()),
C = libsodium_crypto_stream_xchacha20:'xor'(<<"message">>, N, K),
<<"message">> = libsodium_crypto_stream_xchacha20:'xor'(C, N, K).
Summary
Types
A crypto_stream_xchacha20_KEYBYTES (32-byte) binary.
A crypto_stream_xchacha20_NONCEBYTES (24-byte) binary.
Functions
Return CLen bytes of keystream for the noncebytes/0 byte nonce N and the keybytes/0 byte key K.
Return crypto_stream_xchacha20_keybytes(), in bytes.
Generate a random keybytes/0 byte key.
Return crypto_stream_xchacha20_messagebytes_max(), in bytes.
Return crypto_stream_xchacha20_noncebytes(), in bytes.
Encrypt or decrypt M by combining it with the keystream for the nonce
N and key K, starting at block counter 0.
Like 'xor'/3, but start at the 64-byte block counter IC, so the
message is combined with the keystream from byte IC * 64 onwards.
Types
Functions
-spec crypto_stream_xchacha20(CLen, N, K) -> Result when CLen :: non_neg_integer(), N :: n(), K :: k(), Result :: binary().
Return CLen bytes of keystream for the noncebytes/0 byte nonce N and the keybytes/0 byte key K.
-spec keybytes() -> Result when Result :: 32.
Return crypto_stream_xchacha20_keybytes(), in bytes.
-spec keygen() -> Result when Result :: k().
Generate a random keybytes/0 byte key.
-spec messagebytes_max() -> Result when Result :: non_neg_integer().
Return crypto_stream_xchacha20_messagebytes_max(), in bytes.
-spec noncebytes() -> Result when Result :: 24.
Return crypto_stream_xchacha20_noncebytes(), in bytes.
Encrypt or decrypt M by combining it with the keystream for the nonce
N and key K, starting at block counter 0.
-spec xor_ic(M, N, IC, K) -> Result when M :: binary(), N :: n(), IC :: non_neg_integer(), K :: k(), Result :: binary().
Like 'xor'/3, but start at the 64-byte block counter IC, so the
message is combined with the keystream from byte IC * 64 onwards.