Skip to content

Commit 9e278b9

Browse files
authored
Merge pull request #20258 from bdrodes/java_nonce_reuse_tests
Crypto: Add reuse nonce test for Java
2 parents 4a1157b + 15e9bb9 commit 9e278b9

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Test.java:40:47:40:52 | Nonce | Reuse with nonce $@ | Test.java:49:47:49:52 | Nonce | Nonce |
2+
| Test.java:49:47:49:52 | Nonce | Reuse with nonce $@ | Test.java:40:47:40:52 | Nonce | Nonce |
3+
| Test.java:76:48:76:54 | Nonce | Reuse with nonce $@ | Test.java:82:49:82:55 | Nonce | Nonce |
4+
| Test.java:82:49:82:55 | Nonce | Reuse with nonce $@ | Test.java:76:48:76:54 | Nonce | Nonce |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/quantum/Analysis/ReusedNonce.ql
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.example.crypto.artifacts;
2+
3+
import java.security.*;
4+
import javax.crypto.Cipher;
5+
import javax.crypto.KeyGenerator;
6+
import javax.crypto.SecretKey;
7+
import javax.crypto.spec.IvParameterSpec;
8+
9+
public class Test {
10+
11+
public static SecretKey generateAESKey() throws Exception {
12+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
13+
keyGen.init(256);
14+
return keyGen.generateKey();
15+
}
16+
17+
private static byte[] getRandomWrapper1() throws Exception {
18+
byte[] val = new byte[16];
19+
new SecureRandom().nextBytes(val);
20+
return val;
21+
}
22+
23+
private static byte[] getRandomWrapper2A() throws Exception {
24+
byte[] val;
25+
val = getRandomWrapper1();
26+
funcA1(val);
27+
return val;
28+
}
29+
30+
private static byte[] getRandomWrapper2b() throws Exception {
31+
byte[] val;
32+
val = getRandomWrapper1();
33+
return val;
34+
}
35+
36+
private static void funcA1(byte[] iv) throws Exception {
37+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
38+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
39+
SecretKey key = generateAESKey();
40+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // BAD: Reuse of `iv` in funcB1
41+
byte[] ciphertext = cipher.doFinal("Simple Test Data".getBytes());
42+
}
43+
44+
private static void funcB1() throws Exception {
45+
byte[] iv = getRandomWrapper2A();
46+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
47+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
48+
SecretKey key = generateAESKey();
49+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // BAD: Reuse of `iv` in funcA1
50+
byte[] ciphertext = cipher.doFinal("Simple Test Data".getBytes());
51+
}
52+
53+
private static void funcA2() throws Exception {
54+
byte[] iv = getRandomWrapper2b();
55+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
56+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
57+
SecretKey key = generateAESKey();
58+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // GOOD
59+
byte[] ciphertext = cipher.doFinal("Simple Test Data".getBytes());
60+
}
61+
62+
private static void funcB2() throws Exception {
63+
byte[] iv = getRandomWrapper2b();
64+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
65+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
66+
SecretKey key = generateAESKey();
67+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // GOOD
68+
byte[] ciphertext = cipher.doFinal("Simple Test Data".getBytes());
69+
}
70+
71+
private static void funcA3() throws Exception {
72+
byte[] iv = getRandomWrapper2b();
73+
IvParameterSpec ivSpec1 = new IvParameterSpec(iv);
74+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
75+
SecretKey key1 = generateAESKey();
76+
cipher.init(Cipher.ENCRYPT_MODE, key1, ivSpec1); // BAD: reuse of `iv` below
77+
byte[] ciphertext = cipher.doFinal("Simple Test Data".getBytes());
78+
79+
IvParameterSpec ivSpec2 = new IvParameterSpec(iv);
80+
Cipher cipher2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
81+
SecretKey key2 = generateAESKey();
82+
cipher2.init(Cipher.ENCRYPT_MODE, key2, ivSpec2); // BAD: Reuse of `iv` above
83+
byte[] ciphertext2 = cipher2.doFinal("Simple Test Data".getBytes());
84+
}
85+
86+
public static void main(String[] args) {
87+
try {
88+
funcA2();
89+
funcB1();
90+
funcB2();
91+
} catch (Exception e) {
92+
e.printStackTrace();
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)