package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class KCTRBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher {
    private byte[] b;
    private byte[] c;
    private byte[] d;
    private int e;
    private boolean f;
    private org.bouncycastle.crypto.BlockCipher g;

    public KCTRBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        super(blockCipher);
        this.g = blockCipher;
        this.b = new byte[blockCipher.b()];
        this.c = new byte[blockCipher.b()];
        this.d = new byte[blockCipher.b()];
    }

    private void h(int i) {
        while (true) {
            byte[] bArr = this.c;
            if (i >= bArr.length) {
                return;
            }
            int i2 = i + 1;
            byte b = (byte) (bArr[i] + 1);
            bArr[i] = b;
            if (b != 0) {
                return;
            } else {
                i = i2;
            }
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.g.b();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (bArr.length - i < b()) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (bArr2.length - i2 < b()) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        processBytes(bArr, i, b(), bArr2, i2);
        return b();
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        int i = this.e;
        if (i == 0) {
            h(0);
            g();
            this.g.c(this.c, 0, this.d, 0);
            byte[] bArr = this.d;
            int i2 = this.e;
            this.e = i2 + 1;
            return (byte) (bArr[i2] ^ b);
        }
        byte[] bArr2 = this.d;
        int i3 = i + 1;
        this.e = i3;
        byte b2 = (byte) (b ^ bArr2[i]);
        if (i3 == this.c.length) {
            this.e = 0;
        }
        return b2;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.g.getAlgorithmName() + "/KCTR";
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.f = true;
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("invalid parameter passed");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrA = parametersWithIV.a();
        byte[] bArr = this.b;
        int length = bArr.length - bArrA.length;
        org.bouncycastle.util.Arrays.G(bArr, (byte) 0);
        java.lang.System.arraycopy(bArrA, 0, this.b, length, bArrA.length);
        org.bouncycastle.crypto.CipherParameters cipherParametersB = parametersWithIV.b();
        if (cipherParametersB != null) {
            this.g.init(true, cipherParametersB);
        }
        reset();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        if (this.f) {
            this.g.c(this.b, 0, this.c, 0);
        }
        this.g.reset();
        this.e = 0;
    }

    private void g() {
    }
}
