1212import org .bouncycastle .crypto .params .ParametersWithIV ;
1313import org .bouncycastle .util .Arrays ;
1414import org .bouncycastle .util .test .SimpleTest ;
15- import org .junit .Assert ;
15+ import org .bouncycastle .util .test .SimpleTestResult ;
16+ import org .bouncycastle .util .test .TestFailedException ;
1617
1718public abstract class CipherTest
1819 extends SimpleTest
@@ -124,14 +125,14 @@ private void bufferSizeCheck(
124125 }
125126 }
126127
127- interface Instace
128+ interface Instance
128129 {
129- AEADCipher CreateInstace ();
130+ AEADCipher createInstance ();
130131 }
131132
132- static void checkCipher (int aeadLen , int ivLen , int msgLen , Instace instace )
133+ static void checkCipher (int aeadLen , int ivLen , int msgLen , int strength , Instance instance )
133134 {
134- AEADCipher pCipher = instace . CreateInstace ();
135+ AEADCipher pCipher = instance . createInstance ();
135136
136137 try
137138 {
@@ -146,7 +147,7 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
146147
147148 /* Create the Key parameters */
148149 final CipherKeyGenerator myGenerator = new CipherKeyGenerator ();
149- final KeyGenerationParameters myGenParams = new KeyGenerationParameters (myRandom , 128 );
150+ final KeyGenerationParameters myGenParams = new KeyGenerationParameters (myRandom , strength );
150151 myGenerator .init (myGenParams );
151152 final byte [] myKey = myGenerator .generateKey ();
152153 final KeyParameter myKeyParams = new KeyParameter (myKey );
@@ -165,7 +166,7 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
165166 myOutLen += pCipher .doFinal (myEncrypted , myOutLen );
166167
167168 /* Note that myOutLen is too large by DATALEN */
168- pCipher = instace . CreateInstace ();
169+ pCipher = instance . createInstance ();
169170 /* Initialise the cipher for decryption */
170171 pCipher .init (false , myParams );
171172 final int myMaxClearLen = pCipher .getOutputSize (myOutLen );
@@ -187,7 +188,7 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
187188 }
188189 }
189190
190- static void checkAEADCipherOutputSize (int keySize , int ivSize , int blockSize , int tagSize , AEADCipher cipher )
191+ static void checkAEADCipherOutputSize (SimpleTest parent , int keySize , int ivSize , int blockSize , int tagSize , AEADCipher cipher )
191192 throws InvalidCipherTextException
192193 {
193194 final SecureRandom random = new SecureRandom ();
@@ -201,42 +202,53 @@ static void checkAEADCipherOutputSize(int keySize, int ivSize, int blockSize, in
201202 cipher .init (true , new ParametersWithIV (new KeyParameter (key ), iv ));
202203 byte [] ciphertext = new byte [cipher .getOutputSize (plaintext .length )];
203204 //before the encrypt
204- Assert . assertEquals ( plaintext .length + tagSize , ciphertext .length );
205- Assert . assertEquals ( plaintext .length , cipher .getUpdateOutputSize (plaintext .length ) + tmpLength );
205+ isEqualTo ( parent , plaintext .length + tagSize , ciphertext .length );
206+ isEqualTo ( parent , plaintext .length , cipher .getUpdateOutputSize (plaintext .length ) + tmpLength );
206207 //during the encrypt process of the first block
207208 int len = cipher .processBytes (plaintext , 0 , tmpLength , ciphertext , 0 );
208- Assert . assertEquals ( plaintext .length + tagSize , len + cipher .getOutputSize (plaintext .length - tmpLength ));
209- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (plaintext .length - tmpLength ) + tmpLength );
209+ isEqualTo ( parent , plaintext .length + tagSize , len + cipher .getOutputSize (plaintext .length - tmpLength ));
210+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (plaintext .length - tmpLength ) + tmpLength );
210211 //during the encrypt process of the second block
211212 len += cipher .processBytes (plaintext , tmpLength , blockSize , ciphertext , len );
212- Assert . assertEquals ( plaintext .length + tagSize , len + cipher .getOutputSize (plaintext .length - tmpLength - blockSize ));
213- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (plaintext .length - tmpLength - blockSize ) + tmpLength );
213+ isEqualTo ( parent , plaintext .length + tagSize , len + cipher .getOutputSize (plaintext .length - tmpLength - blockSize ));
214+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (plaintext .length - tmpLength - blockSize ) + tmpLength );
214215 //process the remaining bytes
215216 len += cipher .processBytes (plaintext , tmpLength + blockSize , blockSize , ciphertext , len );
216- Assert . assertEquals ( plaintext .length + tagSize , len + cipher .getOutputSize (0 ));
217- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (0 ) + tmpLength );
217+ isEqualTo ( parent , plaintext .length + tagSize , len + cipher .getOutputSize (0 ));
218+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (0 ) + tmpLength );
218219 //process doFinal
219220 len += cipher .doFinal (ciphertext , len );
220- Assert . assertEquals ( len , ciphertext .length );
221+ isEqualTo ( parent , len , ciphertext .length );
221222
222223 cipher .init (false , new ParametersWithIV (new KeyParameter (key ), iv ));
223224 //before the encrypt
224- Assert . assertEquals ( plaintext .length , cipher .getOutputSize (ciphertext .length ));
225- Assert . assertEquals ( plaintext .length , cipher .getUpdateOutputSize (ciphertext .length ) + tmpLength );
225+ isEqualTo ( parent , plaintext .length , cipher .getOutputSize (ciphertext .length ));
226+ isEqualTo ( parent , plaintext .length , cipher .getUpdateOutputSize (ciphertext .length ) + tmpLength );
226227 //during the encrypt process of the first block
227228 len = cipher .processBytes (ciphertext , 0 , tmpLength , plaintext , 0 );
228- Assert . assertEquals ( plaintext .length , len + cipher .getOutputSize (ciphertext .length - tmpLength ));
229- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (ciphertext .length - tmpLength ) + tmpLength );
229+ isEqualTo ( parent , plaintext .length , len + cipher .getOutputSize (ciphertext .length - tmpLength ));
230+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (ciphertext .length - tmpLength ) + tmpLength );
230231 //during the encrypt process of the second block
231232 len += cipher .processBytes (ciphertext , tmpLength , blockSize , plaintext , len );
232- Assert . assertEquals ( plaintext .length , len + cipher .getOutputSize (ciphertext .length - tmpLength - blockSize ));
233- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (ciphertext .length - tmpLength - blockSize ) + tmpLength );
233+ isEqualTo ( parent , plaintext .length , len + cipher .getOutputSize (ciphertext .length - tmpLength - blockSize ));
234+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (ciphertext .length - tmpLength - blockSize ) + tmpLength );
234235 //process the remaining bytes
235236 len += cipher .processBytes (ciphertext , tmpLength + blockSize , blockSize + tagSize , plaintext , len );
236- Assert . assertEquals ( plaintext .length , len + cipher .getOutputSize (0 ));
237- Assert . assertEquals ( plaintext .length , len + cipher .getUpdateOutputSize (0 ) + tmpLength );
237+ isEqualTo ( parent , plaintext .length , len + cipher .getOutputSize (0 ));
238+ isEqualTo ( parent , plaintext .length , len + cipher .getUpdateOutputSize (0 ) + tmpLength );
238239 //process doFinal
239240 len += cipher .doFinal (plaintext , len );
240- Assert .assertEquals (len , plaintext .length );
241+ isEqualTo (parent , len , plaintext .length );
242+ }
243+
244+ static void isEqualTo (
245+ SimpleTest parent ,
246+ int a ,
247+ int b )
248+ {
249+ if (a != b )
250+ {
251+ throw new TestFailedException (SimpleTestResult .failed (parent , "no message" ));
252+ }
241253 }
242254}
0 commit comments