package org.bouncycastle.crypto.modes;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.params.ParametersWithIV;

/* JADX INFO: loaded from: classes.dex */
public class PGPCFBBlockCipher implements BlockCipher {
    private byte[] FR;
    private byte[] FRE;
    private byte[] IV;
    private int blockSize;
    private BlockCipher cipher;
    private int count;
    private boolean forEncryption;
    private boolean inlineIv;
    private byte[] tmp;

    public PGPCFBBlockCipher(BlockCipher blockCipher, boolean z) {
        this.cipher = blockCipher;
        this.inlineIv = z;
        this.blockSize = blockCipher.getBlockSize();
        this.IV = new byte[this.blockSize];
        this.FR = new byte[this.blockSize];
        this.FRE = new byte[this.blockSize];
        this.tmp = new byte[this.blockSize];
    }

    private int decryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) throws IllegalStateException, DataLengthException {
        if (this.blockSize + i > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (this.blockSize + i2 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        for (int i3 = 0; i3 < this.blockSize; i3++) {
            bArr2[i2 + i3] = encryptByte(bArr[i + i3], i3);
        }
        for (int i4 = 0; i4 < this.blockSize; i4++) {
            this.FR[i4] = bArr[i + i4];
        }
        return this.blockSize;
    }

    private int decryptBlockWithIV(byte[] bArr, int i, byte[] bArr2, int i2) throws IllegalStateException, DataLengthException {
        byte[] bArr3;
        if (this.blockSize + i > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (this.blockSize + i2 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        if (this.count == 0) {
            for (int i3 = 0; i3 < this.blockSize; i3++) {
                this.FR[i3] = bArr[i + i3];
            }
            this.cipher.processBlock(this.FR, 0, this.FRE, 0);
            this.count += this.blockSize;
            return 0;
        }
        if (this.count != this.blockSize) {
            if (this.count >= this.blockSize + 2) {
                System.arraycopy(bArr, i, this.tmp, 0, this.blockSize);
                bArr2[i2 + 0] = encryptByte(this.tmp[0], this.blockSize - 2);
                bArr2[i2 + 1] = encryptByte(this.tmp[1], this.blockSize - 1);
                System.arraycopy(this.tmp, 0, this.FR, this.blockSize - 2, 2);
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                int i4 = 0;
                while (true) {
                    int i5 = this.blockSize - 2;
                    bArr3 = this.tmp;
                    if (i4 >= i5) {
                        break;
                    }
                    bArr2[i2 + i4 + 2] = encryptByte(bArr3[i4 + 2], i4);
                    i4++;
                }
                System.arraycopy(bArr3, 2, this.FR, 0, this.blockSize - 2);
            }
            return this.blockSize;
        }
        System.arraycopy(bArr, i, this.tmp, 0, this.blockSize);
        System.arraycopy(this.FR, 2, this.FR, 0, this.blockSize - 2);
        this.FR[this.blockSize - 2] = this.tmp[0];
        this.FR[this.blockSize - 1] = this.tmp[1];
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        int i6 = 0;
        while (true) {
            int i7 = this.blockSize - 2;
            byte[] bArr4 = this.tmp;
            if (i6 >= i7) {
                System.arraycopy(bArr4, 2, this.FR, 0, this.blockSize - 2);
                this.count += 2;
                return this.blockSize - 2;
            }
            bArr2[i2 + i6] = encryptByte(bArr4[i6 + 2], i6);
            i6++;
        }
    }

    private int encryptBlock(byte[] bArr, int i, byte[] bArr2, int i2) throws IllegalStateException, DataLengthException {
        if (this.blockSize + i > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (this.blockSize + i2 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        for (int i3 = 0; i3 < this.blockSize; i3++) {
            bArr2[i2 + i3] = encryptByte(bArr[i + i3], i3);
        }
        for (int i4 = 0; i4 < this.blockSize; i4++) {
            this.FR[i4] = bArr2[i2 + i4];
        }
        return this.blockSize;
    }

    private int encryptBlockWithIV(byte[] bArr, int i, byte[] bArr2, int i2) throws IllegalStateException, DataLengthException {
        if (this.blockSize + i > bArr.length) {
            throw new DataLengthException("input buffer too short");
        }
        if (this.count != 0) {
            if (this.count >= this.blockSize + 2) {
                if (this.blockSize + i2 > bArr2.length) {
                    throw new OutputLengthException("output buffer too short");
                }
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                for (int i3 = 0; i3 < this.blockSize; i3++) {
                    bArr2[i2 + i3] = encryptByte(bArr[i + i3], i3);
                }
                System.arraycopy(bArr2, i2, this.FR, 0, this.blockSize);
            }
            return this.blockSize;
        }
        if ((this.blockSize * 2) + i2 + 2 > bArr2.length) {
            throw new OutputLengthException("output buffer too short");
        }
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        for (int i4 = 0; i4 < this.blockSize; i4++) {
            bArr2[i2 + i4] = encryptByte(this.IV[i4], i4);
        }
        System.arraycopy(bArr2, i2, this.FR, 0, this.blockSize);
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        bArr2[this.blockSize + i2] = encryptByte(this.IV[this.blockSize - 2], 0);
        bArr2[this.blockSize + i2 + 1] = encryptByte(this.IV[this.blockSize - 1], 1);
        System.arraycopy(bArr2, i2 + 2, this.FR, 0, this.blockSize);
        this.cipher.processBlock(this.FR, 0, this.FRE, 0);
        int i5 = 0;
        while (true) {
            int i6 = this.blockSize;
            int i7 = this.blockSize;
            if (i5 >= i6) {
                System.arraycopy(bArr2, i2 + i7 + 2, this.FR, 0, this.blockSize);
                this.count += (this.blockSize * 2) + 2;
                return (this.blockSize * 2) + 2;
            }
            bArr2[i7 + i2 + 2 + i5] = encryptByte(bArr[i + i5], i5);
            i5++;
        }
    }

    private byte encryptByte(byte b, int i) {
        return (byte) (b ^ this.FRE[i]);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public String getAlgorithmName() {
        StringBuilder sbAppend;
        String str;
        if (this.inlineIv) {
            sbAppend = new StringBuilder().append(this.cipher.getAlgorithmName());
            str = "/PGPCFBwithIV";
        } else {
            sbAppend = new StringBuilder().append(this.cipher.getAlgorithmName());
            str = "/PGPCFB";
        }
        return sbAppend.append(str).toString();
    }

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

    public BlockCipher getUnderlyingCipher() {
        return this.cipher;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, CipherParameters cipherParameters) throws IllegalArgumentException {
        BlockCipher blockCipher;
        this.forEncryption = z;
        if (cipherParameters instanceof ParametersWithIV) {
            ParametersWithIV parametersWithIV = (ParametersWithIV) cipherParameters;
            byte[] iv = parametersWithIV.getIV();
            int length = iv.length;
            int length2 = this.IV.length;
            byte[] bArr = this.IV;
            if (length < length2) {
                System.arraycopy(iv, 0, bArr, this.IV.length - iv.length, iv.length);
                for (int i = 0; i < this.IV.length - iv.length; i++) {
                    this.IV[i] = 0;
                }
            } else {
                System.arraycopy(iv, 0, bArr, 0, this.IV.length);
            }
            reset();
            blockCipher = this.cipher;
            cipherParameters = parametersWithIV.getParameters();
        } else {
            reset();
            blockCipher = this.cipher;
        }
        blockCipher.init(true, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int processBlock(byte[] bArr, int i, byte[] bArr2, int i2) throws IllegalStateException, DataLengthException {
        boolean z = this.inlineIv;
        boolean z2 = this.forEncryption;
        if (z) {
            return z2 ? encryptBlockWithIV(bArr, i, bArr2, i2) : decryptBlockWithIV(bArr, i, bArr2, i2);
        }
        return z2 ? encryptBlock(bArr, i, bArr2, i2) : decryptBlock(bArr, i, bArr2, i2);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        this.count = 0;
        for (int i = 0; i != this.FR.length; i++) {
            boolean z = this.inlineIv;
            byte[] bArr = this.FR;
            if (z) {
                bArr[i] = 0;
            } else {
                bArr[i] = this.IV[i];
            }
        }
        this.cipher.reset();
    }
}
