Search in sources :

Example 1 with AEADBlockCipher

use of org.bouncycastle.crypto.modes.AEADBlockCipher in project Conversations by siacs.

the class AbstractConnectionManager method createOutputStream.

private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
    FileOutputStream os;
    try {
        os = new FileOutputStream(file, append);
        if (file.getKey() == null) {
            return os;
        }
    } catch (FileNotFoundException e) {
        return null;
    }
    try {
        if (gcm) {
            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
            cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
            return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
        } else {
            IvParameterSpec ips = new IvParameterSpec(file.getIv());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
            Log.d(Config.LOGTAG, "opening encrypted output stream");
            return new CipherOutputStream(os, cipher);
        }
    } catch (InvalidKeyException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (NoSuchPaddingException e) {
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        return null;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) FileNotFoundException(java.io.FileNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) FileOutputStream(java.io.FileOutputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 2 with AEADBlockCipher

use of org.bouncycastle.crypto.modes.AEADBlockCipher in project syncany by syncany.

the class AesGcmCipherSpec method newCipherInputStream.

@Override
public InputStream newCipherInputStream(InputStream underlyingInputStream, byte[] secretKey, byte[] iv) throws CipherException {
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
    return new org.bouncycastle.crypto.io.CipherInputStream(underlyingInputStream, cipher);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 3 with AEADBlockCipher

use of org.bouncycastle.crypto.modes.AEADBlockCipher in project syncany by syncany.

the class TwofishGcmCipherSpec method newCipherInputStream.

@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);
}
Also used : AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) TwofishEngine(org.bouncycastle.crypto.engines.TwofishEngine) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 4 with AEADBlockCipher

use of org.bouncycastle.crypto.modes.AEADBlockCipher in project Conversations by siacs.

the class AbstractConnectionManager method createInputStream.

public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
    FileInputStream is;
    int size;
    is = new FileInputStream(file);
    size = (int) file.getSize();
    if (file.getKey() == null) {
        return new Pair<InputStream, Integer>(is, size);
    }
    try {
        if (gcm) {
            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
            cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
            InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
            return new Pair<>(cis, cipher.getOutputSize(size));
        } else {
            IvParameterSpec ips = new IvParameterSpec(file.getIv());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
            Log.d(Config.LOGTAG, "opening encrypted input stream");
            final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
            return new Pair<InputStream, Integer>(new CipherInputStream(is, cipher), s);
        }
    } catch (InvalidKeyException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (NoSuchPaddingException e) {
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        return null;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FileInputStream(java.io.FileInputStream) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) Pair(android.util.Pair)

Example 5 with AEADBlockCipher

use of org.bouncycastle.crypto.modes.AEADBlockCipher in project syncany by syncany.

the class AesGcmCipherSpec method newCipherOutputStream.

@Override
public OutputStream newCipherOutputStream(OutputStream underlyingOutputStream, byte[] secretKey, byte[] iv) throws CipherException {
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(secretKey), MAC_SIZE, iv));
    return new org.bouncycastle.crypto.io.CipherOutputStream(underlyingOutputStream, cipher);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Aggregations

AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)8 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)8 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)8 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)8 AESEngine (org.bouncycastle.crypto.engines.AESEngine)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Cipher (javax.crypto.Cipher)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 TwofishEngine (org.bouncycastle.crypto.engines.TwofishEngine)2 InvalidCipherTextIOException (org.bouncycastle.crypto.io.InvalidCipherTextIOException)2 Test (org.junit.Test)2 Pair (android.util.Pair)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1