|
39 | 39 | import javax.crypto.spec.SecretKeySpec; |
40 | 40 |
|
41 | 41 | import org.bouncycastle.crypto.BufferedBlockCipher; |
| 42 | +import org.bouncycastle.crypto.DefaultMultiBlockCipher; |
42 | 43 | import org.bouncycastle.crypto.engines.AESEngine; |
43 | 44 | import org.bouncycastle.crypto.engines.DESEngine; |
44 | 45 | import org.bouncycastle.crypto.paddings.PKCS7Padding; |
@@ -1747,6 +1748,7 @@ public void performTest() |
1747 | 1748 | testIncorrectCipherModes(); |
1748 | 1749 | doFinalTest(); |
1749 | 1750 | testOverlapping(); |
| 1751 | + testOverlapping2(); |
1750 | 1752 | } |
1751 | 1753 |
|
1752 | 1754 | private void doFinalTest() |
@@ -1806,6 +1808,41 @@ private void testOverlapping() |
1806 | 1808 | } |
1807 | 1809 | } |
1808 | 1810 |
|
| 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 | + |
1809 | 1846 | public static void main( |
1810 | 1847 | String[] args) |
1811 | 1848 | { |
|
0 commit comments