Skip to content

Commit 4a369f0

Browse files
author
gefeili
committed
Fix KXTSBlockCipher
1 parent 9ed602e commit 4a369f0

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

core/src/main/java/org/bouncycastle/crypto/modes/KXTSBlockCipher.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
123123
{
124124
throw new IllegalArgumentException("Partial blocks not supported");
125125
}
126-
126+
if (input == output && segmentsOverlap(inOff, len, outOff, len))
127+
{
128+
input = new byte[len];
129+
System.arraycopy(output, inOff, input, 0, len);
130+
inOff = 0;
131+
}
127132
for (int pos = 0; pos < len; pos += blockSize)
128133
{
129134
processBlocks(input, inOff + pos, output, outOff + pos);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void performTest()
9696
CCMModeTests();
9797
XTSModeTests();
9898
GCMModeTests();
99+
testOverlapping();
99100
}
100101

101102
public static void main(
@@ -1464,4 +1465,41 @@ private void doFinalTest(AEADBlockCipher cipher, byte[] key, byte[] iv, byte[] a
14641465
fail("Failed doFinal test - after: " + cipher.getAlgorithmName());
14651466
}
14661467
}
1468+
1469+
private void testOverlapping()
1470+
{
1471+
SecureRandom random = new SecureRandom();
1472+
byte[] keyBytes = new byte[16];
1473+
byte[] iv = new byte[16];
1474+
random.nextBytes(keyBytes);
1475+
KXTSBlockCipher bc = new KXTSBlockCipher(new DSTU7624Engine(128));
1476+
ParametersWithIV param = new ParametersWithIV(new KeyParameter(keyBytes), iv);
1477+
1478+
int offset = 1 + random.nextInt(bc.getBlockSize() - 1) + bc.getBlockSize();
1479+
byte[] data = new byte[bc.getBlockSize() * 4 + offset];
1480+
byte[] expected = new byte[bc.getOutputSize(bc.getBlockSize() * 3)];
1481+
random.nextBytes(data);
1482+
1483+
bc.init(true, param);
1484+
int len = bc.processBytes(data, 0, expected.length, expected, 0);
1485+
bc.doFinal(expected, len);
1486+
bc.init(true, param);
1487+
len = bc.processBytes(data, 0, expected.length, data, offset);
1488+
bc.doFinal(data, offset + len);
1489+
1490+
if (!areEqual(expected, Arrays.copyOfRange(data, offset, offset + expected.length)))
1491+
{
1492+
fail("failed to overlapping of encryption");
1493+
}
1494+
1495+
bc.init(false, param);
1496+
bc.processBytes(data, 0, expected.length, expected, 0);
1497+
bc.init(false, param);
1498+
bc.processBytes(data, 0, expected.length, data, offset);
1499+
1500+
if (!areEqual(expected, Arrays.copyOfRange(data, offset, offset + expected.length)))
1501+
{
1502+
fail("failed to overlapping of encryption");
1503+
}
1504+
}
14671505
}

0 commit comments

Comments
 (0)