SV.WEAK.KEYS.RSA
Insufficient key length in Cryptographic Algorithm
Klocwork reports a SV.WEAK.KEYS.RSA defect when the RSA 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 RSA algorithms with keys that are less than 2048 bits.
Vulnerable code example
import java.security.KeyPairGenerator;
class KeyGenRSATest {
public static void main(String[] args) {
KeyPairGenerator generatorRSA = KeyPairGenerator.getInstance("RSA");
generatorRSA.initialize(1024); // SV.WEAK.KEYS.RSA (!)
}
}
Klocwork reports an SV.WEAK.KEYS.RSA defect on line 6, indicating, "Cryptographic Algorithm uses key less than 2048 bits". An RSA algorithm needs a key that is at least 2048 bits.
Fixed code example
import java.security.KeyPairGenerator;
class KeyGenRSATest {
public static void main(String[] args) {
KeyPairGenerator generatorRSA = KeyPairGenerator.getInstance("RSA");
generatorRSA.initialize(2048); // no SV.WEAK.KEYS.RSA
}
}
Klocwork no longer reports a defect because the RSA algorithm uses a key that is 2048 bits.
Related checkers
External guidance
- CWE-326: Inadequate Encryption Strength
- OWASP A3:2017 Sensitive Data Exposure
- OWASP A2:2021 Cryptographic Failures
- V-222555 (APSC-DV-001860): The application must use mechanisms meeting the requirements of applicable federal laws, Executive Orders, directives, policies, regulations, standards, and guidance for authentication to a cryptographic module.
Security training
Application security training materials provided by Secure Code Warrior.