Search in sources :

Example 1 with NativeGCMCipher

use of com.facebook.crypto.cipher.NativeGCMCipher in project conceal by facebook.

the class CryptoAlgoGcm method wrap.

@Override
public InputStream wrap(InputStream is, Entity entity) throws IOException, CryptoInitializationException, KeyChainException {
    byte cryptoVersion = (byte) is.read();
    byte cipherID = (byte) is.read();
    Assertions.checkArgumentForIO(cryptoVersion == VersionCodes.CIPHER_SERIALIZATION_VERSION, "Unexpected crypto version " + cryptoVersion);
    Assertions.checkArgumentForIO(cipherID == mConfig.cipherId, "Unexpected cipher ID " + cipherID);
    byte[] iv = new byte[mConfig.ivLength];
    // if iv is not fully read EOFException will be thrown
    new DataInputStream(is).readFully(iv);
    NativeGCMCipher gcmCipher = new NativeGCMCipher(mNativeLibrary);
    gcmCipher.decryptInit(mKeyChain.getCipherKey(), iv);
    byte[] entityBytes = entity.getBytes();
    computeCipherAad(gcmCipher, cryptoVersion, cipherID, entityBytes);
    return new NativeGCMCipherInputStream(is, gcmCipher, mConfig.tagLength);
}
Also used : NativeGCMCipherInputStream(com.facebook.crypto.streams.NativeGCMCipherInputStream) NativeGCMCipher(com.facebook.crypto.cipher.NativeGCMCipher) DataInputStream(java.io.DataInputStream)

Example 2 with NativeGCMCipher

use of com.facebook.crypto.cipher.NativeGCMCipher in project conceal by facebook.

the class NativeGCMCipherHelper method getOutputStream.

public OutputStream getOutputStream(OutputStream cipherStream, byte[] encryptBuffer) throws IOException, CryptoInitializationException {
    NativeGCMCipher gcmCipher = new NativeGCMCipher(mNativeCryptoLibrary);
    gcmCipher.encryptInit(mKey, mIv);
    cipherStream.write(mIv);
    return new NativeGCMCipherOutputStream(cipherStream, gcmCipher, encryptBuffer, mTagLength);
}
Also used : NativeGCMCipherOutputStream(com.facebook.crypto.streams.NativeGCMCipherOutputStream) NativeGCMCipher(com.facebook.crypto.cipher.NativeGCMCipher)

Example 3 with NativeGCMCipher

use of com.facebook.crypto.cipher.NativeGCMCipher in project conceal by facebook.

the class NativeGCMCipherHelper method getInputStream.

public InputStream getInputStream(InputStream cipherStream) throws IOException, CryptoInitializationException {
    byte[] iv = new byte[CryptoConfig.KEY_128.ivLength];
    cipherStream.read(iv);
    NativeGCMCipher gcmCipher = new NativeGCMCipher(mNativeCryptoLibrary);
    gcmCipher.decryptInit(mKey, iv);
    return new NativeGCMCipherInputStream(cipherStream, gcmCipher, mTagLength);
}
Also used : NativeGCMCipherInputStream(com.facebook.crypto.streams.NativeGCMCipherInputStream) NativeGCMCipher(com.facebook.crypto.cipher.NativeGCMCipher)

Example 4 with NativeGCMCipher

use of com.facebook.crypto.cipher.NativeGCMCipher in project conceal by facebook.

the class CryptoAlgoGcm method wrap.

@Override
public OutputStream wrap(OutputStream cipherStream, Entity entity, byte[] buffer) throws IOException, CryptoInitializationException, KeyChainException {
    cipherStream.write(VersionCodes.CIPHER_SERIALIZATION_VERSION);
    cipherStream.write(mConfig.cipherId);
    byte[] iv = mKeyChain.getNewIV();
    NativeGCMCipher gcmCipher = new NativeGCMCipher(mNativeLibrary);
    gcmCipher.encryptInit(mKeyChain.getCipherKey(), iv);
    cipherStream.write(iv);
    byte[] entityBytes = entity.getBytes();
    computeCipherAad(gcmCipher, VersionCodes.CIPHER_SERIALIZATION_VERSION, mConfig.cipherId, entityBytes);
    return new NativeGCMCipherOutputStream(cipherStream, gcmCipher, buffer, mConfig.tagLength);
}
Also used : NativeGCMCipherOutputStream(com.facebook.crypto.streams.NativeGCMCipherOutputStream) NativeGCMCipher(com.facebook.crypto.cipher.NativeGCMCipher)

Aggregations

NativeGCMCipher (com.facebook.crypto.cipher.NativeGCMCipher)4 NativeGCMCipherInputStream (com.facebook.crypto.streams.NativeGCMCipherInputStream)2 NativeGCMCipherOutputStream (com.facebook.crypto.streams.NativeGCMCipherOutputStream)2 DataInputStream (java.io.DataInputStream)1