Packages

Library implementing Noise protocol

Current section

Files

Jump to
noise_protocol lib noise crypto cipher aes_gcm.ex
Raw

lib/noise/crypto/cipher/aes_gcm.ex

defmodule Noise.Crypto.Cipher.AESGCM do
use Noise.Crypto.Cipher
@impl Noise.Crypto.Cipher
def encrypt(k, n, ad, plain_text) do
{cipher_text, tag} =
:crypto.crypto_one_time_aead(:aes_256_gcm, k, nonce(n), plain_text, ad, true)
cipher_text <> tag
end
@impl Noise.Crypto.Cipher
def decrypt(k, n, ad, cipher_text) do
cipher_len = byte_size(cipher_text) - 16
<<cipher::binary-size(cipher_len), tag::binary-size(16)>> = cipher_text
:crypto.crypto_one_time_aead(:aes_256_gcm, k, nonce(n), cipher, ad, tag, false)
end
defp nonce(n), do: <<0::32, n::unsigned-integer-64>>
end