libsodium_crypto_secretbox_xsalsa20poly1305 (libsodium v3.0.1)

View Source

The NaCl XSalsa20-Poly1305 secret box primitive behind libsodium_crypto_secretbox.

Only the NaCl padded interface is available here: the plaintext passed to crypto_secretbox_xsalsa20poly1305/3 must start with zerobytes/0 zero bytes, and the ciphertext starts with boxzerobytes/0 zero bytes followed by the macbytes/0 byte tag. Keys are keybytes/0 bytes and nonces noncebytes/0 bytes; a nonce must never be reused with the same key. Prefer libsodium_crypto_secretbox:easy/3 in new code.

Key = libsodium_crypto_secretbox_xsalsa20poly1305:keygen(),
Nonce = libsodium_randombytes:buf(libsodium_crypto_secretbox_xsalsa20poly1305:noncebytes()),
Padded = <<0:256, "message">>,
Boxed = libsodium_crypto_secretbox_xsalsa20poly1305:crypto_secretbox_xsalsa20poly1305(Padded, Nonce, Key),
<<0:128, _Tag:16/binary, _Ciphertext:7/binary>> = Boxed,
Padded = libsodium_crypto_secretbox_xsalsa20poly1305:open(Boxed, Nonce, Key).

Summary

Types

k()

A crypto_secretbox_xsalsa20poly1305_KEYBYTES (32-byte) binary.

n()

A crypto_secretbox_xsalsa20poly1305_NONCEBYTES (24-byte) binary.

Functions

Return crypto_secretbox_xsalsa20poly1305_boxzerobytes(), in bytes.

Encrypt M, which must start with zerobytes/0 zero bytes, with the noncebytes/0 byte nonce N and the keybytes/0 byte key K.

Return crypto_secretbox_xsalsa20poly1305_keybytes(), in bytes.

Generate a random keybytes/0 byte key.

Return crypto_secretbox_xsalsa20poly1305_macbytes(), in bytes.

Return crypto_secretbox_xsalsa20poly1305_messagebytes_max(), in bytes.

Return crypto_secretbox_xsalsa20poly1305_noncebytes(), in bytes.

Verify and decrypt C, which must start with boxzerobytes/0 zero bytes, with the nonce N and key K.

Return crypto_secretbox_xsalsa20poly1305_zerobytes(), in bytes.

Types

k()

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

A crypto_secretbox_xsalsa20poly1305_KEYBYTES (32-byte) binary.

n()

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

A crypto_secretbox_xsalsa20poly1305_NONCEBYTES (24-byte) binary.

Functions

boxzerobytes()

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

Return crypto_secretbox_xsalsa20poly1305_boxzerobytes(), in bytes.

crypto_secretbox_xsalsa20poly1305(M, N, K)

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

Encrypt M, which must start with zerobytes/0 zero bytes, with the noncebytes/0 byte nonce N and the keybytes/0 byte 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.

keybytes()

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

Return crypto_secretbox_xsalsa20poly1305_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_xsalsa20poly1305_macbytes(), in bytes.

messagebytes_max()

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

Return crypto_secretbox_xsalsa20poly1305_messagebytes_max(), in bytes.

noncebytes()

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

Return crypto_secretbox_xsalsa20poly1305_noncebytes(), in bytes.

open(C, N, K)

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

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.

zerobytes()

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

Return crypto_secretbox_xsalsa20poly1305_zerobytes(), in bytes.