Search in sources :

Example 6 with ShortBufferException

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

the class myMac method testMac10.

/**
     * Test for <code>doFinal(byte[] output, int outOffset)</code> method
     * Assertion:
     * throws ShotBufferException when outOffset  is negative or
     * outOffset >= output.length  or when given buffer is small
     */
public void testMac10() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, IllegalStateException, InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac[] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte[] b = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
    byte[] byteA = new byte[b.length];
    SecretKeySpec sks = new SecretKeySpec(b, "SHA1");
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(sks);
        try {
            macs[i].doFinal(null, 10);
            fail("ShortBufferException must be thrown");
        } catch (ShortBufferException e) {
        }
        try {
            macs[i].doFinal(byteA, -4);
            fail("ShortBufferException must be thrown");
        } catch (ShortBufferException e) {
        }
        try {
            macs[i].doFinal(byteA, 10);
            fail("ShortBufferException must be thrown");
        } catch (ShortBufferException e) {
        }
        try {
            macs[i].doFinal(new byte[1], 0);
            fail("ShortBufferException must be thrown");
        } catch (ShortBufferException e) {
        }
        byte[] res = macs[i].doFinal();
        try {
            macs[i].doFinal(new byte[res.length - 1], 0);
            fail("ShortBufferException must be thrown");
        } catch (ShortBufferException e) {
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) Mac(javax.crypto.Mac)

Example 7 with ShortBufferException

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

the class CipherTest method test_doFinal$BI.

public void test_doFinal$BI() 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];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 0, 10);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 3, 8);
    int len = c.doFinal(b1, 0);
    assertEquals(0, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b1, 0, 24);
    try {
        c.doFinal(b, 0);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    c.update(b, 3, 6);
    try {
        c.doFinal(b1, 5);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : 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) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 8 with ShortBufferException

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

the class CipherTest method test_doFinal$BII$B.

public void test_doFinal$BII$B() 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];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b, 0, 10, b1);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10, b1);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len = c.doFinal(b, 0, 16, b1);
    assertEquals(16, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b1, 0, 24, new byte[42]);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.doFinal(b, 3, 6, b1);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : 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) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 9 with ShortBufferException

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

the class ShortBufferExceptionTest method testShortBufferException02.

/**
     * Test for <code>ShortBufferException(String)</code> constructor
     * Assertion: constructs ShortBufferException with detail message msg.
     * Parameter <code>msg</code> is not null.
     */
public void testShortBufferException02() {
    ShortBufferException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new ShortBufferException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

Example 10 with ShortBufferException

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

the class ShortBufferExceptionTest method testShortBufferException03.

/**
     * Test for <code>ShortBufferException(String)</code> constructor
     * Assertion: constructs ShortBufferException when <code>msg</code> is
     * null
     */
public void testShortBufferException03() {
    String msg = null;
    ShortBufferException tE = new ShortBufferException(msg);
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : ShortBufferException(javax.crypto.ShortBufferException)

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