package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RFC5649WrapEngine implements org.bouncycastle.crypto.Wrapper {
    private static final byte[] e = {-90, 89, 89, -90};
    private final org.bouncycastle.crypto.BlockCipher a;
    private final byte[] b = new byte[4];
    private org.bouncycastle.crypto.params.KeyParameter c = null;
    private boolean d = true;

    public RFC5649WrapEngine(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.a = blockCipher;
    }

    private byte[] c(byte[] bArr) {
        int length = bArr.length;
        int i = (8 - (length % 8)) % 8;
        byte[] bArr2 = new byte[length + i];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, length);
        if (i != 0) {
            java.lang.System.arraycopy(new byte[i], 0, bArr2, length, i);
        }
        return bArr2;
    }

    private byte[] d(byte[] bArr, int i, int i2, byte[] bArr2) {
        int i3 = i2 - 8;
        byte[] bArr3 = new byte[i3];
        byte[] bArr4 = new byte[16];
        java.lang.System.arraycopy(bArr, i, bArr4, 0, 8);
        java.lang.System.arraycopy(bArr, i + 8, bArr3, 0, i3);
        this.a.init(false, this.c);
        int i4 = (i2 / 8) - 1;
        for (int i5 = 5; i5 >= 0; i5--) {
            for (int i6 = i4; i6 >= 1; i6--) {
                int i7 = (i6 - 1) * 8;
                java.lang.System.arraycopy(bArr3, i7, bArr4, 8, 8);
                int i8 = (i4 * i5) + i6;
                int i9 = 1;
                while (i8 != 0) {
                    int i10 = 8 - i9;
                    bArr4[i10] = (byte) (bArr4[i10] ^ ((byte) i8));
                    i8 >>>= 8;
                    i9++;
                }
                this.a.c(bArr4, 0, bArr4, 0);
                java.lang.System.arraycopy(bArr4, 8, bArr3, i7, 8);
            }
        }
        java.lang.System.arraycopy(bArr4, 0, bArr2, 0, 8);
        return bArr3;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] a(byte[] bArr, int i, int i2) {
        if (!this.d) {
            throw new java.lang.IllegalStateException("not set for wrapping");
        }
        byte[] bArr2 = new byte[8];
        java.lang.System.arraycopy(this.b, 0, bArr2, 0, 4);
        org.bouncycastle.util.Pack.h(i2, bArr2, 4);
        byte[] bArr3 = new byte[i2];
        java.lang.System.arraycopy(bArr, i, bArr3, 0, i2);
        byte[] bArrC = c(bArr3);
        if (bArrC.length != 8) {
            org.bouncycastle.crypto.engines.RFC3394WrapEngine rFC3394WrapEngine = new org.bouncycastle.crypto.engines.RFC3394WrapEngine(this.a);
            rFC3394WrapEngine.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(this.c, bArr2));
            return rFC3394WrapEngine.a(bArrC, 0, bArrC.length);
        }
        int length = bArrC.length + 8;
        byte[] bArr4 = new byte[length];
        java.lang.System.arraycopy(bArr2, 0, bArr4, 0, 8);
        java.lang.System.arraycopy(bArrC, 0, bArr4, 8, bArrC.length);
        this.a.init(true, this.c);
        int iB = this.a.b();
        for (int i3 = 0; i3 < length; i3 += iB) {
            this.a.c(bArr4, i3, bArr4, i3);
        }
        return bArr4;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] b(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        byte[] bArrD;
        if (this.d) {
            throw new java.lang.IllegalStateException("not set for unwrapping");
        }
        int i3 = i2 / 8;
        if (i3 * 8 != i2) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("unwrap data must be a multiple of 8 bytes");
        }
        if (i3 <= 1) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("unwrap data must be at least 16 bytes");
        }
        byte[] bArr2 = new byte[i2];
        java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
        byte[] bArr3 = new byte[i2];
        byte[] bArr4 = new byte[8];
        if (i3 == 2) {
            this.a.init(false, this.c);
            int iB = this.a.b();
            for (int i4 = 0; i4 < i2; i4 += iB) {
                this.a.c(bArr2, i4, bArr3, i4);
            }
            java.lang.System.arraycopy(bArr3, 0, bArr4, 0, 8);
            int i5 = i2 - 8;
            bArrD = new byte[i5];
            java.lang.System.arraycopy(bArr3, 8, bArrD, 0, i5);
        } else {
            bArrD = d(bArr, i, i2, bArr4);
        }
        int i6 = 4;
        byte[] bArr5 = new byte[4];
        java.lang.System.arraycopy(bArr4, 0, bArr5, 0, 4);
        int iA = org.bouncycastle.util.Pack.a(bArr4, 4);
        boolean z = org.bouncycastle.util.Arrays.z(bArr5, this.b);
        int length = bArrD.length;
        if (iA <= length - 8) {
            z = false;
        }
        if (iA > length) {
            z = false;
        }
        int i7 = length - iA;
        if (i7 >= 8 || i7 < 0) {
            z = false;
        } else {
            i6 = i7;
        }
        byte[] bArr6 = new byte[i6];
        java.lang.System.arraycopy(bArrD, bArrD.length - i6, bArr6, 0, i6);
        if (!org.bouncycastle.util.Arrays.z(bArr6, new byte[i6])) {
            z = false;
        }
        if (!z) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("checksum failed");
        }
        byte[] bArr7 = new byte[iA];
        java.lang.System.arraycopy(bArrD, 0, bArr7, 0, iA);
        return bArr7;
    }

    @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.d = 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.c = (org.bouncycastle.crypto.params.KeyParameter) cipherParameters;
            java.lang.System.arraycopy(e, 0, this.b, 0, 4);
        } 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 != 4) {
                throw new java.lang.IllegalArgumentException("IV length not equal to 4");
            }
            this.c = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
            java.lang.System.arraycopy(bArrA, 0, this.b, 0, 4);
        }
    }
}
