package org.bouncycastle.crypto.digests;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class GOST3411Digest implements org.bouncycastle.crypto.ExtendedDigest, org.bouncycastle.util.Memoable {
    private static final byte[] t = {0, -1, 0, -1, 0, -1, 0, -1, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, -1, 0, -1, 0, 0, -1, -1, 0, 0, 0, -1, -1, 0, -1};
    private final org.bouncycastle.crypto.CryptoServicePurpose a;
    private byte[] b;
    private byte[] c;
    private byte[] d;
    private byte[] e;
    private byte[][] f;
    private byte[] g;
    private int h;
    private long i;
    private org.bouncycastle.crypto.BlockCipher j;
    private byte[] k;
    private byte[] l;
    byte[] m;
    short[] n;
    short[] o;
    byte[] p;
    byte[] q;
    byte[] r;
    byte[] s;

    public GOST3411Digest() {
        this(org.bouncycastle.crypto.CryptoServicePurpose.ANY);
    }

    private byte[] e(byte[] bArr) {
        for (int i = 0; i < 8; i++) {
            this.m[i] = (byte) (bArr[i] ^ bArr[i + 8]);
        }
        java.lang.System.arraycopy(bArr, 8, bArr, 0, 24);
        java.lang.System.arraycopy(this.m, 0, bArr, 24, 8);
        return bArr;
    }

    private void f(byte[] bArr, byte[] bArr2, int i, byte[] bArr3, int i2) {
        this.j.init(true, new org.bouncycastle.crypto.params.KeyParameter(bArr));
        this.j.c(bArr3, i2, bArr2, i);
    }

    private byte[] g(byte[] bArr) {
        for (int i = 0; i < 8; i++) {
            byte[] bArr2 = this.l;
            int i2 = i * 4;
            bArr2[i2] = bArr[i];
            bArr2[i2 + 1] = bArr[i + 8];
            bArr2[i2 + 2] = bArr[i + 16];
            bArr2[i2 + 3] = bArr[i + 24];
        }
        return this.l;
    }

    private void h(byte[] bArr, short[] sArr) {
        for (int i = 0; i < bArr.length / 2; i++) {
            int i2 = i * 2;
            sArr[i] = (short) ((bArr[i2] & 255) | ((bArr[i2 + 1] << 8) & 65280));
        }
    }

    private void i(short[] sArr, byte[] bArr) {
        for (int i = 0; i < bArr.length / 2; i++) {
            int i2 = i * 2;
            short s = sArr[i];
            bArr[i2 + 1] = (byte) (s >> 8);
            bArr[i2] = (byte) s;
        }
    }

    private void k() {
        org.bouncycastle.util.Pack.H(this.i * 8, this.c, 0);
        while (this.h != 0) {
            update((byte) 0);
        }
        m(this.c, 0);
        m(this.e, 0);
    }

    private void l(byte[] bArr) {
        h(bArr, this.n);
        short[] sArr = this.o;
        short[] sArr2 = this.n;
        sArr[15] = (short) (((((sArr2[0] ^ sArr2[1]) ^ sArr2[2]) ^ sArr2[3]) ^ sArr2[12]) ^ sArr2[15]);
        java.lang.System.arraycopy(sArr2, 1, sArr, 0, 15);
        i(this.o, bArr);
    }

    private void n(byte[] bArr) {
        int i = 0;
        int i2 = 0;
        while (true) {
            byte[] bArr2 = this.e;
            if (i == bArr2.length) {
                return;
            }
            int i3 = (bArr2[i] & 255) + (bArr[i] & 255) + i2;
            bArr2[i] = (byte) i3;
            i2 = i3 >>> 8;
            i++;
        }
    }

    public org.bouncycastle.util.Memoable a() {
        return new org.bouncycastle.crypto.digests.GOST3411Digest(this);
    }

    public void d(org.bouncycastle.util.Memoable memoable) {
        org.bouncycastle.crypto.digests.GOST3411Digest gOST3411Digest = (org.bouncycastle.crypto.digests.GOST3411Digest) memoable;
        byte[] bArr = gOST3411Digest.k;
        this.k = bArr;
        this.j.init(true, new org.bouncycastle.crypto.params.ParametersWithSBox(null, bArr));
        reset();
        byte[] bArr2 = gOST3411Digest.b;
        java.lang.System.arraycopy(bArr2, 0, this.b, 0, bArr2.length);
        byte[] bArr3 = gOST3411Digest.c;
        java.lang.System.arraycopy(bArr3, 0, this.c, 0, bArr3.length);
        byte[] bArr4 = gOST3411Digest.d;
        java.lang.System.arraycopy(bArr4, 0, this.d, 0, bArr4.length);
        byte[] bArr5 = gOST3411Digest.e;
        java.lang.System.arraycopy(bArr5, 0, this.e, 0, bArr5.length);
        byte[] bArr6 = gOST3411Digest.f[1];
        java.lang.System.arraycopy(bArr6, 0, this.f[1], 0, bArr6.length);
        byte[] bArr7 = gOST3411Digest.f[2];
        java.lang.System.arraycopy(bArr7, 0, this.f[2], 0, bArr7.length);
        byte[] bArr8 = gOST3411Digest.f[3];
        java.lang.System.arraycopy(bArr8, 0, this.f[3], 0, bArr8.length);
        byte[] bArr9 = gOST3411Digest.g;
        java.lang.System.arraycopy(bArr9, 0, this.g, 0, bArr9.length);
        this.h = gOST3411Digest.h;
        this.i = gOST3411Digest.i;
    }

    @Override // org.bouncycastle.crypto.Digest
    public int doFinal(byte[] bArr, int i) {
        k();
        byte[] bArr2 = this.b;
        java.lang.System.arraycopy(bArr2, 0, bArr, i, bArr2.length);
        reset();
        return 32;
    }

    @Override // org.bouncycastle.crypto.Digest
    public java.lang.String getAlgorithmName() {
        return "GOST3411";
    }

    @Override // org.bouncycastle.crypto.ExtendedDigest
    public int getByteLength() {
        return 32;
    }

    @Override // org.bouncycastle.crypto.Digest
    public int getDigestSize() {
        return 32;
    }

    protected org.bouncycastle.crypto.CryptoServiceProperties j() {
        return org.bouncycastle.crypto.digests.Utils.a(this, 256, this.a);
    }

    protected void m(byte[] bArr, int i) {
        java.lang.System.arraycopy(bArr, i, this.d, 0, 32);
        java.lang.System.arraycopy(this.b, 0, this.q, 0, 32);
        java.lang.System.arraycopy(this.d, 0, this.r, 0, 32);
        for (int i2 = 0; i2 < 32; i2++) {
            this.s[i2] = (byte) (this.q[i2] ^ this.r[i2]);
        }
        f(g(this.s), this.p, 0, this.b, 0);
        for (int i3 = 1; i3 < 4; i3++) {
            byte[] bArrE = e(this.q);
            for (int i4 = 0; i4 < 32; i4++) {
                this.q[i4] = (byte) (bArrE[i4] ^ this.f[i3][i4]);
            }
            this.r = e(e(this.r));
            for (int i5 = 0; i5 < 32; i5++) {
                this.s[i5] = (byte) (this.q[i5] ^ this.r[i5]);
            }
            int i6 = i3 * 8;
            f(g(this.s), this.p, i6, this.b, i6);
        }
        for (int i7 = 0; i7 < 12; i7++) {
            l(this.p);
        }
        for (int i8 = 0; i8 < 32; i8++) {
            byte[] bArr2 = this.p;
            bArr2[i8] = (byte) (bArr2[i8] ^ this.d[i8]);
        }
        l(this.p);
        for (int i9 = 0; i9 < 32; i9++) {
            byte[] bArr3 = this.p;
            bArr3[i9] = (byte) (this.b[i9] ^ bArr3[i9]);
        }
        for (int i10 = 0; i10 < 61; i10++) {
            l(this.p);
        }
        byte[] bArr4 = this.p;
        byte[] bArr5 = this.b;
        java.lang.System.arraycopy(bArr4, 0, bArr5, 0, bArr5.length);
    }

    @Override // org.bouncycastle.crypto.Digest
    public void reset() {
        this.i = 0L;
        this.h = 0;
        int i = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i >= bArr.length) {
                break;
            }
            bArr[i] = 0;
            i++;
        }
        int i2 = 0;
        while (true) {
            byte[] bArr2 = this.c;
            if (i2 >= bArr2.length) {
                break;
            }
            bArr2[i2] = 0;
            i2++;
        }
        int i3 = 0;
        while (true) {
            byte[] bArr3 = this.d;
            if (i3 >= bArr3.length) {
                break;
            }
            bArr3[i3] = 0;
            i3++;
        }
        int i4 = 0;
        while (true) {
            byte[] bArr4 = this.f[1];
            if (i4 >= bArr4.length) {
                break;
            }
            bArr4[i4] = 0;
            i4++;
        }
        int i5 = 0;
        while (true) {
            byte[] bArr5 = this.f[3];
            if (i5 >= bArr5.length) {
                break;
            }
            bArr5[i5] = 0;
            i5++;
        }
        int i6 = 0;
        while (true) {
            byte[] bArr6 = this.e;
            if (i6 >= bArr6.length) {
                break;
            }
            bArr6[i6] = 0;
            i6++;
        }
        int i7 = 0;
        while (true) {
            byte[] bArr7 = this.g;
            if (i7 >= bArr7.length) {
                byte[] bArr8 = t;
                java.lang.System.arraycopy(bArr8, 0, this.f[2], 0, bArr8.length);
                return;
            } else {
                bArr7[i7] = 0;
                i7++;
            }
        }
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte b) {
        byte[] bArr = this.g;
        int i = this.h;
        int i2 = i + 1;
        this.h = i2;
        bArr[i] = b;
        if (i2 == bArr.length) {
            n(bArr);
            m(this.g, 0);
            this.h = 0;
        }
        this.i++;
    }

    public GOST3411Digest(org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose) {
        this.b = new byte[32];
        this.c = new byte[32];
        this.d = new byte[32];
        this.e = new byte[32];
        this.f = (byte[][]) java.lang.reflect.Array.newInstance((java.lang.Class<?>) java.lang.Byte.TYPE, 4, 32);
        this.g = new byte[32];
        this.j = new org.bouncycastle.crypto.engines.GOST28147Engine();
        this.l = new byte[32];
        this.m = new byte[8];
        this.n = new short[16];
        this.o = new short[16];
        this.p = new byte[32];
        this.q = new byte[32];
        this.r = new byte[32];
        this.s = new byte[32];
        this.a = cryptoServicePurpose;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(j());
        byte[] bArrJ = org.bouncycastle.crypto.engines.GOST28147Engine.j("D-A");
        this.k = bArrJ;
        this.j.init(true, new org.bouncycastle.crypto.params.ParametersWithSBox(null, bArrJ));
        reset();
    }

    @Override // org.bouncycastle.crypto.Digest
    public void update(byte[] bArr, int i, int i2) {
        while (this.h != 0 && i2 > 0) {
            update(bArr[i]);
            i++;
            i2--;
        }
        while (true) {
            byte[] bArr2 = this.g;
            if (i2 < bArr2.length) {
                break;
            }
            java.lang.System.arraycopy(bArr, i, bArr2, 0, bArr2.length);
            n(this.g);
            m(this.g, 0);
            byte[] bArr3 = this.g;
            i += bArr3.length;
            i2 -= bArr3.length;
            this.i += (long) bArr3.length;
        }
        while (i2 > 0) {
            update(bArr[i]);
            i++;
            i2--;
        }
    }

    public GOST3411Digest(org.bouncycastle.crypto.digests.GOST3411Digest gOST3411Digest) {
        this.b = new byte[32];
        this.c = new byte[32];
        this.d = new byte[32];
        this.e = new byte[32];
        this.f = (byte[][]) java.lang.reflect.Array.newInstance((java.lang.Class<?>) java.lang.Byte.TYPE, 4, 32);
        this.g = new byte[32];
        this.j = new org.bouncycastle.crypto.engines.GOST28147Engine();
        this.l = new byte[32];
        this.m = new byte[8];
        this.n = new short[16];
        this.o = new short[16];
        this.p = new byte[32];
        this.q = new byte[32];
        this.r = new byte[32];
        this.s = new byte[32];
        this.a = gOST3411Digest.a;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(j());
        d(gOST3411Digest);
    }

    public GOST3411Digest(byte[] bArr) {
        this(bArr, org.bouncycastle.crypto.CryptoServicePurpose.ANY);
    }

    public GOST3411Digest(byte[] bArr, org.bouncycastle.crypto.CryptoServicePurpose cryptoServicePurpose) {
        this.b = new byte[32];
        this.c = new byte[32];
        this.d = new byte[32];
        this.e = new byte[32];
        this.f = (byte[][]) java.lang.reflect.Array.newInstance((java.lang.Class<?>) java.lang.Byte.TYPE, 4, 32);
        this.g = new byte[32];
        this.j = new org.bouncycastle.crypto.engines.GOST28147Engine();
        this.l = new byte[32];
        this.m = new byte[8];
        this.n = new short[16];
        this.o = new short[16];
        this.p = new byte[32];
        this.q = new byte[32];
        this.r = new byte[32];
        this.s = new byte[32];
        this.a = cryptoServicePurpose;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(j());
        byte[] bArrJ = org.bouncycastle.util.Arrays.j(bArr);
        this.k = bArrJ;
        this.j.init(true, new org.bouncycastle.crypto.params.ParametersWithSBox(null, bArrJ));
        reset();
    }
}
