package org.bouncycastle.crypto.macs;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ISO9797Alg3Mac implements org.bouncycastle.crypto.Mac {
    private byte[] a;
    private byte[] b;
    private int c;
    private org.bouncycastle.crypto.BlockCipher d;
    private org.bouncycastle.crypto.paddings.BlockCipherPadding e;
    private int f;
    private org.bouncycastle.crypto.params.KeyParameter g;
    private org.bouncycastle.crypto.params.KeyParameter h;

    public ISO9797Alg3Mac(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, blockCipher.b() * 8, null);
    }

    @Override // org.bouncycastle.crypto.Mac
    public int doFinal(byte[] bArr, int i) {
        int iB = this.d.b();
        if (this.e == null) {
            while (true) {
                int i2 = this.c;
                if (i2 >= iB) {
                    break;
                }
                this.b[i2] = 0;
                this.c = i2 + 1;
            }
        } else {
            if (this.c == iB) {
                this.d.c(this.b, 0, this.a, 0);
                this.c = 0;
            }
            this.e.a(this.b, this.c);
        }
        this.d.c(this.b, 0, this.a, 0);
        org.bouncycastle.crypto.engines.DESEngine dESEngine = new org.bouncycastle.crypto.engines.DESEngine();
        dESEngine.init(false, this.g);
        byte[] bArr2 = this.a;
        dESEngine.c(bArr2, 0, bArr2, 0);
        dESEngine.init(true, this.h);
        byte[] bArr3 = this.a;
        dESEngine.c(bArr3, 0, bArr3, 0);
        java.lang.System.arraycopy(this.a, 0, bArr, i, this.f);
        reset();
        return this.f;
    }

    @Override // org.bouncycastle.crypto.Mac
    public java.lang.String getAlgorithmName() {
        return "ISO9797Alg3";
    }

    @Override // org.bouncycastle.crypto.Mac
    public int getMacSize() {
        return this.f;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void init(org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.KeyParameter keyParameter;
        reset();
        boolean z = cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter;
        if (!z && !(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("params must be an instance of KeyParameter or ParametersWithIV");
        }
        byte[] bArrB = (z ? (org.bouncycastle.crypto.params.KeyParameter) cipherParameters : (org.bouncycastle.crypto.params.KeyParameter) ((org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters).b()).b();
        if (bArrB.length == 16) {
            keyParameter = new org.bouncycastle.crypto.params.KeyParameter(bArrB, 0, 8);
            this.g = new org.bouncycastle.crypto.params.KeyParameter(bArrB, 8, 8);
            this.h = keyParameter;
        } else {
            if (bArrB.length != 24) {
                throw new java.lang.IllegalArgumentException("Key must be either 112 or 168 bit long");
            }
            keyParameter = new org.bouncycastle.crypto.params.KeyParameter(bArrB, 0, 8);
            this.g = new org.bouncycastle.crypto.params.KeyParameter(bArrB, 8, 8);
            this.h = new org.bouncycastle.crypto.params.KeyParameter(bArrB, 16, 8);
        }
        boolean z2 = cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV;
        org.bouncycastle.crypto.BlockCipher blockCipher = this.d;
        if (z2) {
            blockCipher.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(keyParameter, ((org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters).a()));
        } else {
            blockCipher.init(true, keyParameter);
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void reset() {
        int i = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i >= bArr.length) {
                this.c = 0;
                this.d.reset();
                return;
            } else {
                bArr[i] = 0;
                i++;
            }
        }
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte b) {
        int i = this.c;
        byte[] bArr = this.b;
        if (i == bArr.length) {
            this.d.c(bArr, 0, this.a, 0);
            this.c = 0;
        }
        byte[] bArr2 = this.b;
        int i2 = this.c;
        this.c = i2 + 1;
        bArr2[i2] = b;
    }

    public ISO9797Alg3Mac(org.bouncycastle.crypto.BlockCipher blockCipher, int i, org.bouncycastle.crypto.paddings.BlockCipherPadding blockCipherPadding) {
        if (i % 8 != 0) {
            throw new java.lang.IllegalArgumentException("MAC size must be multiple of 8");
        }
        if (!(blockCipher instanceof org.bouncycastle.crypto.engines.DESEngine)) {
            throw new java.lang.IllegalArgumentException("cipher must be instance of DESEngine");
        }
        this.d = org.bouncycastle.crypto.modes.CBCBlockCipher.h(blockCipher);
        this.e = blockCipherPadding;
        this.f = i / 8;
        this.a = new byte[blockCipher.b()];
        this.b = new byte[blockCipher.b()];
        this.c = 0;
    }

    @Override // org.bouncycastle.crypto.Mac
    public void update(byte[] bArr, int i, int i2) {
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("Can't have a negative input length!");
        }
        int iB = this.d.b();
        int i3 = this.c;
        int i4 = iB - i3;
        if (i2 > i4) {
            java.lang.System.arraycopy(bArr, i, this.b, i3, i4);
            this.d.c(this.b, 0, this.a, 0);
            this.c = 0;
            i2 -= i4;
            i += i4;
            while (i2 > iB) {
                this.d.c(bArr, i, this.a, 0);
                i2 -= iB;
                i += iB;
            }
        }
        java.lang.System.arraycopy(bArr, i, this.b, this.c, i2);
        this.c += i2;
    }

    public ISO9797Alg3Mac(org.bouncycastle.crypto.BlockCipher blockCipher, org.bouncycastle.crypto.paddings.BlockCipherPadding blockCipherPadding) {
        this(blockCipher, blockCipher.b() * 8, blockCipherPadding);
    }
}
