logo

Database

Java Hardcoded Gcm Nonce

Description

Detects hardcoded GCM initialization vectors/nonces in Java cryptographic implementations. Using static nonces in GCM mode breaks the security model and can lead to cryptographic attacks that compromise data confidentiality and authenticity. GCM requires unique nonces for each encryption operation with the same key to maintain security.

Weakness:

395 - Insecure generation of random numbers - Static IV

Category: Functionality Abuse

Detection Strategy

    Scans Java source code that imports the javax.crypto.spec package

    Identifies constructor calls to GCMParameterSpec class

    Examines the nonce parameter (typically the second argument) passed to the GCMParameterSpec constructor

    Reports vulnerability when the nonce parameter is determined to be hardcoded or static (not dynamically generated)

    Flags cases where the nonce value can be traced back to a constant definition rather than secure random generation

Vulnerable code example

import javax.crypto.spec.GCMParameterSpec;

public class VulnerableGcmNonce {
    private static final String HARDCODED_NONCE = "000000000000";
    
    public void encryptWithHardcodedNonce() {
        byte[] nonce = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Hardcoded nonce array
        ...

✅ Secure code example

import javax.crypto.spec.GCMParameterSpec;
import java.security.SecureRandom;

public class SecureGcmNonce {
    
    public void encryptWithRandomNonce() {
        // SAFE: Generate fresh random nonce for each encryption
        byte[] nonce = new byte[12];...