logo

Database

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.

Weakness:

395 - Insecure generation of random numbers - Static IV

Category: Functionality Abuse

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)