package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class G3413CTRBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher {
    private final int b;
    private byte[] c;
    private byte[] d;
    private byte[] e;
    private final int f;
    private final org.bouncycastle.crypto.BlockCipher g;
    private int h;
    private boolean i;

    public G3413CTRBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, blockCipher.b() * 8);
    }

    private byte[] g() {
        byte[] bArr = this.c;
        byte[] bArr2 = new byte[bArr.length];
        this.g.c(bArr, 0, bArr2, 0);
        return org.bouncycastle.crypto.modes.GOST3413CipherUtil.b(bArr2, this.b);
    }

    private void h() {
        byte[] bArr = this.c;
        int length = bArr.length - 1;
        bArr[length] = (byte) (bArr[length] + 1);
    }

    private void i() {
        int i = this.f;
        this.d = new byte[i / 2];
        this.c = new byte[i];
        this.e = new byte[this.b];
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.b;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        processBytes(bArr, i, this.b, bArr2, i2);
        return this.b;
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        if (this.h == 0) {
            this.e = g();
        }
        byte[] bArr = this.e;
        int i = this.h;
        byte b2 = (byte) (b ^ bArr[i]);
        int i2 = i + 1;
        this.h = i2;
        if (i2 == this.b) {
            this.h = 0;
            h();
        }
        return b2;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.g.getAlgorithmName() + "/GCTR";
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.BlockCipher blockCipher;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            i();
            byte[] bArrJ = org.bouncycastle.util.Arrays.j(parametersWithIV.a());
            this.d = bArrJ;
            if (bArrJ.length != this.f / 2) {
                throw new java.lang.IllegalArgumentException("Parameter IV length must be == blockSize/2");
            }
            java.lang.System.arraycopy(bArrJ, 0, this.c, 0, bArrJ.length);
            for (int length = this.d.length; length < this.f; length++) {
                this.c[length] = 0;
            }
            if (parametersWithIV.b() != null) {
                blockCipher = this.g;
                cipherParameters = parametersWithIV.b();
                blockCipher.init(true, cipherParameters);
            }
        } else {
            i();
            if (cipherParameters != null) {
                blockCipher = this.g;
                blockCipher.init(true, cipherParameters);
            }
        }
        this.i = true;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        if (this.i) {
            byte[] bArr = this.d;
            java.lang.System.arraycopy(bArr, 0, this.c, 0, bArr.length);
            for (int length = this.d.length; length < this.f; length++) {
                this.c[length] = 0;
            }
            this.h = 0;
            this.g.reset();
        }
    }

    public G3413CTRBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, int i) {
        super(blockCipher);
        this.h = 0;
        if (i < 0 || i > blockCipher.b() * 8) {
            throw new java.lang.IllegalArgumentException("Parameter bitBlockSize must be in range 0 < bitBlockSize <= " + (blockCipher.b() * 8));
        }
        this.g = blockCipher;
        int iB = blockCipher.b();
        this.f = iB;
        this.b = i / 8;
        this.c = new byte[iB];
    }
}
