155 lines
6.5 KiB
Java
155 lines
6.5 KiB
Java
|
package org.bouncycastle.jcajce.provider.asymmetric.dh;
|
||
|
|
||
|
import java.math.BigInteger;
|
||
|
import java.security.InvalidAlgorithmParameterException;
|
||
|
import java.security.InvalidKeyException;
|
||
|
import java.security.Key;
|
||
|
import java.security.NoSuchAlgorithmException;
|
||
|
import java.security.SecureRandom;
|
||
|
import java.security.spec.AlgorithmParameterSpec;
|
||
|
import javax.crypto.SecretKey;
|
||
|
import javax.crypto.ShortBufferException;
|
||
|
import javax.crypto.interfaces.DHPrivateKey;
|
||
|
import javax.crypto.interfaces.DHPublicKey;
|
||
|
import javax.crypto.spec.DHParameterSpec;
|
||
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
import org.bouncycastle.crypto.DerivationFunction;
|
||
|
import org.bouncycastle.crypto.agreement.kdf.DHKEKGenerator;
|
||
|
import org.bouncycastle.crypto.digests.SHA1Digest;
|
||
|
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi;
|
||
|
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class KeyAgreementSpi extends BaseAgreementSpi {
|
||
|
private BigInteger g;
|
||
|
private BigInteger p;
|
||
|
private BigInteger result;
|
||
|
private BigInteger x;
|
||
|
|
||
|
@Override // javax.crypto.KeyAgreementSpi
|
||
|
protected void engineInit(Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
||
|
DHParameterSpec params;
|
||
|
if (!(key instanceof DHPrivateKey)) {
|
||
|
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
|
||
|
}
|
||
|
DHPrivateKey dHPrivateKey = (DHPrivateKey) key;
|
||
|
if (algorithmParameterSpec == null) {
|
||
|
this.p = dHPrivateKey.getParams().getP();
|
||
|
params = dHPrivateKey.getParams();
|
||
|
} else {
|
||
|
if (!(algorithmParameterSpec instanceof DHParameterSpec)) {
|
||
|
if (!(algorithmParameterSpec instanceof UserKeyingMaterialSpec)) {
|
||
|
throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
|
||
|
}
|
||
|
this.p = dHPrivateKey.getParams().getP();
|
||
|
this.g = dHPrivateKey.getParams().getG();
|
||
|
this.ukmParameters = ((UserKeyingMaterialSpec) algorithmParameterSpec).getUserKeyingMaterial();
|
||
|
BigInteger x = dHPrivateKey.getX();
|
||
|
this.result = x;
|
||
|
this.x = x;
|
||
|
}
|
||
|
params = (DHParameterSpec) algorithmParameterSpec;
|
||
|
this.p = params.getP();
|
||
|
}
|
||
|
this.g = params.getG();
|
||
|
BigInteger x2 = dHPrivateKey.getX();
|
||
|
this.result = x2;
|
||
|
this.x = x2;
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.KeyAgreementSpi
|
||
|
protected void engineInit(Key key, SecureRandom secureRandom) throws InvalidKeyException {
|
||
|
if (!(key instanceof DHPrivateKey)) {
|
||
|
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
|
||
|
}
|
||
|
DHPrivateKey dHPrivateKey = (DHPrivateKey) key;
|
||
|
this.p = dHPrivateKey.getParams().getP();
|
||
|
this.g = dHPrivateKey.getParams().getG();
|
||
|
BigInteger x = dHPrivateKey.getX();
|
||
|
this.result = x;
|
||
|
this.x = x;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi, javax.crypto.KeyAgreementSpi
|
||
|
public byte[] engineGenerateSecret() throws IllegalStateException {
|
||
|
if (this.x != null) {
|
||
|
return super.engineGenerateSecret();
|
||
|
}
|
||
|
throw new IllegalStateException("Diffie-Hellman not initialised.");
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi, javax.crypto.KeyAgreementSpi
|
||
|
public SecretKey engineGenerateSecret(String str) throws NoSuchAlgorithmException {
|
||
|
if (this.x != null) {
|
||
|
return str.equals("TlsPremasterSecret") ? new SecretKeySpec(trimZeroes(bigIntToBytes(this.result)), str) : super.engineGenerateSecret(str);
|
||
|
}
|
||
|
throw new IllegalStateException("Diffie-Hellman not initialised.");
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi, javax.crypto.KeyAgreementSpi
|
||
|
public int engineGenerateSecret(byte[] bArr, int i) throws IllegalStateException, ShortBufferException {
|
||
|
if (this.x != null) {
|
||
|
return super.engineGenerateSecret(bArr, i);
|
||
|
}
|
||
|
throw new IllegalStateException("Diffie-Hellman not initialised.");
|
||
|
}
|
||
|
|
||
|
@Override // javax.crypto.KeyAgreementSpi
|
||
|
protected Key engineDoPhase(Key key, boolean z) throws InvalidKeyException, IllegalStateException {
|
||
|
if (this.x == null) {
|
||
|
throw new IllegalStateException("Diffie-Hellman not initialised.");
|
||
|
}
|
||
|
if (!(key instanceof DHPublicKey)) {
|
||
|
throw new InvalidKeyException("DHKeyAgreement doPhase requires DHPublicKey");
|
||
|
}
|
||
|
DHPublicKey dHPublicKey = (DHPublicKey) key;
|
||
|
if (!dHPublicKey.getParams().getG().equals(this.g) || !dHPublicKey.getParams().getP().equals(this.p)) {
|
||
|
throw new InvalidKeyException("DHPublicKey not for this KeyAgreement!");
|
||
|
}
|
||
|
if (z) {
|
||
|
this.result = dHPublicKey.getY().modPow(this.x, this.p);
|
||
|
return null;
|
||
|
}
|
||
|
BigInteger modPow = dHPublicKey.getY().modPow(this.x, this.p);
|
||
|
this.result = modPow;
|
||
|
return new BCDHPublicKey(modPow, dHPublicKey.getParams());
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi
|
||
|
public byte[] calcSecret() {
|
||
|
return bigIntToBytes(this.result);
|
||
|
}
|
||
|
|
||
|
protected byte[] bigIntToBytes(BigInteger bigInteger) {
|
||
|
int bitLength = (this.p.bitLength() + 7) / 8;
|
||
|
byte[] byteArray = bigInteger.toByteArray();
|
||
|
if (byteArray.length == bitLength) {
|
||
|
return byteArray;
|
||
|
}
|
||
|
if (byteArray[0] != 0 || byteArray.length != bitLength + 1) {
|
||
|
byte[] bArr = new byte[bitLength];
|
||
|
System.arraycopy(byteArray, 0, bArr, bitLength - byteArray.length, byteArray.length);
|
||
|
return bArr;
|
||
|
}
|
||
|
int length = byteArray.length - 1;
|
||
|
byte[] bArr2 = new byte[length];
|
||
|
System.arraycopy(byteArray, 1, bArr2, 0, length);
|
||
|
return bArr2;
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public static class DHwithRFC2631KDF extends KeyAgreementSpi {
|
||
|
public DHwithRFC2631KDF() {
|
||
|
super("DHwithRFC2631KDF", new DHKEKGenerator(new SHA1Digest()));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public KeyAgreementSpi(String str, DerivationFunction derivationFunction) {
|
||
|
super(str, derivationFunction);
|
||
|
}
|
||
|
|
||
|
public KeyAgreementSpi() {
|
||
|
super("Diffie-Hellman", null);
|
||
|
}
|
||
|
}
|