package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class VMPCEngine implements org.bouncycastle.crypto.StreamCipher {
    protected byte a = 0;
    protected byte[] b = null;
    protected byte c = 0;
    protected byte[] d;
    protected byte[] e;

    protected void a(byte[] bArr, byte[] bArr2) {
        this.c = (byte) 0;
        this.b = new byte[256];
        for (int i = 0; i < 256; i++) {
            this.b[i] = (byte) i;
        }
        for (int i2 = 0; i2 < 768; i2++) {
            byte[] bArr3 = this.b;
            byte b = this.c;
            int i3 = i2 & 255;
            byte b2 = bArr3[i3];
            byte b3 = bArr3[(b + b2 + bArr[i2 % bArr.length]) & 255];
            this.c = b3;
            bArr3[i3] = bArr3[b3 & 255];
            bArr3[b3 & 255] = b2;
        }
        for (int i4 = 0; i4 < 768; i4++) {
            byte[] bArr4 = this.b;
            byte b4 = this.c;
            int i5 = i4 & 255;
            byte b5 = bArr4[i5];
            byte b6 = bArr4[(b4 + b5 + bArr2[i4 % bArr2.length]) & 255];
            this.c = b6;
            bArr4[i5] = bArr4[b6 & 255];
            bArr4[b6 & 255] = b5;
        }
        this.a = (byte) 0;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public byte e(byte b) {
        byte[] bArr = this.b;
        byte b2 = this.c;
        byte b3 = this.a;
        byte b4 = bArr[(b2 + bArr[b3 & 255]) & 255];
        this.c = b4;
        byte b5 = bArr[(bArr[bArr[b4 & 255] & 255] + 1) & 255];
        byte b6 = bArr[b3 & 255];
        bArr[b3 & 255] = bArr[b4 & 255];
        bArr[b4 & 255] = b6;
        this.a = (byte) ((b3 + 1) & 255);
        return (byte) (b ^ b5);
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public java.lang.String getAlgorithmName() {
        return "VMPC";
    }

    @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("VMPC init parameters must include an IV");
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        if (!(parametersWithIV.b() instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("VMPC init parameters must include a key");
        }
        org.bouncycastle.crypto.params.KeyParameter keyParameter = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
        byte[] bArrA = parametersWithIV.a();
        this.d = bArrA;
        if (bArrA == null || bArrA.length < 1 || bArrA.length > 768) {
            throw new java.lang.IllegalArgumentException("VMPC requires 1 to 768 bytes of IV");
        }
        byte[] bArrB = keyParameter.b();
        this.e = bArrB;
        a(bArrB, this.d);
        java.lang.String algorithmName = getAlgorithmName();
        byte[] bArr = this.e;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(new org.bouncycastle.crypto.constraints.DefaultServiceProperties(algorithmName, bArr.length >= 32 ? 256 : bArr.length * 8, cipherParameters, org.bouncycastle.crypto.engines.Utils.a(z)));
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        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");
        }
        for (int i4 = 0; i4 < i2; i4++) {
            byte[] bArr3 = this.b;
            byte b = this.c;
            byte b2 = this.a;
            byte b3 = bArr3[(b + bArr3[b2 & 255]) & 255];
            this.c = b3;
            byte b4 = bArr3[(bArr3[bArr3[b3 & 255] & 255] + 1) & 255];
            byte b5 = bArr3[b2 & 255];
            bArr3[b2 & 255] = bArr3[b3 & 255];
            bArr3[b3 & 255] = b5;
            this.a = (byte) ((b2 + 1) & 255);
            bArr2[i4 + i3] = (byte) (bArr[i4 + i] ^ b4);
        }
        return i2;
    }

    @Override // org.bouncycastle.crypto.StreamCipher
    public void reset() {
        a(this.e, this.d);
    }
}
