libsodium_crypto_box_curve25519xsalsa20poly1305 (libsodium v3.0.1)

View Source

The X25519, XSalsa20, and Poly1305 construction behind libsodium_crypto_box, exposed through the NaCl compatibility interface.

Messages passed to crypto_box_curve25519xsalsa20poly1305/4 and afternm/3 must start with zerobytes/0 zero bytes, and ciphertexts passed to open/4 and open_afternm/3 must start with boxzerobytes/0 zero bytes; outputs have the same length as their inputs. Nonces are noncebytes/0 bytes and must never be reused for the same pair of keys.

Prefer libsodium_crypto_box for new code.

Mod = libsodium_crypto_box_curve25519xsalsa20poly1305,
{AlicePK, AliceSK} = Mod:keypair(),
{BobPK, BobSK} = Mod:keypair(),
Nonce = libsodium_randombytes:buf(Mod:noncebytes()),
Padding = binary:copy(<<0>>, Mod:zerobytes()),
<<0:128, Boxed/binary>> = Mod:crypto_box_curve25519xsalsa20poly1305(<<Padding/binary, "message">>, Nonce, BobPK, AliceSK),
<<0:256, "message">> = Mod:open(<<0:128, Boxed/binary>>, Nonce, AlicePK, BobSK).

Summary

Types

k()

A crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES (32-byte) binary.

n()

A crypto_box_curve25519xsalsa20poly1305_NONCEBYTES (24-byte) binary.

A crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES (32-byte) binary.

A crypto_box_curve25519xsalsa20poly1305_SEEDBYTES (32-byte) binary.

A crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES (32-byte) binary.

Functions

Like crypto_box_curve25519xsalsa20poly1305/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 afternm/3 and open_afternm/3.

Return crypto_box_curve25519xsalsa20poly1305_beforenmbytes(), in bytes.

Return crypto_box_curve25519xsalsa20poly1305_boxzerobytes(), in bytes.

Encrypt 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.

Generate a random key pair. Returns {PK, SK}.

Return crypto_box_curve25519xsalsa20poly1305_macbytes(), in bytes.

Return crypto_box_curve25519xsalsa20poly1305_messagebytes_max(), in bytes.

Return crypto_box_curve25519xsalsa20poly1305_noncebytes(), in bytes.

Verify and decrypt 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.

Return crypto_box_curve25519xsalsa20poly1305_publickeybytes(), in bytes.

Return crypto_box_curve25519xsalsa20poly1305_secretkeybytes(), in bytes.

Deterministically derive a key pair from the seedbytes/0 byte Seed. Returns {PK, SK}.

Return crypto_box_curve25519xsalsa20poly1305_seedbytes(), in bytes.

Return crypto_box_curve25519xsalsa20poly1305_zerobytes(), in bytes.

Types

k()

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

A crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES (32-byte) binary.

n()

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

A crypto_box_curve25519xsalsa20poly1305_NONCEBYTES (24-byte) binary.

pk()

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

A crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES (32-byte) binary.

seed()

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

A crypto_box_curve25519xsalsa20poly1305_SEEDBYTES (32-byte) binary.

sk()

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

A crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES (32-byte) binary.

Functions

afternm(M, N, K)

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

Like crypto_box_curve25519xsalsa20poly1305/4, using the shared key K precomputed by beforenm/2.

beforenm(PK, SK)

-spec beforenm(PK, SK) -> Result when PK :: pk(), SK :: sk(), Result :: k() | -1.

Precompute the beforenmbytes/0 byte shared key for the public key PK and the secret key SK, for use with afternm/3 and open_afternm/3.

Returns -1 when PK is a low-order point.

beforenmbytes()

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

Return crypto_box_curve25519xsalsa20poly1305_beforenmbytes(), in bytes.

boxzerobytes()

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

Return crypto_box_curve25519xsalsa20poly1305_boxzerobytes(), in bytes.

crypto_box_curve25519xsalsa20poly1305(M, N, PK, SK)

-spec crypto_box_curve25519xsalsa20poly1305(M, N, PK, SK) -> Result
                                               when
                                                   M :: binary(),
                                                   N :: n(),
                                                   PK :: pk(),
                                                   SK :: sk(),
                                                   Result :: binary() | -1.

Encrypt 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.

keypair()

-spec keypair() -> Result when Result :: {pk(), sk()} | -1.

Generate a random key pair. Returns {PK, SK}.

macbytes()

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

Return crypto_box_curve25519xsalsa20poly1305_macbytes(), in bytes.

messagebytes_max()

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

Return crypto_box_curve25519xsalsa20poly1305_messagebytes_max(), in bytes.

noncebytes()

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

Return crypto_box_curve25519xsalsa20poly1305_noncebytes(), in bytes.

open(C, N, PK, SK)

-spec open(C, N, PK, SK) -> Result
              when C :: binary(), N :: n(), PK :: pk(), SK :: sk(), Result :: binary() | -1.

Verify and decrypt 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.

open_afternm(C, N, K)

-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.

publickeybytes()

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

Return crypto_box_curve25519xsalsa20poly1305_publickeybytes(), in bytes.

secretkeybytes()

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

Return crypto_box_curve25519xsalsa20poly1305_secretkeybytes(), in bytes.

seed_keypair(Seed)

-spec seed_keypair(Seed) -> Result when Seed :: seed(), Result :: {pk(), sk()} | -1.

Deterministically derive a key pair from the seedbytes/0 byte Seed. Returns {PK, SK}.

seedbytes()

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

Return crypto_box_curve25519xsalsa20poly1305_seedbytes(), in bytes.

zerobytes()

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

Return crypto_box_curve25519xsalsa20poly1305_zerobytes(), in bytes.