package org.bouncycastle.openpgp;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.bcpg.AEADAlgorithmTags;
import org.bouncycastle.bcpg.AEADEncDataPacket;
import org.bouncycastle.bcpg.InputStreamPacket;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.openpgp.operator.PGPDataDecryptor;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.io.TeeInputStream;

/* JADX INFO: loaded from: classes2.dex */
public abstract class PGPEncryptedData implements SymmetricKeyAlgorithmTags, AEADAlgorithmTags {
    InputStreamPacket encData;
    InputStream encStream;
    PGPDigestCalculator integrityCalculator;
    TruncatedStream truncStream;

    protected static class TruncatedStream extends InputStream {
        int bufPtr;
        InputStream in;
        int[] lookAhead = new int[22];
        byte[] readBuffer = new byte[8192];

        TruncatedStream(InputStream inputStream) throws IOException {
            for (int i = 0; i != this.lookAhead.length; i++) {
                int[] iArr = this.lookAhead;
                int i2 = inputStream.read();
                iArr[i] = i2;
                if (i2 < 0) {
                    throw new EOFException();
                }
            }
            this.bufPtr = 0;
            this.in = inputStream;
        }

        int[] getLookAhead() {
            int[] iArr = new int[this.lookAhead.length];
            int i = this.bufPtr;
            int i2 = 0;
            int i3 = 0;
            while (i != this.lookAhead.length) {
                iArr[i3] = this.lookAhead[i];
                i++;
                i3++;
            }
            while (i2 != this.bufPtr) {
                iArr[i3] = this.lookAhead[i2];
                i2++;
                i3++;
            }
            return iArr;
        }

        @Override // java.io.InputStream
        public int read() throws IOException {
            int i = this.in.read();
            if (i < 0) {
                return -1;
            }
            int i2 = this.lookAhead[this.bufPtr];
            this.lookAhead[this.bufPtr] = i;
            this.bufPtr = (this.bufPtr + 1) % this.lookAhead.length;
            return i2;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr) throws IOException {
            return read(bArr, 0, bArr.length);
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            int i3 = i + i2;
            if ((i | i2 | i3 | (bArr.length - i3)) < 0) {
                throw new IndexOutOfBoundsException();
            }
            if (i2 == 0) {
                return 0;
            }
            int i4 = this.in.read(this.readBuffer, 0, Math.min(this.readBuffer.length, i2));
            if (i4 < 0) {
                return -1;
            }
            int iMin = Math.min(i4, this.lookAhead.length);
            for (int i5 = 0; i5 < iMin; i5++) {
                bArr[i + i5] = (byte) this.lookAhead[(this.bufPtr + i5) % this.lookAhead.length];
            }
            int i6 = i4 - iMin;
            for (int i7 = i6; i7 < i4; i7++) {
                this.lookAhead[this.bufPtr] = this.readBuffer[i7] & 255;
                this.bufPtr = (this.bufPtr + 1) % this.lookAhead.length;
            }
            if (i6 != 0) {
                System.arraycopy(this.readBuffer, 0, bArr, i + iMin, i6);
            }
            return i4;
        }
    }

    PGPEncryptedData(InputStreamPacket inputStreamPacket) {
        this.encData = inputStreamPacket;
    }

    public int getAlgorithm() {
        throw new UnsupportedOperationException("not supported - override required");
    }

    public InputStreamPacket getEncData() {
        return this.encData;
    }

    public InputStream getInputStream() {
        return this.encData.getInputStream();
    }

    public int getVersion() {
        throw new UnsupportedOperationException("not supported - override required");
    }

    public boolean isAEAD() {
        if (this.encData instanceof AEADEncDataPacket) {
            return true;
        }
        return (this.encData instanceof SymmetricEncIntegrityPacket) && ((SymmetricEncIntegrityPacket) this.encData).getVersion() == 2;
    }

    public boolean isIntegrityProtected() {
        return this.encData instanceof SymmetricEncIntegrityPacket;
    }

    boolean processSymmetricEncIntegrityPacketDataStream(boolean z, PGPDataDecryptor pGPDataDecryptor, InputStream inputStream) throws IOException {
        this.encStream = pGPDataDecryptor.getInputStream(inputStream);
        if (z) {
            this.truncStream = new TruncatedStream(this.encStream);
            this.integrityCalculator = pGPDataDecryptor.getIntegrityCalculator();
            this.encStream = new TeeInputStream(this.truncStream, this.integrityCalculator.getOutputStream());
        }
        int blockSize = pGPDataDecryptor.getBlockSize();
        byte[] bArr = new byte[blockSize];
        int i = 0;
        while (true) {
            InputStream inputStream2 = this.encStream;
            if (i == blockSize) {
                int i2 = inputStream2.read();
                int i3 = this.encStream.read();
                if (i2 < 0 || i3 < 0) {
                    throw new EOFException("unexpected end of stream.");
                }
                return ((bArr[blockSize + (-2)] == ((byte) i2) && bArr[blockSize - 1] == ((byte) i3)) || (i2 == 0 && i3 == 0)) ? false : true;
            }
            int i4 = inputStream2.read();
            if (i4 < 0) {
                throw new EOFException("unexpected end of stream.");
            }
            bArr[i] = (byte) i4;
            i++;
        }
    }

    public boolean verify() throws IOException, PGPException {
        if (!isIntegrityProtected()) {
            throw new PGPException("data not integrity protected.");
        }
        while (this.encStream.read() >= 0) {
        }
        if (isAEAD()) {
            return true;
        }
        int[] lookAhead = this.truncStream.getLookAhead();
        OutputStream outputStream = this.integrityCalculator.getOutputStream();
        outputStream.write((byte) lookAhead[0]);
        outputStream.write((byte) lookAhead[1]);
        byte[] digest = this.integrityCalculator.getDigest();
        int length = digest.length;
        byte[] bArr = new byte[length];
        for (int i = 0; i != length; i++) {
            bArr[i] = (byte) lookAhead[i + 2];
        }
        return Arrays.constantTimeAreEqual(digest, bArr);
    }
}
