package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RFC3394WrapEngine implements org.bouncycastle.crypto.Wrapper {
    private static final byte[] f = {-90, -90, -90, -90, -90, -90, -90, -90};
    private final org.bouncycastle.crypto.BlockCipher a;
    private final boolean b;
    private final byte[] c;
    private org.bouncycastle.crypto.params.KeyParameter d;
    private boolean e;

    public RFC3394WrapEngine(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, false);
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] a(byte[] bArr, int i, int i2) {
        if (!this.e) {
            throw new java.lang.IllegalStateException("not set for wrapping");
        }
        if (i2 < 8) {
            throw new org.bouncycastle.crypto.DataLengthException("wrap data must be at least 8 bytes");
        }
        int i3 = i2 / 8;
        if (i3 * 8 != i2) {
            throw new org.bouncycastle.crypto.DataLengthException("wrap data must be a multiple of 8 bytes");
        }
        this.a.init(this.b, this.d);
        byte[] bArr2 = this.c;
        byte[] bArr3 = new byte[bArr2.length + i2];
        java.lang.System.arraycopy(bArr2, 0, bArr3, 0, bArr2.length);
        java.lang.System.arraycopy(bArr, i, bArr3, this.c.length, i2);
        if (i3 == 1) {
            this.a.c(bArr3, 0, bArr3, 0);
            return bArr3;
        }
        byte[] bArr4 = new byte[this.c.length + 8];
        for (int i4 = 0; i4 != 6; i4++) {
            for (int i5 = 1; i5 <= i3; i5++) {
                java.lang.System.arraycopy(bArr3, 0, bArr4, 0, this.c.length);
                int i6 = i5 * 8;
                java.lang.System.arraycopy(bArr3, i6, bArr4, this.c.length, 8);
                this.a.c(bArr4, 0, bArr4, 0);
                int i7 = (i3 * i4) + i5;
                int i8 = 1;
                while (i7 != 0) {
                    int length = this.c.length - i8;
                    bArr4[length] = (byte) (((byte) i7) ^ bArr4[length]);
                    i7 >>>= 8;
                    i8++;
                }
                java.lang.System.arraycopy(bArr4, 0, bArr3, 0, 8);
                java.lang.System.arraycopy(bArr4, 8, bArr3, i6, 8);
            }
        }
        return bArr3;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] b(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        if (this.e) {
            throw new java.lang.IllegalStateException("not set for unwrapping");
        }
        if (i2 < 16) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("unwrap data too short");
        }
        int i3 = i2 / 8;
        if (i3 * 8 != i2) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("unwrap data must be a multiple of 8 bytes");
        }
        int i4 = 1;
        this.a.init(!this.b, this.d);
        byte[] bArr2 = this.c;
        byte[] bArr3 = new byte[i2 - bArr2.length];
        byte[] bArr4 = new byte[bArr2.length];
        byte[] bArr5 = new byte[bArr2.length + 8];
        int i5 = i3 - 1;
        if (i5 == 1) {
            this.a.c(bArr, i, bArr5, 0);
            java.lang.System.arraycopy(bArr5, 0, bArr4, 0, this.c.length);
            java.lang.System.arraycopy(bArr5, this.c.length, bArr3, 0, 8);
        } else {
            java.lang.System.arraycopy(bArr, i, bArr4, 0, bArr2.length);
            byte[] bArr6 = this.c;
            java.lang.System.arraycopy(bArr, bArr6.length + i, bArr3, 0, i2 - bArr6.length);
            for (int i6 = 5; i6 >= 0; i6--) {
                int i7 = i5;
                while (i7 >= i4) {
                    java.lang.System.arraycopy(bArr4, 0, bArr5, 0, this.c.length);
                    int i8 = (i7 - 1) * 8;
                    java.lang.System.arraycopy(bArr3, i8, bArr5, this.c.length, 8);
                    int i9 = (i5 * i6) + i7;
                    int i10 = i4;
                    while (i9 != 0) {
                        int i11 = i4;
                        int length = this.c.length - i10;
                        bArr5[length] = (byte) (bArr5[length] ^ ((byte) i9));
                        i9 >>>= 8;
                        i10++;
                        i4 = i11;
                    }
                    this.a.c(bArr5, 0, bArr5, 0);
                    java.lang.System.arraycopy(bArr5, 0, bArr4, 0, 8);
                    java.lang.System.arraycopy(bArr5, 8, bArr3, i8, 8);
                    i7--;
                    i4 = i4;
                }
            }
        }
        if (i5 != i4) {
            if (!org.bouncycastle.util.Arrays.z(bArr4, this.c)) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("checksum failed");
            }
        } else if (!org.bouncycastle.util.Arrays.z(bArr4, this.c)) {
            java.lang.System.arraycopy(bArr, i, bArr4, 0, this.c.length);
            byte[] bArr7 = this.c;
            java.lang.System.arraycopy(bArr, i + bArr7.length, bArr3, 0, i2 - bArr7.length);
            for (int i12 = 5; i12 >= 0; i12--) {
                java.lang.System.arraycopy(bArr4, 0, bArr5, 0, this.c.length);
                java.lang.System.arraycopy(bArr3, 0, bArr5, this.c.length, 8);
                int i13 = (i5 * i12) + 1;
                int i14 = 1;
                while (i13 != 0) {
                    int length2 = this.c.length - i14;
                    bArr5[length2] = (byte) (((byte) i13) ^ bArr5[length2]);
                    i13 >>>= 8;
                    i14++;
                }
                this.a.c(bArr5, 0, bArr5, 0);
                java.lang.System.arraycopy(bArr5, 0, bArr4, 0, 8);
                java.lang.System.arraycopy(bArr5, 8, bArr3, 0, 8);
            }
            if (!org.bouncycastle.util.Arrays.z(bArr4, this.c)) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("checksum failed");
            }
        }
        return bArr3;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public java.lang.String getAlgorithmName() {
        return this.a.getAlgorithmName();
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.e = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            cipherParameters = ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a();
        }
        if (cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter) {
            this.d = (org.bouncycastle.crypto.params.KeyParameter) cipherParameters;
            java.lang.System.arraycopy(f, 0, this.c, 0, 8);
        } else if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            byte[] bArrA = parametersWithIV.a();
            if (bArrA.length != 8) {
                throw new java.lang.IllegalArgumentException("IV not equal to 8");
            }
            this.d = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
            java.lang.System.arraycopy(bArrA, 0, this.c, 0, 8);
        }
    }

    public RFC3394WrapEngine(org.bouncycastle.crypto.BlockCipher blockCipher, boolean z) {
        this.c = new byte[8];
        this.d = null;
        this.e = true;
        this.a = blockCipher;
        this.b = !z;
    }
}
