Skip to content

Commit b4133b9

Browse files
author
gefeili
committed
Add test for DefaultMultiBlockCipher
1 parent 4f8ede4 commit b4133b9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

prov/src/test/java/org/bouncycastle/jce/provider/test/BlockCipherTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import javax.crypto.spec.SecretKeySpec;
4040

4141
import org.bouncycastle.crypto.BufferedBlockCipher;
42+
import org.bouncycastle.crypto.DefaultMultiBlockCipher;
4243
import org.bouncycastle.crypto.engines.AESEngine;
4344
import org.bouncycastle.crypto.engines.DESEngine;
4445
import org.bouncycastle.crypto.paddings.PKCS7Padding;
@@ -1747,6 +1748,7 @@ public void performTest()
17471748
testIncorrectCipherModes();
17481749
doFinalTest();
17491750
testOverlapping();
1751+
testOverlapping2();
17501752
}
17511753

17521754
private void doFinalTest()
@@ -1806,6 +1808,41 @@ private void testOverlapping()
18061808
}
18071809
}
18081810

1811+
private void testOverlapping2()
1812+
{
1813+
//Skip the dofinal of the test
1814+
DefaultMultiBlockCipher bc = new AESEngine();
1815+
SecureRandom random = new SecureRandom();
1816+
byte[] keyBytes = new byte[16];
1817+
random.nextBytes(keyBytes);
1818+
KeyParameter key = new KeyParameter(keyBytes);
1819+
1820+
int offset = 2 + random.nextInt(bc.getBlockSize() - 1);
1821+
byte[] data = new byte[bc.getBlockSize() * 2 + offset];
1822+
byte[] expected = new byte[bc.getBlockSize() * 2];
1823+
random.nextBytes(data);
1824+
1825+
bc.init(true, key);
1826+
bc.processBlocks(data, 0, 2, expected, 0);
1827+
bc.init(true, key);
1828+
bc.processBlocks(data, 0, 2, data, offset);
1829+
1830+
if (!areEqual(expected, Arrays.copyOfRange(data, offset, offset + bc.getBlockSize() * 2)))
1831+
{
1832+
fail("failed to overlapping of encryption");
1833+
}
1834+
1835+
bc.init(false, key);
1836+
bc.processBlocks(data, 0, 2, expected, 0);
1837+
bc.init(false, key);
1838+
bc.processBlocks(data, 0, 2, data, offset);
1839+
1840+
if (!areEqual(expected, Arrays.copyOfRange(data, offset, offset + bc.getBlockSize() * 2)))
1841+
{
1842+
fail("failed to overlapping of encryption");
1843+
}
1844+
}
1845+
18091846
public static void main(
18101847
String[] args)
18111848
{

0 commit comments

Comments
 (0)