Skip to content

Commit a3120b1

Browse files
committed
merged with main
1 parent e237fb4 commit a3120b1

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

core/src/test/java/org/bouncycastle/crypto/test/CipherTest.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import org.bouncycastle.crypto.params.ParametersWithIV;
1313
import org.bouncycastle.util.Arrays;
1414
import 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

1718
public 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
}

core/src/test/java/org/bouncycastle/crypto/test/ElephantTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,30 @@ public String getName()
2828
public void performTest()
2929
throws Exception
3030
{
31-
CipherTest.checkAEADCipherOutputSize(16, 12, 20, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
32-
CipherTest.checkAEADCipherOutputSize(16, 12, 22, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
33-
CipherTest.checkAEADCipherOutputSize(16, 12, 25, 16, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
31+
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 20, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
32+
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 22, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
33+
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 25, 16, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
3434
// //testVectors(ElephantEngine.ElephantParameters.elephant160, "v160_2");
3535
ElephantEngine elephant = new ElephantEngine(ElephantEngine.ElephantParameters.elephant200);
3636
testExceptions(elephant, elephant.getKeyBytesSize(), elephant.getIVBytesSize(), elephant.getBlockSize());
3737
testParameters(elephant, 16, 12, 16);
38-
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instace()
38+
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instance()
3939
{
40-
@Override
41-
public AEADCipher CreateInstace()
40+
public AEADCipher createInstance()
4241
{
4342
return new ElephantEngine(ElephantEngine.ElephantParameters.elephant160);
4443
}
4544
});
46-
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instace()
45+
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instance()
4746
{
48-
@Override
49-
public AEADCipher CreateInstace()
47+
public AEADCipher createInstance()
5048
{
5149
return new ElephantEngine(ElephantEngine.ElephantParameters.elephant176);
5250
}
5351
});
54-
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instace()
52+
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instance()
5553
{
56-
@Override
57-
public AEADCipher CreateInstace()
54+
public AEADCipher createInstance()
5855
{
5956
return new ElephantEngine(ElephantEngine.ElephantParameters.elephant200);
6057
}

core/src/test/java/org/bouncycastle/crypto/test/ISAPTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public void performTest()
5151
testVectors("isapk128av20", IsapType.ISAP_K_128A);
5252
testVectors("isapk128v20", IsapType.ISAP_K_128);
5353
testVectors();
54-
CipherTest.checkAEADCipherOutputSize(16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128A));
55-
CipherTest.checkAEADCipherOutputSize(16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
56-
CipherTest.checkAEADCipherOutputSize(16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));
57-
CipherTest.checkAEADCipherOutputSize(16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128));
54+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128A));
55+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
56+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));
57+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128));
5858
}
5959

6060
private void testVectors(String filename, IsapType isapType)

core/src/test/java/org/bouncycastle/crypto/test/PhotonBeetleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void performTest()
4141
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb32, "v32");
4242
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb128, "v128");
4343
testExceptions(new PhotonBeetleDigest(), 32);
44-
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
45-
CipherTest.checkAEADCipherOutputSize(16, 16, 4, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
44+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
45+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 4, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
4646
}
4747

4848
private void testVectorsHash()

core/src/test/java/org/bouncycastle/crypto/test/XoodyakTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void performTest()
3939
testExceptions(xoodyak, xoodyak.getKeyBytesSize(), xoodyak.getIVBytesSize(), xoodyak.getBlockSize());
4040
testParameters(xoodyak, 16, 16, 16);
4141
testExceptions(new XoodyakDigest(), 32);
42-
CipherTest.checkAEADCipherOutputSize(16, 16, 24, 16, new XoodyakEngine());
42+
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 24, 16, new XoodyakEngine());
4343
}
4444

4545
private void testVectorsHash()

0 commit comments

Comments
 (0)