/** * Gets the counter for input stream position. * * @param position the given position in the data. * @return the counter for input stream position. */ protected long getCounter(long position) { return position / cipher.getBlockSize(); }
/** * Gets the padding for input stream position. * * @param position the given position in the data. * @return the padding for input stream position. */ protected byte getPadding(long position) { return (byte) (position % cipher.getBlockSize()); }
/** * Checks and floors buffer size. * * @param cipher the {@link CryptoCipher} instance. * @param bufferSize the buffer size. * @return the remaining buffer size. */ static int checkBufferSize(CryptoCipher cipher, int bufferSize) { Utils.checkArgument(bufferSize >= CryptoInputStream.MIN_BUFFER_SIZE, "Minimum value of buffer size is " + CryptoInputStream.MIN_BUFFER_SIZE + "."); return bufferSize - bufferSize % cipher.getBlockSize(); } }
/** * Resets the {@link #cipher}: calculate counter and {@link #padding}. * * @throws IOException if an I/O error occurs. */ private void resetCipher() throws IOException { final long counter = streamOffset / cipher.getBlockSize(); padding = (byte) (streamOffset % cipher.getBlockSize()); inBuffer.position(padding); // Set proper position for input data. CtrCryptoInputStream.calculateIV(initIV, counter, iv); try { cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } cipherReset = false; }
/** * Constructs a {@link CryptoInputStream}. * * @param input the input data. * @param cipher the cipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException { this.input = input; this.cipher = cipher; this.bufferSize = CryptoInputStream.checkBufferSize(cipher, bufferSize); this.key = key; this.params = params; if (!(params instanceof IvParameterSpec)) { // other AlgorithmParameterSpec such as GCMParameterSpec is not // supported now. throw new IOException("Illegal parameters"); } inBuffer = ByteBuffer.allocateDirect(this.bufferSize); outBuffer = ByteBuffer.allocateDirect(this.bufferSize + cipher.getBlockSize()); outBuffer.limit(0); initCipher(); }
/** * Constructs a {@link CryptoOutputStream}. * * @param output the output stream. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param params the algorithm parameters. * @throws IOException if an I/O error occurs. */ protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException { this.output = output; this.bufferSize = CryptoInputStream.checkBufferSize(cipher, bufferSize); this.cipher = cipher; this.key = key; this.params = params; if (!(params instanceof IvParameterSpec)) { // other AlgorithmParameterSpec such as GCMParameterSpec is not // supported now. throw new IOException("Illegal parameters"); } inBuffer = ByteBuffer.allocateDirect(this.bufferSize); outBuffer = ByteBuffer.allocateDirect(this.bufferSize + cipher.getBlockSize()); initCipher(); }