56 lines
2.1 KiB
Plaintext
56 lines
2.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When CBC (Cypher Block Chaining) is used for encryption, the IV (Initialization Vector) must be random an unpredictable. Otherwise it exposes the encrypted value to crypto-analysis attacks like "Chosen-Plaintext Attacks".
|
|
|
|
|
|
An IV should be used in one and only one encryption cycle because its purpose is to ensure that a different cyphertext value results each time a given plain text value is encrypted.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
public String cbcEncrypt(String strKey, String plainText) {
|
|
|
|
String strIV = "7cVgr5cbdCZVw5WY";
|
|
IvParameterSpec ivSpec = new IvParameterSpec(strIV.getBytes("UTF-8"));
|
|
SecretKeySpec skeySpec = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
|
|
|
|
/* Ciphering */
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); // Noncompliant
|
|
byte[] encryptedBytes = cipher.doFinal(plaintText.getBytes("UTF-8"));
|
|
return DatatypeConverter.printBase64Binary(ivBytes) + ";" + DatatypeConverter.printBase64Binary(encryptedBytes);
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
private SecureRandom random = new SecureRandom();
|
|
|
|
public void cbcEncrypt(String strKey, String plainText) {
|
|
|
|
byte ivBytes[] = new byte[16];
|
|
random.nextBytes(ivBytes);
|
|
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
|
|
SecretKeySpec skeySpec = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
|
|
|
|
/* Ciphering */
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
|
|
byte[] encryptedBytes = cipher.doFinal(plaintText.getBytes("UTF-8"));
|
|
return DatatypeConverter.printBase64Binary(ivBytes) + ";" + DatatypeConverter.printBase64Binary(encryptedBytes);
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* OWASP - https://owasp.org/Top10/A02_2021-Cryptographic_Failures/[Top 10 2021 Category A2 - Cryptographic Failures]
|
|
* CWE - https://cwe.mitre.org/data/definitions/329[CWE-329 - Not Using a Random IV with CBC Mode]
|
|
* OWASP - https://owasp.org/www-project-top-ten/2017/A6_2017-Security_Misconfiguration[Top 10 2017 Category A6 - Security Misconfiguration]
|
|
|