Search in sources :

Example 1 with ECPublicKey

use of org.bouncycastle.jce.interfaces.ECPublicKey in project robovm by robovm.

the class ECUtil method generatePublicKeyParameter.

public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
            return new ECPublicKeyParameters(((BCECPublicKey) k).engineGetQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPublicKey) {
        java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
        ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams(), false);
        return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW(), false), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC public key");
            }
            PublicKey publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            if (publicKey instanceof java.security.interfaces.ECPublicKey) {
                return ECUtil.generatePublicKeyParameter(publicKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC public key: " + e.toString());
        }
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) PublicKey(java.security.PublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with ECPublicKey

use of org.bouncycastle.jce.interfaces.ECPublicKey in project XobotOS by xamarin.

the class ECUtil method generatePublicKeyParameter.

public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = ProviderUtil.getEcImplicitlyCa();
            return new ECPublicKeyParameters(((JCEECPublicKey) k).engineGetQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPublicKey) {
        java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
        ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams(), false);
        return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW(), false), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) JCEECPublicKey(org.bouncycastle.jce.provider.JCEECPublicKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 3 with ECPublicKey

use of org.bouncycastle.jce.interfaces.ECPublicKey in project XobotOS by xamarin.

the class KeyAgreement method engineDoPhase.

protected Key engineDoPhase(Key key, boolean lastPhase) throws InvalidKeyException, IllegalStateException {
    if (parameters == null) {
        throw new IllegalStateException(kaAlgorithm + " not initialised.");
    }
    if (!lastPhase) {
        throw new IllegalStateException(kaAlgorithm + " can only be between two parties.");
    }
    CipherParameters pubKey;
    // BEGIN android-removed
    // if (agreement instanceof ECMQVBasicAgreement)
    // {
    //     if (!(key instanceof MQVPublicKey))
    //     {
    //         throw new InvalidKeyException(kaAlgorithm + " key agreement requires "
    //             + getSimpleName(MQVPublicKey.class) + " for doPhase");
    //     }
    //
    //     MQVPublicKey mqvPubKey = (MQVPublicKey)key;
    //     ECPublicKeyParameters staticKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getStaticKey());
    //     ECPublicKeyParameters ephemKey = (ECPublicKeyParameters)
    //         ECUtil.generatePublicKeyParameter(mqvPubKey.getEphemeralKey());
    //
    //     pubKey = new MQVPublicParameters(staticKey, ephemKey);
    //
    //     // TODO Validate that all the keys are using the same parameters?
    // }
    // else
    // END android-removed
    {
        if (!(key instanceof ECPublicKey)) {
            throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPublicKey.class) + " for doPhase");
        }
        pubKey = ECUtil.generatePublicKeyParameter((PublicKey) key);
    // TODO Validate that all the keys are using the same parameters?
    }
    result = agreement.calculateAgreement(pubKey);
    return null;
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)3 ECPublicKey (org.bouncycastle.jce.interfaces.ECPublicKey)3 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)2 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)2 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)2 PublicKey (java.security.PublicKey)1 CipherParameters (org.bouncycastle.crypto.CipherParameters)1 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)1 JCEECPublicKey (org.bouncycastle.jce.provider.JCEECPublicKey)1