added a test for allowed secret key size

This commit is contained in:
Dave
2015-06-28 21:17:29 +02:00
parent 6166e77646
commit 64e408a67a

View File

@@ -11,6 +11,7 @@ import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
@@ -27,6 +28,14 @@ public class NoUtilTest {
public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException { public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
NoCore.setup(); NoCore.setup();
} }
@Test
public void testAllowedKeySize() throws NoSuchAlgorithmException {
if (Cipher.getMaxAllowedKeyLength(NoUtil.CIPHER_KEY_SPEC) < NoUtil.AES_STRENGTH) {
fail("Max allowed key length for CIPHER_TYPE (AES) less than required.");
}
}
@Test @Test
public void testBytesToChars() { public void testBytesToChars() {
@@ -115,7 +124,7 @@ public class NoUtilTest {
} }
@Test @Test
public void testByteKeyEncryptionDecryptionAES() { public void testByteKeyEncryptionDecryptionAES() throws IllegalBlockSizeException, BadPaddingException {
final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'};
final byte[] originalByteKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; final byte[] originalByteKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
@@ -127,21 +136,11 @@ public class NoUtilTest {
try { try {
NoUtil.decrypt(encryptedByByteKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); NoUtil.decrypt(encryptedByByteKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'});
fail("Did not throw BadPaddingException while decrypting with bad key."); fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (IllegalBlockSizeException e) {
fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// Do nothing, correct // Do nothing, correct
} }
byte[] decryptedByByteKey; byte[] decryptedByByteKey = NoUtil.decrypt(encryptedByByteKey, byteKey);
try {
decryptedByByteKey = NoUtil.decrypt(encryptedByByteKey, byteKey);
} catch (IllegalBlockSizeException | BadPaddingException e) {
fail("Encountered an error of type " + e.getClass().getSimpleName()
+ " while decrypting with valid password");
decryptedByByteKey = new byte[] {};
}
assertTrue(Arrays.equals(originalBytes, decryptedByByteKey)); assertTrue(Arrays.equals(originalBytes, decryptedByByteKey));
byte[] nullByte = null; byte[] nullByte = null;
@@ -161,10 +160,6 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
try { try {
@@ -172,15 +167,11 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
} }
@Test @Test
public void testCharKeyEncryptionDecryptionAES() { public void testCharKeyEncryptionDecryptionAES() throws IllegalBlockSizeException, BadPaddingException {
final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'};
final char[] originalCharKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; final char[] originalCharKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
@@ -192,21 +183,11 @@ public class NoUtilTest {
try { try {
NoUtil.decrypt(encryptedByCharKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); NoUtil.decrypt(encryptedByCharKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'});
fail("Did not throw BadPaddingException while decrypting with bad key."); fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (IllegalBlockSizeException e) {
fail("Did not throw BadPaddingException while decrypting with bad key.");
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// Do nothing, correct // Do nothing, correct
} }
byte[] decryptedByCharKey; byte[] decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey);
try {
decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey);
} catch (IllegalBlockSizeException | BadPaddingException e) {
fail("Encountered an error of type " + e.getClass().getSimpleName()
+ " while decrypting with valid password");
decryptedByCharKey = new byte[] {};
}
assertTrue(Arrays.equals(originalBytes, decryptedByCharKey)); assertTrue(Arrays.equals(originalBytes, decryptedByCharKey));
byte[] nullByte = null; byte[] nullByte = null;
@@ -216,10 +197,6 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
try { try {
@@ -227,10 +204,6 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
try { try {
@@ -238,29 +211,16 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
} }
@Test @Test
public void testNoKeyEncryptionDecryptionAES() { public void testNoKeyEncryptionDecryptionAES() throws IllegalBlockSizeException, BadPaddingException {
final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'};
byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length);
byte[] encrypted = NoUtil.encrypt(bytes); byte[] encrypted = NoUtil.encrypt(bytes);
byte[] decrypted = NoUtil.decrypt(encrypted);
byte[] decrypted;
try {
decrypted = NoUtil.decrypt(encrypted);
} catch (IllegalBlockSizeException | BadPaddingException e) {
fail("Encountered an error of type " + e.getClass().getSimpleName()
+ " while decrypting with valid password");
decrypted = new byte[] {};
}
assertTrue(Arrays.equals(originalBytes, decrypted)); assertTrue(Arrays.equals(originalBytes, decrypted));
try { try {
@@ -268,15 +228,12 @@ public class NoUtilTest {
fail("Allowed null parameter without thrown exception."); fail("Allowed null parameter without thrown exception.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} catch (IllegalBlockSizeException e) {
fail("Allowed null parameter without thrown exception.");
} catch (BadPaddingException e) {
fail("Allowed null parameter without thrown exception.");
} }
} }
@Test @Test
public void testEncryptionDecryptionRSA() { public void testEncryptionDecryptionRSA() throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
KeyPairGenerator kpg; KeyPairGenerator kpg;
try { try {
kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM); kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM);
@@ -284,42 +241,25 @@ public class NoUtilTest {
throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e); throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e);
} }
try { kpg.initialize(NoUtil.RSA_STRENGTH,
kpg.initialize(NoUtil.RSA_STRENGTH, SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER));
} catch (NoSuchAlgorithmException e) {
throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e);
} catch (NoSuchProviderException e) {
throw new NoDashFatalException("Value for SECURERANDOM_PROVIDER not valid.", e);
}
KeyPair keyPair = kpg.generateKeyPair(); KeyPair keyPair = kpg.generateKeyPair();
KeyPair keyPair2 = kpg.generateKeyPair(); KeyPair keyPair2 = kpg.generateKeyPair();
final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'};
byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length);
byte[] encrypted = NoUtil.encryptRSA(bytes, keyPair.getPublic()); byte[] encrypted = NoUtil.encryptRSA(bytes, keyPair.getPublic());
try { try {
NoUtil.decryptRSA(encrypted, keyPair2.getPrivate()); NoUtil.decryptRSA(encrypted, keyPair2.getPrivate());
fail("Did not throw exception with incorrect private key."); fail("Did not throw exception with incorrect private key.");
} catch (InvalidKeyException e) {
fail("Did not throw exception with incorrect private key.");
} catch (IllegalBlockSizeException e) {
fail("Did not throw exception with incorrect private key.");
} catch (BadPaddingException e) { } catch (BadPaddingException e) {
// Do nothing, correct // Do nothing, correct
} }
byte[] decrypted; byte[] decrypted = NoUtil.decryptRSA(encrypted, keyPair.getPrivate());
try {
decrypted = NoUtil.decryptRSA(encrypted, keyPair.getPrivate());
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
fail("Threw exception with correct private key.");
decrypted = new byte[] {};
}
assertTrue(Arrays.equals(originalBytes, decrypted)); assertTrue(Arrays.equals(originalBytes, decrypted));
} }