Search in sources :

Example 11 with CipherInputStream

use of javax.crypto.CipherInputStream in project ExoPlayer by google.

the class Aes128DataSource method open.

@Override
public long open(DataSpec dataSpec) throws IOException {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
    Key cipherKey = new SecretKeySpec(encryptionKey, "AES");
    AlgorithmParameterSpec cipherIV = new IvParameterSpec(encryptionIv);
    try {
        cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherIV);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }
    cipherInputStream = new CipherInputStream(new DataSourceInputStream(upstream, dataSpec), cipher);
    return C.LENGTH_UNSET;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 12 with CipherInputStream

use of javax.crypto.CipherInputStream in project wycheproof by google.

the class CipherInputStreamTest method testDecrypt.

/** JDK-8016249: CipherInputStream in decrypt mode fails on close with AEAD ciphers */
@SuppressWarnings("InsecureCryptoUsage")
public void testDecrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        InputStream is = new ByteArrayInputStream(t.ct);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] result = new byte[t.pt.length];
        int totalLength = 0;
        int length = 0;
        do {
            length = cis.read(result, totalLength, result.length - totalLength);
            if (length > 0) {
                totalLength += length;
            }
        } while (length >= 0 && totalLength != result.length);
        assertEquals(-1, cis.read());
        cis.close();
        assertEquals(TestUtil.bytesToHex(t.pt), TestUtil.bytesToHex(result));
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Example 13 with CipherInputStream

use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.

the class CipherInputStreamExceptions method gcm_oneReadByte.

/*
     * Verify noexception thrown when 1 byte is read from a GCM stream
     * and then closed
     * This test:
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Read one byte from the stream, expect no exception thrown.
     *   4) Close stream,expect no exception thrown.
     */
static void gcm_oneReadByte() throws Exception {
    System.out.println("Running gcm_oneReadByte test");
    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    try {
        in.read();
        System.out.println("  Pass.");
    } catch (Exception e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) RuntimeException(java.lang.RuntimeException) AEADBadTagException(javax.crypto.AEADBadTagException) Exception(java.lang.Exception)

Example 14 with CipherInputStream

use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.

the class CipherInputStreamExceptions method gcm_AEADBadTag.

/* Full read stream, check that getMoreData() is throwing an exception
     * This test
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Changes the last byte to invalidate the authetication tag.
     *   3) Fully reads CipherInputStream to decrypt the message and closes
     */
static void gcm_AEADBadTag() throws Exception {
    Cipher c;
    byte[] read = new byte[200];
    System.out.println("Running gcm_AEADBadTag");
    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    try {
        int size = in.read(read);
        throw new RuntimeException("Fail: CipherInputStream.read() " + "returned " + size + " and didn't throw an exception.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    } finally {
        in.close();
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Throwable(java.lang.Throwable) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) AEADBadTagException(javax.crypto.AEADBadTagException)

Example 15 with CipherInputStream

use of javax.crypto.CipherInputStream in project jdk8u_jdk by JetBrains.

the class CipherInputStreamExceptions method gcm_oneReadByteCorrupt.

/*
     * Verify exception thrown when 1 byte is read from a corrupted GCM stream
     * and then closed
     * This test:
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Changes the last byte to invalidate the authetication tag.
     *   3) Read one byte from the stream, expect exception thrown.
     *   4) Close stream,expect no exception thrown.
     */
static void gcm_oneReadByteCorrupt() throws Exception {
    System.out.println("Running gcm_oneReadByteCorrupt test");
    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    try {
        in.read();
        System.out.println("  Fail. No exception thrown.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    }
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Throwable(java.lang.Throwable) IOException(java.io.IOException) AEADBadTagException(javax.crypto.AEADBadTagException)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)54 Cipher (javax.crypto.Cipher)31 ByteArrayInputStream (java.io.ByteArrayInputStream)18 IOException (java.io.IOException)17 InputStream (java.io.InputStream)14 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 Key (java.security.Key)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 IvParameterSpec (javax.crypto.spec.IvParameterSpec)6 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 BufferedInputStream (java.io.BufferedInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 Throwable (java.lang.Throwable)3 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3