367 lines
17 KiB
Java
367 lines
17 KiB
Java
package org.bouncycastle.jcajce.provider.asymmetric.ec;
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.Key;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
import java.security.SecureRandom;
|
|
import java.security.spec.AlgorithmParameterSpec;
|
|
import org.bouncycastle.asn1.x9.X9IntegerConverter;
|
|
import org.bouncycastle.crypto.BasicAgreement;
|
|
import org.bouncycastle.crypto.CipherParameters;
|
|
import org.bouncycastle.crypto.DerivationFunction;
|
|
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
|
|
import org.bouncycastle.crypto.agreement.ECDHCBasicAgreement;
|
|
import org.bouncycastle.crypto.agreement.ECMQVBasicAgreement;
|
|
import org.bouncycastle.crypto.agreement.kdf.ConcatenationKDFGenerator;
|
|
import org.bouncycastle.crypto.digests.SHA1Digest;
|
|
import org.bouncycastle.crypto.digests.SHA224Digest;
|
|
import org.bouncycastle.crypto.digests.SHA256Digest;
|
|
import org.bouncycastle.crypto.digests.SHA384Digest;
|
|
import org.bouncycastle.crypto.digests.SHA512Digest;
|
|
import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
|
|
import org.bouncycastle.crypto.params.ECDomainParameters;
|
|
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
|
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
|
import org.bouncycastle.crypto.params.MQVPrivateParameters;
|
|
import org.bouncycastle.crypto.params.MQVPublicParameters;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
|
|
import org.bouncycastle.jcajce.spec.MQVParameterSpec;
|
|
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
|
|
import org.bouncycastle.jce.interfaces.ECPrivateKey;
|
|
import org.bouncycastle.jce.interfaces.ECPublicKey;
|
|
import org.bouncycastle.jce.interfaces.MQVPrivateKey;
|
|
import org.bouncycastle.jce.interfaces.MQVPublicKey;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class KeyAgreementSpi extends BaseAgreementSpi {
|
|
private static final X9IntegerConverter converter = new X9IntegerConverter();
|
|
private BasicAgreement agreement;
|
|
private String kaAlgorithm;
|
|
private MQVParameterSpec mqvParameters;
|
|
private ECDomainParameters parameters;
|
|
private BigInteger result;
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected void engineInit(Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
if (algorithmParameterSpec != null && !(algorithmParameterSpec instanceof MQVParameterSpec) && !(algorithmParameterSpec instanceof UserKeyingMaterialSpec)) {
|
|
throw new InvalidAlgorithmParameterException("No algorithm parameters supported");
|
|
}
|
|
initFromKey(key, algorithmParameterSpec);
|
|
}
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected void engineInit(Key key, SecureRandom secureRandom) throws InvalidKeyException {
|
|
initFromKey(key, null);
|
|
}
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected Key engineDoPhase(Key key, boolean z) throws InvalidKeyException, IllegalStateException {
|
|
CipherParameters generatePublicKeyParameter;
|
|
if (this.parameters == null) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(this.kaAlgorithm);
|
|
sb.append(" not initialised.");
|
|
throw new IllegalStateException(sb.toString());
|
|
}
|
|
if (!z) {
|
|
StringBuilder sb2 = new StringBuilder();
|
|
sb2.append(this.kaAlgorithm);
|
|
sb2.append(" can only be between two parties.");
|
|
throw new IllegalStateException(sb2.toString());
|
|
}
|
|
if (this.agreement instanceof ECMQVBasicAgreement) {
|
|
if (key instanceof MQVPublicKey) {
|
|
MQVPublicKey mQVPublicKey = (MQVPublicKey) key;
|
|
generatePublicKeyParameter = new MQVPublicParameters((ECPublicKeyParameters) ECUtil.generatePublicKeyParameter(mQVPublicKey.getStaticKey()), (ECPublicKeyParameters) ECUtil.generatePublicKeyParameter(mQVPublicKey.getEphemeralKey()));
|
|
} else {
|
|
generatePublicKeyParameter = new MQVPublicParameters((ECPublicKeyParameters) ECUtil.generatePublicKeyParameter((PublicKey) key), (ECPublicKeyParameters) ECUtil.generatePublicKeyParameter(this.mqvParameters.getOtherPartyEphemeralKey()));
|
|
}
|
|
} else {
|
|
if (!(key instanceof PublicKey)) {
|
|
StringBuilder sb3 = new StringBuilder();
|
|
sb3.append(this.kaAlgorithm);
|
|
sb3.append(" key agreement requires ");
|
|
sb3.append(getSimpleName(ECPublicKey.class));
|
|
sb3.append(" for doPhase");
|
|
throw new InvalidKeyException(sb3.toString());
|
|
}
|
|
generatePublicKeyParameter = ECUtil.generatePublicKeyParameter((PublicKey) key);
|
|
}
|
|
this.result = this.agreement.calculateAgreement(generatePublicKeyParameter);
|
|
return null;
|
|
}
|
|
|
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi
|
|
public byte[] calcSecret() {
|
|
return bigIntToBytes(this.result);
|
|
}
|
|
|
|
protected byte[] bigIntToBytes(BigInteger bigInteger) {
|
|
X9IntegerConverter x9IntegerConverter = converter;
|
|
return x9IntegerConverter.integerToBytes(bigInteger, x9IntegerConverter.getByteLength(this.parameters.getCurve()));
|
|
}
|
|
|
|
private void initFromKey(Key key, AlgorithmParameterSpec algorithmParameterSpec) throws InvalidKeyException {
|
|
CipherParameters cipherParameters;
|
|
ECPrivateKeyParameters eCPrivateKeyParameters;
|
|
ECPrivateKeyParameters eCPrivateKeyParameters2;
|
|
ECPublicKeyParameters eCPublicKeyParameters = null;
|
|
if (this.agreement instanceof ECMQVBasicAgreement) {
|
|
this.mqvParameters = null;
|
|
boolean z = key instanceof MQVPrivateKey;
|
|
if (!z && !(algorithmParameterSpec instanceof MQVParameterSpec)) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(this.kaAlgorithm);
|
|
sb.append(" key agreement requires ");
|
|
sb.append(getSimpleName(MQVParameterSpec.class));
|
|
sb.append(" for initialisation");
|
|
throw new InvalidKeyException(sb.toString());
|
|
}
|
|
if (z) {
|
|
MQVPrivateKey mQVPrivateKey = (MQVPrivateKey) key;
|
|
eCPrivateKeyParameters2 = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mQVPrivateKey.getStaticPrivateKey());
|
|
eCPrivateKeyParameters = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mQVPrivateKey.getEphemeralPrivateKey());
|
|
if (mQVPrivateKey.getEphemeralPublicKey() != null) {
|
|
eCPublicKeyParameters = (ECPublicKeyParameters) ECUtil.generatePublicKeyParameter(mQVPrivateKey.getEphemeralPublicKey());
|
|
}
|
|
} else {
|
|
MQVParameterSpec mQVParameterSpec = (MQVParameterSpec) algorithmParameterSpec;
|
|
ECPrivateKeyParameters eCPrivateKeyParameters3 = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
|
|
eCPrivateKeyParameters = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mQVParameterSpec.getEphemeralPrivateKey());
|
|
eCPublicKeyParameters = mQVParameterSpec.getEphemeralPublicKey() != null ? (ECPublicKeyParameters) ECUtil.generatePublicKeyParameter(mQVParameterSpec.getEphemeralPublicKey()) : null;
|
|
this.mqvParameters = mQVParameterSpec;
|
|
this.ukmParameters = mQVParameterSpec.getUserKeyingMaterial();
|
|
eCPrivateKeyParameters2 = eCPrivateKeyParameters3;
|
|
}
|
|
CipherParameters mQVPrivateParameters = new MQVPrivateParameters(eCPrivateKeyParameters2, eCPrivateKeyParameters, eCPublicKeyParameters);
|
|
this.parameters = eCPrivateKeyParameters2.getParameters();
|
|
cipherParameters = mQVPrivateParameters;
|
|
} else {
|
|
if (!(key instanceof PrivateKey)) {
|
|
StringBuilder sb2 = new StringBuilder();
|
|
sb2.append(this.kaAlgorithm);
|
|
sb2.append(" key agreement requires ");
|
|
sb2.append(getSimpleName(ECPrivateKey.class));
|
|
sb2.append(" for initialisation");
|
|
throw new InvalidKeyException(sb2.toString());
|
|
}
|
|
ECPrivateKeyParameters eCPrivateKeyParameters4 = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
|
|
this.parameters = eCPrivateKeyParameters4.getParameters();
|
|
this.ukmParameters = algorithmParameterSpec instanceof UserKeyingMaterialSpec ? ((UserKeyingMaterialSpec) algorithmParameterSpec).getUserKeyingMaterial() : null;
|
|
cipherParameters = eCPrivateKeyParameters4;
|
|
}
|
|
this.agreement.init(cipherParameters);
|
|
}
|
|
|
|
private static String getSimpleName(Class cls) {
|
|
String name = cls.getName();
|
|
return name.substring(name.lastIndexOf(46) + 1);
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class CDHwithSHA1KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public CDHwithSHA1KDFAndSharedInfo() {
|
|
super("ECCDHwithSHA1KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class CDHwithSHA224KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public CDHwithSHA224KDFAndSharedInfo() {
|
|
super("ECCDHwithSHA224KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class CDHwithSHA256KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public CDHwithSHA256KDFAndSharedInfo() {
|
|
super("ECCDHwithSHA256KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class CDHwithSHA384KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public CDHwithSHA384KDFAndSharedInfo() {
|
|
super("ECCDHwithSHA384KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class CDHwithSHA512KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public CDHwithSHA512KDFAndSharedInfo() {
|
|
super("ECCDHwithSHA512KDF", new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DH extends KeyAgreementSpi {
|
|
public DH() {
|
|
super("ECDH", new ECDHBasicAgreement(), null);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHC extends KeyAgreementSpi {
|
|
public DHC() {
|
|
super("ECDHC", new ECDHCBasicAgreement(), null);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA1CKDF extends KeyAgreementSpi {
|
|
public DHwithSHA1CKDF() {
|
|
super("ECDHwithSHA1CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA1KDF extends KeyAgreementSpi {
|
|
public DHwithSHA1KDF() {
|
|
super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA1KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public DHwithSHA1KDFAndSharedInfo() {
|
|
super("ECDHwithSHA1KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA224KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public DHwithSHA224KDFAndSharedInfo() {
|
|
super("ECDHwithSHA224KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA256CKDF extends KeyAgreementSpi {
|
|
public DHwithSHA256CKDF() {
|
|
super("ECDHwithSHA256CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA256Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA256KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public DHwithSHA256KDFAndSharedInfo() {
|
|
super("ECDHwithSHA256KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA384CKDF extends KeyAgreementSpi {
|
|
public DHwithSHA384CKDF() {
|
|
super("ECDHwithSHA384CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA384Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA384KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public DHwithSHA384KDFAndSharedInfo() {
|
|
super("ECDHwithSHA384KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA512CKDF extends KeyAgreementSpi {
|
|
public DHwithSHA512CKDF() {
|
|
super("ECDHwithSHA512CKDF", new ECDHCBasicAgreement(), new ConcatenationKDFGenerator(new SHA512Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class DHwithSHA512KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public DHwithSHA512KDFAndSharedInfo() {
|
|
super("ECDHwithSHA512KDF", new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQV extends KeyAgreementSpi {
|
|
public MQV() {
|
|
super("ECMQV", new ECMQVBasicAgreement(), null);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA1CKDF extends KeyAgreementSpi {
|
|
public MQVwithSHA1CKDF() {
|
|
super("ECMQVwithSHA1CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA1KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public MQVwithSHA1KDFAndSharedInfo() {
|
|
super("ECMQVwithSHA1KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA224CKDF extends KeyAgreementSpi {
|
|
public MQVwithSHA224CKDF() {
|
|
super("ECMQVwithSHA224CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA224Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA224KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public MQVwithSHA224KDFAndSharedInfo() {
|
|
super("ECMQVwithSHA224KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA224Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA256CKDF extends KeyAgreementSpi {
|
|
public MQVwithSHA256CKDF() {
|
|
super("ECMQVwithSHA256CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA256Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA256KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public MQVwithSHA256KDFAndSharedInfo() {
|
|
super("ECMQVwithSHA256KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA384CKDF extends KeyAgreementSpi {
|
|
public MQVwithSHA384CKDF() {
|
|
super("ECMQVwithSHA384CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA384Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA384KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public MQVwithSHA384KDFAndSharedInfo() {
|
|
super("ECMQVwithSHA384KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA512CKDF extends KeyAgreementSpi {
|
|
public MQVwithSHA512CKDF() {
|
|
super("ECMQVwithSHA512CKDF", new ECMQVBasicAgreement(), new ConcatenationKDFGenerator(new SHA512Digest()));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class MQVwithSHA512KDFAndSharedInfo extends KeyAgreementSpi {
|
|
public MQVwithSHA512KDFAndSharedInfo() {
|
|
super("ECMQVwithSHA512KDF", new ECMQVBasicAgreement(), new KDF2BytesGenerator(new SHA512Digest()));
|
|
}
|
|
}
|
|
|
|
protected KeyAgreementSpi(String str, BasicAgreement basicAgreement, DerivationFunction derivationFunction) {
|
|
super(str, derivationFunction);
|
|
this.kaAlgorithm = str;
|
|
this.agreement = basicAgreement;
|
|
}
|
|
}
|