Skip to content

Commit c1ee3d6

Browse files
author
gefeili
committed
Fix the bugs of getUpdateOutputSize and getOutputSize in ISAPEngine, PhotonBeetleEngine and XoodyakEngine.
1 parent cb38f25 commit c1ee3d6

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/ISAPEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ public byte[] getMac()
961961
@Override
962962
public int getUpdateOutputSize(int len)
963963
{
964-
return len + message.size();
964+
return len + message.size() + (forEncryption ? 16 : -16);
965965
}
966966

967967
@Override

core/src/main/java/org/bouncycastle/crypto/engines/PhotonBeetleEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public byte[] getMac()
270270
@Override
271271
public int getUpdateOutputSize(int len)
272272
{
273-
return len + message.size();
273+
return len + message.size() + (forEncryption ? TAG_INBYTES : -TAG_INBYTES);
274274
}
275275

276276
@Override

core/src/main/java/org/bouncycastle/crypto/engines/XoodyakEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public byte[] getMac()
262262
@Override
263263
public int getUpdateOutputSize(int len)
264264
{
265-
return len + message.size();
265+
return len + message.size() + (forEncryption ? TAGLEN : -TAGLEN);
266266
}
267267

268268
@Override

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1516

1617
public abstract class CipherTest
1718
extends SimpleTest
@@ -128,7 +129,7 @@ interface Instace
128129
AEADCipher CreateInstace();
129130
}
130131

131-
static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
132+
static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
132133
{
133134
AEADCipher pCipher = instace.CreateInstace();
134135

@@ -185,4 +186,57 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
185186
throw new RuntimeException(e);
186187
}
187188
}
189+
190+
static void checkAEADCipherOutputSize(int keySize, int ivSize, int blockSize, int tagSize, AEADCipher cipher)
191+
throws InvalidCipherTextException
192+
{
193+
final SecureRandom random = new SecureRandom();
194+
int tmpLength = random.nextInt(blockSize - 1) + 1;
195+
final byte[] plaintext = new byte[blockSize * 2 + tmpLength];
196+
byte[] key = new byte[keySize];
197+
byte[] iv = new byte[ivSize];
198+
random.nextBytes(key);
199+
random.nextBytes(iv);
200+
random.nextBytes(plaintext);
201+
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
202+
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
203+
//before the encrypt
204+
Assert.assertEquals(plaintext.length + tagSize, ciphertext.length);
205+
Assert.assertEquals(plaintext.length + tagSize, cipher.getUpdateOutputSize(plaintext.length));
206+
//during the encrypt process of the first block
207+
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 + tagSize, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength));
210+
//during the encrypt process of the second block
211+
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 + tagSize, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength - blockSize));
214+
//process the remaining bytes
215+
len += cipher.processBytes(plaintext, tmpLength + blockSize , blockSize, ciphertext, len);
216+
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(0));
217+
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getUpdateOutputSize(0));
218+
//process doFinal
219+
len += cipher.doFinal(ciphertext, len);
220+
Assert.assertEquals(len, ciphertext.length);
221+
222+
cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
223+
//before the encrypt
224+
Assert.assertEquals(plaintext.length, cipher.getOutputSize(ciphertext.length));
225+
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(ciphertext.length));
226+
//during the encrypt process of the first block
227+
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));
230+
//during the encrypt process of the second block
231+
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));
234+
//process the remaining bytes
235+
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));
238+
//process doFinal
239+
len += cipher.doFinal(plaintext, len);
240+
Assert.assertEquals(len, plaintext.length);
241+
}
188242
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +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, 16, 16, new ISAPEngine(IsapType.ISAP_K_128A));
55+
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128));
56+
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_A_128A));
57+
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_A_128));
5458
}
5559

5660
private void testVectors(String filename, IsapType isapType)
@@ -282,6 +286,7 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
282286
}
283287

284288
aeadBlockCipher.init(true, params);
289+
c1 = new byte[aeadBlockCipher.getOutputSize(m.length)];
285290
try
286291
{
287292
aeadBlockCipher.doFinal(c1, m.length);
@@ -431,10 +436,11 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
431436
{
432437
m7[i] = (byte)rand.nextInt();
433438
}
439+
440+
aeadBlockCipher.init(true, params);
434441
byte[] c7 = new byte[aeadBlockCipher.getOutputSize(m7.length)];
435442
byte[] c8 = new byte[c7.length];
436443
byte[] c9 = new byte[c7.length];
437-
aeadBlockCipher.init(true, params);
438444
aeadBlockCipher.processAADBytes(aad2, 0, aad2.length);
439445
offset = aeadBlockCipher.processBytes(m7, 0, m7.length, c7, 0);
440446
aeadBlockCipher.doFinal(c7, offset);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +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, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
4446
}
4547

4648
private void testVectorsHash()
@@ -228,6 +230,7 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
228230
}
229231

230232
aeadBlockCipher.init(true, params);
233+
c1 = new byte[aeadBlockCipher.getOutputSize(m.length)];
231234
try
232235
{
233236
aeadBlockCipher.doFinal(c1, m.length);
@@ -379,10 +382,10 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
379382
{
380383
m7[i] = (byte)rand.nextInt();
381384
}
385+
aeadBlockCipher.init(true, params);
382386
byte[] c7 = new byte[aeadBlockCipher.getOutputSize(m7.length)];
383387
byte[] c8 = new byte[c7.length];
384388
byte[] c9 = new byte[c7.length];
385-
aeadBlockCipher.init(true, params);
386389
aeadBlockCipher.processAADBytes(aad2, 0, aad2.length);
387390
offset = aeadBlockCipher.processBytes(m7, 0, m7.length, c7, 0);
388391
aeadBlockCipher.doFinal(c7, offset);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +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, 16, 16, new XoodyakEngine());
4243
}
4344

4445
private void testVectorsHash()
@@ -233,6 +234,7 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
233234
}
234235

235236
aeadBlockCipher.init(true, params);
237+
c1 = new byte[aeadBlockCipher.getOutputSize(m.length)];
236238
try
237239
{
238240
aeadBlockCipher.doFinal(c1, m.length);
@@ -384,10 +386,11 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
384386
{
385387
m7[i] = (byte)rand.nextInt();
386388
}
389+
390+
aeadBlockCipher.init(true, params);
387391
byte[] c7 = new byte[aeadBlockCipher.getOutputSize(m7.length)];
388392
byte[] c8 = new byte[c7.length];
389393
byte[] c9 = new byte[c7.length];
390-
aeadBlockCipher.init(true, params);
391394
aeadBlockCipher.processAADBytes(aad2, 0, aad2.length);
392395
offset = aeadBlockCipher.processBytes(m7, 0, m7.length, c7, 0);
393396
aeadBlockCipher.doFinal(c7, offset);

0 commit comments

Comments
 (0)