package org.bouncycastle.openpgp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.HashUtils;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.OnePassSignaturePacket;
import org.bouncycastle.bcpg.Packet;
import org.bouncycastle.openpgp.operator.PGPContentVerifier;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public class PGPOnePassSignature extends PGPDefaultSignatureGenerator {
    private OnePassSignaturePacket sigPack;
    private PGPContentVerifier verifier;

    public PGPOnePassSignature(BCPGInputStream bCPGInputStream) throws IOException, PGPException {
        this(cast(bCPGInputStream.readPacket()));
    }

    PGPOnePassSignature(OnePassSignaturePacket onePassSignaturePacket) {
        super(onePassSignaturePacket.getVersion() == 3 ? 4 : onePassSignaturePacket.getVersion());
        this.sigPack = onePassSignaturePacket;
        this.sigType = onePassSignaturePacket.getSignatureType();
    }

    private static OnePassSignaturePacket cast(Packet packet) throws IOException {
        if (packet instanceof OnePassSignaturePacket) {
            return (OnePassSignaturePacket) packet;
        }
        throw new IOException("unexpected packet in stream: " + packet);
    }

    private void checkSaltSize() throws PGPException {
        int v6SignatureSaltSizeInBytes;
        if (getVersion() == 6 && (v6SignatureSaltSizeInBytes = HashUtils.getV6SignatureSaltSizeInBytes(getHashAlgorithm())) != getSalt().length) {
            throw new PGPException("RFC9580 defines the salt size for " + PGPUtil.getDigestName(getHashAlgorithm()) + " as " + v6SignatureSaltSizeInBytes + " octets, but signature has " + getSalt().length + " octets.");
        }
    }

    private void compareSalt(PGPSignature pGPSignature) throws PGPException {
        if (this.version == 6 && !Arrays.constantTimeAreEqual(getSalt(), pGPSignature.getSalt())) {
            throw new PGPException("Salt in OnePassSignaturePacket does not match salt in SignaturePacket.");
        }
    }

    private void updateWithSalt() throws PGPException {
        if (this.version == 6) {
            try {
                this.sigOut.write(getSalt());
            } catch (IOException e) {
                throw new PGPException("Cannot salt the signature.", e);
            }
        }
    }

    public void encode(OutputStream outputStream) throws IOException {
        BCPGOutputStream.wrap(outputStream).writePacket(this.sigPack);
    }

    public byte[] getEncoded() throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        encode(byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public byte[] getFingerprint() {
        return this.sigPack.getFingerprint();
    }

    public int getHashAlgorithm() {
        return this.sigPack.getHashAlgorithm();
    }

    public int getKeyAlgorithm() {
        return this.sigPack.getKeyAlgorithm();
    }

    public long getKeyID() {
        return this.sigPack.getKeyID();
    }

    public KeyIdentifier getKeyIdentifier() {
        return new KeyIdentifier(getFingerprint(), getKeyID());
    }

    public byte[] getSalt() {
        return this.sigPack.getSalt();
    }

    public int getSignatureType() {
        return this.sigPack.getSignatureType();
    }

    public int getVersion() {
        return this.sigPack.getVersion();
    }

    public void init(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, PGPPublicKey pGPPublicKey) throws PGPException {
        this.verifier = pGPContentVerifierBuilderProvider.get(this.sigPack.getKeyAlgorithm(), this.sigPack.getHashAlgorithm()).build(pGPPublicKey);
        this.lastb = (byte) 0;
        this.sigOut = this.verifier.getOutputStream();
        checkSaltSize();
        updateWithSalt();
    }

    public boolean isContaining() {
        return this.sigPack.isContaining();
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte b) {
        super.update(b);
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte[] bArr) {
        super.update(bArr);
    }

    @Override // org.bouncycastle.openpgp.PGPDefaultSignatureGenerator
    public /* bridge */ /* synthetic */ void update(byte[] bArr, int i, int i2) {
        super.update(bArr, i, i2);
    }

    public boolean verify(PGPSignature pGPSignature) throws PGPException {
        compareSalt(pGPSignature);
        try {
            this.sigOut.write(pGPSignature.getSignatureTrailer());
            this.sigOut.close();
            return this.verifier.verify(pGPSignature.getSignature());
        } catch (IOException e) {
            throw new PGPException("unable to add trailer: " + e.getMessage(), e);
        }
    }
}
