138 lines
6.2 KiB
Java
138 lines
6.2 KiB
Java
package org.bouncycastle.jcajce.provider.asymmetric.gost;
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
import java.security.SecureRandom;
|
|
import java.security.SignatureException;
|
|
import java.security.spec.AlgorithmParameterSpec;
|
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
|
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
|
|
import org.bouncycastle.crypto.DSA;
|
|
import org.bouncycastle.crypto.Digest;
|
|
import org.bouncycastle.crypto.digests.GOST3411Digest;
|
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
|
import org.bouncycastle.crypto.signers.GOST3410Signer;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.GOST3410Util;
|
|
import org.bouncycastle.jce.interfaces.ECKey;
|
|
import org.bouncycastle.jce.interfaces.ECPublicKey;
|
|
import org.bouncycastle.jce.interfaces.GOST3410Key;
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class SignatureSpi extends java.security.SignatureSpi implements PKCSObjectIdentifiers, X509ObjectIdentifiers {
|
|
private SecureRandom random;
|
|
private Digest digest = new GOST3411Digest();
|
|
private DSA signer = new GOST3410Signer();
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected boolean engineVerify(byte[] bArr) throws SignatureException {
|
|
byte[] bArr2 = new byte[this.digest.getDigestSize()];
|
|
this.digest.doFinal(bArr2, 0);
|
|
try {
|
|
byte[] bArr3 = new byte[32];
|
|
byte[] bArr4 = new byte[32];
|
|
System.arraycopy(bArr, 0, bArr4, 0, 32);
|
|
System.arraycopy(bArr, 32, bArr3, 0, 32);
|
|
BigInteger bigInteger = new BigInteger(1, bArr4);
|
|
return this.signer.verifySignature(bArr2, new BigInteger[]{new BigInteger(1, bArr3), bigInteger}[0], bigInteger);
|
|
} catch (Exception unused) {
|
|
throw new SignatureException("error decoding signature bytes.");
|
|
}
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineUpdate(byte[] bArr, int i, int i2) throws SignatureException {
|
|
this.digest.update(bArr, i, i2);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineUpdate(byte b) throws SignatureException {
|
|
this.digest.update(b);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected byte[] engineSign() throws SignatureException {
|
|
byte[] bArr = new byte[this.digest.getDigestSize()];
|
|
this.digest.doFinal(bArr, 0);
|
|
try {
|
|
byte[] bArr2 = new byte[64];
|
|
BigInteger[] generateSignature = this.signer.generateSignature(bArr);
|
|
byte[] byteArray = generateSignature[0].toByteArray();
|
|
byte[] byteArray2 = generateSignature[1].toByteArray();
|
|
if (byteArray2[0] != 0) {
|
|
System.arraycopy(byteArray2, 0, bArr2, 32 - byteArray2.length, byteArray2.length);
|
|
} else {
|
|
System.arraycopy(byteArray2, 1, bArr2, 32 - (byteArray2.length - 1), byteArray2.length - 1);
|
|
}
|
|
if (byteArray[0] != 0) {
|
|
System.arraycopy(byteArray, 0, bArr2, 64 - byteArray.length, byteArray.length);
|
|
} else {
|
|
System.arraycopy(byteArray, 1, bArr2, 64 - (byteArray.length - 1), byteArray.length - 1);
|
|
}
|
|
return bArr2;
|
|
} catch (Exception e) {
|
|
throw new SignatureException(e.toString());
|
|
}
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineSetParameter(AlgorithmParameterSpec algorithmParameterSpec) {
|
|
throw new UnsupportedOperationException("engineSetParameter unsupported");
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineSetParameter(String str, Object obj) {
|
|
throw new UnsupportedOperationException("engineSetParameter unsupported");
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
|
|
AsymmetricKeyParameter generatePublicKeyParameter;
|
|
if (publicKey instanceof ECPublicKey) {
|
|
generatePublicKeyParameter = ECUtil.generatePublicKeyParameter(publicKey);
|
|
} else if (publicKey instanceof GOST3410Key) {
|
|
generatePublicKeyParameter = GOST3410Util.generatePublicKeyParameter(publicKey);
|
|
} else {
|
|
try {
|
|
PublicKey publicKey2 = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
|
|
if (!(publicKey2 instanceof ECPublicKey)) {
|
|
throw new InvalidKeyException("can't recognise key type in DSA based signer");
|
|
}
|
|
generatePublicKeyParameter = ECUtil.generatePublicKeyParameter(publicKey2);
|
|
} catch (Exception unused) {
|
|
throw new InvalidKeyException("can't recognise key type in DSA based signer");
|
|
}
|
|
}
|
|
this.digest.reset();
|
|
this.signer.init(false, generatePublicKeyParameter);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitSign(PrivateKey privateKey, SecureRandom secureRandom) throws InvalidKeyException {
|
|
this.random = secureRandom;
|
|
engineInitSign(privateKey);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
|
|
AsymmetricKeyParameter generatePrivateKeyParameter = privateKey instanceof ECKey ? ECUtil.generatePrivateKeyParameter(privateKey) : GOST3410Util.generatePrivateKeyParameter(privateKey);
|
|
this.digest.reset();
|
|
SecureRandom secureRandom = this.random;
|
|
if (secureRandom != null) {
|
|
this.signer.init(true, new ParametersWithRandom(generatePrivateKeyParameter, secureRandom));
|
|
} else {
|
|
this.signer.init(true, generatePrivateKeyParameter);
|
|
}
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected Object engineGetParameter(String str) {
|
|
throw new UnsupportedOperationException("engineSetParameter unsupported");
|
|
}
|
|
}
|