Skip to content

Commit 33aa6c9

Browse files
committed
Crypto: Adding tests for reuse nonce query for JAVA/JCA.
1 parent fd7668d commit 33aa6c9

File tree

3 files changed

+113
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)