Search in sources :

Example 31 with AlgorithmParameterSpec

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

the class BaseWrapCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
            // try next spec
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 32 with AlgorithmParameterSpec

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

the class OpenSSLCipher method engineInit.

@Override
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    final AlgorithmParameterSpec spec;
    try {
        spec = params.getParameterSpec(IvParameterSpec.class);
    } catch (InvalidParameterSpecException e) {
        throw new InvalidAlgorithmParameterException(e);
    }
    engineInit(opmode, key, spec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 33 with AlgorithmParameterSpec

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

the class KeyPairGeneratorTest method test_getInstance.

public void test_getInstance() throws Exception {
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if (!type.equals("KeyPairGenerator")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            // AndroidKeyStore is tested in CTS.
            if ("AndroidKeyStore".equals(provider.getName())) {
                continue;
            }
            AlgorithmParameterSpec params = null;
            // TODO: detect if we're running in vogar and run the full test
            if ("DH".equals(algorithm)) {
                params = getDHParams();
            }
            try {
                // KeyPairGenerator.getInstance(String)
                KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(algorithm);
                assertEquals(algorithm, kpg1.getAlgorithm());
                if (params != null) {
                    kpg1.initialize(params);
                }
                test_KeyPairGenerator(kpg1);
                // KeyPairGenerator.getInstance(String, Provider)
                KeyPairGenerator kpg2 = KeyPairGenerator.getInstance(algorithm, provider);
                assertEquals(algorithm, kpg2.getAlgorithm());
                assertEquals(provider, kpg2.getProvider());
                if (params != null) {
                    kpg2.initialize(params);
                }
                test_KeyPairGenerator(kpg2);
                // KeyPairGenerator.getInstance(String, String)
                KeyPairGenerator kpg3 = KeyPairGenerator.getInstance(algorithm, provider.getName());
                assertEquals(algorithm, kpg3.getAlgorithm());
                assertEquals(provider, kpg3.getProvider());
                if (params != null) {
                    kpg3.initialize(params);
                }
                test_KeyPairGenerator(kpg3);
            } catch (Exception e) {
                throw new Exception("Problem testing KeyPairGenerator." + algorithm, e);
            }
        }
    }
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) KeyPairGenerator(java.security.KeyPairGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Provider(java.security.Provider)

Example 34 with AlgorithmParameterSpec

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

the class ExemptionMechanismSpiTest method testExemptionMechanismSpi01.

/**
     * Test for <code>ExemptionMechanismSpi</code> constructor Assertion:
     * constructs ExemptionMechanismSpi
     * @throws Exception
     */
public void testExemptionMechanismSpi01() throws Exception {
    Mock_ExemptionMechanismSpi emSpi = new Mock_ExemptionMechanismSpi() {
    };
    int len = MyExemptionMechanismSpi.getLength();
    byte[] bbRes = emSpi.engineGenExemptionBlob();
    assertEquals("Incorrect length", bbRes.length, len);
    assertEquals("Incorrect result", emSpi.engineGenExemptionBlob(new byte[1], len), len);
    assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100));
    Key key = null;
    AlgorithmParameters params = null;
    AlgorithmParameterSpec parSpec = null;
    try {
        emSpi.engineInit(key);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    try {
        emSpi.engineInit(key, params);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    try {
        emSpi.engineInit(key, parSpec);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    key = ((MyExemptionMechanismSpi) emSpi).new tmp1Key("Proba", new byte[0]);
    try {
        emSpi.engineInit(key);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    try {
        emSpi.engineInit(key, params);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    try {
        emSpi.engineInit(key, parSpec);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    key = ((MyExemptionMechanismSpi) emSpi).new tmpKey("Proba", new byte[0]);
    emSpi.engineInit(key);
    emSpi.engineInit(key, AlgorithmParameters.getInstance("DH"));
    emSpi.engineInit(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
    assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100));
}
Also used : RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) Key(java.security.Key) AlgorithmParameters(java.security.AlgorithmParameters)

Example 35 with AlgorithmParameterSpec

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

the class KeyAgreementSpiTest method testKeyAgreementSpi01.

/**
     * Test for <code>KeyAgreementSpi</code> constructor Assertion: constructs
     * KeyAgreementSpi
     */
public void testKeyAgreementSpi01() throws InvalidKeyException, ShortBufferException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    Mock_KeyAgreementSpi kaSpi = new Mock_KeyAgreementSpi();
    assertNull("Not null result", kaSpi.engineDoPhase(null, true));
    try {
        kaSpi.engineDoPhase(null, false);
        fail("IllegalStateException must be thrown");
    } catch (IllegalStateException e) {
    }
    byte[] bb = kaSpi.engineGenerateSecret();
    assertEquals("Length is not 0", bb.length, 0);
    assertEquals("Returned integer is not 0", kaSpi.engineGenerateSecret(new byte[1], 10), -1);
    assertNull("Not null result", kaSpi.engineGenerateSecret("aaa"));
    try {
        kaSpi.engineGenerateSecret("");
        fail("NoSuchAlgorithmException must be thrown");
    } catch (NoSuchAlgorithmException e) {
    }
    Key key = null;
    try {
        kaSpi.engineInit(key, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    AlgorithmParameterSpec params = null;
    try {
        kaSpi.engineInit(key, params, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)186 IvParameterSpec (javax.crypto.spec.IvParameterSpec)59 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)51 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecureRandom (java.security.SecureRandom)27 SecretKey (javax.crypto.SecretKey)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BigInteger (java.math.BigInteger)21 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 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)17 KeyGenerator (javax.crypto.KeyGenerator)17 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)15 IOException (java.io.IOException)14