Skip to content

Commit 429e64d

Browse files
author
gefeili
committed
Fix the bugs in ISAPEngine, PhotonBeetleEngine, and XoodyakEngine about output size calculation
1 parent 3925e69 commit 429e64d

File tree

9 files changed

+29
-22
lines changed

9 files changed

+29
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ public int getUpdateOutputSize(int len)
455455
case EncAad:
456456
case EncData:
457457
case EncInit:
458-
return inputOff + len + CRYPTO_ABYTES;
458+
int total = inputOff + len;
459+
return total - total % BLOCK_SIZE;
459460
case DecData:
460461
return inputOff + len;
461462
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ public byte[] getMac()
961961
@Override
962962
public int getUpdateOutputSize(int len)
963963
{
964-
return len + message.size() + (forEncryption ? 16 : -16);
964+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -16));
965+
return total - total % ISAP_rH_SZ;
965966
}
966967

967968
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ public byte[] getMac()
270270
@Override
271271
public int getUpdateOutputSize(int len)
272272
{
273-
return len + message.size() + (forEncryption ? TAG_INBYTES : -TAG_INBYTES);
273+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -TAG_INBYTES));
274+
return total - total % RATE_INBYTES;
274275
}
275276

276277
@Override

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class XoodyakEngine
3636
private byte[] iv;
3737
private final int PhaseDown = 1;
3838
private final int PhaseUp = 2;
39-
// private final int NLANES = 12;
39+
// private final int NLANES = 12;
4040
// private final int NROWS = 3;
4141
// private final int NCOLUMS = 4;
4242
private final int MAXROUNDS = 12;
@@ -262,7 +262,8 @@ public byte[] getMac()
262262
@Override
263263
public int getUpdateOutputSize(int len)
264264
{
265-
return len + message.size() + (forEncryption ? TAGLEN : -TAGLEN);
265+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -TAGLEN));
266+
return total - total % Rkout;
266267
}
267268

268269
@Override
@@ -371,7 +372,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
371372
a3 ^= e3;
372373
a7 ^= e3;
373374
a11 ^= e3;
374-
375+
375376
/* Rho-west: plane shift */
376377
int b0 = a0;
377378
int b1 = a1;
@@ -390,7 +391,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
390391

391392
/* Iota: round ant */
392393
b0 ^= RC[i];
393-
394+
394395
/* Chi: non linear layer */
395396
a0 = b0 ^ (~b4 & b8);
396397
a1 = b1 ^ (~b5 & b9);
@@ -406,7 +407,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
406407
b9 ^= (~b1 & b5);
407408
b10 ^= (~b2 & b6);
408409
b11 ^= (~b3 & b7);
409-
410+
410411
/* Rho-east: plane shift */
411412
a4 = Integers.rotateLeft(a4, 1);
412413
a5 = Integers.rotateLeft(a5, 1);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,39 +202,39 @@ static void checkAEADCipherOutputSize(int keySize, int ivSize, int blockSize, in
202202
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
203203
//before the encrypt
204204
Assert.assertEquals(plaintext.length + tagSize, ciphertext.length);
205-
Assert.assertEquals(plaintext.length + tagSize, cipher.getUpdateOutputSize(plaintext.length));
205+
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(plaintext.length) + tmpLength);
206206
//during the encrypt process of the first block
207207
int len = cipher.processBytes(plaintext, 0, tmpLength, ciphertext, 0);
208208
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(plaintext.length - tmpLength));
209-
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength));
209+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength) + tmpLength);
210210
//during the encrypt process of the second block
211211
len += cipher.processBytes(plaintext, tmpLength , blockSize, ciphertext, len);
212212
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));
213+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength - blockSize) + tmpLength);
214214
//process the remaining bytes
215215
len += cipher.processBytes(plaintext, tmpLength + blockSize , blockSize, ciphertext, len);
216216
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(0));
217-
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getUpdateOutputSize(0));
217+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(0) + tmpLength);
218218
//process doFinal
219219
len += cipher.doFinal(ciphertext, len);
220220
Assert.assertEquals(len, ciphertext.length);
221221

222222
cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
223223
//before the encrypt
224224
Assert.assertEquals(plaintext.length, cipher.getOutputSize(ciphertext.length));
225-
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(ciphertext.length));
225+
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(ciphertext.length) + tmpLength);
226226
//during the encrypt process of the first block
227227
len = cipher.processBytes(ciphertext, 0, tmpLength, plaintext, 0);
228228
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(ciphertext.length - tmpLength));
229-
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength));
229+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength) + tmpLength);
230230
//during the encrypt process of the second block
231231
len += cipher.processBytes(ciphertext, tmpLength , blockSize, plaintext, len);
232232
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(ciphertext.length - tmpLength - blockSize));
233-
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength - blockSize));
233+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength - blockSize) + tmpLength);
234234
//process the remaining bytes
235235
len += cipher.processBytes(ciphertext, tmpLength + blockSize , blockSize + tagSize, plaintext, len);
236236
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(0));
237-
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(0));
237+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(0) + tmpLength);
238238
//process doFinal
239239
len += cipher.doFinal(plaintext, len);
240240
Assert.assertEquals(len, plaintext.length);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ 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, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
3134
//testVectors(ElephantEngine.ElephantParameters.elephant160, "v160_2");
3235
CipherTest.checkCipher(10, 12, 40, 128, new CipherTest.Instace()
3336
{

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, 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));
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));
5858
}
5959

6060
private void testVectors(String filename, IsapType isapType)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void performTest()
4242
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb128, "v128");
4343
testExceptions(new PhotonBeetleDigest(), 32);
4444
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
45-
CipherTest.checkAEADCipherOutputSize(16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
45+
CipherTest.checkAEADCipherOutputSize(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, 16, 16, new XoodyakEngine());
42+
CipherTest.checkAEADCipherOutputSize(16, 16, 24, 16, new XoodyakEngine());
4343
}
4444

4545
private void testVectorsHash()

0 commit comments

Comments
 (0)