package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class Poly1305 implements org.bouncycastle.crypto.Mac {
    private final org.bouncycastle.crypto.BlockCipher a;
    private final byte[] b;
    private int c;
    private int d;
    private int e;
    private int f;
    private int g;
    private int h;
    private int i;
    private int j;
    private int k;
    private int l;
    private int m;
    private int n;
    private int o;
    private final byte[] p;
    private int q;
    private int r;
    private int s;
    private int t;
    private int u;
    private int v;

    public Poly1305() {
        this.b = new byte[1];
        this.p = new byte[16];
        this.q = 0;
        this.a = null;
    }

    private static final long a(int i, int i2) {
        return (((long) i) & 4294967295L) * ((long) i2);
    }

    private void b() {
        int i = this.q;
        if (i < 16) {
            this.p[i] = 1;
            for (int i2 = i + 1; i2 < 16; i2++) {
                this.p[i2] = 0;
            }
        }
        long jQ = org.bouncycastle.util.Pack.q(this.p, 0);
        long j = jQ & 4294967295L;
        long jQ2 = ((long) org.bouncycastle.util.Pack.q(this.p, 4)) & 4294967295L;
        long jQ3 = ((long) org.bouncycastle.util.Pack.q(this.p, 8)) & 4294967295L;
        long jQ4 = 4294967295L & ((long) org.bouncycastle.util.Pack.q(this.p, 12));
        int i3 = (int) (((long) this.r) + (jQ & 67108863));
        this.r = i3;
        this.s = (int) (((long) this.s) + ((((jQ2 << 32) | j) >>> 26) & 67108863));
        this.t = (int) (((long) this.t) + (((jQ2 | (jQ3 << 32)) >>> 20) & 67108863));
        this.u = (int) (((long) this.u) + ((((jQ4 << 32) | jQ3) >>> 14) & 67108863));
        int i4 = (int) (((long) this.v) + (jQ4 >>> 8));
        this.v = i4;
        if (this.q == 16) {
            this.v = i4 + 16777216;
        }
        long jA = a(i3, this.c) + a(this.s, this.k) + a(this.t, this.j) + a(this.u, this.i) + a(this.v, this.h);
        long jA2 = a(this.r, this.d) + a(this.s, this.c) + a(this.t, this.k) + a(this.u, this.j) + a(this.v, this.i);
        long jA3 = a(this.r, this.e) + a(this.s, this.d) + a(this.t, this.c) + a(this.u, this.k) + a(this.v, this.j);
        long jA4 = a(this.r, this.f) + a(this.s, this.e) + a(this.t, this.d) + a(this.u, this.c) + a(this.v, this.k);
        long jA5 = a(this.r, this.g) + a(this.s, this.f) + a(this.t, this.e) + a(this.u, this.d) + a(this.v, this.c);
        long j2 = jA2 + (jA >>> 26);
        long j3 = jA3 + (j2 >>> 26);
        this.t = ((int) j3) & 67108863;
        long j4 = jA4 + (j3 >>> 26);
        this.u = ((int) j4) & 67108863;
        long j5 = jA5 + (j4 >>> 26);
        this.v = ((int) j5) & 67108863;
        int i5 = (((int) jA) & 67108863) + (((int) (j5 >>> 26)) * 5);
        this.s = (((int) j2) & 67108863) + (i5 >>> 26);
        this.r = i5 & 67108863;
    }

    private void c(byte[] bArr, byte[] bArr2) {
        if (bArr.length != 32) {
            throw new java.lang.IllegalArgumentException("Poly1305 key must be 256 bits.");
        }
        int i = 16;
        if (this.a != null && (bArr2 == null || bArr2.length != 16)) {
            throw new java.lang.IllegalArgumentException("Poly1305 requires a 128 bit IV.");
        }
        int iQ = org.bouncycastle.util.Pack.q(bArr, 0);
        int iQ2 = org.bouncycastle.util.Pack.q(bArr, 4);
        int iQ3 = org.bouncycastle.util.Pack.q(bArr, 8);
        int iQ4 = org.bouncycastle.util.Pack.q(bArr, 12);
        this.c = 67108863 & iQ;
        int i2 = ((iQ >>> 26) | (iQ2 << 6)) & 67108611;
        this.d = i2;
        int i3 = ((iQ2 >>> 20) | (iQ3 << 12)) & 67092735;
        this.e = i3;
        int i4 = ((iQ3 >>> 14) | (iQ4 << 18)) & 66076671;
        this.f = i4;
        int i5 = (iQ4 >>> 8) & 1048575;
        this.g = i5;
        this.h = i2 * 5;
        this.i = i3 * 5;
        this.j = i4 * 5;
        this.k = i5 * 5;
        org.bouncycastle.crypto.BlockCipher blockCipher = this.a;
        if (blockCipher != null) {
            byte[] bArr3 = new byte[16];
            blockCipher.init(true, new org.bouncycastle.crypto.params.KeyParameter(bArr, 16, 16));
            this.a.c(bArr2, 0, bArr3, 0);
            i = 0;
            bArr = bArr3;
        }
        this.l = org.bouncycastle.util.Pack.q(bArr, i);
        this.m = org.bouncycastle.util.Pack.q(bArr, i + 4);
        this.n = org.bouncycastle.util.Pack.q(bArr, i + 8);
        this.o = org.bouncycastle.util.Pack.q(bArr, i + 12);
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        if (i + 16 > bArr.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer is too short.");
        }
        if (this.q > 0) {
            b();
        }
        int i2 = this.s;
        int i3 = this.r;
        int i4 = i2 + (i3 >>> 26);
        int i5 = this.t + (i4 >>> 26);
        int i6 = this.u + (i5 >>> 26);
        int i7 = i5 & 67108863;
        int i8 = this.v + (i6 >>> 26);
        int i9 = i6 & 67108863;
        int i10 = (i3 & 67108863) + ((i8 >>> 26) * 5);
        int i11 = i8 & 67108863;
        int i12 = (i4 & 67108863) + (i10 >>> 26);
        int i13 = i10 & 67108863;
        int i14 = i13 + 5;
        int i15 = (i14 >>> 26) + i12;
        int i16 = (i15 >>> 26) + i7;
        int i17 = (i16 >>> 26) + i9;
        int i18 = 67108863 & i17;
        int i19 = ((i17 >>> 26) + i11) - 67108864;
        int i20 = (i19 >>> 31) - 1;
        int i21 = ~i20;
        int i22 = (i13 & i21) | (i14 & 67108863 & i20);
        this.r = i22;
        int i23 = (i12 & i21) | (i15 & 67108863 & i20);
        this.s = i23;
        int i24 = (i7 & i21) | (i16 & 67108863 & i20);
        this.t = i24;
        int i25 = (i18 & i20) | (i9 & i21);
        this.u = i25;
        int i26 = (i11 & i21) | (i19 & i20);
        this.v = i26;
        long j = (((long) (i22 | (i23 << 26))) & 4294967295L) + (((long) this.l) & 4294967295L);
        long j2 = (((long) ((i23 >>> 6) | (i24 << 20))) & 4294967295L) + (((long) this.m) & 4294967295L);
        long j3 = (((long) ((i24 >>> 12) | (i25 << 14))) & 4294967295L) + (((long) this.n) & 4294967295L);
        long j4 = (((long) ((i25 >>> 18) | (i26 << 8))) & 4294967295L) + (4294967295L & ((long) this.o));
        org.bouncycastle.util.Pack.l((int) j, bArr, i);
        long j5 = j2 + (j >>> 32);
        org.bouncycastle.util.Pack.l((int) j5, bArr, i + 4);
        long j6 = j3 + (j5 >>> 32);
        org.bouncycastle.util.Pack.l((int) j6, bArr, i + 8);
        org.bouncycastle.util.Pack.l((int) (j4 + (j6 >>> 32)), bArr, i + 12);
        reset();
        return 16;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        if (this.a == null) {
            return "Poly1305";
        }
        return "Poly1305-" + this.a.getAlgorithmName();
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return 16;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArrA;
        if (this.a == null) {
            bArrA = null;
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("Poly1305 requires an IV when used with a block cipher.");
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            bArrA = parametersWithIV.a();
            cipherParameters = parametersWithIV.b();
        }
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("Poly1305 requires a key.");
        }
        c(((org.bouncycastle.crypto.params.KeyParameter) cipherParameters).b(), bArrA);
        reset();
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        this.q = 0;
        this.v = 0;
        this.u = 0;
        this.t = 0;
        this.s = 0;
        this.r = 0;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        byte[] bArr = this.b;
        bArr[0] = b;
        update(bArr, 0, 1);
    }

    public Poly1305(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.b = new byte[1];
        this.p = new byte[16];
        this.q = 0;
        if (blockCipher.b() != 16) {
            throw new java.lang.IllegalArgumentException("Poly1305 requires a 128 bit block cipher.");
        }
        this.a = blockCipher;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        int i3 = 0;
        while (i2 > i3) {
            if (this.q == 16) {
                b();
                this.q = 0;
            }
            int iMin = java.lang.Math.min(i2 - i3, 16 - this.q);
            java.lang.System.arraycopy(bArr, i3 + i, this.p, this.q, iMin);
            i3 += iMin;
            this.q += iMin;
        }
    }
}
