Skip to content

Commit 5acb393

Browse files
authored
Merge pull request #395 from panoob/develop
fix wrong arrayLocal in extractSootArray for HardcodedError
2 parents c1725f5 + 56a4541 commit 5acb393

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

CryptoAnalysis/src/test/java/tests/headless/StaticAnalysisDemoTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ public void predicateInstanceOfExample() {
144144
scanner.run();
145145
assertErrors(scanner.getErrorCollection());
146146
}
147+
148+
@Test
149+
public void hardCodedExample() {
150+
String mavenProjectPath = new File("../CryptoAnalysisTargets/HardcodedTestExamples/").getAbsolutePath();
151+
MavenProject mavenProject = createAndCompile(mavenProjectPath);
152+
HeadlessCryptoScanner scanner = createScanner(mavenProject);
153+
154+
setErrorsCount("<TruePositive: byte[] getKey(char[],byte[],int,int)>", HardCodedError.class, 1);
155+
setErrorsCount("<TruePositive: byte[] getKey(char[],byte[],int,int)>", RequiredPredicateError.class, 2);
156+
157+
setErrorsCount("<TrueNegative: byte[] getKey(char[],byte[],int,int)>", HardCodedError.class, 0);
158+
setErrorsCount("<TrueNegative: byte[] getKey(char[],byte[],int,int)>", RequiredPredicateError.class, 0);
159+
160+
scanner.run();
161+
assertErrors(scanner.getErrorCollection());
162+
}
147163

148164
@Test
149165
public void sslExample() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>example</groupId>
5+
<artifactId>PBEKeySpec-TP</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>PBEKeySpec-TP</name>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.8.0</version>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import javax.crypto.SecretKeyFactory;
2+
import javax.crypto.spec.PBEKeySpec;
3+
import java.security.SecureRandom;
4+
5+
public class TrueNegative {
6+
7+
public void trueNegative() {
8+
byte[] pass = new byte[256];
9+
byte[] salt = new byte[256];
10+
11+
SecureRandom secureRandom = new SecureRandom();
12+
secureRandom.nextBytes(salt);
13+
secureRandom.nextBytes(pass);
14+
15+
// convert byte array to char array
16+
char[] passwd = new char[pass.length];
17+
for(int i=0; i < pass.length; i++){
18+
passwd[i] = (char) (pass[i]&0xff);
19+
}
20+
21+
byte[] key = getKey(passwd, salt, 10000, 256);
22+
}
23+
24+
public static byte[] getKey(char[] pass, byte[] salt, int iterations, int size) {
25+
// generate a key via a PBEKeySpec
26+
try{
27+
PBEKeySpec spec = new PBEKeySpec(pass, salt, iterations, size);
28+
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
29+
byte[] key = skf.generateSecret(spec).getEncoded();
30+
spec.clearPassword();
31+
return key;
32+
} catch (Exception e) {
33+
}
34+
return null;
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import javax.crypto.SecretKeyFactory;
2+
import javax.crypto.spec.PBEKeySpec;
3+
import java.security.SecureRandom;
4+
5+
public class TruePositive {
6+
7+
public void truePositive() {
8+
char[] passwd = {'t','h','i','s'};
9+
byte[] salt = new byte[256];
10+
11+
SecureRandom secureRandom = new SecureRandom();
12+
secureRandom.nextBytes(salt);
13+
14+
byte[] key = getKey(passwd, salt, 10000, 256);
15+
}
16+
17+
public static byte[] getKey(char[] pass, byte[] salt, int iterations, int size) {
18+
// generate a key via a PBEKeySpec
19+
try{
20+
PBEKeySpec spec = new PBEKeySpec(pass, salt, iterations, size);
21+
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
22+
byte[] key = skf.generateSecret(spec).getEncoded();
23+
spec.clearPassword();
24+
return key;
25+
} catch (Exception e) {
26+
}
27+
return null;
28+
}
29+
}

0 commit comments

Comments
 (0)