@@ -96,6 +96,7 @@ public void performTest()
9696 CCMModeTests ();
9797 XTSModeTests ();
9898 GCMModeTests ();
99+ testOverlapping ();
99100 }
100101
101102 public static void main (
@@ -1464,4 +1465,41 @@ private void doFinalTest(AEADBlockCipher cipher, byte[] key, byte[] iv, byte[] a
14641465 fail ("Failed doFinal test - after: " + cipher .getAlgorithmName ());
14651466 }
14661467 }
1468+
1469+ private void testOverlapping ()
1470+ {
1471+ SecureRandom random = new SecureRandom ();
1472+ byte [] keyBytes = new byte [16 ];
1473+ byte [] iv = new byte [16 ];
1474+ random .nextBytes (keyBytes );
1475+ KXTSBlockCipher bc = new KXTSBlockCipher (new DSTU7624Engine (128 ));
1476+ ParametersWithIV param = new ParametersWithIV (new KeyParameter (keyBytes ), iv );
1477+
1478+ int offset = 1 + random .nextInt (bc .getBlockSize () - 1 ) + bc .getBlockSize ();
1479+ byte [] data = new byte [bc .getBlockSize () * 4 + offset ];
1480+ byte [] expected = new byte [bc .getOutputSize (bc .getBlockSize () * 3 )];
1481+ random .nextBytes (data );
1482+
1483+ bc .init (true , param );
1484+ int len = bc .processBytes (data , 0 , expected .length , expected , 0 );
1485+ bc .doFinal (expected , len );
1486+ bc .init (true , param );
1487+ len = bc .processBytes (data , 0 , expected .length , data , offset );
1488+ bc .doFinal (data , offset + len );
1489+
1490+ if (!areEqual (expected , Arrays .copyOfRange (data , offset , offset + expected .length )))
1491+ {
1492+ fail ("failed to overlapping of encryption" );
1493+ }
1494+
1495+ bc .init (false , param );
1496+ bc .processBytes (data , 0 , expected .length , expected , 0 );
1497+ bc .init (false , param );
1498+ bc .processBytes (data , 0 , expected .length , data , offset );
1499+
1500+ if (!areEqual (expected , Arrays .copyOfRange (data , offset , offset + expected .length )))
1501+ {
1502+ fail ("failed to overlapping of encryption" );
1503+ }
1504+ }
14671505}
0 commit comments