Skip to content

Commit

Permalink
Updates for release of BouncyCastle 1.56 (#8)
Browse files Browse the repository at this point in the history
- add maven entry for BC 1.56 and update the versions range
- DHIES weak crypto tests now pass if algs not even supported
- some ECIES entries moved to the invalid list
- ECIES decryption init calls fixed to pass the correct parameters
- EciesTest.testModifyPoint now expects GeneralSecurityException
  • Loading branch information
peterdettman authored and thaidn committed Feb 3, 2017
1 parent 06d73e0 commit fa01b5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ maven_jar(
artifact = "org.bouncycastle:bcprov-jdk15on:1.55",
)

maven_jar(
name = "bouncycastle_1_56",
artifact = "org.bouncycastle:bcprov-jdk15on:1.56",
)

maven_jar(
name = "spongycastle_core_1_50",
artifact = "com.madgag.spongycastle:core:1.50.0.0",
Expand Down
6 changes: 3 additions & 3 deletions build_defs.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
bouncycastle_versions = range(49, 56)
bouncycastle_versions = range(49, 57)

# These targets run all tests.
def bouncycastle_all_tests(srcs, deps, size, test_class):
"""BouncyCastle version-specific tests."""

# Generates BouncyCastleAllTests_1_55, ..., BouncyCastleAllTests_1_49
# Generates BouncyCastleAllTests_1_56, ..., BouncyCastleAllTests_1_49
for version in bouncycastle_versions:
native.java_test(
name = "BouncyCastleAllTests_1_%s" % version,
Expand All @@ -31,7 +31,7 @@ def bouncycastle_all_tests(srcs, deps, size, test_class):
def bouncycastle_tests(srcs, deps, size, test_class):
"""BouncyCastle version-specific tests."""

# Generates BouncyCastleTest_1_55, ..., BouncyCastleTest_1_49
# Generates BouncyCastleTest_1_56, ..., BouncyCastleTest_1_49
for version in bouncycastle_versions:
native.java_test(
name = "BouncyCastleTest_1_%s" % version,
Expand Down
22 changes: 17 additions & 5 deletions java/com/google/security/wycheproof/testcases/DhiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public DHParameterSpec ike2048() {
}

/**
* WARNING: This test uses weak crypto (i.e. DHIESWithAES). Checks that key agreement using DHIES
* works in the sense that it can decrypt what it encrypts. Unfortunately it seems that there is
* no secure mode using AES.
* WARNING: This test uses weak crypto (i.e. DHIESWithAES), if supported. Checks that key agreement
* using DHIES works in the sense that it can decrypt what it encrypts. Unfortunately it seems that
* there is no secure mode using AES.
*/
@SuppressWarnings("InsecureCryptoUsage")
public void testDhiesBasic() throws Exception {
Expand All @@ -84,7 +84,13 @@ public void testDhiesBasic() throws Exception {
PrivateKey priv = keyPair.getPrivate();
PublicKey pub = keyPair.getPublic();
byte[] message = "Hello".getBytes("UTF-8");
Cipher dhies = Cipher.getInstance("DHIESwithAES");
Cipher dhies;
try {
dhies = Cipher.getInstance("DHIESwithAES");
} catch (NoSuchAlgorithmException ex) {
// The algorithm isn't supported - even better!
return;
}
dhies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = dhies.doFinal(message);
System.out.println("testDhiesBasic:" + TestUtil.bytesToHex(ciphertext));
Expand All @@ -106,7 +112,13 @@ public void testDhiesCorrupt() throws Exception {
PrivateKey priv = keyPair.getPrivate();
PublicKey pub = keyPair.getPublic();
byte[] message = new byte[32];
Cipher dhies = Cipher.getInstance("DHIESwithAES");
Cipher dhies;
try {
dhies = Cipher.getInstance("DHIESwithAES");
} catch (NoSuchAlgorithmException ex) {
// The algorithm isn't supported - even better!
return;
}
dhies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = dhies.doFinal(message);
for (int i = 0; i < ciphertext.length; i++) {
Expand Down
25 changes: 14 additions & 11 deletions java/com/google/security/wycheproof/testcases/EciesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.security.wycheproof;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void testEciesBasic() throws Exception {
ecies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = ecies.doFinal(message);
System.out.println("testEciesBasic:" + TestUtil.bytesToHex(ciphertext));
ecies.init(Cipher.DECRYPT_MODE, priv);
ecies.init(Cipher.DECRYPT_MODE, priv, ecies.getParameters());
byte[] decrypted = ecies.doFinal(ciphertext);
assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
Expand All @@ -98,6 +99,8 @@ public void testInvalidNames() throws Exception {
new String[] {
"ECIESWITHAES/CBC/PKCS5PADDING",
"ECIESWITHAES/CBC/PKCS7PADDING",
"ECIESWITHAES/DHAES/NOPADDING",
"ECIESWITHDESEDE/DHAES/NOPADDING",
"ECIESWITHAES/ECB/NOPADDING",
"ECIESWITHAES/CTR/NOPADDING",
};
Expand All @@ -116,14 +119,12 @@ public void testInvalidNames() throws Exception {
// expect.
@SuppressWarnings("InsecureCryptoUsage")
public void testValidNames() throws Exception {
String[] invalidNames =
String[] validNames =
new String[] {
"ECIESWITHAES/DHAES/NOPADDING",
"ECIES/DHAES/PKCS7PADDING",
"ECIESWITHDESEDE/DHAES/NOPADDING",
"ECIESWITHAES-CBC/NONE/NOPADDING",
};
for (String algorithm : invalidNames) {
for (String algorithm : validNames) {
Cipher.getInstance(algorithm);
}
}
Expand Down Expand Up @@ -172,7 +173,7 @@ public void testExceptions(String algorithm) throws Exception {
ecies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = ecies.doFinal(message);
System.out.println(TestUtil.bytesToHex(ciphertext));
ecies.init(Cipher.DECRYPT_MODE, priv);
ecies.init(Cipher.DECRYPT_MODE, priv, ecies.getParameters());
HashSet<String> exceptions = new HashSet<String>();
for (int byteNr = kemSize; byteNr < ciphertext.length; byteNr++) {
for (int bit = 0; bit < 8; bit++) {
Expand Down Expand Up @@ -210,13 +211,15 @@ public void testModifyPoint() throws Exception {
ecies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = ecies.doFinal(message);
ciphertext[2] ^= (byte) 1;
ecies.init(Cipher.DECRYPT_MODE, priv);
ecies.init(Cipher.DECRYPT_MODE, priv, ecies.getParameters());
try {
ecies.doFinal(ciphertext);
fail("This should not work");
} catch (java.lang.IllegalArgumentException ex) {
// This is what BouncyCastle throws when the points are not on the curve.
// Maybe GeneralSecurityException would be better.
} catch (GeneralSecurityException ex) {
// This is as expected
} catch (Exception ex) {
fail("Expected subclass of java.security.GeneralSecurityException, but got: "
+ ex.getClass().getName());
}
}

Expand Down Expand Up @@ -281,7 +284,7 @@ public void testIsAlias(String algorithmA, String algorithmB) throws Exception {
byte[] message = "Hello".getBytes("UTF-8");
eciesA.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] ciphertext = eciesA.doFinal(message);
eciesB.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
eciesB.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), eciesA.getParameters());
byte[] decrypted = eciesB.doFinal(ciphertext);
assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
Expand Down

0 comments on commit fa01b5a

Please sign in to comment.