private byte[] process(AEADBlockCipher blockCipher, byte[] in) { byte[] buf = new byte[blockCipher.getOutputSize(in.length)]; int bytesWritten = blockCipher.processBytes(in, 0, in.length, buf, 0); try { bytesWritten += blockCipher.doFinal(buf, bytesWritten); } catch (InvalidCipherTextException e) { throw new IllegalStateException("unable to encrypt/decrypt", e); } if (bytesWritten == buf.length) { return buf; } byte[] out = new byte[bytesWritten]; System.arraycopy(buf, 0, out, 0, bytesWritten); return out; }
@Override public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException { AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine()); cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv)); return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher); } }
/** * Given the IV, encrypt the provided data * * @param IV initialization vector * @param message data to be encrypted * @return byte array with the cipher text * @throws RuntimeException */ public byte[] encrypt(final byte[] IV, final byte[] message) throws RuntimeException { AEADParameters aeadParams = new AEADParameters( new KeyParameter(key), TAG_LENGTH, IV, authData); cipher.init(true, aeadParams); byte[] cipherText = newBuffer(cipher.getOutputSize(message.length)); int outputOffset = cipher.processBytes(message, 0, message.length, cipherText, 0); try { cipher.doFinal(cipherText, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Error: ", e); } return cipherText; }
@Override protected void _udpEncrypt(byte[] data, ByteArrayOutputStream stream) throws Exception { ByteBuffer buffer = ByteBuffer.wrap(data); int remaining = buffer.remaining(); buffer.get(encBuffer, 0, remaining); encCipher.init(true, getCipherParameters(true)); encCipher.doFinal( encBuffer, encCipher.processBytes(encBuffer, 0, remaining, encBuffer, 0) ); stream.write(encBuffer, 0, remaining + getTagLength()); }
excessEnd = cipher.doFinal(excess, 0); } catch (InvalidCipherTextException e) { throw new AEADVerificationFailedException(); int outLength = cipher.getUpdateOutputSize(read); if(outLength > length) { byte[] outputTemp = new byte[outLength]; int decryptedBytes = cipher.processBytes(temp, 0, read, outputTemp, 0); assert(decryptedBytes == outLength); System.arraycopy(outputTemp, 0, buf, offset, length); return length; } else { int decryptedBytes = cipher.processBytes(temp, 0, read, buf, offset); if(decryptedBytes > 0) return decryptedBytes;
bufLen = aeadBlockCipher.getOutputSize(updateSize); bufLen = aeadBlockCipher.getUpdateOutputSize(updateSize);
@Override public void close() throws IOException { byte[] output = new byte[cipher.getOutputSize(0)]; try { cipher.doFinal(output, 0); } catch (InvalidCipherTextException e) { // Impossible??? throw new RuntimeException("Impossible: "+e); } out.write(output); out.close(); }
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException { return cipher.processBytes(in, inOff, len, out, outOff); }
@Override public int doFinal(final byte[] out, final int outOff) throws CryptoException { try { return cipherDelegate.doFinal(out, outOff); } catch (InvalidCipherTextException e) { throw new CryptoException("Error finalizing cipher", e); } }
@Override public void write(byte[] buf, int offset, int length) throws IOException { byte[] output = new byte[cipher.getUpdateOutputSize(length)]; cipher.processBytes(buf, offset, length, output, 0); out.write(output); }
@Override public int getOutputSize(final int len) { return cipherDelegate.getOutputSize(len); }
public org.bouncycastle.crypto.BlockCipher getUnderlyingCipher() { return cipher.getUnderlyingCipher(); }
public int getUpdateOutputSize(int len) { return cipher.getUpdateOutputSize(len); }
/** * Given the IV, decrypt the provided data * * @param IV initialization vector * @param cipherText data to be decrypted * @return byte array with the plain text * @throws RuntimeException */ public byte[] decrypt(byte[] IV, byte[] cipherText) throws RuntimeException { AEADParameters aeadParams = new AEADParameters( new KeyParameter(key), TAG_LENGTH, IV, authData); cipher.init(false, aeadParams); byte[] buffer = newByteArray(cipherText); byte[] plainText = newBuffer(cipher.getOutputSize(cipherText.length)); int outputOffset = cipher.processBytes(buffer, 0, buffer.length, plainText, 0); try { cipher.doFinal(plainText, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Error: ", e); } return plainText; }
@Override protected void _udpDecrypt(byte[] data, ByteArrayOutputStream stream) throws Exception { ByteBuffer buffer = ByteBuffer.wrap(data); int remaining = buffer.remaining(); buffer.get(decBuffer, 0, remaining); decCipher.init(false, getCipherParameters(false)); decCipher.doFinal( decBuffer, decCipher.processBytes(decBuffer, 0, remaining, decBuffer, 0) ); stream.write(decBuffer, 0, remaining - getTagLength()); } }
bufLen = aeadBlockCipher.getOutputSize(updateSize); bufLen = aeadBlockCipher.getUpdateOutputSize(updateSize);
@Override public int processBytes(final byte[] in, final int inOff, final int len, final byte[] out, final int outOff) throws CryptoException { try { return cipherDelegate.processBytes(in, inOff, len, out, outOff); } catch (RuntimeException e) { throw new CryptoException("Cipher processing error", e); } }
public int doFinal(byte[] out, int outOff) throws IllegalStateException, BadPaddingException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { if (aeadBadTagConstructor != null) { BadPaddingException aeadBadTag = null; try { aeadBadTag = (BadPaddingException)aeadBadTagConstructor .newInstance(new Object[]{e.getMessage()}); } catch (Exception i) { // Shouldn't happen, but fall through to BadPaddingException } if (aeadBadTag != null) { throw aeadBadTag; } } throw new BadPaddingException(e.getMessage()); } } }
public int getOutputSize(int len) { return cipher.getOutputSize(len); }
protected BaseBlockCipher( AEADBlockCipher engine, boolean fixedIv, int ivLength) { this.baseEngine = engine.getUnderlyingCipher(); this.fixedIv = fixedIv; this.ivLength = ivLength; this.cipher = new AEADGenericBlockCipher(engine); }