Skip to content

Commit

Permalink
Adapt BargaCryptoMisuse tests to conform rulesets 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
smeyer198 committed Nov 13, 2023
1 parent 34b0b7e commit e3e9c97
Show file tree
Hide file tree
Showing 41 changed files with 1,391 additions and 206 deletions.
410 changes: 320 additions & 90 deletions CryptoAnalysis/src/test/java/tests/headless/BragaCryptoMisusesTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,35 @@

public final class RawSignatureRSA {

public static void main(String args[]) {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() {
byte[] msg = "demo msg".getBytes();
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunJSSE");
kpg.initialize(4096);
KeyPair kp = kpg.generateKeyPair();
Signature sig = Signature.getInstance("SHA1WithRSA", "SunJSSE");
sig.initSign(kp.getPrivate());
sig.update(msg);
byte[] signed = sig.sign();
sig.initVerify(kp.getPublic());
sig.update(msg);
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException | SignatureException e) {
}
}

/**
* Original test without updates
*/
public void negativeTestCase() {
byte[] msg = "demo msg".getBytes();
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunJSSE");

// Since 3.0.0: key size of 2048 is not allowed
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
Signature sig = Signature.getInstance("SHA1WithRSA", "SunJSSE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void main(String[] args) {

public static KeyPair genRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
kpGen.initialize(4096, new SecureRandom());
return kpGen.generateKeyPair();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void main(String[] args) {

public static KeyPair genRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
kpGen.initialize(4096, new SecureRandom());
return kpGen.generateKeyPair();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void main(String[] args) {

public static KeyPair genRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
kpGen.initialize(4096, new SecureRandom());
return kpGen.generateKeyPair();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void main(String[] args) {

public static KeyPair genRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
kpGen.initialize(4096, new SecureRandom());
return kpGen.generateKeyPair();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static X509Certificate getRootCert() throws Exception {

public static KeyPair genRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
kpGen.initialize(4096, new SecureRandom());
return kpGen.generateKeyPair();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,42 @@
import org.bouncycastle.jce.provider.*;

public final class InsecureDefaultOAEP {

/**
* Original test with updated constraints:
* MGF1ParameterSpec.SHA1 -> MGF1ParameterSpec.SHA512
* new OAEPParameterSpec("SHA1", "MGF1", mgf1ps, PSource.PSpecified.DEFAULT) -> new OAEPParameterSpec("SHA512", "MGF1", mgf1ps, PSource.PSpecified.DEFAULT)
*/
public void positiveTestCase() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, NoSuchProviderException, InvalidAlgorithmParameterException {
Security.addProvider(new BouncyCastleProvider());

int ksize = 384;
int hsize = 160;
String rsaName = "RSA/None/OAEPPadding";
int maxLenBytes = (ksize - 2 * hsize) / 8 - 2;

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(ksize);
KeyPair kp = kpg.generateKeyPair();

public static void main(String args[])
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, NoSuchProviderException, InvalidAlgorithmParameterException {
MGF1ParameterSpec mgf1ps = MGF1ParameterSpec.SHA512;
OAEPParameterSpec OAEPps = new OAEPParameterSpec("SHA-512", "MGF1", mgf1ps, PSource.PSpecified.DEFAULT);
Cipher c = Cipher.getInstance(rsaName, "BC");

c.init(Cipher.ENCRYPT_MODE, kp.getPublic(), OAEPps);
byte[] pt1 = "This is a demo text".substring(0, maxLenBytes).getBytes();
byte[] ct = c.doFinal(pt1);

c.init(Cipher.DECRYPT_MODE, kp.getPrivate(), OAEPps);
byte[] pt2 = c.doFinal(ct);
}

/**
* Original test without updates
*/
public void negativeTestCase() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException, NoSuchProviderException, InvalidAlgorithmParameterException {
Security.addProvider(new BouncyCastleProvider());

int ksize = 384;
Expand All @@ -24,6 +55,7 @@ public static void main(String args[])
kpg.initialize(ksize);
KeyPair kp = kpg.generateKeyPair();

// Since 3.0.0: SHA1 is not allowed for both parameter specs
MGF1ParameterSpec mgf1ps = MGF1ParameterSpec.SHA1;
OAEPParameterSpec OAEPps = new OAEPParameterSpec("SHA1", "MGF1", mgf1ps, PSource.PSpecified.DEFAULT);
Cipher c = Cipher.getInstance(rsaName, "BC");
Expand All @@ -34,7 +66,6 @@ public static void main(String args[])

c.init(Cipher.DECRYPT_MODE, kp.getPrivate(), OAEPps);
byte[] pt2 = c.doFinal(ct);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,44 @@

public final class InsecureDefaultRSA {

public static void main(String args[]) {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("Insecure default RSA.").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");
g.initialize(4096);
KeyPair kp = g.generateKeyPair();

Cipher enc = Cipher.getInstance("RSA", "BC");
enc.init(Cipher.ENCRYPT_MODE, kp.getPublic());
Cipher dec = Cipher.getInstance("RSA", "BC");
dec.init(Cipher.DECRYPT_MODE, kp.getPrivate());

byte[][] ct = new byte[2][];
for (int i = 0; i < 2; i++) {
ct[i] = enc.doFinal(msg1);
byte[] pt2 = dec.doFinal(ct[i]);
}

} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | NoSuchProviderException e) {
}
}

/**
* Original test without updates
*/
public void negativeTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("Insecure default RSA.").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");

// Since 3.0.0: key size of 2048 is not allowed
g.initialize(2048);
KeyPair kp = g.generateKeyPair();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,44 @@

public final class InsecurePaddingRSA1 {

public static void main(String args[]) {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");
g.initialize(4096);
KeyPair kp = g.generateKeyPair();

Cipher enc = Cipher.getInstance("RSA/ECB/NoPadding", "BC");
enc.init(Cipher.ENCRYPT_MODE, kp.getPublic());
Cipher dec = Cipher.getInstance("RSA/ECB/NoPadding", "BC");
dec.init(Cipher.DECRYPT_MODE, kp.getPrivate());

byte[][] ct = new byte[2][];
for (int i = 0; i < 2; i++) {
ct[i] = enc.doFinal(msg1);
byte[] deciphered = dec.doFinal(ct[i]);
}

} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | NoSuchProviderException e) {
}
}

/**
* Original test without updates
*/
public void negativeTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");

// Since 3.0.0: key size of 2048 is not allowed
g.initialize(2048);
KeyPair kp = g.generateKeyPair();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,44 @@

public final class InsecurePaddingRSA2 {

public static void main(String args[]) {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");
g.initialize(4096);
KeyPair kp = g.generateKeyPair();

Cipher enc = Cipher.getInstance("RSA/None/NoPadding", "BC");
enc.init(Cipher.ENCRYPT_MODE, kp.getPublic());
Cipher dec = Cipher.getInstance("RSA/None/NoPadding", "BC");
dec.init(Cipher.DECRYPT_MODE, kp.getPrivate());

byte[][] ct = new byte[2][];
for (int i = 0; i < 2; i++) {
ct[i] = enc.doFinal(msg1);
byte[] deciphered = dec.doFinal(ct[i]);
}

} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | NoSuchProviderException e) {
}
}

/**
* Original test without updates
*/
public void negativeTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");

// Since 3.0.0: key size of 2048 is not allowed
g.initialize(2048);
KeyPair kp = g.generateKeyPair();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,44 @@

public final class InsecurePaddingRSA3 {

public static void main(String args[]) {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");
g.initialize(4096);
KeyPair kp = g.generateKeyPair();

Cipher enc = Cipher.getInstance("RSA/NONE/NoPadding", "BC");
enc.init(Cipher.ENCRYPT_MODE, kp.getPublic());
Cipher dec = Cipher.getInstance("RSA/NONE/NoPadding", "BC");
dec.init(Cipher.DECRYPT_MODE, kp.getPrivate());

byte[][] ct = new byte[2][];
for (int i = 0; i < 2; i++) {
ct[i] = enc.doFinal(msg1);
byte[] deciphered = dec.doFinal(ct[i]);
}

} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | NoSuchProviderException e) {
}
}

/**
* Original test without updates
*/
public void negativeTestCase() {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] msg1 = ("demo msg").getBytes();
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");

// Since 3.0.0: key size of 2048 is not allowed
g.initialize(2048);
KeyPair kp = g.generateKeyPair();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,34 @@

public final class PKCS1Signature {

public static void main(String[] args) throws Exception {
/**
* Original test with updated constraints:
* kg.initialize(2048, ...) -> kg.initialize(4096, ...)
*/
public void positiveTestCase() throws Exception {
Security.addProvider(new BouncyCastleProvider());

KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA", "BC");
kg.initialize(4096, new SecureRandom());
KeyPair kp = kg.generateKeyPair();
Signature sig = Signature.getInstance("SHA1withRSA", "BC");

byte[] m = "Testing RSA PKCS1".getBytes("UTF-8");

sig.initSign(kp.getPrivate(), new SecureRandom());
sig.update(m);
byte[] s = sig.sign();

sig.initVerify(kp.getPublic());
sig.update(m);
}

public void negativeTestCase() throws Exception {
Security.addProvider(new BouncyCastleProvider());

KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA", "BC");

// Since 3.0.0: key size of 2048 is not allowed
kg.initialize(2048, new SecureRandom());
KeyPair kp = kg.generateKeyPair();
Signature sig = Signature.getInstance("SHA1withRSA", "BC");
Expand All @@ -26,6 +49,5 @@ public static void main(String[] args) throws Exception {

sig.initVerify(kp.getPublic());
sig.update(m);

}
}
Loading

0 comments on commit e3e9c97

Please sign in to comment.