Search in sources :

Example 41 with ShortBufferException

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

the class CipherTest method testUpdatebyteArrayintintbyteArrayint.

/*
     * Class under test for int update(byte[], int, int, byte[], int)
     */
public void testUpdatebyteArrayintintbyteArrayint() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    byte[] b1 = new byte[6];
    Cipher c = Cipher.getInstance("DESede");
    try {
        c.update(b, 0, 10, b1, 5);
        fail();
    } catch (IllegalStateException expected) {
    }
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.update(b, 0, 10, b1, 5);
        fail();
    } catch (ShortBufferException expected) {
    }
    b1 = new byte[30];
    c.update(b, 0, 10, b1, 5);
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher)

Example 42 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 43 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 44 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 45 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)

Aggregations

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