Skip to content

Commit a62bb8e

Browse files
Add additional test case
1 parent 960a4e5 commit a62bb8e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

java/ql/test/query-tests/security/CWE-1204/StaticInitializationVector.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,26 @@ public byte[] encryptWithRandomIvWithArraysCopy(byte[] key, byte[] plaintext) th
164164
cipher.update(plaintext);
165165
return cipher.doFinal();
166166
}
167+
168+
public byte[] generate(int size) throws Exception {
169+
if (size == 0) {
170+
return new byte[0];
171+
}
172+
byte[] randomBytes = new byte[size];
173+
SecureRandom.getInstanceStrong().nextBytes(randomBytes);
174+
return randomBytes;
175+
}
176+
177+
// GOOD: AES-CBC with a random IV
178+
public byte[] encryptWithGeneratedIvByteArray(byte[] key, byte[] plaintext) throws Exception {
179+
byte[] iv = generate(16);
180+
181+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
182+
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
183+
184+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
185+
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
186+
cipher.update(plaintext);
187+
return cipher.doFinal();
188+
}
167189
}

0 commit comments

Comments
 (0)