package org.bouncycastle.crypto;

/* JADX INFO: loaded from: classes2.dex */
public class DefaultBufferedBlockCipher extends BufferedBlockCipher {
    protected byte[] buf;
    protected int bufOff;
    protected BlockCipher cipher;
    protected boolean forEncryption;
    protected MultiBlockCipher mbCipher;
    protected boolean partialBlockOkay;
    protected boolean pgpCFB;

    protected DefaultBufferedBlockCipher() {
    }

    public DefaultBufferedBlockCipher(BlockCipher blockCipher) {
        this.cipher = blockCipher;
        if (blockCipher instanceof MultiBlockCipher) {
            this.mbCipher = (MultiBlockCipher) blockCipher;
            this.buf = new byte[this.mbCipher.getMultiBlockSize()];
        } else {
            this.mbCipher = null;
            this.buf = new byte[blockCipher.getBlockSize()];
        }
        boolean z = false;
        this.bufOff = 0;
        String algorithmName = blockCipher.getAlgorithmName();
        int iIndexOf = algorithmName.indexOf(47) + 1;
        this.pgpCFB = iIndexOf > 0 && algorithmName.startsWith("PGP", iIndexOf);
        if (this.pgpCFB || (blockCipher instanceof StreamCipher)) {
            this.partialBlockOkay = true;
            return;
        }
        if (iIndexOf > 0 && algorithmName.startsWith("OpenPGP", iIndexOf)) {
            z = true;
        }
        this.partialBlockOkay = z;
    }

    private int processBuffer(byte[] bArr, int i) {
        this.bufOff = 0;
        return this.mbCipher != null ? this.mbCipher.processBlocks(this.buf, 0, this.buf.length / this.mbCipher.getBlockSize(), bArr, i) : this.cipher.processBlock(this.buf, 0, bArr, i);
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int doFinal(byte[] bArr, int i) throws InvalidCipherTextException, IllegalStateException, DataLengthException {
        byte[] bArr2;
        int i2;
        int iProcessBlocks;
        int blockSize;
        try {
            if (this.bufOff + i > bArr.length) {
                throw new OutputLengthException("output buffer too short for doFinal()");
            }
            int i3 = 0;
            if (this.bufOff != 0) {
                if (this.mbCipher != null) {
                    int blockSize2 = this.bufOff / this.mbCipher.getBlockSize();
                    bArr2 = bArr;
                    i2 = i;
                    iProcessBlocks = this.mbCipher.processBlocks(this.buf, 0, blockSize2, bArr2, i2) + 0;
                    blockSize = blockSize2 * this.mbCipher.getBlockSize();
                } else {
                    bArr2 = bArr;
                    i2 = i;
                    iProcessBlocks = 0;
                    blockSize = 0;
                }
                if (this.bufOff != blockSize) {
                    if (!this.partialBlockOkay) {
                        throw new DataLengthException("data not block size aligned");
                    }
                    this.cipher.processBlock(this.buf, blockSize, this.buf, blockSize);
                    System.arraycopy(this.buf, blockSize, bArr2, i2 + iProcessBlocks, this.bufOff - blockSize);
                    iProcessBlocks += this.bufOff - blockSize;
                    this.bufOff = 0;
                }
                i3 = iProcessBlocks;
            }
            reset();
            return i3;
        } catch (Throwable th) {
            reset();
            throw th;
        }
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int getBlockSize() {
        return this.cipher.getBlockSize();
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int getOutputSize(int i) {
        int blockSize;
        if (this.pgpCFB && this.forEncryption) {
            i += this.bufOff;
            blockSize = this.cipher.getBlockSize() + 2;
        } else {
            blockSize = this.bufOff;
        }
        return i + blockSize;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public BlockCipher getUnderlyingCipher() {
        return this.cipher;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int getUpdateOutputSize(int i) {
        int length;
        int length2;
        int i2 = i + this.bufOff;
        if (this.pgpCFB) {
            boolean z = this.forEncryption;
            byte[] bArr = this.buf;
            if (z) {
                length2 = (i2 % bArr.length) - (this.cipher.getBlockSize() + 2);
            } else {
                length = bArr.length;
            }
            return i2 - length2;
        }
        length = this.buf.length;
        length2 = i2 % length;
        return i2 - length2;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
        this.forEncryption = z;
        reset();
        this.cipher.init(z, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int processByte(byte b, byte[] bArr, int i) throws IllegalStateException, DataLengthException {
        byte[] bArr2 = this.buf;
        int i2 = this.bufOff;
        this.bufOff = i2 + 1;
        bArr2[i2] = b;
        if (this.bufOff == this.buf.length) {
            return processBuffer(bArr, i);
        }
        return 0;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws IllegalStateException, DataLengthException {
        byte[] bArr3;
        int i4;
        if (i2 < 0) {
            throw new IllegalArgumentException("Can't have a negative input length!");
        }
        int blockSize = getBlockSize();
        int updateOutputSize = getUpdateOutputSize(i2);
        if (updateOutputSize > 0 && updateOutputSize + i3 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        int length = this.buf.length - this.bufOff;
        int iProcessBuffer = 0;
        if (i2 > length) {
            if (this.bufOff != 0) {
                System.arraycopy(bArr, i, this.buf, this.bufOff, length);
                i += length;
                i2 -= length;
            }
            if (bArr == bArr2) {
                bArr = new byte[i2];
                System.arraycopy(bArr2, i, bArr, 0, i2);
                i4 = 0;
            } else {
                i4 = i;
            }
            byte[] bArr4 = bArr;
            iProcessBuffer = this.bufOff != 0 ? 0 + processBuffer(bArr2, i3) : 0;
            if (this.mbCipher != null) {
                int multiBlockSize = (i2 / this.mbCipher.getMultiBlockSize()) * (this.mbCipher.getMultiBlockSize() / blockSize);
                if (multiBlockSize > 0) {
                    bArr3 = bArr2;
                    iProcessBuffer += this.mbCipher.processBlocks(bArr4, i4, multiBlockSize, bArr3, i3 + iProcessBuffer);
                    int i5 = multiBlockSize * blockSize;
                    i2 -= i5;
                    i4 += i5;
                } else {
                    bArr3 = bArr2;
                }
                i = i4;
            } else {
                bArr3 = bArr2;
                i = i4;
                while (i2 > this.buf.length) {
                    iProcessBuffer += this.cipher.processBlock(bArr4, i, bArr3, i3 + iProcessBuffer);
                    i2 -= blockSize;
                    i += blockSize;
                }
            }
            bArr = bArr4;
        } else {
            bArr3 = bArr2;
        }
        System.arraycopy(bArr, i, this.buf, this.bufOff, i2);
        this.bufOff += i2;
        return this.bufOff == this.buf.length ? iProcessBuffer + processBuffer(bArr3, i3 + iProcessBuffer) : iProcessBuffer;
    }

    @Override // org.bouncycastle.crypto.BufferedBlockCipher
    public void reset() {
        for (int i = 0; i < this.buf.length; i++) {
            this.buf[i] = 0;
        }
        this.bufOff = 0;
        this.cipher.reset();
    }
}
