Skip to content

Commit 134689f

Browse files
committed
Move segmentsOverlap methods to Arrays class
1 parent b9b1325 commit 134689f

14 files changed

+33
-58
lines changed

core/src/main/java/org/bouncycastle/crypto/BufferedBlockCipher.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.crypto;
22

3+
import org.bouncycastle.util.Arrays;
34

45
/**
56
* A wrapper class that allows block ciphers to be used to process data in
@@ -237,7 +238,7 @@ public int processBytes(
237238
System.arraycopy(in, inOff, buf, bufOff, gapLen);
238239
inOff += gapLen;
239240
len -= gapLen;
240-
if (in == out && segmentsOverlap(inOff, len, outOff, length))
241+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
241242
{
242243
in = new byte[len];
243244
System.arraycopy(out, inOff, in, 0, len);
@@ -357,10 +358,4 @@ public void reset()
357358
//
358359
cipher.reset();
359360
}
360-
361-
protected boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
362-
{
363-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
364-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
365-
}
366361
}

core/src/main/java/org/bouncycastle/crypto/DefaultBufferedBlockCipher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.crypto;
22

3+
import org.bouncycastle.util.Arrays;
34

45
/**
56
* A wrapper class that allows block ciphers to be used to process data in
@@ -239,7 +240,7 @@ public int processBytes(
239240
System.arraycopy(in, inOff, buf, bufOff, gapLen);
240241
inOff += gapLen;
241242
len -= gapLen;
242-
if (in == out && segmentsOverlap(inOff, len, outOff, length))
243+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
243244
{
244245
in = new byte[len];
245246
System.arraycopy(out, inOff, in, 0, len);

core/src/main/java/org/bouncycastle/crypto/DefaultMultiBlockCipher.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.crypto;
22

3+
import org.bouncycastle.util.Arrays;
4+
35
public abstract class DefaultMultiBlockCipher
46
implements MultiBlockCipher
57
{
@@ -21,7 +23,7 @@ public int processBlocks(byte[] in, int inOff, int blockCount, byte[] out, int o
2123
int resultLen = 0;
2224
int blockSize = this.getMultiBlockSize();
2325
int len = blockCount * blockSize;
24-
if (in == out && segmentsOverlap(inOff, len, outOff, len))
26+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, len))
2527
{
2628
in = new byte[len];
2729
System.arraycopy(out, inOff, in, 0, len);
@@ -36,10 +38,4 @@ public int processBlocks(byte[] in, int inOff, int blockCount, byte[] out, int o
3638

3739
return resultLen;
3840
}
39-
40-
protected boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
41-
{
42-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
43-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
44-
}
4541
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public int processByte(byte input, byte[] output, int outOff)
637637
@Override
638638
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
639639
{
640-
if (input == output && segmentsOverlap(inOff, len, outOff, processor.getUpdateOutputSize(len)))
640+
if (input == output && Arrays.segmentsOverlap(inOff, len, outOff, processor.getUpdateOutputSize(len)))
641641
{
642642
input = new byte[len];
643643
System.arraycopy(output, inOff, input, 0, len);
@@ -793,7 +793,7 @@ protected int processEncDecBytes(byte[] input, int inOff, int len, byte[] output
793793
resultLength = length + m_bufPos - (forEncryption ? 0 : MAC_SIZE);
794794
ensureSufficientOutputBuffer(output, outOff, resultLength - resultLength % BlockSize);
795795
resultLength = 0;
796-
if (input == output && segmentsOverlap(inOff, len, outOff, length))
796+
if (input == output && Arrays.segmentsOverlap(inOff, len, outOff, length))
797797
{
798798
input = new byte[len];
799799
System.arraycopy(output, inOff, input, 0, len);
@@ -1077,12 +1077,6 @@ protected void finishAAD3(State nextState, boolean isDoFinal)
10771077
m_state = nextState;
10781078
}
10791079

1080-
private boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
1081-
{
1082-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
1083-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
1084-
}
1085-
10861080
protected abstract void finishAAD(State nextState, boolean isDoFinal);
10871081

10881082
protected abstract void init(byte[] key, byte[] iv);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bouncycastle.crypto.InvalidCipherTextException;
77
import org.bouncycastle.crypto.OutputLengthException;
88
import org.bouncycastle.crypto.StreamBlockCipher;
9+
import org.bouncycastle.util.Arrays;
910

1011
/**
1112
* A Cipher Text Stealing (CTS) mode cipher. CTS allows block ciphers to
@@ -148,7 +149,7 @@ public int processBytes(
148149
System.arraycopy(in, inOff, buf, bufOff, gapLen);
149150
inOff += gapLen;
150151
len -= gapLen;
151-
if (in == out && segmentsOverlap(inOff, len, outOff, length))
152+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
152153
{
153154
in = new byte[len];
154155
System.arraycopy(out, inOff, in, 0, len);

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) t
307307
{
308308
throw new IllegalArgumentException("'outOff' cannot be negative");
309309
}
310-
if (in == out && segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
310+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
311311
{
312312
in = new byte[len];
313313
System.arraycopy(out, inOff, in, 0, len);
@@ -617,10 +617,4 @@ private void reset(boolean clearMac, boolean resetCipher)
617617
processAADBytes(initialAAD, 0, initialAAD.length);
618618
}
619619
}
620-
621-
protected boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
622-
{
623-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
624-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
625-
}
626620
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
224224
{
225225
throw new DataLengthException("Input buffer too short");
226226
}
227-
if (in == out && segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
227+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
228228
{
229229
in = new byte[len];
230230
System.arraycopy(out, inOff, in, 0, len);
@@ -390,10 +390,4 @@ private boolean verifyMac(byte[] mac, int off)
390390

391391
return nonEqual == 0;
392392
}
393-
394-
protected boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
395-
{
396-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
397-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
398-
}
399393
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
383383
{
384384
throw new DataLengthException("Input buffer too short");
385385
}
386-
if (in == out && segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
386+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, getUpdateOutputSize(len)))
387387
{
388388
in = new byte[len];
389389
System.arraycopy(out, inOff, in, 0, len);
@@ -754,10 +754,4 @@ private void checkStatus()
754754
throw new IllegalStateException("GCM cipher needs to be initialised");
755755
}
756756
}
757-
758-
protected boolean segmentsOverlap(int inOff, int inLen, int outOff, int outLen)
759-
{
760-
// please ensure a valid check for inLen > 0 and outLen > 0 outside this function
761-
return inOff <= outOff + outLen && outOff <= inOff + inLen;
762-
}
763757
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.bouncycastle.crypto.DefaultBufferedBlockCipher;
77
import org.bouncycastle.crypto.OutputLengthException;
88
import org.bouncycastle.crypto.params.ParametersWithIV;
9+
import org.bouncycastle.util.Arrays;
910
import org.bouncycastle.util.Pack;
1011

1112
/**
@@ -123,21 +124,21 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
123124
{
124125
throw new IllegalArgumentException("Partial blocks not supported");
125126
}
126-
if (input == output && segmentsOverlap(inOff, len, outOff, len))
127+
if (input == output && Arrays.segmentsOverlap(inOff, len, outOff, len))
127128
{
128129
input = new byte[len];
129130
System.arraycopy(output, inOff, input, 0, len);
130131
inOff = 0;
131132
}
132133
for (int pos = 0; pos < len; pos += blockSize)
133134
{
134-
processBlocks(input, inOff + pos, output, outOff + pos);
135+
processBlock(input, inOff + pos, output, outOff + pos);
135136
}
136137

137138
return len;
138139
}
139140

140-
private void processBlocks(byte[] input, int inOff, byte[] output, int outOff)
141+
private void processBlock(byte[] input, int inOff, byte[] output, int outOff)
141142
{
142143
/*
143144
* A somewhat arbitrary limit of 2^32 - 1 blocks

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.bouncycastle.crypto.DefaultBufferedBlockCipher;
1010
import org.bouncycastle.crypto.InvalidCipherTextException;
1111
import org.bouncycastle.crypto.OutputLengthException;
12+
import org.bouncycastle.util.Arrays;
1213

1314
/**
1415
* A Cipher Text Stealing (CTS) mode cipher. CTS allows block ciphers to
@@ -157,7 +158,7 @@ public int processBytes(
157158
System.arraycopy(in, inOff, buf, bufOff, gapLen);
158159
inOff += gapLen;
159160
len -= gapLen;
160-
if (in == out && segmentsOverlap(inOff, len, outOff, length))
161+
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
161162
{
162163
in = new byte[len];
163164
System.arraycopy(out, inOff, in, 0, len);

0 commit comments

Comments
 (0)