Skip to content

Commit

Permalink
further tweaking of doFinal.
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Mar 8, 2019
1 parent 49f06a4 commit 12e88a1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
public class JceBlockCipherImpl
implements TlsBlockCipherImpl
{
private static final int BUF_SIZE = 32 * 1024;

private final int cipherMode;
private final Cipher cipher;
private final String algorithm;
Expand Down Expand Up @@ -58,6 +60,14 @@ public int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output
try
{
// to avoid performance issue in FIPS jar 1.0.0-1.0.2
while (inputLength > BUF_SIZE)
{
outputOffset += cipher.update(input, inputOffset, BUF_SIZE, output, outputOffset);

inputOffset += BUF_SIZE;
inputLength -= BUF_SIZE;
}

int len = cipher.update(input, inputOffset, inputLength, output, outputOffset) ;

len += cipher.doFinal(output, outputOffset + len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
public class JceBlockCipherWithCBCImplicitIVImpl
implements TlsBlockCipherImpl
{
private static final int BUF_SIZE = 32 * 1024;

private final Cipher cipher;
private final String algorithm;
private final boolean isEncrypting;
Expand Down Expand Up @@ -61,6 +63,14 @@ public int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output
}

// to avoid performance issue in FIPS jar 1.0.0-1.0.2
while (inputLength > BUF_SIZE)
{
outputOffset += cipher.update(input, inputOffset, BUF_SIZE, output, outputOffset);

inputOffset += BUF_SIZE;
inputLength -= BUF_SIZE;
}

int len = cipher.update(input, inputOffset, inputLength, output, outputOffset) ;

len += cipher.doFinal(output, outputOffset + len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class JceChaCha20Poly1305 implements TlsAEADCipherImpl
{
private static final int BUF_SIZE = 32 * 1024;
private static final byte[] ZEROES = new byte[15];

protected final Cipher cipher;
Expand Down Expand Up @@ -55,11 +56,17 @@ public int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output
System.arraycopy(input, inputOffset, tmp, 64, ciphertextLength);

// to avoid performance issue in FIPS jar 1.0.0-1.0.2
int len = cipher.update(tmp, 0, tmp.length, tmp, 0) ;
int tmpOff = 0;
while (tmpOff < tmp.length - BUF_SIZE)
{
tmpOff += cipher.update(tmp, tmpOff, BUF_SIZE, tmp, tmpOff);
}

int len = cipher.update(tmp, tmpOff, tmp.length - tmpOff, tmp, tmpOff);

len += cipher.doFinal(tmp, len);

if (tmp.length != len)
if (tmp.length != tmpOff + len)
{
throw new IllegalStateException();
}
Expand All @@ -86,7 +93,18 @@ public int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output
byte[] tmp = new byte[64 + ciphertextLength];
System.arraycopy(input, inputOffset, tmp, 64, ciphertextLength);

if (tmp.length != cipher.doFinal(tmp, 0, tmp.length, tmp, 0))
// to avoid performance issue in FIPS jar 1.0.0-1.0.2
int tmpOff = 0;
while (tmpOff < tmp.length - BUF_SIZE)
{
tmpOff += cipher.update(tmp, tmpOff, BUF_SIZE, tmp, tmpOff);
}

int len = cipher.update(tmp, tmpOff, tmp.length - tmpOff, tmp, tmpOff);

len += cipher.doFinal(tmp, len);

if (tmp.length != tmpOff + len)
{
throw new IllegalStateException();
}
Expand Down

0 comments on commit 12e88a1

Please sign in to comment.