SV.WEAK.KEYS.EC

Insufficient key length in Cryptographic Algorithm

Klocwork reports a SV.WEAK.KEYS.EC defect when the EC cryptographic algorithm is used with a key that is of insufficient size.

Vulnerability and risk

Small key size can lead to algorithm breaks that can then lead to the leakage of sensitive data. Algorithms need to be robust against the powerful computing techniques that are used to perform brute force attacks.

Mitigation and prevention

Avoid implementing EC algorithms with keys that are less than 256 bits.

Vulnerable code example

Copy
import java.security.KeyPairGenerator;
import java.security.spec.ECGenParameterSpec;
 
class KeyGenECTest {
  public static void main(String[] args) {
    KeyPairGenerator generatorEC = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec specEC = new ECGenParameterSpec("secp112r1");   // SV.WEAK.KEYS.EC (!)
  }
}

Klocwork reports an SV.WEAK.KEYS.EC defect on line 6, indicating, "Cryptographic Algorithm uses key less than 256 bits". An EC algorithm needs a key that is at least 256 bits.

Fixed code example

Copy
import java.security.KeyPairGenerator;
import java.security.spec.ECGenParameterSpec;
 
class KeyGenECTest {
  public static void main(String[] args) {
    KeyPairGenerator generatorEC = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec specEC = new ECGenParameterSpec("secp256r1");  // no SV.WEAK.KEYS.EC
  }
}

Klocwork no longer reports a defect because the EC algorithm uses a key that is 256 bits.

Security training

Application security training materials provided by Secure Code Warrior.