package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class GOFBBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher {
    private byte[] b;
    private byte[] c;
    private byte[] d;
    private int e;
    private final int f;
    private final org.bouncycastle.crypto.BlockCipher g;
    boolean h;
    int i;
    int j;

    public GOFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        super(blockCipher);
        this.h = true;
        this.g = blockCipher;
        int iB = blockCipher.b();
        this.f = iB;
        if (iB != 8) {
            throw new java.lang.IllegalArgumentException("GCTR only for 64 bit block ciphers");
        }
        this.b = new byte[blockCipher.b()];
        this.c = new byte[blockCipher.b()];
        this.d = new byte[blockCipher.b()];
    }

    private int g(byte[] bArr, int i) {
        return ((bArr[i + 3] << 24) & (-16777216)) + ((bArr[i + 2] << 16) & 16711680) + ((bArr[i + 1] << 8) & 65280) + (bArr[i] & 255);
    }

    private void h(int i, byte[] bArr, int i2) {
        bArr[i2 + 3] = (byte) (i >>> 24);
        bArr[i2 + 2] = (byte) (i >>> 16);
        bArr[i2 + 1] = (byte) (i >>> 8);
        bArr[i2] = (byte) i;
    }

    @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.e == 0) {
            if (this.h) {
                this.h = false;
                this.g.c(this.c, 0, this.d, 0);
                this.i = g(this.d, 0);
                this.j = g(this.d, 4);
            }
            int i = this.i + android.R.attr.cacheColorHint;
            this.i = i;
            int i2 = this.j;
            int i3 = i2 + android.R.attr.hand_minute;
            this.j = i3;
            if (i3 < 16843012 && i3 > 0) {
                this.j = i2 + android.R.attr.format;
            }
            h(i, this.c, 0);
            h(this.j, this.c, 4);
            this.g.c(this.c, 0, this.d, 0);
        }
        byte[] bArr = this.d;
        int i4 = this.e;
        int i5 = i4 + 1;
        this.e = i5;
        byte b2 = (byte) (b ^ bArr[i4]);
        int i6 = this.f;
        if (i5 == i6) {
            this.e = 0;
            byte[] bArr2 = this.c;
            java.lang.System.arraycopy(bArr2, i6, bArr2, 0, bArr2.length - i6);
            byte[] bArr3 = this.d;
            byte[] bArr4 = this.c;
            int length = bArr4.length;
            int i7 = this.f;
            java.lang.System.arraycopy(bArr3, 0, bArr4, length - i7, i7);
        }
        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;
        this.h = true;
        this.i = 0;
        this.j = 0;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            byte[] bArrA = parametersWithIV.a();
            int length = bArrA.length;
            byte[] bArr = this.b;
            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.b;
                    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) {
                return;
            }
            blockCipher = this.g;
            cipherParameters = parametersWithIV.b();
        } else {
            reset();
            if (cipherParameters == null) {
                return;
            } else {
                blockCipher = this.g;
            }
        }
        blockCipher.init(true, cipherParameters);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        this.h = true;
        this.i = 0;
        this.j = 0;
        byte[] bArr = this.b;
        java.lang.System.arraycopy(bArr, 0, this.c, 0, bArr.length);
        this.e = 0;
        this.g.reset();
    }
}
