libsodium_crypto_box (libsodium v3.0.1)
View SourcePublic-key authenticated encryption (X25519, XSalsa20, and Poly1305).
A sender encrypts a message with the recipient's public key PK and their
own secret key SK; the recipient opens it with the sender's public key
and their own secret key. Keys are publickeybytes/0 and
secretkeybytes/0 bytes, and nonces are noncebytes/0 bytes. A nonce must
never be reused for the same pair of keys; random nonces are safe at this
size. Ciphertexts are macbytes/0 bytes longer than their messages.
beforenm/2 precomputes the beforenmbytes/0 byte shared key used by the
_afternm variants. seal/2 and seal_open/3 provide anonymous
encryption to a public key using an ephemeral sender key pair; sealed boxes
are sealbytes/0 bytes longer than their messages.
crypto_box/4, open/4, afternm/3, and open_afternm/3 are the NaCl
compatibility interface: messages must start with zerobytes/0 zero bytes
and ciphertexts with boxzerobytes/0 zero bytes. Prefer the easy API.
{AlicePK, AliceSK} = libsodium_crypto_box:keypair(),
{BobPK, BobSK} = libsodium_crypto_box:keypair(),
Nonce = libsodium_randombytes:buf(libsodium_crypto_box:noncebytes()),
Ciphertext = libsodium_crypto_box:easy(<<"message">>, Nonce, BobPK, AliceSK),
<<"message">> = libsodium_crypto_box:open_easy(Ciphertext, Nonce, AlicePK, BobSK),
Sealed = libsodium_crypto_box:seal(<<"anonymous">>, BobPK),
<<"anonymous">> = libsodium_crypto_box:seal_open(Sealed, BobPK, BobSK).
Summary
Types
A crypto_box_BEFORENMBYTES (32-byte) binary.
A crypto_box_MACBYTES (16-byte) binary.
A crypto_box_NONCEBYTES (24-byte) binary.
A crypto_box_PUBLICKEYBYTES (32-byte) binary.
A crypto_box_SEEDBYTES (32-byte) binary.
A crypto_box_SECRETKEYBYTES (32-byte) binary.
Functions
Like crypto_box/4, using the shared key K precomputed by beforenm/2.
Precompute the beforenmbytes/0 byte shared key for the public key PK
and the secret key SK, for use with the _afternm functions.
Return crypto_box_beforenmbytes(), in bytes.
Return crypto_box_boxzerobytes(), in bytes.
NaCl-compatible encryption of M, which must start with zerobytes/0
zero bytes, using the nonce N, the recipient's public key PK, and
the sender's secret key SK.
Like easy/4, but returns the ciphertext and the macbytes/0 byte
authentication tag separately as {C, MAC}.
Like detached/4, using the shared key K precomputed by beforenm/2.
Returns {C, MAC}.
Encrypt and authenticate M with the nonce N, the recipient's public
key PK, and the sender's secret key SK.
Like easy/4, using the shared key K precomputed by beforenm/2.
Generate a random key pair. Returns {PK, SK}.
Return crypto_box_macbytes(), in bytes.
Return crypto_box_messagebytes_max(), in bytes.
Return crypto_box_noncebytes(), in bytes.
NaCl-compatible verification and decryption of C, which must start
with boxzerobytes/0 zero bytes, using the nonce N, the sender's
public key PK, and the recipient's secret key SK.
Like open/4, using the shared key K precomputed by beforenm/2.
Returns -1 when verification fails.
Verify the detached tag MAC and decrypt C with the nonce N, the
sender's public key PK, and the recipient's secret key SK.
Like open_detached/5, using the shared key K precomputed by
beforenm/2. Returns -1 when verification fails.
Verify and decrypt C, produced by easy/4, with the nonce N, the
sender's public key PK, and the recipient's secret key SK.
Like open_easy/4, using the shared key K precomputed by
beforenm/2. Returns -1 when verification fails.
Return crypto_box_primitive() as an atom.
Return crypto_box_publickeybytes(), in bytes.
Anonymously encrypt M for the recipient's public key PK using a new
ephemeral key pair. The result is sealbytes/0 bytes longer than M;
only the recipient can open it, but the sender cannot be identified.
Open the sealed box C with the recipient's key pair PK and SK.
Return crypto_box_sealbytes(), in bytes.
Return crypto_box_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed.
Returns {PK, SK}.
Return crypto_box_seedbytes(), in bytes.
Return crypto_box_zerobytes(), in bytes.
Types
-type k() :: <<_:256>>.
A crypto_box_BEFORENMBYTES (32-byte) binary.
-type mac() :: <<_:128>>.
A crypto_box_MACBYTES (16-byte) binary.
-type n() :: <<_:192>>.
A crypto_box_NONCEBYTES (24-byte) binary.
-type pk() :: <<_:256>>.
A crypto_box_PUBLICKEYBYTES (32-byte) binary.
-type seed() :: <<_:256>>.
A crypto_box_SEEDBYTES (32-byte) binary.
-type sk() :: <<_:256>>.
A crypto_box_SECRETKEYBYTES (32-byte) binary.
Functions
Like crypto_box/4, using the shared key K precomputed by beforenm/2.
Precompute the beforenmbytes/0 byte shared key for the public key PK
and the secret key SK, for use with the _afternm functions.
Returns -1 when PK is a low-order point.
-spec beforenmbytes() -> Result when Result :: 32.
Return crypto_box_beforenmbytes(), in bytes.
-spec boxzerobytes() -> Result when Result :: 16.
Return crypto_box_boxzerobytes(), in bytes.
-spec crypto_box(M, N, PK, SK) -> Result when M :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
NaCl-compatible encryption of M, which must start with zerobytes/0
zero bytes, using the nonce N, the recipient's public key PK, and
the sender's secret key SK.
Returns a ciphertext of the same length as M whose first
boxzerobytes/0 bytes are zero, or -1 when M is shorter than
zerobytes/0.
-spec detached(M, N, PK, SK) -> Result when M :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: {binary(), mac()} | -1.
Like easy/4, but returns the ciphertext and the macbytes/0 byte
authentication tag separately as {C, MAC}.
-spec detached_afternm(M, N, K) -> Result when M :: binary(), N :: n(), K :: k(), Result :: {binary(), mac()} | -1.
Like detached/4, using the shared key K precomputed by beforenm/2.
Returns {C, MAC}.
-spec easy(M, N, PK, SK) -> Result when M :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
Encrypt and authenticate M with the nonce N, the recipient's public
key PK, and the sender's secret key SK.
Returns the macbytes/0 byte authentication tag followed by the
ciphertext, or -1 on failure.
-spec easy_afternm(M, N, K) -> Result when M :: binary(), N :: n(), K :: k(), Result :: binary() | -1.
Like easy/4, using the shared key K precomputed by beforenm/2.
Generate a random key pair. Returns {PK, SK}.
-spec macbytes() -> Result when Result :: 16.
Return crypto_box_macbytes(), in bytes.
-spec messagebytes_max() -> Result when Result :: non_neg_integer().
Return crypto_box_messagebytes_max(), in bytes.
-spec noncebytes() -> Result when Result :: 24.
Return crypto_box_noncebytes(), in bytes.
-spec open(C, N, PK, SK) -> Result when C :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
NaCl-compatible verification and decryption of C, which must start
with boxzerobytes/0 zero bytes, using the nonce N, the sender's
public key PK, and the recipient's secret key SK.
Returns a plaintext of the same length as C whose first zerobytes/0
bytes are zero, or -1 when verification fails.
-spec open_afternm(C, N, K) -> Result when C :: binary(), N :: n(), K :: k(), Result :: binary() | -1.
Like open/4, using the shared key K precomputed by beforenm/2.
Returns -1 when verification fails.
-spec open_detached(C, MAC, N, PK, SK) -> Result when C :: binary(), MAC :: mac(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
Verify the detached tag MAC and decrypt C with the nonce N, the
sender's public key PK, and the recipient's secret key SK.
Returns the plaintext, or -1 when verification fails.
-spec open_detached_afternm(C, MAC, N, K) -> Result when C :: binary(), MAC :: mac(), N :: n(), K :: k(), Result :: binary() | -1.
Like open_detached/5, using the shared key K precomputed by
beforenm/2. Returns -1 when verification fails.
-spec open_easy(C, N, PK, SK) -> Result when C :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
Verify and decrypt C, produced by easy/4, with the nonce N, the
sender's public key PK, and the recipient's secret key SK.
Returns the plaintext, or -1 when verification fails.
-spec open_easy_afternm(C, N, K) -> Result when C :: binary(), N :: n(), K :: k(), Result :: binary() | -1.
Like open_easy/4, using the shared key K precomputed by
beforenm/2. Returns -1 when verification fails.
-spec primitive() -> Result when Result :: curve25519xsalsa20poly1305.
Return crypto_box_primitive() as an atom.
-spec publickeybytes() -> Result when Result :: 32.
Return crypto_box_publickeybytes(), in bytes.
Anonymously encrypt M for the recipient's public key PK using a new
ephemeral key pair. The result is sealbytes/0 bytes longer than M;
only the recipient can open it, but the sender cannot be identified.
-spec seal_open(C, PK, SK) -> Result when C :: binary(), PK :: pk(), SK :: sk(), Result :: binary() | -1.
Open the sealed box C with the recipient's key pair PK and SK.
Returns the plaintext, or -1 when verification fails. Raises badarg
when C is shorter than sealbytes/0.
-spec sealbytes() -> Result when Result :: 48.
Return crypto_box_sealbytes(), in bytes.
-spec secretkeybytes() -> Result when Result :: 32.
Return crypto_box_secretkeybytes(), in bytes.
Deterministically derive a key pair from the seedbytes/0 byte Seed.
Returns {PK, SK}.
-spec seedbytes() -> Result when Result :: 32.
Return crypto_box_seedbytes(), in bytes.
-spec zerobytes() -> Result when Result :: 32.
Return crypto_box_zerobytes(), in bytes.