what-the-bank/sources/org/bouncycastle/crypto/agreement/DHAgreement.java

55 lines
2.6 KiB
Java

package org.bouncycastle.crypto.agreement;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
import org.bouncycastle.crypto.params.DHParameters;
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
/* loaded from: classes6.dex */
public class DHAgreement {
private DHParameters dhParams;
private DHPrivateKeyParameters key;
private BigInteger privateValue;
private SecureRandom random;
public void init(CipherParameters cipherParameters) {
if (cipherParameters instanceof ParametersWithRandom) {
ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters;
this.random = parametersWithRandom.getRandom();
cipherParameters = parametersWithRandom.getParameters();
} else {
this.random = new SecureRandom();
}
AsymmetricKeyParameter asymmetricKeyParameter = (AsymmetricKeyParameter) cipherParameters;
if (!(asymmetricKeyParameter instanceof DHPrivateKeyParameters)) {
throw new IllegalArgumentException("DHEngine expects DHPrivateKeyParameters");
}
DHPrivateKeyParameters dHPrivateKeyParameters = (DHPrivateKeyParameters) asymmetricKeyParameter;
this.key = dHPrivateKeyParameters;
this.dhParams = dHPrivateKeyParameters.getParameters();
}
public BigInteger calculateMessage() {
DHKeyPairGenerator dHKeyPairGenerator = new DHKeyPairGenerator();
dHKeyPairGenerator.init(new DHKeyGenerationParameters(this.random, this.dhParams));
AsymmetricCipherKeyPair generateKeyPair = dHKeyPairGenerator.generateKeyPair();
this.privateValue = ((DHPrivateKeyParameters) generateKeyPair.getPrivate()).getX();
return ((DHPublicKeyParameters) generateKeyPair.getPublic()).getY();
}
public BigInteger calculateAgreement(DHPublicKeyParameters dHPublicKeyParameters, BigInteger bigInteger) {
if (!dHPublicKeyParameters.getParameters().equals(this.dhParams)) {
throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters.");
}
BigInteger p = this.dhParams.getP();
return bigInteger.modPow(this.key.getX(), p).multiply(dHPublicKeyParameters.getY().modPow(this.privateValue, p)).mod(p);
}
}