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.");
}
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.");
}
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;
}
Aggregations