81 lines
3.5 KiB
Java
81 lines
3.5 KiB
Java
package org.bouncycastle.pqc.jcajce.provider.newhope;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.Key;
|
|
import java.security.SecureRandom;
|
|
import java.security.spec.AlgorithmParameterSpec;
|
|
import javax.crypto.ShortBufferException;
|
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi;
|
|
import org.bouncycastle.pqc.crypto.ExchangePair;
|
|
import org.bouncycastle.pqc.crypto.newhope.NHAgreement;
|
|
import org.bouncycastle.pqc.crypto.newhope.NHExchangePairGenerator;
|
|
import org.bouncycastle.pqc.crypto.newhope.NHPublicKeyParameters;
|
|
import org.bouncycastle.util.Arrays;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class KeyAgreementSpi extends BaseAgreementSpi {
|
|
private NHAgreement agreement;
|
|
private NHExchangePairGenerator exchangePairGenerator;
|
|
private BCNHPublicKey otherPartyKey;
|
|
private byte[] shared;
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected void engineInit(Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
|
throw new InvalidAlgorithmParameterException("NewHope does not require parameters");
|
|
}
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected void engineInit(Key key, SecureRandom secureRandom) throws InvalidKeyException {
|
|
if (key == null) {
|
|
this.exchangePairGenerator = new NHExchangePairGenerator(secureRandom);
|
|
return;
|
|
}
|
|
NHAgreement nHAgreement = new NHAgreement();
|
|
this.agreement = nHAgreement;
|
|
nHAgreement.init(((BCNHPrivateKey) key).getKeyParams());
|
|
}
|
|
|
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi, javax.crypto.KeyAgreementSpi
|
|
public byte[] engineGenerateSecret() throws IllegalStateException {
|
|
byte[] clone = Arrays.clone(this.shared);
|
|
Arrays.fill(this.shared, (byte) 0);
|
|
return clone;
|
|
}
|
|
|
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi, javax.crypto.KeyAgreementSpi
|
|
public int engineGenerateSecret(byte[] bArr, int i) throws IllegalStateException, ShortBufferException {
|
|
byte[] bArr2 = this.shared;
|
|
System.arraycopy(bArr2, 0, bArr, i, bArr2.length);
|
|
Arrays.fill(this.shared, (byte) 0);
|
|
return this.shared.length;
|
|
}
|
|
|
|
@Override // javax.crypto.KeyAgreementSpi
|
|
protected Key engineDoPhase(Key key, boolean z) throws InvalidKeyException, IllegalStateException {
|
|
if (!z) {
|
|
throw new IllegalStateException("NewHope can only be between two parties.");
|
|
}
|
|
BCNHPublicKey bCNHPublicKey = (BCNHPublicKey) key;
|
|
this.otherPartyKey = bCNHPublicKey;
|
|
NHExchangePairGenerator nHExchangePairGenerator = this.exchangePairGenerator;
|
|
if (nHExchangePairGenerator == null) {
|
|
this.shared = this.agreement.calculateAgreement(bCNHPublicKey.getKeyParams());
|
|
return null;
|
|
}
|
|
ExchangePair GenerateExchange = nHExchangePairGenerator.GenerateExchange((AsymmetricKeyParameter) bCNHPublicKey.getKeyParams());
|
|
this.shared = GenerateExchange.getSharedValue();
|
|
return new BCNHPublicKey((NHPublicKeyParameters) GenerateExchange.getPublicKey());
|
|
}
|
|
|
|
@Override // org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi
|
|
public byte[] calcSecret() {
|
|
return engineGenerateSecret();
|
|
}
|
|
|
|
public KeyAgreementSpi() {
|
|
super("NH", null);
|
|
}
|
|
}
|