package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DESedeWrapEngine implements org.bouncycastle.crypto.Wrapper {
    private static final byte[] h = {74, -35, -94, 44, 121, -24, 33, 5};
    private org.bouncycastle.crypto.modes.CBCBlockCipher a;
    private org.bouncycastle.crypto.params.KeyParameter b;
    private org.bouncycastle.crypto.params.ParametersWithIV c;
    private byte[] d;
    private boolean e;
    org.bouncycastle.crypto.Digest f = org.bouncycastle.crypto.util.DigestFactory.c();
    byte[] g = new byte[20];

    private byte[] c(byte[] bArr) {
        byte[] bArr2 = new byte[8];
        this.f.update(bArr, 0, bArr.length);
        this.f.doFinal(this.g, 0);
        java.lang.System.arraycopy(this.g, 0, bArr2, 0, 8);
        return bArr2;
    }

    private boolean d(byte[] bArr, byte[] bArr2) {
        return org.bouncycastle.util.Arrays.z(c(bArr), bArr2);
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] a(byte[] bArr, int i, int i2) {
        if (!this.e) {
            throw new java.lang.IllegalStateException("Not initialized for wrapping");
        }
        byte[] bArr2 = new byte[i2];
        java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
        byte[] bArrC = c(bArr2);
        int length = bArrC.length + i2;
        byte[] bArr3 = new byte[length];
        java.lang.System.arraycopy(bArr2, 0, bArr3, 0, i2);
        java.lang.System.arraycopy(bArrC, 0, bArr3, i2, bArrC.length);
        int iB = this.a.b();
        if (length % iB != 0) {
            throw new java.lang.IllegalStateException("Not multiple of block length");
        }
        this.a.init(true, this.c);
        byte[] bArr4 = new byte[length];
        for (int i3 = 0; i3 != length; i3 += iB) {
            this.a.c(bArr3, i3, bArr4, i3);
        }
        byte[] bArr5 = this.d;
        int length2 = bArr5.length + length;
        byte[] bArr6 = new byte[length2];
        java.lang.System.arraycopy(bArr5, 0, bArr6, 0, bArr5.length);
        java.lang.System.arraycopy(bArr4, 0, bArr6, this.d.length, length);
        org.bouncycastle.util.Arrays.h0(bArr6);
        this.a.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(this.b, h));
        for (int i4 = 0; i4 != length2; i4 += iB) {
            this.a.c(bArr6, i4, bArr6, i4);
        }
        return bArr6;
    }

    @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 (bArr == null) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Null pointer as ciphertext");
        }
        int iB = this.a.b();
        if (i2 % iB != 0) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Ciphertext not multiple of " + iB);
        }
        this.a.init(false, new org.bouncycastle.crypto.params.ParametersWithIV(this.b, h));
        byte[] bArr2 = new byte[i2];
        for (int i3 = 0; i3 != i2; i3 += iB) {
            this.a.c(bArr, i + i3, bArr2, i3);
        }
        org.bouncycastle.util.Arrays.h0(bArr2);
        byte[] bArr3 = new byte[8];
        this.d = bArr3;
        int i4 = i2 - 8;
        byte[] bArr4 = new byte[i4];
        java.lang.System.arraycopy(bArr2, 0, bArr3, 0, 8);
        java.lang.System.arraycopy(bArr2, 8, bArr4, 0, i4);
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = new org.bouncycastle.crypto.params.ParametersWithIV(this.b, this.d);
        this.c = parametersWithIV;
        this.a.init(false, parametersWithIV);
        byte[] bArr5 = new byte[i4];
        for (int i5 = 0; i5 != i4; i5 += iB) {
            this.a.c(bArr4, i5, bArr5, i5);
        }
        int i6 = i2 - 16;
        byte[] bArr6 = new byte[i6];
        byte[] bArr7 = new byte[8];
        java.lang.System.arraycopy(bArr5, 0, bArr6, 0, i6);
        java.lang.System.arraycopy(bArr5, i6, bArr7, 0, 8);
        if (d(bArr6, bArr7)) {
            return bArr6;
        }
        throw new org.bouncycastle.crypto.InvalidCipherTextException("Checksum inside ciphertext is corrupted");
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public java.lang.String getAlgorithmName() {
        return "DESede";
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        java.security.SecureRandom secureRandomD;
        this.e = z;
        this.a = new org.bouncycastle.crypto.modes.CBCBlockCipher(new org.bouncycastle.crypto.engines.DESedeEngine());
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            org.bouncycastle.crypto.CipherParameters cipherParametersA = parametersWithRandom.a();
            java.security.SecureRandom secureRandomB = parametersWithRandom.b();
            cipherParameters = cipherParametersA;
            secureRandomD = secureRandomB;
        } else {
            secureRandomD = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
        }
        if (cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter) {
            this.b = (org.bouncycastle.crypto.params.KeyParameter) cipherParameters;
            if (this.e) {
                byte[] bArr = new byte[8];
                this.d = bArr;
                secureRandomD.nextBytes(bArr);
                this.c = new org.bouncycastle.crypto.params.ParametersWithIV(this.b, this.d);
                return;
            }
            return;
        }
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            this.c = parametersWithIV;
            this.d = parametersWithIV.a();
            this.b = (org.bouncycastle.crypto.params.KeyParameter) this.c.b();
            if (!this.e) {
                throw new java.lang.IllegalArgumentException("You should not supply an IV for unwrapping");
            }
            byte[] bArr2 = this.d;
            if (bArr2 == null || bArr2.length != 8) {
                throw new java.lang.IllegalArgumentException("IV is not 8 octets");
            }
        }
    }
}
