diff options
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java | 69 |
1 files changed, 19 insertions, 50 deletions
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 f80add1a..11cd903b 100644 --- a/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java +++ b/core/src/main/java/org/bouncycastle/crypto/io/CipherInputStream.java @@ -12,28 +12,28 @@ import org.bouncycastle.crypto.modes.AEADBlockCipher; /** * A CipherInputStream is composed of an InputStream and a cipher so that read() methods return data * that are read in from the underlying InputStream but have been additionally processed by the - * cipher. The cipher must be fully initialized before being used by a CipherInputStream. - * <p/> - * For example, if the cipher is initialized for decryption, the + * Cipher. The cipher must be fully initialized before being used by a CipherInputStream. + * <p> + * For example, if the Cipher is initialized for decryption, the * CipherInputStream will attempt to read in data and decrypt them, * before returning the decrypted data. */ public class CipherInputStream extends FilterInputStream { - private static final int INPUT_BUF_SIZE = 2048; - private BufferedBlockCipher bufferedBlockCipher; private StreamCipher streamCipher; private AEADBlockCipher aeadBlockCipher; - private byte[] buf; - private final byte[] inBuf = new byte[INPUT_BUF_SIZE]; + private final byte[] buf; + private final byte[] inBuf; 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,6 +45,9 @@ public class CipherInputStream super(is); this.bufferedBlockCipher = cipher; + + buf = new byte[cipher.getOutputSize(INPUT_BUF_SIZE)]; + inBuf = new byte[INPUT_BUF_SIZE]; } public CipherInputStream( @@ -54,6 +57,9 @@ public class CipherInputStream super(is); this.streamCipher = cipher; + + buf = new byte[INPUT_BUF_SIZE]; + inBuf = new byte[INPUT_BUF_SIZE]; } /** @@ -64,6 +70,9 @@ public class CipherInputStream super(is); this.aeadBlockCipher = cipher; + + buf = new byte[cipher.getOutputSize(INPUT_BUF_SIZE)]; + inBuf = new byte[INPUT_BUF_SIZE]; } /** @@ -99,7 +108,6 @@ public class CipherInputStream try { - ensureCapacity(read, false); if (bufferedBlockCipher != null) { maxBuf = bufferedBlockCipher.processBytes(inBuf, 0, read, buf, 0); @@ -116,7 +124,7 @@ public class CipherInputStream } catch (Exception e) { - throw new CipherIOException("Error processing stream ", e); + throw new IOException("Error processing stream " + e); } } return maxBuf; @@ -128,7 +136,6 @@ public class CipherInputStream try { finalized = true; - ensureCapacity(0, true); if (bufferedBlockCipher != null) { maxBuf = bufferedBlockCipher.doFinal(buf, 0); @@ -148,7 +155,7 @@ public class CipherInputStream } catch (Exception e) { - throw new CipherIOException("Error finalising cipher ", e); + throw new IOException("Error finalising cipher " + e); } } @@ -252,49 +259,11 @@ public class CipherInputStream } /** - * Ensure the ciphertext buffer has space sufficient to accept an upcoming output. - * - * @param updateSize the size of the pending update. - * @param <code>true</code> 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 |