Skip to content

Commit

Permalink
test: add test to JreCipherWrapper for all algorithms (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuisathaverat authored Oct 12, 2020
1 parent 92078f2 commit b6230a3
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions test/com/trilead/ssh2/crypto/cipher/JreCipherWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

public class JreCipherWrapperTest {

@Test
public void shouldMatchJreBehavior() throws Exception {
public void shouldMatchJreBehavior(String cipherName, int blokSize, int keySize) throws Exception {
String algorithm = cipherName.contains("/") ? cipherName.substring(0, cipherName.indexOf('/')) : cipherName;
SecureRandom rng = new SecureRandom();
byte[] iv = new byte[16];
byte[] iv = new byte[blokSize];
rng.nextBytes(iv);
byte[] key = new byte[16];
byte[] key = new byte[keySize];
rng.nextBytes(key);
JreCipherWrapper cipher = JreCipherWrapper.getInstance("AES/CTR/NoPadding", new IvParameterSpec(iv));
assertEquals(16, cipher.getBlockSize());
JreCipherWrapper cipher = JreCipherWrapper.getInstance(cipherName, new IvParameterSpec(iv));
assertEquals(blokSize, cipher.getBlockSize());
cipher.init(true, key);
byte[] plaintext = new byte[256];
rng.nextBytes(plaintext);
Expand All @@ -28,24 +28,63 @@ public void shouldMatchJreBehavior() throws Exception {
cipher.transformBlock(plaintext, i, ciphertext, i);
}

Cipher jreCipher = Cipher.getInstance("AES/CTR/NoPadding");
jreCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
Cipher jreCipher = Cipher.getInstance(cipherName);
jreCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, algorithm), new IvParameterSpec(iv));
byte[] decrypted = jreCipher.doFinal(ciphertext);
assertArrayEquals(plaintext, decrypted);

// now the reverse
rng.nextBytes(iv);
rng.nextBytes(key);
rng.nextBytes(plaintext);
jreCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
jreCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, algorithm), new IvParameterSpec(iv));
ciphertext = jreCipher.doFinal(plaintext);

cipher = JreCipherWrapper.getInstance("AES/CTR/NoPadding", new IvParameterSpec(iv));
cipher = JreCipherWrapper.getInstance(cipherName, new IvParameterSpec(iv));
cipher.init(false, key);
Arrays.fill(decrypted, (byte) 0);
for (int i = 0; i < plaintext.length; i += cipher.getBlockSize()) {
cipher.transformBlock(ciphertext, i, decrypted, i);
}
assertArrayEquals(plaintext, decrypted);
}

@Test
public void testMatchBehaviorAesCtrNoPadding() throws Exception {
shouldMatchJreBehavior("AES/CTR/NoPadding", 16, 16);
shouldMatchJreBehavior("AES/CTR/NoPadding", 16, 24);
shouldMatchJreBehavior("AES/CTR/NoPadding", 16, 32);
}

@Test
public void testMatchBehaviorAesCbcNoPadding() throws Exception {
shouldMatchJreBehavior("AES/CBC/NoPadding", 16,16);
shouldMatchJreBehavior("AES/CBC/NoPadding", 16,24);
shouldMatchJreBehavior("AES/CBC/NoPadding", 16,32);
}

@Test
public void testMatchBehaviorBlowfishCtrNoPaddingg() throws Exception {
shouldMatchJreBehavior("Blowfish/CTR/NoPadding", 8, 16);
}

@Test
public void testMatchBehaviorBlowfishCbcNoPaddingg() throws Exception {
shouldMatchJreBehavior("Blowfish/CBC/NoPadding", 8, 16);
}

@Test
public void testMatchBehaviorDESedeCtrNoPadding() throws Exception {
shouldMatchJreBehavior("DESede/CTR/NoPadding", 8, 24);
}

@Test
public void testMatchBehaviorDESedeCbcNoPadding() throws Exception {
shouldMatchJreBehavior("DESede/CBC/NoPadding", 8, 24);
}

@Test
public void testMatchBehaviorDESCbcNoPadding() throws Exception {
shouldMatchJreBehavior("DES/CBC/NoPadding", 8, 8);
}
}

0 comments on commit b6230a3

Please sign in to comment.