libsodium_crypto_aead_xchacha20poly1305 (libsodium v3.0.1)

View Source

XChaCha20-Poly1305 authenticated encryption with additional data, the recommended general-purpose AEAD construction.

Its ietf_npubbytes/0 (24) byte nonce is long enough to be generated at random for every message (for example with libsodium_randombytes:buf/1) without practical risk of collision. Keys are ietf_keybytes/0 bytes and a nonce must still never be reused with the same key.

Key = libsodium_crypto_aead_xchacha20poly1305:ietf_keygen(),
Nonce = libsodium_randombytes:buf(libsodium_crypto_aead_xchacha20poly1305:ietf_npubbytes()),
Ciphertext = libsodium_crypto_aead_xchacha20poly1305:ietf_encrypt(<<"message">>, <<"header">>, Nonce, Key),
<<"message">> = libsodium_crypto_aead_xchacha20poly1305:ietf_decrypt(Ciphertext, <<"header">>, Nonce, Key).

Summary

Types

k()

A crypto_aead_xchacha20poly1305_ietf_KEYBYTES (32-byte) binary.

A crypto_aead_xchacha20poly1305_ietf_ABYTES (16-byte) binary.

A crypto_aead_xchacha20poly1305_ietf_NPUBBYTES (24-byte) binary.

Functions

Return crypto_aead_xchacha20poly1305_ietf_abytes(), in bytes.

Verify and decrypt the ciphertext C with the additional data AD (default empty), nonce NPub, and key K.

Verify and decrypt the ciphertext C with the additional data AD (default empty), nonce NPub, and key K.

Verify the detached tag MAC and decrypt C with the additional data AD (default empty). Returns the plaintext, or -1 when verification fails.

Verify the detached tag MAC and decrypt C with the additional data AD (default empty). Returns the plaintext, or -1 when verification fails.

Encrypt and authenticate M together with the additional data AD (default empty) using the ietf_npubbytes/0 byte nonce NPub and the ietf_keybytes/0 byte key K.

Encrypt and authenticate M together with the additional data AD (default empty) using the ietf_npubbytes/0 byte nonce NPub and the ietf_keybytes/0 byte key K.

Encrypt and authenticate M with the additional data AD (default empty), returning the ciphertext and the ietf_abytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

Encrypt and authenticate M with the additional data AD (default empty), returning the ciphertext and the ietf_abytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

Return crypto_aead_xchacha20poly1305_ietf_keybytes(), in bytes.

Generate a random ietf_keybytes/0 byte key.

Return crypto_aead_xchacha20poly1305_ietf_messagebytes_max(), in bytes.

Return crypto_aead_xchacha20poly1305_ietf_npubbytes(), in bytes.

Return crypto_aead_xchacha20poly1305_ietf_nsecbytes(), in bytes.

Types

k()

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

A crypto_aead_xchacha20poly1305_ietf_KEYBYTES (32-byte) binary.

mac()

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

A crypto_aead_xchacha20poly1305_ietf_ABYTES (16-byte) binary.

npub()

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

A crypto_aead_xchacha20poly1305_ietf_NPUBBYTES (24-byte) binary.

Functions

ietf_abytes()

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

Return crypto_aead_xchacha20poly1305_ietf_abytes(), in bytes.

ietf_decrypt(C, NPub, K)

-spec ietf_decrypt(C, NPub, K) -> Result
                      when C :: binary(), NPub :: npub(), K :: k(), Result :: binary() | -1.

Verify and decrypt the ciphertext C with the additional data AD (default empty), nonce NPub, and key K.

Returns the plaintext, or -1 when verification fails.

ietf_decrypt(C, AD, NPub, K)

-spec ietf_decrypt(C, AD, NPub, K) -> Result
                      when
                          C :: binary(),
                          AD :: binary(),
                          NPub :: npub(),
                          K :: k(),
                          Result :: binary() | -1.

Verify and decrypt the ciphertext C with the additional data AD (default empty), nonce NPub, and key K.

Returns the plaintext, or -1 when verification fails.

ietf_decrypt_detached(C, MAC, NPub, K)

-spec ietf_decrypt_detached(C, MAC, NPub, K) -> Result
                               when
                                   C :: binary(),
                                   MAC :: mac(),
                                   NPub :: npub(),
                                   K :: k(),
                                   Result :: binary() | -1.

Verify the detached tag MAC and decrypt C with the additional data AD (default empty). Returns the plaintext, or -1 when verification fails.

ietf_decrypt_detached(C, MAC, AD, NPub, K)

-spec ietf_decrypt_detached(C, MAC, AD, NPub, K) -> Result
                               when
                                   C :: binary(),
                                   MAC :: mac(),
                                   AD :: binary(),
                                   NPub :: npub(),
                                   K :: k(),
                                   Result :: binary() | -1.

Verify the detached tag MAC and decrypt C with the additional data AD (default empty). Returns the plaintext, or -1 when verification fails.

ietf_encrypt(M, NPub, K)

-spec ietf_encrypt(M, NPub, K) -> Result
                      when M :: binary(), NPub :: npub(), K :: k(), Result :: binary() | -1.

Encrypt and authenticate M together with the additional data AD (default empty) using the ietf_npubbytes/0 byte nonce NPub and the ietf_keybytes/0 byte key K.

Returns the ciphertext followed by an ietf_abytes/0 byte authentication tag, or -1 on failure.

ietf_encrypt(M, AD, NPub, K)

-spec ietf_encrypt(M, AD, NPub, K) -> Result
                      when
                          M :: binary(),
                          AD :: binary(),
                          NPub :: npub(),
                          K :: k(),
                          Result :: binary() | -1.

Encrypt and authenticate M together with the additional data AD (default empty) using the ietf_npubbytes/0 byte nonce NPub and the ietf_keybytes/0 byte key K.

Returns the ciphertext followed by an ietf_abytes/0 byte authentication tag, or -1 on failure.

ietf_encrypt_detached(M, NPub, K)

-spec ietf_encrypt_detached(M, NPub, K) -> Result
                               when
                                   M :: binary(),
                                   NPub :: npub(),
                                   K :: k(),
                                   Result :: {binary(), mac()} | -1.

Encrypt and authenticate M with the additional data AD (default empty), returning the ciphertext and the ietf_abytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

ietf_encrypt_detached(M, AD, NPub, K)

-spec ietf_encrypt_detached(M, AD, NPub, K) -> Result
                               when
                                   M :: binary(),
                                   AD :: binary(),
                                   NPub :: npub(),
                                   K :: k(),
                                   Result :: {binary(), mac()} | -1.

Encrypt and authenticate M with the additional data AD (default empty), returning the ciphertext and the ietf_abytes/0 byte authentication tag separately as {C, MAC}, or -1 on failure.

ietf_keybytes()

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

Return crypto_aead_xchacha20poly1305_ietf_keybytes(), in bytes.

ietf_keygen()

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

Generate a random ietf_keybytes/0 byte key.

ietf_messagebytes_max()

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

Return crypto_aead_xchacha20poly1305_ietf_messagebytes_max(), in bytes.

ietf_npubbytes()

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

Return crypto_aead_xchacha20poly1305_ietf_npubbytes(), in bytes.

ietf_nsecbytes()

-spec ietf_nsecbytes() -> Result when Result :: 0.

Return crypto_aead_xchacha20poly1305_ietf_nsecbytes(), in bytes.