libsodium_crypto_secretbox_xchacha20poly1305 (libsodium v3.0.1)

View Source

Secret-key authenticated encryption with XChaCha20-Poly1305, using the same box layout as libsodium_crypto_secretbox.

easy/3 returns the macbytes/0 byte authentication tag followed by the ciphertext, and detached/3 returns them separately. Keys are keybytes/0 bytes and nonces noncebytes/0 bytes; the nonce can be generated at random but must never be reused with the same key. There is no keygen function: use libsodium_randombytes:buf/1 with keybytes/0.

Key = libsodium_randombytes:buf(libsodium_crypto_secretbox_xchacha20poly1305:keybytes()),
Nonce = libsodium_randombytes:buf(libsodium_crypto_secretbox_xchacha20poly1305:noncebytes()),
Ciphertext = libsodium_crypto_secretbox_xchacha20poly1305:easy(<<"message">>, Nonce, Key),
<<"message">> = libsodium_crypto_secretbox_xchacha20poly1305:open_easy(Ciphertext, Nonce, Key).

Summary

Types

k()

A crypto_secretbox_xchacha20poly1305_KEYBYTES (32-byte) binary.

A crypto_secretbox_xchacha20poly1305_MACBYTES (16-byte) binary.

n()

A crypto_secretbox_xchacha20poly1305_NONCEBYTES (24-byte) binary.

Functions

Encrypt and authenticate M with the nonce N and key K, returning the ciphertext and the macbytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

Encrypt and authenticate M with the noncebytes/0 byte nonce N and the keybytes/0 byte key K.

Return crypto_secretbox_xchacha20poly1305_keybytes(), in bytes.

Return crypto_secretbox_xchacha20poly1305_macbytes(), in bytes.

Return crypto_secretbox_xchacha20poly1305_messagebytes_max(), in bytes.

Return crypto_secretbox_xchacha20poly1305_noncebytes(), in bytes.

Verify the detached tag MAC and decrypt C with the nonce N and key K. Returns the plaintext, or -1 when verification fails.

Verify and decrypt the ciphertext C produced by easy/3 with the nonce N and key K. Returns the plaintext, or -1 when verification fails or C is shorter than macbytes/0.

Types

k()

-type k() :: <<_:256>>.

A crypto_secretbox_xchacha20poly1305_KEYBYTES (32-byte) binary.

mac()

-type mac() :: <<_:128>>.

A crypto_secretbox_xchacha20poly1305_MACBYTES (16-byte) binary.

n()

-type n() :: <<_:192>>.

A crypto_secretbox_xchacha20poly1305_NONCEBYTES (24-byte) binary.

Functions

detached(M, N, K)

-spec detached(M, N, K) -> Result
                  when M :: binary(), N :: n(), K :: k(), Result :: {binary(), mac()} | -1.

Encrypt and authenticate M with the nonce N and key K, returning the ciphertext and the macbytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

easy(M, N, K)

-spec easy(M, N, K) -> Result when M :: binary(), N :: n(), K :: k(), Result :: binary() | -1.

Encrypt and authenticate M with the noncebytes/0 byte nonce N and the keybytes/0 byte key K.

Returns the macbytes/0 byte authentication tag followed by the ciphertext, or -1 on failure.

keybytes()

-spec keybytes() -> Result when Result :: 32.

Return crypto_secretbox_xchacha20poly1305_keybytes(), in bytes.

macbytes()

-spec macbytes() -> Result when Result :: 16.

Return crypto_secretbox_xchacha20poly1305_macbytes(), in bytes.

messagebytes_max()

-spec messagebytes_max() -> Result when Result :: non_neg_integer().

Return crypto_secretbox_xchacha20poly1305_messagebytes_max(), in bytes.

noncebytes()

-spec noncebytes() -> Result when Result :: 24.

Return crypto_secretbox_xchacha20poly1305_noncebytes(), in bytes.

open_detached(C, MAC, N, K)

-spec open_detached(C, MAC, N, K) -> Result
                       when C :: binary(), MAC :: mac(), N :: n(), K :: k(), Result :: binary() | -1.

Verify the detached tag MAC and decrypt C with the nonce N and key K. Returns the plaintext, or -1 when verification fails.

open_easy(C, N, K)

-spec open_easy(C, N, K) -> Result when C :: binary(), N :: n(), K :: k(), Result :: binary() | -1.

Verify and decrypt the ciphertext C produced by easy/3 with the nonce N and key K. Returns the plaintext, or -1 when verification fails or C is shorter than macbytes/0.