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;

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

    public OpenPGPCFBBlockCipher(BlockCipher blockCipher) {
        this.cipher = blockCipher;
        this.blockSize = blockCipher.getBlockSize();
        this.IV = new byte[this.blockSize];
        this.FR = new byte[this.blockSize];
        this.FRE = 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");
        }
        int i3 = 2;
        if (this.count > this.blockSize) {
            byte b = bArr[i];
            this.FR[this.blockSize - 2] = b;
            bArr2[i2] = encryptByte(b, this.blockSize - 2);
            byte b2 = bArr[i + 1];
            this.FR[this.blockSize - 1] = b2;
            bArr2[i2 + 1] = encryptByte(b2, this.blockSize - 1);
            this.cipher.processBlock(this.FR, 0, this.FRE, 0);
            while (i3 < this.blockSize) {
                byte b3 = bArr[i + i3];
                int i4 = i3 - 2;
                this.FR[i4] = b3;
                bArr2[i2 + i3] = encryptByte(b3, i4);
                i3++;
            }
        } else {
            if (this.count == 0) {
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                for (int i5 = 0; i5 < this.blockSize; i5++) {
                    int i6 = i + i5;
                    this.FR[i5] = bArr[i6];
                    bArr2[i2 + i5] = encryptByte(bArr[i6], i5);
                }
            } else if (this.count == this.blockSize) {
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                byte b4 = bArr[i];
                byte b5 = bArr[i + 1];
                bArr2[i2] = encryptByte(b4, 0);
                bArr2[i2 + 1] = encryptByte(b5, 1);
                System.arraycopy(this.FR, 2, this.FR, 0, this.blockSize - 2);
                this.FR[this.blockSize - 2] = b4;
                this.FR[this.blockSize - 1] = b5;
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                while (i3 < this.blockSize) {
                    byte b6 = bArr[i + i3];
                    int i7 = i3 - 2;
                    this.FR[i7] = b6;
                    bArr2[i2 + i3] = encryptByte(b6, i7);
                    i3++;
                }
            }
            this.count += this.blockSize;
        }
        return this.blockSize;
    }

    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");
        }
        int i3 = 2;
        if (this.count > this.blockSize) {
            byte[] bArr3 = this.FR;
            int i4 = this.blockSize - 2;
            byte bEncryptByte = encryptByte(bArr[i], this.blockSize - 2);
            bArr2[i2] = bEncryptByte;
            bArr3[i4] = bEncryptByte;
            byte[] bArr4 = this.FR;
            int i5 = this.blockSize - 1;
            byte bEncryptByte2 = encryptByte(bArr[i + 1], this.blockSize - 1);
            bArr2[i2 + 1] = bEncryptByte2;
            bArr4[i5] = bEncryptByte2;
            this.cipher.processBlock(this.FR, 0, this.FRE, 0);
            while (i3 < this.blockSize) {
                byte[] bArr5 = this.FR;
                int i6 = i3 - 2;
                byte bEncryptByte3 = encryptByte(bArr[i + i3], i6);
                bArr2[i2 + i3] = bEncryptByte3;
                bArr5[i6] = bEncryptByte3;
                i3++;
            }
        } else {
            if (this.count == 0) {
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                for (int i7 = 0; i7 < this.blockSize; i7++) {
                    byte[] bArr6 = this.FR;
                    byte bEncryptByte4 = encryptByte(bArr[i + i7], i7);
                    bArr2[i2 + i7] = bEncryptByte4;
                    bArr6[i7] = bEncryptByte4;
                }
            } else if (this.count == this.blockSize) {
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                bArr2[i2] = encryptByte(bArr[i], 0);
                bArr2[i2 + 1] = encryptByte(bArr[i + 1], 1);
                System.arraycopy(this.FR, 2, this.FR, 0, this.blockSize - 2);
                System.arraycopy(bArr2, i2, this.FR, this.blockSize - 2, 2);
                this.cipher.processBlock(this.FR, 0, this.FRE, 0);
                while (i3 < this.blockSize) {
                    byte[] bArr7 = this.FR;
                    int i8 = i3 - 2;
                    byte bEncryptByte5 = encryptByte(bArr[i + i3], i8);
                    bArr2[i2 + i3] = bEncryptByte5;
                    bArr7[i8] = bEncryptByte5;
                    i3++;
                }
            }
            this.count += this.blockSize;
        }
        return this.blockSize;
    }

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

    @Override // org.bouncycastle.crypto.BlockCipher
    public String getAlgorithmName() {
        return this.cipher.getAlgorithmName() + "/OpenPGPCFB";
    }

    @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 {
        this.forEncryption = z;
        reset();
        this.cipher.init(true, cipherParameters);
    }

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

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        this.count = 0;
        System.arraycopy(this.IV, 0, this.FR, 0, this.FR.length);
        this.cipher.reset();
    }
}
