Python Hardcoded Aead Nonce
Description
This vulnerability detector identifies hardcoded nonces in AEAD (Authenticated Encryption with Associated Data) encryption operations. Using hardcoded nonces in AEAD encryption severely compromises security as it eliminates the randomness required for secure encryption, potentially allowing attackers to break the encryption scheme.
Detection Strategy
• The code imports cryptographic libraries that provide AEAD encryption functionality
• A function call is made to an AEAD encryption method (like encrypt() on AEAD cipher objects)
• The nonce parameter (typically the second argument) to the encryption function is provided
• The nonce value is traced back to its definition and found to be a hardcoded byte string or constant value rather than randomly generated
• The hardcoded nonce is not properly sanitized or randomized before use
Vulnerable code example
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def encrypt_data(key, data):
aesgcm = AESGCM(key)
# VULNERABLE: hardcoded nonce reused across encryptions
return aesgcm.encrypt(b"000000000000", data, None)✅ Secure code example
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def encrypt_data(key, data):
aesgcm = AESGCM(key)
nonce = os.urandom(12) # Generate random nonce for each encryption
return aesgcm.encrypt(nonce, data, None)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.