HAP.Crypto.ChaCha20 (HAP v0.1.0) View Source

Functions to encrypt/tag and decrypt/verify using the chacha20_poly1305 cipher

Link to this section Summary

Functions

Takes a binary containing encrypted data followed by a 16 byte tag, verifies the tag and decrypts the resultant data using the given key and nonce. Can take optional AAD data which is authenticated under the auth_tag but not encrypted.

Takes a plaintext binary and encrypts & tags it using the given key & nonce. Optionally takes AAD data which is authenticated under the auth tag but not included in the returned binary (it is up to the caller to convey the AAD to their counterparty).

Link to this section Types

Specs

aad() :: binary()
Link to this type

ciphertext_with_authdata()

View Source

Specs

ciphertext_with_authdata() :: binary()

Specs

key() :: <<_::256>>

Specs

nonce() :: binary()

Specs

plaintext() :: binary()

Link to this section Functions

Link to this function

decrypt_and_verify(encrypted_data, key, nonce, aad \\ <<>>)

View Source

Specs

decrypt_and_verify(ciphertext_with_authdata(), key(), nonce(), aad()) ::
  {:ok, plaintext()} | {:error, String.t()}

Takes a binary containing encrypted data followed by a 16 byte tag, verifies the tag and decrypts the resultant data using the given key and nonce. Can take optional AAD data which is authenticated under the auth_tag but not encrypted.

Returns {:ok, plaintext} or {:error, message}

Link to this function

encrypt_and_tag(plaintext, key, nonce, aad \\ <<>>)

View Source

Specs

encrypt_and_tag(plaintext(), key(), nonce(), aad()) ::
  {:ok, ciphertext_with_authdata()}

Takes a plaintext binary and encrypts & tags it using the given key & nonce. Optionally takes AAD data which is authenticated under the auth tag but not included in the returned binary (it is up to the caller to convey the AAD to their counterparty).

Returns {:ok, encrypted_data <> auth_tag}