Search in sources :

Example 51 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class CipherTest method test_.

public void test_() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.update(b, 0, 10, b1);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    c.update(b, 0, 16, b1);
    b1 = new byte[3];
    try {
        c.update(b, 3, 15, b1);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher)

Example 52 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class CipherTest method test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer.

public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    ByteBuffer bInput = ByteBuffer.allocate(64);
    ByteBuffer bOutput = ByteBuffer.allocate(64);
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput.put(b, 0, 10);
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput = ByteBuffer.allocate(16);
    bInput.put(b, 0, 16);
    int len = c.doFinal(bInput, bOutput);
    assertEquals(0, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput = ByteBuffer.allocate(64);
    try {
        c.doFinal(bOutput, bInput);
        fail();
    } catch (BadPaddingException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    try {
        c.doFinal(bInput, bInput);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    try {
        c.doFinal(bInput, bOutput.asReadOnlyBuffer());
        fail();
    } catch (ReadOnlyBufferException expected) {
    }
    bInput.rewind();
    bInput.put(b, 0, 16);
    bOutput = ByteBuffer.allocate(8);
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) ByteBuffer(java.nio.ByteBuffer) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 53 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class myCipherSpi method testCipherSpi06.

/**
     * Test for <code>engineDoFinal(ByteBuffer, ByteBuffer)</code> method
     * Assertions:
     * throws NullPointerException if one of these buffers is null;
     * throws ShortBufferException is there is no space in output to hold result
     */
public void testCipherSpi06() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException {
    Mock_CipherSpi cSpi = new Mock_CipherSpi();
    int len = 10;
    byte[] bbuf = new byte[len];
    for (int i = 0; i < bbuf.length; i++) {
        bbuf[i] = (byte) i;
    }
    ByteBuffer bb1 = ByteBuffer.wrap(bbuf);
    ByteBuffer bbNull = null;
    try {
        cSpi.engineDoFinal(bbNull, bb1);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        cSpi.engineDoFinal(bb1, bbNull);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    ByteBuffer bb2 = ByteBuffer.allocate(len);
    bb1.position(bb1.limit());
    assertEquals("Incorrect result", 0, cSpi.engineDoFinal(bb1, bb2));
    bb1.position(0);
    bb2.position(len - 2);
    try {
        cSpi.engineDoFinal(bb1, bb2);
        fail("ShortBufferException must be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
    } catch (ShortBufferException e) {
    }
    int pos = 5;
    bb1.position(pos);
    bb2.position(0);
    assertTrue("Incorrect result", cSpi.engineDoFinal(bb1, bb2) > 0);
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) ByteBuffer(java.nio.ByteBuffer)

Example 54 with ShortBufferException

use of javax.crypto.ShortBufferException in project robovm by robovm.

the class myCipherSpi method testCipherSpi05.

/**
     * Test for <code>engineUpdate(ByteBuffer, ByteBuffer)</code> method
     * Assertions:
     * throws NullPointerException if one of these buffers is null;
     * throws ShortBufferException is there is no space in output to hold result
     */
public void testCipherSpi05() throws ShortBufferException {
    Mock_CipherSpi cSpi = new Mock_CipherSpi();
    byte[] bb = { (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6, (byte) 7, (byte) 8, (byte) 9, (byte) 10 };
    int pos = 5;
    int len = bb.length;
    ByteBuffer bbNull = null;
    ByteBuffer bb1 = ByteBuffer.allocate(len);
    bb1.put(bb);
    bb1.position(0);
    try {
        cSpi.engineUpdate(bbNull, bb1);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        cSpi.engineUpdate(bb1, bbNull);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    ByteBuffer bb2 = ByteBuffer.allocate(bb.length);
    bb1.position(len);
    assertEquals("Incorrect number of stored bytes", 0, cSpi.engineUpdate(bb1, bb2));
    bb1.position(0);
    bb2.position(len - 2);
    try {
        cSpi.engineUpdate(bb1, bb2);
        fail("ShortBufferException bust be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
    } catch (ShortBufferException e) {
    }
    bb1.position(10);
    bb2.position(0);
    assertTrue("Incorrect number of stored bytes", cSpi.engineUpdate(bb1, bb2) > 0);
    bb1.position(bb.length);
    cSpi.engineUpdate(bb1, bb2);
    bb1.position(pos);
    bb2.position(0);
    int res = cSpi.engineUpdate(bb1, bb2);
    assertTrue("Incorrect result", res > 0);
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) ByteBuffer(java.nio.ByteBuffer)

Example 55 with ShortBufferException

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

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)

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