Search in sources :

Example 16 with ShortBufferException

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

the class AndroidKeyStoreCipherSpiBase method engineDoFinal.

@Override
protected final int engineDoFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    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 = engineDoFinal(input.array(), input.arrayOffset() + input.position(), inputSize);
        input.position(input.position() + inputSize);
    } else {
        byte[] inputArray = new byte[inputSize];
        input.get(inputArray);
        outputArray = engineDoFinal(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 17 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)

Example 18 with ShortBufferException

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

the class AndroidKeyStoreCipherSpiBase method engineDoFinal.

@Override
protected final int engineDoFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    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 = engineDoFinal(input.array(), input.arrayOffset() + input.position(), inputSize);
        input.position(input.position() + inputSize);
    } else {
        byte[] inputArray = new byte[inputSize];
        input.get(inputArray);
        outputArray = engineDoFinal(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 19 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 20 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)

Aggregations

ShortBufferException (javax.crypto.ShortBufferException)46 Cipher (javax.crypto.Cipher)14 BadPaddingException (javax.crypto.BadPaddingException)13 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)13 BufferOverflowException (java.nio.BufferOverflowException)10 ByteBuffer (java.nio.ByteBuffer)9 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)9 IvParameterSpec (javax.crypto.spec.IvParameterSpec)9 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)7 SecretKey (javax.crypto.SecretKey)6 InvalidKeyException (java.security.InvalidKeyException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 Random (java.util.Random)5 KeyGenerator (javax.crypto.KeyGenerator)5 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IOException (java.io.IOException)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)2