Search in sources :

Example 46 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class CipherTest method testDoFinalbyteArrayintintbyteArrayint.

/*
     * Class under test for int doFinal(byte[], int, int, byte[], int)
     */
public void testDoFinalbyteArrayintintbyteArrayint() 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, 5);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10, b1, 5);
        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, 0);
    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], 0);
        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, 5);
        fail();
    } catch (IllegalBlockSizeException maybeExpected) {
        assertTrue(StandardNames.IS_RI);
    } catch (ShortBufferException maybeExpected) {
        assertFalse(StandardNames.IS_RI);
    }
}
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 47 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class KeyAgreementTest method testInit04.

/**
     * Test for the methods:
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * <code>generateSecret()</code>
     * Assertions: initializes KeyAgreement and returns byte array
     */
public void testInit04() throws Exception, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
    AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    byte[] bbRes1;
    byte[] bbRes2;
    byte[] bbRes3;
    SecureRandom randomNull = null;
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kAgs.length; i++) {
        kAgs[i].init(privKey, dhPs);
        kAgs[i].doPhase(publKey, true);
        bbRes1 = kAgs[i].generateSecret();
        kAgs[i].init(privKey, dhPs, random);
        kAgs[i].doPhase(publKey, true);
        bbRes2 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes2.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
        }
        kAgs[i].init(privKey, dhPs, randomNull);
        kAgs[i].doPhase(publKey, true);
        bbRes3 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes3.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
        }
        try {
            kAgs[i].init(publKey, dhPs, random);
            fail("InvalidKeyException expected");
        } catch (InvalidKeyException e) {
        //expected
        }
        try {
            kAgs[i].init(privKey, aps, random);
            fail("InvalidAlgorithmParameterException expected");
        } catch (InvalidAlgorithmParameterException e) {
        //expected
        }
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 48 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class APSpecSpi method testKeyGeneratorSpi01.

/**
     * Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
     * KeyGeneratorSpi
     */
public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
    Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
    assertNull("Not null result", kgSpi.engineGenerateKey());
    try {
        kgSpi.engineInit(77, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    try {
        kgSpi.engineInit(new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    AlgorithmParameterSpec aps = null;
    try {
        kgSpi.engineInit(aps, new SecureRandom());
        fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
    } catch (InvalidAlgorithmParameterException e) {
    }
    aps = new APSpecSpi();
    kgSpi.engineInit(aps, new SecureRandom());
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 49 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class myKeyGenerator method testKeyGenerator.

/**
     * Test for <code>KeyGenerator</code> constructor Assertion: returns
     * KeyGenerator object
     */
public void testKeyGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGeneratorSpi spi = new MyKeyGeneratorSpi();
    KeyGenerator keyG = new myKeyGenerator(spi, defaultProvider, defaultAlgorithm);
    assertEquals("Incorrect algorithm", keyG.getAlgorithm(), defaultAlgorithm);
    assertEquals("Incorrect provider", keyG.getProvider(), defaultProvider);
    AlgorithmParameterSpec params = null;
    int keysize = 0;
    try {
        keyG.init(params, null);
        fail("InvalidAlgorithmParameterException must be thrown");
    } catch (InvalidAlgorithmParameterException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    keyG = new myKeyGenerator(null, null, null);
    assertNull("Algorithm must be null", keyG.getAlgorithm());
    assertNull("Provider must be null", keyG.getProvider());
    try {
        keyG.init(params, null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("NullPointerException or InvalidParameterException must be thrown");
    } catch (InvalidParameterException e) {
    } catch (NullPointerException e) {
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGeneratorSpi(javax.crypto.KeyGeneratorSpi) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 50 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class myKeyGenerator method testInitParams.

/*
     * Test for <code>init(AlgorithmParameterSpec params)</code> and
     * <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
     * Assertion: throws InvalidAlgorithmParameterException when params is null
     */
public void testInitParams() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator[] kgs = createKGs();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kgs.length; i++) {
        try {
            kgs[i].init(aps);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            kgs[i].init(aps, new SecureRandom());
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)173 IvParameterSpec (javax.crypto.spec.IvParameterSpec)56 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)49 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecretKey (javax.crypto.SecretKey)27 SecureRandom (java.security.SecureRandom)24 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 BigInteger (java.math.BigInteger)19 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)16 KeyGenerator (javax.crypto.KeyGenerator)16 IOException (java.io.IOException)14 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)14