libsodium_crypto_secretstream_xchacha20poly1305 (libsodium v3.0.1)
View SourceAuthenticated encryption of a sequence of messages with XChaCha20-Poly1305, detecting truncation, reordering, and duplication.
The sender calls init_push/1 with a keybytes/0 byte key to obtain an
opaque state/0 and a headerbytes/0 byte
header to transmit, then push/4 for each message. The receiver calls
init_pull/2 with the header and key and then pull/3 for each
ciphertext. Every call returns a new state and leaves its argument
unchanged; reusing an old state re-encrypts with the same nonce and must
be avoided.
Each message carries a tag: tag_message/0, tag_push/0 (end of a
logical chunk), tag_rekey/0 (derive a new key), or tag_final/0 (end
of the stream). Ciphertexts are abytes/0 bytes longer than their
messages.
Key = libsodium_crypto_secretstream_xchacha20poly1305:keygen(),
{Push0, Header} = libsodium_crypto_secretstream_xchacha20poly1305:init_push(Key),
{Push1, C1} = libsodium_crypto_secretstream_xchacha20poly1305:push(Push0, <<"hello">>, <<>>, libsodium_crypto_secretstream_xchacha20poly1305:tag_message()),
{_, C2} = libsodium_crypto_secretstream_xchacha20poly1305:push(Push1, <<"world">>, <<>>, libsodium_crypto_secretstream_xchacha20poly1305:tag_final()),
Pull0 = libsodium_crypto_secretstream_xchacha20poly1305:init_pull(Header, Key),
{Pull1, <<"hello">>, 0} = libsodium_crypto_secretstream_xchacha20poly1305:pull(Pull0, C1, <<>>),
{_, <<"world">>, 3} = libsodium_crypto_secretstream_xchacha20poly1305:pull(Pull1, C2, <<>>).
Summary
Types
A crypto_secretstream_xchacha20poly1305_HEADERBYTES (24-byte) binary.
A crypto_secretstream_xchacha20poly1305_KEYBYTES (32-byte) binary.
An opaque native crypto_secretstream_xchacha20poly1305_state.
Functions
Return crypto_secretstream_xchacha20poly1305_abytes(), in bytes.
Return crypto_secretstream_xchacha20poly1305_headerbytes(), in bytes.
Start decrypting a stream from the headerbytes/0 byte Header
produced by init_push/1 and the key K.
Start an encrypted stream with the keybytes/0 byte key K.
Return crypto_secretstream_xchacha20poly1305_keybytes(), in bytes.
Generate a random keybytes/0 byte key.
Return crypto_secretstream_xchacha20poly1305_messagebytes_max(), in bytes.
Verify and decrypt the next ciphertext C (at least abytes/0
bytes) with the additional data AD using the stream State.
Encrypt M with the additional data AD (may be empty) and the tag
Tag (an integer from 0 to 255, normally one of the tag_*
constants) using the stream State.
Explicitly derive a new key for the stream State and return the
updated state. The receiver must call rekey/1 at the same point in
the stream.
Return crypto_secretstream_xchacha20poly1305_statebytes(), in bytes.
Return crypto_secretstream_xchacha20poly1305_tag_final().
Return crypto_secretstream_xchacha20poly1305_tag_message().
Return crypto_secretstream_xchacha20poly1305_tag_push().
Return crypto_secretstream_xchacha20poly1305_tag_rekey().
Types
-type header() :: <<_:192>>.
A crypto_secretstream_xchacha20poly1305_HEADERBYTES (24-byte) binary.
-type k() :: <<_:256>>.
A crypto_secretstream_xchacha20poly1305_KEYBYTES (32-byte) binary.
-type state() :: libsodium_nif:crypto_secretstream_xchacha20poly1305_state().
An opaque native crypto_secretstream_xchacha20poly1305_state.
Operations never modify a state: each returns a new one, so earlier states remain valid and can be reused or shared between processes. States cannot be serialized; they exist only within the running VM.
Functions
-spec abytes() -> Result when Result :: 17.
Return crypto_secretstream_xchacha20poly1305_abytes(), in bytes.
-spec headerbytes() -> Result when Result :: 24.
Return crypto_secretstream_xchacha20poly1305_headerbytes(), in bytes.
Start decrypting a stream from the headerbytes/0 byte Header
produced by init_push/1 and the key K.
Returns the stream state, or -1 on failure.
Start an encrypted stream with the keybytes/0 byte key K.
Returns {State, Header}, where Header must be sent to the
receiver for init_pull/2, or -1 on failure.
-spec keybytes() -> Result when Result :: 32.
Return crypto_secretstream_xchacha20poly1305_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_secretstream_xchacha20poly1305_messagebytes_max(), in bytes.
-spec pull(State, C, AD) -> Result when State :: state(), C :: binary(), AD :: binary(), Result :: {binary(), binary(), 0..255} | -1.
Verify and decrypt the next ciphertext C (at least abytes/0
bytes) with the additional data AD using the stream State.
Returns {NewState, M, Tag}, or -1 when C is invalid, out of
order, or tampered with. A tag_final/0 tag marks the end of the
stream.
-spec push(State, M, AD, Tag) -> Result when State :: state(), M :: binary(), AD :: binary(), Tag :: 0..255, Result :: {state(), binary()} | -1.
Encrypt M with the additional data AD (may be empty) and the tag
Tag (an integer from 0 to 255, normally one of the tag_*
constants) using the stream State.
Returns {NewState, C}, where C is abytes/0 bytes longer than
M, or -1 on failure.
Explicitly derive a new key for the stream State and return the
updated state. The receiver must call rekey/1 at the same point in
the stream.
-spec statebytes() -> Result when Result :: 52.
Return crypto_secretstream_xchacha20poly1305_statebytes(), in bytes.
-spec tag_final() -> Result when Result :: 3.
Return crypto_secretstream_xchacha20poly1305_tag_final().
-spec tag_message() -> Result when Result :: 0.
Return crypto_secretstream_xchacha20poly1305_tag_message().
-spec tag_push() -> Result when Result :: 1.
Return crypto_secretstream_xchacha20poly1305_tag_push().
-spec tag_rekey() -> Result when Result :: 2.
Return crypto_secretstream_xchacha20poly1305_tag_rekey().