Search in sources :

Example 1 with ShortBufferException

use of javax.crypto.ShortBufferException in project Signal-Android by WhisperSystems.

the class DecryptingPartInputStream method readFinal.

private int readFinal(byte[] buffer, int offset, int length) throws IOException {
    try {
        int flourish = cipher.doFinal(buffer, offset);
        //mac.update(buffer, offset, flourish);
        byte[] ourMac = mac.doFinal();
        byte[] theirMac = new byte[mac.getMacLength()];
        readFully(theirMac);
        if (!Arrays.equals(ourMac, theirMac))
            throw new IOException("MAC doesn't match! Potential tampering?");
        done = true;
        return flourish;
    } catch (IllegalBlockSizeException e) {
        Log.w(TAG, e);
        throw new IOException("Illegal block size exception!");
    } catch (ShortBufferException e) {
        Log.w(TAG, e);
        throw new IOException("Short buffer exception!");
    } catch (BadPaddingException e) {
        Log.w(TAG, e);
        throw new IOException("Bad padding exception!");
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException)

Example 2 with ShortBufferException

use of javax.crypto.ShortBufferException in project hadoop by apache.

the class TestOpensslCipher method testUpdateArguments.

@Test(timeout = 120000)
public void testUpdateArguments() throws Exception {
    Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null);
    OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding");
    Assert.assertTrue(cipher != null);
    cipher.init(OpensslCipher.ENCRYPT_MODE, key, iv);
    // Require direct buffers
    ByteBuffer input = ByteBuffer.allocate(1024);
    ByteBuffer output = ByteBuffer.allocate(1024);
    try {
        cipher.update(input, output);
        Assert.fail("Input and output buffer should be direct buffer.");
    } catch (IllegalArgumentException e) {
        GenericTestUtils.assertExceptionContains("Direct buffers are required", e);
    }
    // Output buffer length should be sufficient to store output data 
    input = ByteBuffer.allocateDirect(1024);
    output = ByteBuffer.allocateDirect(1000);
    try {
        cipher.update(input, output);
        Assert.fail("Output buffer length should be sufficient " + "to store output data");
    } catch (ShortBufferException e) {
        GenericTestUtils.assertExceptionContains("Output buffer is not sufficient", e);
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with ShortBufferException

use of javax.crypto.ShortBufferException in project android_frameworks_base by ResurrectionRemix.

the class AndroidKeyStoreCipherSpiBase method engineUpdate.

@Override
protected final int engineUpdate(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    if (input == null) {
        throw new NullPointerException("input == null");
    }
    if (output == null) {
        throw new NullPointerException("output == null");
    }
    int inputSize = input.remaining();
    byte[] outputArray;
    if (input.hasArray()) {
        outputArray = engineUpdate(input.array(), input.arrayOffset() + input.position(), inputSize);
        input.position(input.position() + inputSize);
    } else {
        byte[] inputArray = new byte[inputSize];
        input.get(inputArray);
        outputArray = engineUpdate(inputArray, 0, inputSize);
    }
    int outputSize = (outputArray != null) ? outputArray.length : 0;
    if (outputSize > 0) {
        int outputBufferAvailable = output.remaining();
        try {
            output.put(outputArray);
        } catch (BufferOverflowException e) {
            throw new ShortBufferException("Output buffer too small. Produced: " + outputSize + ", available: " + outputBufferAvailable);
        }
    }
    return outputSize;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) BufferOverflowException(java.nio.BufferOverflowException)

Example 4 with ShortBufferException

use of javax.crypto.ShortBufferException in project XobotOS by xamarin.

the class JCEBlockCipher method engineDoFinal.

protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    // BEGIN android-note
    // added ShortBufferException to the throws statement
    // END android-note
    int len = 0;
    // BEGIN android-added
    int outputLen = cipher.getOutputSize(inputLen);
    if (outputLen + outputOffset > output.length) {
        throw new ShortBufferException("need at least " + outputLen + " bytes");
    }
    if (inputLen != 0) {
        len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
    }
    try {
        return (len + cipher.doFinal(output, outputOffset + len));
    } catch (DataLengthException e) {
        throw new IllegalBlockSizeException(e.getMessage());
    } catch (InvalidCipherTextException e) {
        throw new BadPaddingException(e.getMessage());
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Example 5 with ShortBufferException

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

the class TestNonexpanding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        // Comparison
        if (!(plainText.length == cipherText.length)) {
            // authentication tag and cipher text.
            if (mo.equalsIgnoreCase("GCM")) {
                GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
                int cipherTextLength = cipherText.length - spec.getTLen() / 8;
                if (plainText.length == cipherTextLength) {
                    return;
                }
            }
            System.out.println("Original length: " + plainText.length);
            System.out.println("Cipher text length: " + cipherText.length);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

ShortBufferException (javax.crypto.ShortBufferException)69 BadPaddingException (javax.crypto.BadPaddingException)24 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 Cipher (javax.crypto.Cipher)22 InvalidKeyException (java.security.InvalidKeyException)17 ByteBuffer (java.nio.ByteBuffer)15 IvParameterSpec (javax.crypto.spec.IvParameterSpec)15 SecretKeySpec (javax.crypto.spec.SecretKeySpec)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)12 BufferOverflowException (java.nio.BufferOverflowException)10 IOException (java.io.IOException)9 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)7 SecretKey (javax.crypto.SecretKey)6 NoSuchProviderException (java.security.NoSuchProviderException)5 Random (java.util.Random)5 KeyGenerator (javax.crypto.KeyGenerator)5 MessageDigest (java.security.MessageDigest)4