what-the-bank/sources/org/bouncycastle/jcajce/provider/asymmetric/dstu/SignatureSpi.java

134 lines
6.6 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.asymmetric.dstu;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.DEROctetString;
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.DSTU4145Signer;
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
import org.bouncycastle.jce.interfaces.ECKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/* loaded from: classes6.dex */
public class SignatureSpi extends java.security.SignatureSpi implements PKCSObjectIdentifiers, X509ObjectIdentifiers {
private static byte[] DEFAULT_SBOX = {10, 9, 13, 6, 14, 11, 4, 5, 15, 1, 3, 12, 7, 0, 8, 2, 8, 0, 12, 4, 9, 6, 7, 11, 2, 3, 1, 15, 5, 14, 10, 13, 15, 6, 5, 8, 14, 11, 10, 4, 12, 0, 3, 7, 2, 9, 1, 13, 3, 8, 13, 9, 6, 11, 15, 0, 2, 5, 12, 10, 4, 14, 1, 7, 15, 8, 14, 9, 7, 2, 0, 13, 12, 6, 1, 5, 11, 4, 3, 10, 2, 8, 9, 7, 5, 15, 0, 11, 12, 1, 13, 14, 10, 3, 6, 4, 3, 8, 11, 5, 6, 4, 14, 10, 2, 12, 1, 7, 9, 15, 13, 0, 1, 2, 3, 14, 6, 13, 11, 8, 15, 10, 12, 5, 7, 9, 0, 4};
private Digest digest;
private DSA signer = new DSTU4145Signer();
byte[] expandSbox(byte[] bArr) {
byte[] bArr2 = new byte[128];
for (int i = 0; i < bArr.length; i++) {
int i2 = i << 1;
bArr2[i2] = (byte) ((bArr[i] >> 4) & 15);
bArr2[i2 + 1] = (byte) (bArr[i] & 15);
}
return bArr2;
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // java.security.SignatureSpi
public boolean engineVerify(byte[] bArr) throws SignatureException {
byte[] bArr2 = new byte[this.digest.getDigestSize()];
this.digest.doFinal(bArr2, 0);
try {
byte[] octets = ((ASN1OctetString) ASN1OctetString.fromByteArray(bArr)).getOctets();
byte[] bArr3 = new byte[octets.length / 2];
byte[] bArr4 = new byte[octets.length / 2];
System.arraycopy(octets, 0, bArr4, 0, octets.length / 2);
System.arraycopy(octets, octets.length / 2, bArr3, 0, octets.length / 2);
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);
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // java.security.SignatureSpi
public byte[] engineSign() throws SignatureException {
byte[] bArr = new byte[this.digest.getDigestSize()];
this.digest.doFinal(bArr, 0);
try {
BigInteger[] generateSignature = this.signer.generateSignature(bArr);
byte[] byteArray = generateSignature[0].toByteArray();
byte[] byteArray2 = generateSignature[1].toByteArray();
int length = (byteArray.length > byteArray2.length ? byteArray.length : byteArray2.length) << 1;
byte[] bArr2 = new byte[length];
System.arraycopy(byteArray2, 0, bArr2, (length / 2) - byteArray2.length, byteArray2.length);
System.arraycopy(byteArray, 0, bArr2, length - byteArray.length, byteArray.length);
return new DEROctetString(bArr2).getEncoded();
} 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 {
try {
publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
if (!(publicKey instanceof ECPublicKey)) {
throw new InvalidKeyException("can't recognise key type in DSA based signer");
}
generatePublicKeyParameter = ECUtil.generatePublicKeyParameter(publicKey);
} catch (Exception unused) {
throw new InvalidKeyException("can't recognise key type in DSA based signer");
}
}
this.digest = new GOST3411Digest(expandSbox(((BCDSTU4145PublicKey) publicKey).getSbox()));
this.signer.init(false, generatePublicKeyParameter);
}
@Override // java.security.SignatureSpi
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
AsymmetricKeyParameter generatePrivateKeyParameter = privateKey instanceof ECKey ? ECUtil.generatePrivateKeyParameter(privateKey) : null;
this.digest = new GOST3411Digest(DEFAULT_SBOX);
if (((java.security.SignatureSpi) this).appRandom != null) {
this.signer.init(true, new ParametersWithRandom(generatePrivateKeyParameter, ((java.security.SignatureSpi) this).appRandom));
} else {
this.signer.init(true, generatePrivateKeyParameter);
}
}
@Override // java.security.SignatureSpi
protected Object engineGetParameter(String str) {
throw new UnsupportedOperationException("engineSetParameter unsupported");
}
}