libsodium_crypto_box_curve25519xsalsa20poly1305 (libsodium v3.0.1)
View SourceThe 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
A crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES (32-byte) binary.
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
-type k() :: <<_:256>>.
A crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES (32-byte) binary.
-type n() :: <<_:192>>.
A crypto_box_curve25519xsalsa20poly1305_NONCEBYTES (24-byte) binary.
-type pk() :: <<_:256>>.
A crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES (32-byte) binary.
-type seed() :: <<_:256>>.
A crypto_box_curve25519xsalsa20poly1305_SEEDBYTES (32-byte) binary.
-type sk() :: <<_:256>>.
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.
Returns -1 when PK is a low-order point.
-spec beforenmbytes() -> Result when Result :: 32.
Return crypto_box_curve25519xsalsa20poly1305_beforenmbytes(), in bytes.
-spec boxzerobytes() -> Result when Result :: 16.
Return crypto_box_curve25519xsalsa20poly1305_boxzerobytes(), in bytes.
-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.
Generate a random key pair. Returns {PK, SK}.
-spec macbytes() -> Result when Result :: 16.
Return crypto_box_curve25519xsalsa20poly1305_macbytes(), in bytes.
-spec messagebytes_max() -> Result when Result :: non_neg_integer().
Return crypto_box_curve25519xsalsa20poly1305_messagebytes_max(), in bytes.
-spec noncebytes() -> Result when Result :: 24.
Return crypto_box_curve25519xsalsa20poly1305_noncebytes(), in bytes.
-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.
-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 publickeybytes() -> Result when Result :: 32.
Return crypto_box_curve25519xsalsa20poly1305_publickeybytes(), in bytes.
-spec secretkeybytes() -> Result when Result :: 32.
Return crypto_box_curve25519xsalsa20poly1305_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_curve25519xsalsa20poly1305_seedbytes(), in bytes.
-spec zerobytes() -> Result when Result :: 32.
Return crypto_box_curve25519xsalsa20poly1305_zerobytes(), in bytes.