Insufficient cryptographic key size

Choose cryptographic key sizes that meet the required security strength

Description

The same bit length gives different security strengths for RSA, elliptic curves (EC) and symmetric algorithms. Select a key length for the algorithm, implementation and required protection period.

Potential impact

Insufficient strength can put long-lived encrypted data and trusted signatures at risk and fall short of organizational or standards requirements.

Remediation

  • Start with at least 2048 bits for RSA and consider 3072 bits or more for longer protection. For EC, use a reviewed curve such as secp256r1. AES supports only 128-, 192- and 256-bit keys, so 64 bits is an invalid setting.
  • Specify the key size or curve and a secure random generator in KeyPairGenerator or KeyGenerator. Enforce the same minimums on settings read from configuration.
  • When replacing keys, check that you can still decrypt existing data and verify certificates and signatures. Use secure modes and protect the key store and access permissions.

Examples

Before

java
import java.security.KeyPairGenerator;

public class WeakKeyExample {
    public static void main(String[] args) throws Exception {
        // RSA 1024 bits: insufficient baseline
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        rsa.initialize(1024); // BAD: below the 2048-bit baseline
        rsa.generateKeyPair();

    }
}

After

java
import javax.crypto.KeyGenerator;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.ECGenParameterSpec;

public class StrongKeyExample {
    public static void main(String[] args) throws Exception {
        SecureRandom rnd = SecureRandom.getInstanceStrong();

        // RSA 3072 bits: above the 2048-bit baseline
        KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
        rsa.initialize(3072, rnd);
        rsa.generateKeyPair();

        // Specify the reviewed 256-bit EC curve
        KeyPairGenerator ec = KeyPairGenerator.getInstance("EC");
        ec.initialize(new ECGenParameterSpec("secp256r1"), rnd);
        ec.generateKeyPair();

        // AES with a 256-bit key
        KeyGenerator aes = KeyGenerator.getInstance("AES");
        aes.init(256, rnd);
        aes.generateKey();
    }
}

The example uses the same SecureRandom to generate separate 3072-bit RSA, secp256r1 EC and 256-bit AES keys.

References