Skip to content

Commit 2214d2d

Browse files
author
gefeili
committed
TODO: Fix the bug in Romulus-M
1 parent 67fb4d8 commit 2214d2d

File tree

10 files changed

+71
-0
lines changed

10 files changed

+71
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,18 @@ public int processByte(byte input, byte[] output, int outOff)
587587
@Override
588588
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
589589
{
590+
if (input == output)
591+
{
592+
int inEnd = inOff + len;
593+
int outEnd = outOff + processor.getUpdateOutputSize(len);
594+
if ((inOff <= outOff && outOff <= inEnd) ||
595+
(outOff <= inOff && inOff <= outEnd))
596+
{
597+
input = new byte[len];
598+
System.arraycopy(output, inOff, input, 0, len);
599+
inOff = 0;
600+
}
601+
}
590602
boolean forEncryption = checkData(false);
591603
if (forEncryption)
592604
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public void performTest()
108108
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
109109
CipherTest.checkAEADParemeter(this, 20, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
110110

111+
CipherTest.testOverlapping(this,16, 16, 16, 16, new AsconAEAD128());
112+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128));
113+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon128a));
114+
CipherTest.testOverlapping(this, 20, 16, 16, 16, new AsconEngine(AsconEngine.AsconParameters.ascon80pq));
115+
116+
111117
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
112118
{
113119
@Override

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,4 +908,39 @@ static void implTestExceptionsGetUpdateOutputSize(AEADCipher cipher, boolean for
908908
}
909909
}
910910
}
911+
912+
static void testOverlapping(SimpleTest test, int keySize, int ivSize, int macSize, int blockSize, AEADCipher cipher)
913+
throws Exception
914+
{
915+
SecureRandom random = new SecureRandom();
916+
byte[] keyBytes = new byte[keySize];
917+
byte[] ivBytes = new byte[ivSize];
918+
int offset = 1 + random.nextInt(blockSize - 1);
919+
byte[] data = new byte[blockSize * 2 + offset + macSize];
920+
byte[] expected;
921+
random.nextBytes(keyBytes);
922+
random.nextBytes(ivBytes);
923+
random.nextBytes(data);
924+
AEADParameters parameters = new AEADParameters(new KeyParameter(new byte[keySize]), macSize * 8, new byte[ivSize], null);
925+
cipher.init(true, parameters);
926+
expected = new byte[cipher.getOutputSize(blockSize * 2)];
927+
int len = cipher.processBytes(data, 0, blockSize * 2, expected, 0);
928+
cipher.doFinal(expected, len);
929+
cipher.init(true, parameters);
930+
len = cipher.processBytes(data, 0, blockSize * 2, data, offset);
931+
cipher.doFinal(data, len + offset);
932+
test.isTrue("fail on testing overlapping of encryption for " + cipher.getAlgorithmName(),
933+
Arrays.areEqual(expected, 0, expected.length, data, offset, offset + expected.length));
934+
System.arraycopy(data, offset, data, 0, expected.length);
935+
cipher.init(false, parameters);
936+
expected = new byte[cipher.getOutputSize(data.length)];
937+
len = cipher.processBytes(data, 0, blockSize * 2 + macSize, expected, 0);
938+
cipher.doFinal(expected, len);
939+
cipher.init(false, parameters);
940+
len = cipher.processBytes(data, 0, blockSize * 2 + macSize, data, offset);
941+
cipher.doFinal(data, len + offset);
942+
test.isTrue("fail on testing overlapping of decryption for " + cipher.getAlgorithmName(),
943+
Arrays.areEqual(expected, 0, blockSize * 2, data, offset, offset + blockSize * 2));
944+
945+
}
911946
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public void performTest()
3838
CipherTest.checkAEADParemeter(this, 16, 12, 8, 20, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
3939
CipherTest.checkAEADParemeter(this, 16, 12, 8, 22, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
4040
CipherTest.checkAEADParemeter(this, 16, 12, 16, 25, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
41+
CipherTest.testOverlapping(this, 16, 12, 8, 20, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
42+
CipherTest.testOverlapping(this, 16, 12, 8, 22, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
43+
CipherTest.testOverlapping(this, 16, 12, 16, 25, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));
4144
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 20, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant160));
4245
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 22, 8, new ElephantEngine(ElephantEngine.ElephantParameters.elephant176));
4346
CipherTest.checkAEADCipherOutputSize(this, 16, 12, 25, 16, new ElephantEngine(ElephantEngine.ElephantParameters.elephant200));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public AEADCipher createInstance()
3838
});
3939
implTestParametersEngine(new GiftCofbEngine(), 16, 16, 16);
4040
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new GiftCofbEngine());
41+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new GiftCofbEngine());
4142
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new GiftCofbEngine());
4243

4344
CipherTest.checkCipher(16, 16, 40, 128, new CipherTest.Instance()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public String getName()
2424
public void performTest()
2525
throws Exception
2626
{
27+
CipherTest.testOverlapping(this, 16, 12, 8, 20, new Grain128AEADEngine());
2728
CipherTest.implTestVectorsEngine(new Grain128AEADEngine(), "crypto", "LWC_AEAD_KAT_128_96.txt", this);
2829
checkAEADCipherOutputSize(this, 16, 12, 8, new Grain128AEADEngine());
2930
CipherTest.checkCipher(32, 12, 100, 128, new CipherTest.Instance()
@@ -38,6 +39,7 @@ public AEADCipher createInstance()
3839

3940

4041
CipherTest.checkAEADParemeter(this, 16, 12, 8, 20, new Grain128AEADEngine());
42+
4143
testSplitUpdate();
4244
testExceptions();
4345
testLongAEAD();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public AEADCipher createInstance()
8888
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128));
8989
CipherTest.checkAEADParemeter(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128A));
9090
CipherTest.checkAEADParemeter(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128));
91+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128A));
92+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128));
93+
CipherTest.testOverlapping(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128A));
94+
CipherTest.testOverlapping(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128));
9195
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128A));
9296
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
9397
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public void performTest()
5050
testExceptions(new PhotonBeetleDigest(), 32);
5151
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
5252
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
53+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
54+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
5355
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 16, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb128));
5456
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 4, 16, new PhotonBeetleEngine(PhotonBeetleEngine.PhotonBeetleParameters.pb32));
5557
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public void performTest()
7070
CipherTest.checkAEADParemeter(this, 16, 32, 16, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_128));
7171
CipherTest.checkAEADParemeter(this, 32, 32, 32, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_256));
7272

73+
CipherTest.testOverlapping(this, 16, 16, 16, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM128_128));
74+
CipherTest.testOverlapping(this, 24, 24, 24, 24, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM192_192));
75+
CipherTest.testOverlapping(this, 16, 32, 16, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_128));
76+
CipherTest.testOverlapping(this, 32, 32, 32, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_256));
77+
7378
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 16, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM128_128));
7479
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 24, 192, 24, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM192_192));
7580
CipherTest.checkAEADCipherMultipleBlocks(this, 1025, 33, 16, 128, 32, new SparkleEngine(SparkleEngine.SparkleParameters.SCHWAEMM256_128));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public AEADCipher createInstance()
5353
testExceptions(new XoodyakDigest(), 32);
5454
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 24, 16, new XoodyakEngine());
5555
CipherTest.checkAEADParemeter(this, 16, 16, 16, 24, new XoodyakEngine());
56+
CipherTest.testOverlapping(this, 16, 16, 16, 24, new XoodyakEngine());
5657
}
5758

5859
private void testVectors()

0 commit comments

Comments
 (0)