2121public class CipherStreamTest2
2222 extends SimpleTest
2323{
24+ private int streamSize ;
25+
2426 public String getName ()
2527 {
2628 return "CipherStreamTest" ;
@@ -33,32 +35,38 @@ private void testModes(String algo, String[] transforms, boolean authenticated)
3335 for (int i = 0 ; i != transforms .length ; i ++)
3436 {
3537 String transform = transforms [i ];
38+ String cipherName = algo + transform ;
3639
37- testWriteRead (algo + transform , key , authenticated , true , false );
38- testWriteRead (algo + transform , key , authenticated , true , true );
39- testWriteRead (algo + transform , key , authenticated , false , false );
40- testWriteRead (algo + transform , key , authenticated , false , true );
41- testReadWrite (algo + transform , key , authenticated , true , false );
42- testReadWrite (algo + transform , key , authenticated , true , true );
43- testReadWrite (algo + transform , key , authenticated , false , false );
44- testReadWrite (algo + transform , key , authenticated , false , true );
45-
46- if (!(transform .indexOf ("CTS" ) > -1 ))
40+ boolean cts = transform .indexOf ("CTS" ) > -1 ;
41+ if (cts && streamSize < Cipher .getInstance (cipherName , "BC" ).getBlockSize ())
4742 {
48- testWriteReadEmpty (algo + transform , key , authenticated , true , false );
49- testWriteReadEmpty (algo + transform , key , authenticated , true , true );
50- testWriteReadEmpty (algo + transform , key , authenticated , false , false );
51- testWriteReadEmpty (algo + transform , key , authenticated , false , true );
43+ continue ;
44+ }
45+ testWriteRead (cipherName , key , authenticated , true , false );
46+ testWriteRead (cipherName , key , authenticated , true , true );
47+ testWriteRead (cipherName , key , authenticated , false , false );
48+ testWriteRead (cipherName , key , authenticated , false , true );
49+ testReadWrite (cipherName , key , authenticated , true , false );
50+ testReadWrite (cipherName , key , authenticated , true , true );
51+ testReadWrite (cipherName , key , authenticated , false , false );
52+ testReadWrite (cipherName , key , authenticated , false , true );
53+
54+ if (!cts )
55+ {
56+ testWriteReadEmpty (cipherName , key , authenticated , true , false );
57+ testWriteReadEmpty (cipherName , key , authenticated , true , true );
58+ testWriteReadEmpty (cipherName , key , authenticated , false , false );
59+ testWriteReadEmpty (cipherName , key , authenticated , false , true );
5260 }
5361
5462 if (authenticated )
5563 {
56- testTamperedRead (algo + transform , key , true , true );
57- testTamperedRead (algo + transform , key , true , false );
58- testTruncatedRead (algo + transform , key , true , true );
59- testTruncatedRead (algo + transform , key , true , false );
60- testTamperedWrite (algo + transform , key , true , true );
61- testTamperedWrite (algo + transform , key , true , false );
64+ testTamperedRead (cipherName , key , true , true );
65+ testTamperedRead (cipherName , key , true , false );
66+ testTruncatedRead (cipherName , key , true , true );
67+ testTruncatedRead (cipherName , key , true , false );
68+ testTamperedWrite (cipherName , key , true , true );
69+ testTamperedWrite (cipherName , key , true , false );
6270 }
6371 }
6472 }
@@ -94,7 +102,7 @@ private void testTamperedRead(String name, Key key, boolean authenticated, boole
94102 decrypt .init (Cipher .DECRYPT_MODE , key );
95103 }
96104
97- byte [] ciphertext = encrypt .doFinal (new byte [1000 ]);
105+ byte [] ciphertext = encrypt .doFinal (new byte [streamSize ]);
98106
99107 // Tamper
100108 ciphertext [0 ] += 1 ;
@@ -140,10 +148,10 @@ private void testTruncatedRead(String name, Key key, boolean authenticated, bool
140148 decrypt .init (Cipher .DECRYPT_MODE , key );
141149 }
142150
143- byte [] ciphertext = encrypt .doFinal (new byte [1000 ]);
151+ byte [] ciphertext = encrypt .doFinal (new byte [streamSize ]);
144152
145153 // Truncate to just smaller than complete tag
146- byte [] truncated = new byte [ciphertext .length - 1000 - 1 ];
154+ byte [] truncated = new byte [ciphertext .length - streamSize - 1 ];
147155 System .arraycopy (ciphertext , 0 , truncated , 0 , truncated .length );
148156
149157 // Tamper
@@ -201,7 +209,7 @@ private void testTamperedWrite(String name, Key key, boolean authenticated, bool
201209 decrypt .init (Cipher .DECRYPT_MODE , key );
202210 }
203211
204- byte [] ciphertext = encrypt .doFinal (new byte [1000 ]);
212+ byte [] ciphertext = encrypt .doFinal (new byte [streamSize ]);
205213
206214 // Tamper
207215 ciphertext [0 ] += 1 ;
@@ -230,7 +238,7 @@ private void testTamperedWrite(String name, Key key, boolean authenticated, bool
230238 private void testWriteRead (String name , Key key , boolean authenticated , boolean useBc , boolean blocks )
231239 throws Exception
232240 {
233- byte [] data = new byte [1000 ];
241+ byte [] data = new byte [streamSize ];
234242 for (int i = 0 ; i < data .length ; i ++)
235243 {
236244 data [i ] = (byte )(i % 255 );
@@ -271,10 +279,10 @@ private void testWriteRead(String name, Key key, boolean authenticated, boolean
271279 OutputStream cOut = createOutputStream (bOut , encrypt , useBc );
272280 if (blocks )
273281 {
274- int chunkSize = data .length / 8 ;
282+ int chunkSize = Math . max ( 1 , data .length / 8 ) ;
275283 for (int i = 0 ; i < data .length ; i += chunkSize )
276284 {
277- cOut .write (data , i , chunkSize );
285+ cOut .write (data , i , Math . min ( chunkSize , data . length - i ) );
278286 }
279287 }
280288 else
@@ -433,6 +441,17 @@ private static Key generateKey(String name)
433441
434442 public void performTest ()
435443 throws Exception
444+ {
445+ int [] testSizes = new int []{0 , 1 , 7 , 8 , 9 , 15 , 16 , 17 , 1023 , 1024 , 1025 , 2047 , 2048 , 2049 , 4095 , 4096 , 4097 };
446+ for (int i = 0 ; i < testSizes .length ; i ++)
447+ {
448+ this .streamSize = testSizes [i ];
449+ performTests ();
450+ }
451+ }
452+
453+ private void performTests ()
454+ throws Exception
436455 {
437456 final String [] blockCiphers64 = new String []{"BLOWFISH" , "DES" , "DESEDE" , "TEA" , "CAST5" , "RC2" , "XTEA" };
438457
0 commit comments