Search in sources :

Example 6 with RSAKeyGenParameterSpec

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

the class OpenSSLRSAKeyPairGenerator method initialize.

@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof RSAKeyGenParameterSpec)) {
        throw new InvalidAlgorithmParameterException("Only RSAKeyGenParameterSpec supported");
    }
    RSAKeyGenParameterSpec spec = (RSAKeyGenParameterSpec) params;
    final BigInteger publicExponent = spec.getPublicExponent();
    if (publicExponent != null) {
        this.publicExponent = publicExponent.toByteArray();
    }
    this.modulusBits = spec.getKeysize();
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger)

Example 7 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec 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 8 with RSAKeyGenParameterSpec

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

the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec.

public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
    };
    Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
    em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
    try {
        em.init(key, (AlgorithmParameterSpec) null);
        fail("InvalidAlgorithmParameterException expected");
    } catch (InvalidAlgorithmParameterException e) {
    //expected
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    key = kg.generateKey();
    try {
        em.init(null, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //expected
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider) BigInteger(java.math.BigInteger) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key)

Example 9 with RSAKeyGenParameterSpec

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

the class CipherTest method test_initWithKeyAlgorithmParameterSpecSecureRandom.

/**
     * javax.crypto.Cipher#init(int, java.security.Key,
     *        java.security.spec.AlgorithmParameterSpec,
     *        java.security.SecureRandom)
     */
public void test_initWithKeyAlgorithmParameterSpecSecureRandom() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
    byte[] cipherIV = cipher.getIV();
    assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
        fail();
    } catch (InvalidKeyException expected) {
    }
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 10 with RSAKeyGenParameterSpec

use of java.security.spec.RSAKeyGenParameterSpec 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)

Aggregations

RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)32 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)16 BigInteger (java.math.BigInteger)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 KeyPair (java.security.KeyPair)7 KeyPairGeneratorSpec (android.security.KeyPairGeneratorSpec)5 ExportResult (android.security.keymaster.ExportResult)5 ASN1Integer (com.android.org.bouncycastle.asn1.ASN1Integer)5 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)5 DERInteger (com.android.org.bouncycastle.asn1.DERInteger)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InvalidKeyException (java.security.InvalidKeyException)5 PrivateKey (java.security.PrivateKey)5 ProviderException (java.security.ProviderException)5 PublicKey (java.security.PublicKey)5 Certificate (java.security.cert.Certificate)5 CertificateFactory (java.security.cert.CertificateFactory)5 X509Certificate (java.security.cert.X509Certificate)5 ECKey (java.security.interfaces.ECKey)5 ECPublicKey (java.security.interfaces.ECPublicKey)5