package org.bouncycastle.crypto.agreement; import java.math.BigInteger; import org.bouncycastle.crypto.BasicAgreement; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; 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 DHBasicAgreement implements BasicAgreement { private DHParameters dhParams; private DHPrivateKeyParameters key; @Override // org.bouncycastle.crypto.BasicAgreement public void init(CipherParameters cipherParameters) { if (cipherParameters instanceof ParametersWithRandom) { cipherParameters = ((ParametersWithRandom) cipherParameters).getParameters(); } 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(); } @Override // org.bouncycastle.crypto.BasicAgreement public int getFieldSize() { return (this.key.getParameters().getP().bitLength() + 7) / 8; } @Override // org.bouncycastle.crypto.BasicAgreement public BigInteger calculateAgreement(CipherParameters cipherParameters) { DHPublicKeyParameters dHPublicKeyParameters = (DHPublicKeyParameters) cipherParameters; if (dHPublicKeyParameters.getParameters().equals(this.dhParams)) { return dHPublicKeyParameters.getY().modPow(this.key.getX(), this.dhParams.getP()); } throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters."); } }