use of com.github.zhenwei.core.crypto.params.DHMQVPrivateParameters in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (!(key instanceof DHPrivateKey)) {
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
}
DHPrivateKey privKey = (DHPrivateKey) key;
if (params != null) {
if (// p, g override.
params instanceof DHParameterSpec) {
DHParameterSpec p = (DHParameterSpec) params;
this.p = p.getP();
this.g = p.getG();
this.dheParameters = null;
this.ukmParameters = null;
} else if (params instanceof DHUParameterSpec) {
if (unifiedAgreement == null) {
throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.dheParameters = (DHUParameterSpec) params;
this.ukmParameters = ((DHUParameterSpec) params).getUserKeyingMaterial();
if (dheParameters.getEphemeralPublicKey() != null) {
unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(dheParameters.getEphemeralPublicKey())));
} else {
unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey())));
}
} else if (params instanceof MQVParameterSpec) {
if (mqvAgreement == null) {
throw new InvalidAlgorithmParameterException("agreement algorithm not MQV based");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.mqvParameters = (MQVParameterSpec) params;
this.ukmParameters = ((MQVParameterSpec) params).getUserKeyingMaterial();
if (mqvParameters.getEphemeralPublicKey() != null) {
mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(mqvParameters.getEphemeralPublicKey())));
} else {
mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey())));
}
} else if (params instanceof UserKeyingMaterialSpec) {
if (kdf == null) {
throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.dheParameters = null;
this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
} else {
throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
}
} else {
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
}
this.x = privKey.getX();
this.result = bigIntToBytes(x);
}
Aggregations