package org.bouncycastle.crypto.signers; import java.io.IOException; import java.math.BigInteger; import org.bouncycastle.asn1.ASN1EncodableVector; 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.crypto.CipherParameters; import org.bouncycastle.crypto.DSA; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.Signer; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.ParametersWithRandom; /* loaded from: classes6.dex */ public class DSADigestSigner implements Signer { private final Digest digest; private final DSA dsaSigner; private boolean forSigning; @Override // org.bouncycastle.crypto.Signer public boolean verifySignature(byte[] bArr) { if (this.forSigning) { throw new IllegalStateException("DSADigestSigner not initialised for verification"); } byte[] bArr2 = new byte[this.digest.getDigestSize()]; this.digest.doFinal(bArr2, 0); try { BigInteger[] derDecode = derDecode(bArr); return this.dsaSigner.verifySignature(bArr2, derDecode[0], derDecode[1]); } catch (IOException unused) { return false; } } @Override // org.bouncycastle.crypto.Signer public void update(byte[] bArr, int i, int i2) { this.digest.update(bArr, i, i2); } @Override // org.bouncycastle.crypto.Signer public void update(byte b) { this.digest.update(b); } @Override // org.bouncycastle.crypto.Signer public void reset() { this.digest.reset(); } @Override // org.bouncycastle.crypto.Signer public void init(boolean z, CipherParameters cipherParameters) { this.forSigning = z; AsymmetricKeyParameter asymmetricKeyParameter = cipherParameters instanceof ParametersWithRandom ? (AsymmetricKeyParameter) ((ParametersWithRandom) cipherParameters).getParameters() : (AsymmetricKeyParameter) cipherParameters; if (z && !asymmetricKeyParameter.isPrivate()) { throw new IllegalArgumentException("Signing Requires Private Key."); } if (!z && asymmetricKeyParameter.isPrivate()) { throw new IllegalArgumentException("Verification Requires Public Key."); } reset(); this.dsaSigner.init(z, cipherParameters); } @Override // org.bouncycastle.crypto.Signer public byte[] generateSignature() { if (!this.forSigning) { throw new IllegalStateException("DSADigestSigner not initialised for signature generation."); } byte[] bArr = new byte[this.digest.getDigestSize()]; this.digest.doFinal(bArr, 0); BigInteger[] generateSignature = this.dsaSigner.generateSignature(bArr); try { return derEncode(generateSignature[0], generateSignature[1]); } catch (IOException unused) { throw new IllegalStateException("unable to encode signature"); } } private byte[] derEncode(BigInteger bigInteger, BigInteger bigInteger2) throws IOException { ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector(); aSN1EncodableVector.add(new ASN1Integer(bigInteger)); aSN1EncodableVector.add(new ASN1Integer(bigInteger2)); return new DERSequence(aSN1EncodableVector).getEncoded(ASN1Encoding.DER); } 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()}; } public DSADigestSigner(DSA dsa, Digest digest) { this.digest = digest; this.dsaSigner = dsa; } }