Elixir Hardcoded Aead Nonce
Description
This detector identifies hardcoded nonces used with AEAD (Authenticated Encryption with Associated Data) ciphers in Elixir code. Using a fixed nonce with AEAD encryption compromises the security guarantees of the cipher, potentially allowing attackers to recover plaintext or forge authenticated messages.
Detection Strategy
• The detector scans for specific function calls that match a predefined AEAD encryption pattern (likely functions like :aes_gcm.encrypt or similar)
• It examines the cipher argument at a specific position to verify it's an AEAD cipher type (such as :aes_256_gcm)
• It checks the initialization vector (IV/nonce) argument at another specific position to determine if it contains a hardcoded value
• A vulnerability is flagged when both conditions are met: the function uses an AEAD cipher AND the nonce/IV parameter is a hardcoded literal value rather than a dynamically generated one
Vulnerable code example
defmodule VulnerableAead do
def encrypt_with_hardcoded_nonce(key, data, aad) do
# Hardcoded nonce breaks AEAD security - nonces must be unique per encryption
:crypto.crypto_one_time_aead(:aes_256_gcm, key, "000000000000", data, aad, true)
end
def encrypt_with_static_nonce(key, data, aad) do
nonce = "000000000000" # Static nonce allows attacks on encrypted data...✅ Secure code example
defmodule SecureAead do
def encrypt_with_random_nonce(key, data, aad) do
nonce = :crypto.strong_rand_bytes(12) # Generate unique nonce per encryption
:crypto.crypto_one_time_aead(:aes_256_gcm, key, nonce, data, aad, true)
end
def encrypt_with_parameter_nonce(key, nonce, data, aad) do
# Nonce from caller ensures uniqueness responsibility is external...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.