|
10 | 10 | import org.bouncycastle.crypto.InvalidCipherTextException; |
11 | 11 | import org.bouncycastle.crypto.KeyGenerationParameters; |
12 | 12 | import org.bouncycastle.crypto.modes.AEADCipher; |
| 13 | +import org.bouncycastle.crypto.params.AEADParameters; |
13 | 14 | import org.bouncycastle.crypto.params.KeyParameter; |
14 | 15 | import org.bouncycastle.crypto.params.ParametersWithIV; |
15 | 16 | import org.bouncycastle.util.Arrays; |
16 | 17 | import org.bouncycastle.util.test.SimpleTest; |
17 | 18 | import org.bouncycastle.util.test.SimpleTestResult; |
18 | 19 | import org.bouncycastle.util.test.TestFailedException; |
19 | | -import org.junit.Assert; |
20 | 20 |
|
21 | 21 | public abstract class CipherTest |
22 | 22 | extends SimpleTest |
@@ -255,7 +255,7 @@ static void isEqualTo( |
255 | 255 | } |
256 | 256 | } |
257 | 257 |
|
258 | | - static void checkCipher(final BlockCipher pCipher, final int datalen) |
| 258 | + void checkCipher(final BlockCipher pCipher, final int datalen) |
259 | 259 | throws Exception |
260 | 260 | { |
261 | 261 | final SecureRandom random = new SecureRandom(); |
@@ -299,7 +299,53 @@ static void checkCipher(final BlockCipher pCipher, final int datalen) |
299 | 299 | myCipher.doFinal(plaintext, myOutLen); |
300 | 300 |
|
301 | 301 | /* Check that the cipherTexts are identical */ |
302 | | - Assert.assertArrayEquals(myOutput, myOutput2); |
303 | | - Assert.assertArrayEquals(myData, plaintext); |
| 302 | + isTrue(areEqual(myOutput, myOutput2)); |
| 303 | + isTrue(areEqual(myData, plaintext)); |
| 304 | + } |
| 305 | + |
| 306 | + static void checkAEADParemeter(SimpleTest test, int keySize, int ivSize, final int macSize, int blockSize, final AEADCipher cipher) |
| 307 | + throws Exception |
| 308 | + { |
| 309 | + final SecureRandom random = new SecureRandom(); |
| 310 | + final byte[] key = new byte[keySize]; |
| 311 | + final byte[] iv = new byte[ivSize]; |
| 312 | + int tmpLength = random.nextInt(blockSize - 1) + 1; |
| 313 | + final byte[] plaintext = new byte[blockSize * 2 + tmpLength]; |
| 314 | + byte[] aad = new byte[random.nextInt(100) + 2]; |
| 315 | + random.nextBytes(key); |
| 316 | + random.nextBytes(iv); |
| 317 | + random.nextBytes(plaintext); |
| 318 | + random.nextBytes(aad); |
| 319 | + cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); |
| 320 | + byte[] ciphertext1 = new byte[cipher.getOutputSize(plaintext.length)]; |
| 321 | + for (int i = 0; i < aad.length; ++i) |
| 322 | + { |
| 323 | + cipher.processAADByte(aad[i]); |
| 324 | + } |
| 325 | + int len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext1, 0); |
| 326 | + len += cipher.doFinal(ciphertext1, len); |
| 327 | + int aadSplit = random.nextInt(aad.length) + 1; |
| 328 | + cipher.init(true, new AEADParameters(new KeyParameter(key), macSize * 8, iv, Arrays.copyOf(aad, aadSplit))); |
| 329 | + cipher.processAADBytes(aad, aadSplit, aad.length - aadSplit); |
| 330 | + byte[] ciphertext2 = new byte[cipher.getOutputSize(plaintext.length)]; |
| 331 | + len = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext2, 0); |
| 332 | + len += cipher.doFinal(ciphertext2, len); |
| 333 | + test.isTrue("cipher text check", Arrays.areEqual(ciphertext1, ciphertext2)); |
| 334 | + |
| 335 | + test.testException("Invalid value for MAC size: ", "IllegalArgumentException", new TestExceptionOperation() |
| 336 | + { |
| 337 | + @Override |
| 338 | + public void operation() |
| 339 | + throws Exception |
| 340 | + { |
| 341 | + int macSize2 = random.nextInt(); |
| 342 | + while (macSize2 == macSize * 8) |
| 343 | + { |
| 344 | + macSize2 = random.nextInt(); |
| 345 | + } |
| 346 | + cipher.init(true, new AEADParameters(new KeyParameter(key), macSize2, iv, null)); |
| 347 | + } |
| 348 | + }); |
| 349 | + |
304 | 350 | } |
305 | 351 | } |
0 commit comments