package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class OFBBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher {
    private int b;
    private byte[] c;
    private byte[] d;
    private byte[] e;
    private final int f;
    private final org.bouncycastle.crypto.BlockCipher g;

    public OFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, int i) {
        super(blockCipher);
        if (i > blockCipher.b() * 8 || i < 8 || i % 8 != 0) {
            throw new java.lang.IllegalArgumentException("0FB" + i + " not supported");
        }
        this.g = blockCipher;
        this.f = i / 8;
        this.c = new byte[blockCipher.b()];
        this.d = new byte[blockCipher.b()];
        this.e = new byte[blockCipher.b()];
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.f;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        processBytes(bArr, i, this.f, bArr2, i2);
        return this.f;
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        if (this.b == 0) {
            this.g.c(this.d, 0, this.e, 0);
        }
        byte[] bArr = this.e;
        int i = this.b;
        int i2 = i + 1;
        this.b = i2;
        byte b2 = (byte) (b ^ bArr[i]);
        int i3 = this.f;
        if (i2 == i3) {
            this.b = 0;
            byte[] bArr2 = this.d;
            java.lang.System.arraycopy(bArr2, i3, bArr2, 0, bArr2.length - i3);
            byte[] bArr3 = this.e;
            byte[] bArr4 = this.d;
            int length = bArr4.length;
            int i4 = this.f;
            java.lang.System.arraycopy(bArr3, 0, bArr4, length - i4, i4);
        }
        return b2;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.g.getAlgorithmName() + "/OFB" + (this.f * 8);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            reset();
            if (cipherParameters != null) {
                this.g.init(true, cipherParameters);
                return;
            }
            return;
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrA = parametersWithIV.a();
        int length = bArrA.length;
        byte[] bArr = this.c;
        if (length < bArr.length) {
            java.lang.System.arraycopy(bArrA, 0, bArr, bArr.length - bArrA.length, bArrA.length);
            int i = 0;
            while (true) {
                byte[] bArr2 = this.c;
                if (i >= bArr2.length - bArrA.length) {
                    break;
                }
                bArr2[i] = 0;
                i++;
            }
        } else {
            java.lang.System.arraycopy(bArrA, 0, bArr, 0, bArr.length);
        }
        reset();
        if (parametersWithIV.b() != null) {
            this.g.init(true, parametersWithIV.b());
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        byte[] bArr = this.c;
        java.lang.System.arraycopy(bArr, 0, this.d, 0, bArr.length);
        this.b = 0;
        this.g.reset();
    }
}
