From 9f22da58b961dc899fbb14099ed3a13810769d1e Mon Sep 17 00:00:00 2001 From: David Hook Date: Fri, 21 Mar 2014 15:20:30 +1100 Subject: Fixed error introduced in JavaDoc merge --- .../bouncycastle/crypto/io/CipherInputStream.java | 61 ++++++++++++++++------ 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'core/src/main/java/org/bouncycastle') diff --git a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java index 11cd903b..ebebe3d0 100644 --- a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java +++ b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java @@ -21,19 +21,19 @@ import org.bouncycastle.crypto.modes.AEADBlockCipher; public class CipherInputStream extends FilterInputStream { + private static final int INPUT_BUF_SIZE = 2048; + private BufferedBlockCipher bufferedBlockCipher; private StreamCipher streamCipher; private AEADBlockCipher aeadBlockCipher; - private final byte[] buf; - private final byte[] inBuf; + private byte[] buf; + private final byte[] inBuf = new byte[INPUT_BUF_SIZE]; private int bufOff; private int maxBuf; private boolean finalized; - private static final int INPUT_BUF_SIZE = 2048; - /** * Constructs a CipherInputStream from an InputStream and a * BufferedBlockCipher. @@ -45,9 +45,6 @@ public class CipherInputStream super(is); this.bufferedBlockCipher = cipher; - - buf = new byte[cipher.getOutputSize(INPUT_BUF_SIZE)]; - inBuf = new byte[INPUT_BUF_SIZE]; } public CipherInputStream( @@ -57,9 +54,6 @@ public class CipherInputStream super(is); this.streamCipher = cipher; - - buf = new byte[INPUT_BUF_SIZE]; - inBuf = new byte[INPUT_BUF_SIZE]; } /** @@ -70,9 +64,6 @@ public class CipherInputStream super(is); this.aeadBlockCipher = cipher; - - buf = new byte[cipher.getOutputSize(INPUT_BUF_SIZE)]; - inBuf = new byte[INPUT_BUF_SIZE]; } /** @@ -108,6 +99,7 @@ public class CipherInputStream try { + ensureCapacity(read, false); if (bufferedBlockCipher != null) { maxBuf = bufferedBlockCipher.processBytes(inBuf, 0, read, buf, 0); @@ -124,7 +116,7 @@ public class CipherInputStream } catch (Exception e) { - throw new IOException("Error processing stream " + e); + throw new CipherIOException("Error processing stream ", e); } } return maxBuf; @@ -136,6 +128,7 @@ public class CipherInputStream try { finalized = true; + ensureCapacity(0, true); if (bufferedBlockCipher != null) { maxBuf = bufferedBlockCipher.doFinal(buf, 0); @@ -258,12 +251,50 @@ public class CipherInputStream return maxBuf - bufOff; } + /** + * Ensure the ciphertext buffer has space sufficient to accept an upcoming output. + * + * @param updateSize the size of the pending update. + * @param finalOutput true iff this the cipher is to be finalised. + */ + private void ensureCapacity(int updateSize, boolean finalOutput) + { + int bufLen = updateSize; + if (finalOutput) + { + if (bufferedBlockCipher != null) + { + bufLen = bufferedBlockCipher.getOutputSize(updateSize); + } + else if (aeadBlockCipher != null) + { + bufLen = aeadBlockCipher.getOutputSize(updateSize); + } + } + else + { + if (bufferedBlockCipher != null) + { + bufLen = bufferedBlockCipher.getUpdateOutputSize(updateSize); + } + else if (aeadBlockCipher != null) + { + bufLen = aeadBlockCipher.getUpdateOutputSize(updateSize); + } + } + + if ((buf == null) || (buf.length < bufLen)) + { + buf = new byte[bufLen]; + } + } + /** * Closes the underlying input stream and finalises the processing of the data by the cipher. * * @throws IOException if there was an error closing the input stream. * @throws InvalidCipherTextIOException if the data read from the stream was invalid ciphertext - * (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails). + * (e.g. the cipher is an AEAD cipher and the ciphertext tag check fails). */ public void close() throws IOException -- cgit v1.2.3