libsodium_crypto_secretbox (libsodium v3.0.1)

View Source

Secret-key authenticated encryption with XSalsa20-Poly1305 (see primitive/0).

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 is long enough to be generated at random, but must never be reused with the same key.

crypto_secretbox/3 and open/3 are the NaCl compatibility interface, which requires zerobytes/0 leading zero bytes of padding on the plaintext and produces (and expects) boxzerobytes/0 leading zero bytes on the ciphertext. New code should use easy/3 and open_easy/3.

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

Summary

Types

k()

A crypto_secretbox_KEYBYTES (32-byte) binary.

A crypto_secretbox_MACBYTES (16-byte) binary.

n()

A crypto_secretbox_NONCEBYTES (24-byte) binary.

Functions

Return crypto_secretbox_boxzerobytes(), in bytes.

NaCl compatibility interface: encrypt M, which must start with zerobytes/0 zero bytes, with the nonce N and key K.

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_keybytes(), in bytes.

Generate a random keybytes/0 byte key.

Return crypto_secretbox_macbytes(), in bytes.

Return crypto_secretbox_messagebytes_max(), in bytes.

Return crypto_secretbox_noncebytes(), in bytes.

NaCl compatibility interface: verify and decrypt C, which must start with boxzerobytes/0 zero bytes, with the nonce N and key K.

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.

Return crypto_secretbox_primitive() as an atom.

Return crypto_secretbox_zerobytes(), in bytes.

Types

k()

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

A crypto_secretbox_KEYBYTES (32-byte) binary.

mac()

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

A crypto_secretbox_MACBYTES (16-byte) binary.

n()

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

A crypto_secretbox_NONCEBYTES (24-byte) binary.

Functions

boxzerobytes()

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

Return crypto_secretbox_boxzerobytes(), in bytes.

crypto_secretbox(M, N, K)

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

NaCl compatibility interface: encrypt M, which must start with zerobytes/0 zero bytes, with the nonce N and key K.

Returns a ciphertext of the same length as M that starts with boxzerobytes/0 zero bytes, followed by the authentication tag and the encrypted message, or -1 when M is shorter than zerobytes/0. Non-zero padding bytes leak the authentication key.

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_keybytes(), in bytes.

keygen()

-spec keygen() -> Result when Result :: k().

Generate a random keybytes/0 byte key.

macbytes()

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

Return crypto_secretbox_macbytes(), in bytes.

messagebytes_max()

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

Return crypto_secretbox_messagebytes_max(), in bytes.

noncebytes()

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

Return crypto_secretbox_noncebytes(), in bytes.

open(C, N, K)

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

NaCl compatibility interface: verify and decrypt C, which must start with boxzerobytes/0 zero bytes, with the nonce N and key K.

Returns a plaintext of the same length as C that starts with zerobytes/0 zero bytes, or -1 when verification fails or C is shorter than zerobytes/0.

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.

primitive()

-spec primitive() -> Result when Result :: xsalsa20poly1305.

Return crypto_secretbox_primitive() as an atom.

zerobytes()

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

Return crypto_secretbox_zerobytes(), in bytes.