package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class Salsa20Engine implements org.bouncycastle.crypto.SkippingStreamCipher {
    private static final int[] j = org.bouncycastle.util.Pack.t(org.bouncycastle.util.Strings.f("expand 16-byte kexpand 32-byte k"), 0, 8);
    protected static final byte[] k = org.bouncycastle.util.Strings.f("expand 32-byte k");
    protected static final byte[] l = org.bouncycastle.util.Strings.f("expand 16-byte k");
    protected int a;
    private int b;
    protected int[] c;
    protected int[] d;
    private byte[] e;
    private boolean f;
    private int g;
    private int h;
    private int i;

    public Salsa20Engine() {
        this(20);
    }

    private boolean g() {
        int i = this.g + 1;
        this.g = i;
        if (i == 0) {
            int i2 = this.h + 1;
            this.h = i2;
            if (i2 == 0) {
                int i3 = this.i + 1;
                this.i = i3;
                if ((i3 & 32) != 0) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean h(int i) {
        int i2 = this.g + i;
        this.g = i2;
        if (i2 < i && i2 >= 0) {
            int i3 = this.h + 1;
            this.h = i3;
            if (i3 == 0) {
                int i4 = this.i + 1;
                this.i = i4;
                if ((i4 & 32) != 0) {
                    return true;
                }
            }
        }
        return false;
    }

    private void k() {
        this.g = 0;
        this.h = 0;
        this.i = 0;
    }

    public static void n(int i, int[] iArr, int[] iArr2) {
        if (iArr.length != 16) {
            throw new java.lang.IllegalArgumentException();
        }
        if (iArr2.length != 16) {
            throw new java.lang.IllegalArgumentException();
        }
        if (i % 2 != 0) {
            throw new java.lang.IllegalArgumentException("Number of rounds must be even");
        }
        int i2 = iArr[0];
        int i3 = iArr[1];
        int i4 = iArr[2];
        char c = 3;
        int i5 = iArr[3];
        char c2 = 4;
        int i6 = iArr[4];
        char c3 = 5;
        int i7 = iArr[5];
        char c4 = 6;
        int i8 = iArr[6];
        int i9 = 7;
        int i10 = iArr[7];
        int i11 = iArr[8];
        int i12 = 9;
        int i13 = iArr[9];
        int i14 = iArr[10];
        int i15 = iArr[11];
        int i16 = iArr[12];
        int i17 = 13;
        int i18 = iArr[13];
        int i19 = iArr[14];
        int iD = iArr[15];
        int iD2 = i19;
        int iD3 = i18;
        int iD4 = i16;
        int iD5 = i15;
        int iD6 = i14;
        int i20 = i13;
        int i21 = i11;
        int i22 = i10;
        int i23 = i8;
        int iD7 = i7;
        int i24 = i6;
        int i25 = i5;
        int i26 = i4;
        int i27 = i3;
        int iD8 = i2;
        int i28 = i;
        while (true) {
            char c5 = c;
            if (i28 <= 0) {
                char c6 = c2;
                char c7 = c3;
                char c8 = c4;
                iArr2[0] = iD8 + iArr[0];
                iArr2[1] = i27 + iArr[1];
                iArr2[2] = i26 + iArr[2];
                iArr2[c5] = i25 + iArr[c5];
                iArr2[c6] = i24 + iArr[c6];
                iArr2[c7] = iD7 + iArr[c7];
                iArr2[c8] = i23 + iArr[c8];
                iArr2[7] = i22 + iArr[7];
                iArr2[8] = i21 + iArr[8];
                iArr2[9] = i20 + iArr[9];
                iArr2[10] = iD6 + iArr[10];
                iArr2[11] = iD5 + iArr[11];
                iArr2[12] = iD4 + iArr[12];
                iArr2[13] = iD3 + iArr[13];
                iArr2[14] = iD2 + iArr[14];
                iArr2[15] = iD + iArr[15];
                return;
            }
            int iD9 = org.bouncycastle.util.Integers.d(iD8 + iD4, i9) ^ i24;
            int iD10 = i21 ^ org.bouncycastle.util.Integers.d(iD9 + iD8, i12);
            char c9 = c2;
            int iD11 = iD4 ^ org.bouncycastle.util.Integers.d(iD10 + iD9, i17);
            char c10 = c3;
            char c11 = c4;
            int iD12 = iD8 ^ org.bouncycastle.util.Integers.d(iD11 + iD10, 18);
            int iD13 = i20 ^ org.bouncycastle.util.Integers.d(iD7 + i27, i9);
            int iD14 = iD3 ^ org.bouncycastle.util.Integers.d(iD13 + iD7, i12);
            int iD15 = org.bouncycastle.util.Integers.d(iD14 + iD13, i17) ^ i27;
            int iD16 = org.bouncycastle.util.Integers.d(iD15 + iD14, 18) ^ iD7;
            int iD17 = iD2 ^ org.bouncycastle.util.Integers.d(iD6 + i23, 7);
            int iD18 = i26 ^ org.bouncycastle.util.Integers.d(iD17 + iD6, 9);
            int iD19 = i23 ^ org.bouncycastle.util.Integers.d(iD18 + iD17, 13);
            int iD20 = iD6 ^ org.bouncycastle.util.Integers.d(iD19 + iD18, 18);
            int iD21 = i25 ^ org.bouncycastle.util.Integers.d(iD + iD5, 7);
            int iD22 = i22 ^ org.bouncycastle.util.Integers.d(iD21 + iD, 9);
            int iD23 = iD5 ^ org.bouncycastle.util.Integers.d(iD22 + iD21, 13);
            int iD24 = iD ^ org.bouncycastle.util.Integers.d(iD23 + iD22, 18);
            int iD25 = iD15 ^ org.bouncycastle.util.Integers.d(iD12 + iD21, 7);
            int iD26 = org.bouncycastle.util.Integers.d(iD25 + iD12, 9) ^ iD18;
            int iD27 = iD21 ^ org.bouncycastle.util.Integers.d(iD26 + iD25, 13);
            iD8 = iD12 ^ org.bouncycastle.util.Integers.d(iD27 + iD26, 18);
            int iD28 = org.bouncycastle.util.Integers.d(iD16 + iD9, 7) ^ iD19;
            int iD29 = org.bouncycastle.util.Integers.d(iD28 + iD16, 9) ^ iD22;
            int iD30 = iD9 ^ org.bouncycastle.util.Integers.d(iD29 + iD28, 13);
            iD7 = iD16 ^ org.bouncycastle.util.Integers.d(iD30 + iD29, 18);
            iD5 = iD23 ^ org.bouncycastle.util.Integers.d(iD20 + iD13, 7);
            int iD31 = org.bouncycastle.util.Integers.d(iD5 + iD20, 9) ^ iD10;
            int iD32 = org.bouncycastle.util.Integers.d(iD31 + iD5, 13) ^ iD13;
            iD6 = iD20 ^ org.bouncycastle.util.Integers.d(iD32 + iD31, 18);
            iD4 = iD11 ^ org.bouncycastle.util.Integers.d(iD24 + iD17, 7);
            iD3 = iD14 ^ org.bouncycastle.util.Integers.d(iD4 + iD24, 9);
            iD2 = iD17 ^ org.bouncycastle.util.Integers.d(iD3 + iD4, 13);
            iD = iD24 ^ org.bouncycastle.util.Integers.d(iD2 + iD3, 18);
            i28 -= 2;
            i21 = iD31;
            i27 = iD25;
            i23 = iD28;
            i24 = iD30;
            i22 = iD29;
            i20 = iD32;
            c = c5;
            c2 = c9;
            c3 = c10;
            c4 = c11;
            i9 = 7;
            i26 = iD26;
            i25 = iD27;
            i12 = 9;
            i17 = 13;
        }
    }

    protected void a() {
        int[] iArr = this.c;
        int i = iArr[8] + 1;
        iArr[8] = i;
        if (i == 0) {
            iArr[9] = iArr[9] + 1;
        }
    }

    protected void b(long j2) {
        int i = (int) (j2 >>> 32);
        int i2 = (int) j2;
        if (i > 0) {
            int[] iArr = this.c;
            iArr[9] = iArr[9] + i;
        }
        int[] iArr2 = this.c;
        int i3 = iArr2[8];
        int i4 = i2 + i3;
        iArr2[8] = i4;
        if (i3 == 0 || i4 >= i3) {
            return;
        }
        iArr2[9] = iArr2[9] + 1;
    }

    protected void c(byte[] bArr) {
        n(this.a, this.c, this.d);
        org.bouncycastle.util.Pack.n(this.d, bArr, 0);
    }

    protected long d() {
        int[] iArr = this.c;
        return (((long) iArr[9]) << 32) | (((long) iArr[8]) & 4294967295L);
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public byte e(byte b) {
        if (g()) {
            throw new org.bouncycastle.crypto.MaxBytesExceededException("2^70 byte limit per IV; Change IV");
        }
        byte[] bArr = this.e;
        int i = this.b;
        byte b2 = (byte) (b ^ bArr[i]);
        int i2 = (i + 1) & 63;
        this.b = i2;
        if (i2 == 0) {
            a();
            c(this.e);
        }
        return b2;
    }

    protected int f() {
        return 8;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public java.lang.String getAlgorithmName() {
        if (this.a == 20) {
            return "Salsa20";
        }
        return "Salsa20/" + this.a;
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long getPosition() {
        return (d() * 64) + ((long) this.b);
    }

    protected void i(int i, int[] iArr, int i2) {
        int i3 = (i - 16) / 4;
        int[] iArr2 = j;
        iArr[i2] = iArr2[i3];
        iArr[i2 + 1] = iArr2[i3 + 1];
        iArr[i2 + 2] = iArr2[i3 + 2];
        iArr[i2 + 3] = iArr2[i3 + 3];
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException(getAlgorithmName() + " Init parameters must include an IV");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        byte[] bArrA = parametersWithIV.a();
        if (bArrA == null || bArrA.length != f()) {
            throw new java.lang.IllegalArgumentException(getAlgorithmName() + " requires exactly " + f() + " bytes of IV");
        }
        org.bouncycastle.crypto.CipherParameters cipherParametersB = parametersWithIV.b();
        if (cipherParametersB == null) {
            if (!this.f) {
                throw new java.lang.IllegalStateException(getAlgorithmName() + " KeyParameter can not be null for first initialisation");
            }
            o(null, bArrA);
        } else {
            if (!(cipherParametersB instanceof org.bouncycastle.crypto.params.KeyParameter)) {
                throw new java.lang.IllegalArgumentException(getAlgorithmName() + " Init parameters must contain a KeyParameter (or null for re-init)");
            }
            byte[] bArrB = ((org.bouncycastle.crypto.params.KeyParameter) cipherParametersB).b();
            o(bArrB, bArrA);
            org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(getAlgorithmName(), bArrB.length * 8, cipherParameters, org.bouncycastle.crypto.engines.Utils.a(z)));
        }
        reset();
        this.f = true;
    }

    protected void j() {
        int[] iArr = this.c;
        iArr[9] = 0;
        iArr[8] = 0;
    }

    protected void l() {
        int[] iArr = this.c;
        int i = iArr[8];
        if (i == 0 && iArr[9] == 0) {
            throw new java.lang.IllegalStateException("attempt to reduce counter past zero.");
        }
        int i2 = i - 1;
        iArr[8] = i2;
        if (i2 == -1) {
            iArr[9] = iArr[9] - 1;
        }
    }

    protected void m(long j2) {
        int i = (int) (j2 >>> 32);
        int i2 = (int) j2;
        if (i != 0) {
            int[] iArr = this.c;
            int i3 = iArr[9];
            if ((((long) i3) & 4294967295L) < (((long) i) & 4294967295L)) {
                throw new java.lang.IllegalStateException("attempt to reduce counter past zero.");
            }
            iArr[9] = i3 - i;
        }
        int[] iArr2 = this.c;
        int i4 = iArr2[8];
        if ((((long) i4) & 4294967295L) >= (4294967295L & ((long) i2))) {
            iArr2[8] = i4 - i2;
            return;
        }
        int i5 = iArr2[9];
        if (i5 == 0) {
            throw new java.lang.IllegalStateException("attempt to reduce counter past zero.");
        }
        iArr2[9] = i5 - 1;
        iArr2[8] = i4 - i2;
    }

    protected void o(byte[] bArr, byte[] bArr2) {
        if (bArr != null) {
            if (bArr.length != 16 && bArr.length != 32) {
                throw new java.lang.IllegalArgumentException(getAlgorithmName() + " requires 128 bit or 256 bit key");
            }
            int length = (bArr.length - 16) / 4;
            int[] iArr = this.c;
            int[] iArr2 = j;
            iArr[0] = iArr2[length];
            iArr[5] = iArr2[length + 1];
            iArr[10] = iArr2[length + 2];
            iArr[15] = iArr2[length + 3];
            org.bouncycastle.util.Pack.s(bArr, 0, iArr, 1, 4);
            org.bouncycastle.util.Pack.s(bArr, bArr.length - 16, this.c, 11, 4);
        }
        org.bouncycastle.util.Pack.s(bArr2, 0, this.c, 6, 2);
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (!this.f) {
            throw new java.lang.IllegalStateException(getAlgorithmName() + " not initialised");
        }
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i3 + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        if (h(i2)) {
            throw new org.bouncycastle.crypto.MaxBytesExceededException("2^70 byte limit per IV would be exceeded; Change IV");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            byte[] bArr3 = this.e;
            int i5 = this.b;
            bArr2[i4 + i3] = (byte) (bArr3[i5] ^ bArr[i4 + i]);
            int i6 = (i5 + 1) & 63;
            this.b = i6;
            if (i6 == 0) {
                a();
                c(this.e);
            }
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void reset() {
        this.b = 0;
        k();
        j();
        c(this.e);
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long seekTo(long j2) {
        reset();
        return skip(j2);
    }

    @Override // org.bouncycastle.crypto.SkippingCipher
    public long skip(long j2) {
        long j3;
        if (j2 >= 0) {
            if (j2 >= 64) {
                long j4 = j2 / 64;
                b(j4);
                j3 = j2 - (j4 * 64);
            } else {
                j3 = j2;
            }
            int i = this.b;
            int i2 = (((int) j3) + i) & 63;
            this.b = i2;
            if (i2 < i) {
                a();
            }
        } else {
            long j5 = -j2;
            if (j5 >= 64) {
                long j6 = j5 / 64;
                m(j6);
                j5 -= j6 * 64;
            }
            for (long j7 = 0; j7 < j5; j7++) {
                if (this.b == 0) {
                    l();
                }
                this.b = (this.b - 1) & 63;
            }
        }
        c(this.e);
        return j2;
    }

    public Salsa20Engine(int i) {
        this.b = 0;
        this.c = new int[16];
        this.d = new int[16];
        this.e = new byte[64];
        this.f = false;
        if (i <= 0 || (i & 1) != 0) {
            throw new java.lang.IllegalArgumentException("'rounds' must be a positive, even number");
        }
        this.a = i;
    }
}
