Search in sources :

Example 36 with ECGenParameterSpec

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

the class ECGenParameterSpecTest method testGetName.

/**
     * Test for <code>getName()</code> method<br>
     *
     * Assertion: returns the <code>name</code>
     */
public final void testGetName() {
    String name = "someName";
    ECGenParameterSpec ps = new ECGenParameterSpec(name);
    assertEquals(name, ps.getName());
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec)

Example 37 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreKeyPairGeneratorSpi method initAlgorithmSpecificParameters.

private void initAlgorithmSpecificParameters() throws InvalidAlgorithmParameterException {
    AlgorithmParameterSpec algSpecificSpec = mSpec.getAlgorithmParameterSpec();
    switch(mKeymasterAlgorithm) {
        case KeymasterDefs.KM_ALGORITHM_RSA:
            {
                BigInteger publicExponent = null;
                if (algSpecificSpec instanceof RSAKeyGenParameterSpec) {
                    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) algSpecificSpec;
                    if (mKeySizeBits == -1) {
                        mKeySizeBits = rsaSpec.getKeysize();
                    } else if (mKeySizeBits != rsaSpec.getKeysize()) {
                        throw new InvalidAlgorithmParameterException("RSA key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + rsaSpec.getKeysize());
                    }
                    publicExponent = rsaSpec.getPublicExponent();
                } else if (algSpecificSpec != null) {
                    throw new InvalidAlgorithmParameterException("RSA may only use RSAKeyGenParameterSpec");
                }
                if (publicExponent == null) {
                    publicExponent = RSAKeyGenParameterSpec.F4;
                }
                if (publicExponent.compareTo(BigInteger.ZERO) < 1) {
                    throw new InvalidAlgorithmParameterException("RSA public exponent must be positive: " + publicExponent);
                }
                if (publicExponent.compareTo(KeymasterArguments.UINT64_MAX_VALUE) > 0) {
                    throw new InvalidAlgorithmParameterException("Unsupported RSA public exponent: " + publicExponent + ". Maximum supported value: " + KeymasterArguments.UINT64_MAX_VALUE);
                }
                mRSAPublicExponent = publicExponent;
                break;
            }
        case KeymasterDefs.KM_ALGORITHM_EC:
            if (algSpecificSpec instanceof ECGenParameterSpec) {
                ECGenParameterSpec ecSpec = (ECGenParameterSpec) algSpecificSpec;
                String curveName = ecSpec.getName();
                Integer ecSpecKeySizeBits = SUPPORTED_EC_NIST_CURVE_NAME_TO_SIZE.get(curveName.toLowerCase(Locale.US));
                if (ecSpecKeySizeBits == null) {
                    throw new InvalidAlgorithmParameterException("Unsupported EC curve name: " + curveName + ". Supported: " + SUPPORTED_EC_NIST_CURVE_NAMES);
                }
                if (mKeySizeBits == -1) {
                    mKeySizeBits = ecSpecKeySizeBits;
                } else if (mKeySizeBits != ecSpecKeySizeBits) {
                    throw new InvalidAlgorithmParameterException("EC key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + ecSpecKeySizeBits);
                }
            } else if (algSpecificSpec != null) {
                throw new InvalidAlgorithmParameterException("EC may only use ECGenParameterSpec");
            }
            break;
        default:
            throw new ProviderException("Unsupported algorithm: " + mKeymasterAlgorithm);
    }
}
Also used : BigInteger(java.math.BigInteger) ASN1Integer(com.android.org.bouncycastle.asn1.ASN1Integer) DERInteger(com.android.org.bouncycastle.asn1.DERInteger) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ProviderException(java.security.ProviderException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 38 with ECGenParameterSpec

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

the class KeyPairGeneratorTest method test_KeyPairGenerator.

private void test_KeyPairGenerator(KeyPairGenerator kpg) throws Exception {
    // without a call to initialize
    test_KeyPair(kpg, kpg.genKeyPair());
    test_KeyPair(kpg, kpg.generateKeyPair());
    String algorithm = kpg.getAlgorithm();
    List<Integer> keySizes = getKeySizes(algorithm);
    for (int keySize : keySizes) {
        kpg.initialize(keySize);
        test_KeyPair(kpg, kpg.genKeyPair());
        test_KeyPair(kpg, kpg.generateKeyPair());
        kpg.initialize(keySize, (SecureRandom) null);
        test_KeyPair(kpg, kpg.genKeyPair());
        test_KeyPair(kpg, kpg.generateKeyPair());
        kpg.initialize(keySize, new SecureRandom());
        test_KeyPair(kpg, kpg.genKeyPair());
        test_KeyPair(kpg, kpg.generateKeyPair());
    }
    if (("EC".equals(algorithm)) || ("ECDH".equals(algorithm)) || ("ECDSA".equals(algorithm))) {
        for (String curveName : EC_NAMED_CURVES) {
            kpg.initialize(new ECGenParameterSpec(curveName));
            test_KeyPair(kpg, kpg.genKeyPair());
            test_KeyPair(kpg, kpg.generateKeyPair());
            kpg.initialize(new ECGenParameterSpec(curveName), (SecureRandom) null);
            test_KeyPair(kpg, kpg.genKeyPair());
            test_KeyPair(kpg, kpg.generateKeyPair());
            kpg.initialize(new ECGenParameterSpec(curveName), new SecureRandom());
            test_KeyPair(kpg, kpg.genKeyPair());
            test_KeyPair(kpg, kpg.generateKeyPair());
        }
    }
}
Also used : BigInteger(java.math.BigInteger) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SecureRandom(java.security.SecureRandom)

Example 39 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project android_frameworks_base by ResurrectionRemix.

the class AndroidKeyStoreKeyPairGeneratorSpi method initAlgorithmSpecificParameters.

private void initAlgorithmSpecificParameters() throws InvalidAlgorithmParameterException {
    AlgorithmParameterSpec algSpecificSpec = mSpec.getAlgorithmParameterSpec();
    switch(mKeymasterAlgorithm) {
        case KeymasterDefs.KM_ALGORITHM_RSA:
            {
                BigInteger publicExponent = null;
                if (algSpecificSpec instanceof RSAKeyGenParameterSpec) {
                    RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec) algSpecificSpec;
                    if (mKeySizeBits == -1) {
                        mKeySizeBits = rsaSpec.getKeysize();
                    } else if (mKeySizeBits != rsaSpec.getKeysize()) {
                        throw new InvalidAlgorithmParameterException("RSA key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + rsaSpec.getKeysize());
                    }
                    publicExponent = rsaSpec.getPublicExponent();
                } else if (algSpecificSpec != null) {
                    throw new InvalidAlgorithmParameterException("RSA may only use RSAKeyGenParameterSpec");
                }
                if (publicExponent == null) {
                    publicExponent = RSAKeyGenParameterSpec.F4;
                }
                if (publicExponent.compareTo(BigInteger.ZERO) < 1) {
                    throw new InvalidAlgorithmParameterException("RSA public exponent must be positive: " + publicExponent);
                }
                if (publicExponent.compareTo(KeymasterArguments.UINT64_MAX_VALUE) > 0) {
                    throw new InvalidAlgorithmParameterException("Unsupported RSA public exponent: " + publicExponent + ". Maximum supported value: " + KeymasterArguments.UINT64_MAX_VALUE);
                }
                mRSAPublicExponent = publicExponent;
                break;
            }
        case KeymasterDefs.KM_ALGORITHM_EC:
            if (algSpecificSpec instanceof ECGenParameterSpec) {
                ECGenParameterSpec ecSpec = (ECGenParameterSpec) algSpecificSpec;
                String curveName = ecSpec.getName();
                Integer ecSpecKeySizeBits = SUPPORTED_EC_NIST_CURVE_NAME_TO_SIZE.get(curveName.toLowerCase(Locale.US));
                if (ecSpecKeySizeBits == null) {
                    throw new InvalidAlgorithmParameterException("Unsupported EC curve name: " + curveName + ". Supported: " + SUPPORTED_EC_NIST_CURVE_NAMES);
                }
                if (mKeySizeBits == -1) {
                    mKeySizeBits = ecSpecKeySizeBits;
                } else if (mKeySizeBits != ecSpecKeySizeBits) {
                    throw new InvalidAlgorithmParameterException("EC key size must match " + " between " + mSpec + " and " + algSpecificSpec + ": " + mKeySizeBits + " vs " + ecSpecKeySizeBits);
                }
            } else if (algSpecificSpec != null) {
                throw new InvalidAlgorithmParameterException("EC may only use ECGenParameterSpec");
            }
            break;
        default:
            throw new ProviderException("Unsupported algorithm: " + mKeymasterAlgorithm);
    }
}
Also used : BigInteger(java.math.BigInteger) ASN1Integer(com.android.org.bouncycastle.asn1.ASN1Integer) DERInteger(com.android.org.bouncycastle.asn1.DERInteger) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ProviderException(java.security.ProviderException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) BigInteger(java.math.BigInteger) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) DERBitString(com.android.org.bouncycastle.asn1.DERBitString) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 40 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project jdk8u_jdk by JetBrains.

the class SupportedEllipticCurvesExtension method isAvailableCurve.

// check whether the curve is supported by the underlying providers
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }
        // cache the parameters
        idToParams.put(curveId, params);
        return true;
    }
    return false;
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

ECGenParameterSpec (java.security.spec.ECGenParameterSpec)67 KeyPairGenerator (java.security.KeyPairGenerator)31 AlgorithmParameters (java.security.AlgorithmParameters)23 KeyPair (java.security.KeyPair)23 BigInteger (java.math.BigInteger)19 PublicKey (java.security.PublicKey)19 ECPublicKey (java.security.interfaces.ECPublicKey)17 ECParameterSpec (java.security.spec.ECParameterSpec)15 ECPoint (java.security.spec.ECPoint)15 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)13 Test (org.junit.Test)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 KeySpec (java.security.spec.KeySpec)11 ECPrivateKey (java.security.interfaces.ECPrivateKey)9 PrivateKey (java.security.PrivateKey)8 SecureRandom (java.security.SecureRandom)8 Cipher (javax.crypto.Cipher)8 GeneralSecurityException (java.security.GeneralSecurityException)7 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)7