what-the-bank/sources/org/bouncycastle/jcajce/provider/asymmetric/dsa/DSASigner.java

271 lines
10 KiB
Java

package org.bouncycastle.jcajce.provider.asymmetric.dsa;
import java.io.IOException;
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.SignatureSpi;
import java.security.interfaces.DSAKey;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DSA;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.NullDigest;
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.SHA3Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.signers.HMacDSAKCalculator;
import org.jmrtd.PassportService;
/* loaded from: classes6.dex */
public class DSASigner extends SignatureSpi implements PKCSObjectIdentifiers, X509ObjectIdentifiers {
private Digest digest;
private SecureRandom random;
private DSA signer;
@Override // java.security.SignatureSpi
protected boolean engineVerify(byte[] bArr) throws SignatureException {
byte[] bArr2 = new byte[this.digest.getDigestSize()];
this.digest.doFinal(bArr2, 0);
try {
BigInteger[] derDecode = derDecode(bArr);
return this.signer.verifySignature(bArr2, derDecode[0], derDecode[1]);
} 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 {
BigInteger[] generateSignature = this.signer.generateSignature(bArr);
return derEncode(generateSignature[0], generateSignature[1]);
} 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 DSAKey) {
generatePublicKeyParameter = DSAUtil.generatePublicKeyParameter(publicKey);
} else {
try {
generatePublicKeyParameter = DSAUtil.generatePublicKeyParameter(new BCDSAPublicKey(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded())));
} 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 {
CipherParameters generatePrivateKeyParameter = DSAUtil.generatePrivateKeyParameter(privateKey);
SecureRandom secureRandom = this.random;
if (secureRandom != null) {
generatePrivateKeyParameter = new ParametersWithRandom(generatePrivateKeyParameter, secureRandom);
}
this.digest.reset();
this.signer.init(true, generatePrivateKeyParameter);
}
@Override // java.security.SignatureSpi
protected Object engineGetParameter(String str) {
throw new UnsupportedOperationException("engineSetParameter unsupported");
}
private byte[] derEncode(BigInteger bigInteger, BigInteger bigInteger2) throws IOException {
return new DERSequence(new ASN1Integer[]{new ASN1Integer(bigInteger), new ASN1Integer(bigInteger2)}).getEncoded(ASN1Encoding.DER);
}
/* loaded from: classes6.dex */
public static class detDSA extends DSASigner {
public detDSA() {
super(new SHA1Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA1Digest())));
}
}
/* loaded from: classes6.dex */
public static class detDSA224 extends DSASigner {
public detDSA224() {
super(new SHA224Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA224Digest())));
}
}
/* loaded from: classes6.dex */
public static class detDSA256 extends DSASigner {
public detDSA256() {
super(new SHA256Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA256Digest())));
}
}
/* loaded from: classes6.dex */
public static class detDSA384 extends DSASigner {
public detDSA384() {
super(new SHA384Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA384Digest())));
}
}
/* loaded from: classes6.dex */
public static class detDSA512 extends DSASigner {
public detDSA512() {
super(new SHA512Digest(), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA512Digest())));
}
}
/* loaded from: classes6.dex */
public static class detDSASha3_224 extends DSASigner {
public detDSASha3_224() {
super(new SHA3Digest(PassportService.DEFAULT_MAX_BLOCKSIZE), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA3Digest(PassportService.DEFAULT_MAX_BLOCKSIZE))));
}
}
/* loaded from: classes6.dex */
public static class detDSASha3_256 extends DSASigner {
public detDSASha3_256() {
super(new SHA3Digest(256), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA3Digest(256))));
}
}
/* loaded from: classes6.dex */
public static class detDSASha3_384 extends DSASigner {
public detDSASha3_384() {
super(new SHA3Digest(384), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA3Digest(384))));
}
}
/* loaded from: classes6.dex */
public static class detDSASha3_512 extends DSASigner {
public detDSASha3_512() {
super(new SHA3Digest(512), new org.bouncycastle.crypto.signers.DSASigner(new HMacDSAKCalculator(new SHA3Digest(512))));
}
}
/* loaded from: classes6.dex */
public static class dsa224 extends DSASigner {
public dsa224() {
super(new SHA224Digest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsa256 extends DSASigner {
public dsa256() {
super(new SHA256Digest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsa384 extends DSASigner {
public dsa384() {
super(new SHA384Digest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsa512 extends DSASigner {
public dsa512() {
super(new SHA512Digest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsaSha3_224 extends DSASigner {
public dsaSha3_224() {
super(new SHA3Digest(PassportService.DEFAULT_MAX_BLOCKSIZE), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsaSha3_256 extends DSASigner {
public dsaSha3_256() {
super(new SHA3Digest(256), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsaSha3_384 extends DSASigner {
public dsaSha3_384() {
super(new SHA3Digest(384), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class dsaSha3_512 extends DSASigner {
public dsaSha3_512() {
super(new SHA3Digest(512), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class noneDSA extends DSASigner {
public noneDSA() {
super(new NullDigest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
/* loaded from: classes6.dex */
public static class stdDSA extends DSASigner {
public stdDSA() {
super(new SHA1Digest(), new org.bouncycastle.crypto.signers.DSASigner());
}
}
private BigInteger[] derDecode(byte[] bArr) throws IOException {
ASN1Sequence aSN1Sequence = (ASN1Sequence) ASN1Primitive.fromByteArray(bArr);
return new BigInteger[]{((ASN1Integer) aSN1Sequence.getObjectAt(0)).getValue(), ((ASN1Integer) aSN1Sequence.getObjectAt(1)).getValue()};
}
protected DSASigner(Digest digest, DSA dsa) {
this.digest = digest;
this.signer = dsa;
}
}