Skip to content

Commit ac52173

Browse files
author
gefeili
committed
Fix the bug in AEADBufferBaseEngine.processBytes when MAC_SIZE>BlockSize
1 parent 811083a commit ac52173

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
165165
{
166166
while (m_bufPos > BlockSize && len + m_bufPos > BlockSize + MAC_SIZE)
167167
{
168-
validateAndProcessBuffer(m_buf, resultLength, output, outOff);
168+
validateAndProcessBuffer(m_buf, resultLength, output, outOff + resultLength);
169169
m_bufPos -= BlockSize;
170170
resultLength += BlockSize;
171-
outOff += BlockSize;
172171
}
173172
if (m_bufPos != 0)
174173
{
@@ -178,10 +177,9 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
178177
available = Math.max(BlockSize - m_bufPos, 0);
179178
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
180179
inOff += available;
181-
validateAndProcessBuffer(m_buf, 0, output, outOff);
180+
validateAndProcessBuffer(m_buf, 0, output, outOff + resultLength);
182181
resultLength += BlockSize;
183182
len -= available;
184-
outOff += BlockSize;
185183
}
186184
else
187185
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.bouncycastle.crypto.digests.AsconXof128;
2020
import org.bouncycastle.crypto.engines.AsconAEAD128;
2121
import org.bouncycastle.crypto.engines.AsconEngine;
22+
import org.bouncycastle.crypto.engines.ElephantEngine;
2223
import org.bouncycastle.crypto.modes.AEADCipher;
2324
import org.bouncycastle.crypto.params.AEADParameters;
2425
import org.bouncycastle.crypto.params.KeyParameter;
@@ -140,6 +141,10 @@ public AEADCipher createInstance()
140141
DigestTest.checkDigestReset(this, new AsconXof(AsconXof.AsconParameters.AsconXofA));
141142
DigestTest.checkDigestReset(this, new AsconDigest(AsconDigest.AsconParameters.AsconHash));
142143
DigestTest.checkDigestReset(this, new AsconDigest(AsconDigest.AsconParameters.AsconHashA));
144+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 16, new AsconAEAD128());
145+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
146+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
147+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 160, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
143148
}
144149

145150
public void testBufferingEngine_ascon128()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public void operation()
355355
* @param AEADLEN AEAD length.
356356
* @param NONCELEN Nonce length.
357357
* */
358-
static void checkAEADCipherMultipleBlocks(SimpleTest test, int DATALEN, int PARTLEN, int AEADLEN, int NONCELEN, final AEADCipher pCipher)
358+
static void checkAEADCipherMultipleBlocks(SimpleTest test, int DATALEN, int PARTLEN, int AEADLEN, int strength, int NONCELEN, final AEADCipher pCipher)
359359
throws InvalidCipherTextException
360360
{
361361
/* Obtain some random data */
@@ -369,7 +369,7 @@ static void checkAEADCipherMultipleBlocks(SimpleTest test, int DATALEN, int PART
369369

370370
/* Create the Key parameters */
371371
final CipherKeyGenerator myGenerator = new CipherKeyGenerator();
372-
final KeyGenerationParameters myGenParams = new KeyGenerationParameters(myRandom, 128);
372+
final KeyGenerationParameters myGenParams = new KeyGenerationParameters(myRandom, strength);
373373
myGenerator.init(myGenParams);
374374
final byte[] myKey = myGenerator.generateKey();
375375
final KeyParameter myKeyParams = new KeyParameter(myKey);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public String getName()
2727
public void performTest()
2828
throws Exception
2929
{
30-
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
31-
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
32-
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
30+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
31+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
32+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 41, 10, 128, 12, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
3333
CipherTest.checkAEADParemeter(this, 16, 12, 8, 20, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
3434
CipherTest.checkAEADParemeter(this, 16, 12, 8, 22, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
3535
CipherTest.checkAEADParemeter(this, 16, 12, 16, 25, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public String getName()
3333
public void performTest()
3434
throws Exception
3535
{
36+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new ISAPEngine(IsapType.ISAP_A_128A));
3637
testVectors("isapa128av20", IsapType.ISAP_A_128A);
3738
testVectors("isapa128v20", IsapType.ISAP_A_128);
3839
testVectors("isapk128av20", IsapType.ISAP_K_128A);
@@ -92,6 +93,10 @@ public AEADCipher createInstance()
9293
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
9394
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));
9495
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128));
96+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new ISAPEngine(IsapType.ISAP_K_128A));
97+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new ISAPEngine(IsapType.ISAP_K_128));
98+
99+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new ISAPEngine(IsapType.ISAP_A_128));
95100
}
96101

97102
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
@@ -31,8 +31,8 @@ public String getName()
3131
public void performTest()
3232
throws Exception
3333
{
34-
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 16 , new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
35-
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 16 , new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
34+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 128, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
35+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 19, 100, 128, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
3636
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb32, "v32");
3737
testVectors(PhotonBeetleEngine.PhotonBeetleParameters.pb128, "v128");
3838
DigestTest.checkDigestReset(this, new PhotonBeetleDigest());

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public void performTest()
7070
CipherTest.checkAEADParemeter(this, 24, 24, 24, 24, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM192_192));
7171
CipherTest.checkAEADParemeter(this, 16, 32, 16, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_128));
7272
CipherTest.checkAEADParemeter(this, 32, 32, 32, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_256));
73+
74+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM128_128));
75+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 24, 192, 24, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM192_192));
76+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_128));
77+
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 32, 256, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_256));
7378
}
7479

7580
public void testBufferingEngine_SCHWAEMM128_128()
@@ -414,7 +419,7 @@ private void implTestVectorsEngine(SparkleEngine.SparkleParameters pbp, String f
414419
mismatch("Reccover Keystream " + map.get("Count"), (String)map.get("PT"), rv);
415420
}
416421
}
417-
System.out.println(map.get("Count") + " pass");
422+
//System.out.println(map.get("Count") + " pass");
418423
map.clear();
419424
}
420425
else

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String getName()
3333
public void performTest()
3434
throws Exception
3535
{
36-
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 18, 100, 16 , new XoodyakEngine());
36+
CipherTest.checkAEADCipherMultipleBlocks(this, 1024, 18, 100, 128, 16, new XoodyakEngine());
3737
testVectors();
3838
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
3939
{

0 commit comments

Comments
 (0)