package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RC2WrapEngine implements org.bouncycastle.crypto.Wrapper {
    private static final byte[] i = {74, -35, -94, 44, 121, -24, 33, 5};
    private org.bouncycastle.crypto.modes.CBCBlockCipher a;
    private org.bouncycastle.crypto.CipherParameters b;
    private org.bouncycastle.crypto.params.ParametersWithIV c;
    private byte[] d;
    private boolean e;
    private java.security.SecureRandom f;
    org.bouncycastle.crypto.Digest g = org.bouncycastle.crypto.util.DigestFactory.c();
    byte[] h = new byte[20];

    private byte[] c(byte[] bArr) {
        byte[] bArr2 = new byte[8];
        this.g.update(bArr, 0, bArr.length);
        this.g.doFinal(this.h, 0);
        java.lang.System.arraycopy(this.h, 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 i2, int i3) {
        if (!this.e) {
            throw new java.lang.IllegalStateException("Not initialized for wrapping");
        }
        int i4 = i3 + 1;
        int i5 = i4 % 8;
        int i6 = i5 != 0 ? (8 - i5) + i4 : i4;
        byte[] bArr2 = new byte[i6];
        bArr2[0] = (byte) i3;
        java.lang.System.arraycopy(bArr, i2, bArr2, 1, i3);
        int i7 = (i6 - i3) - 1;
        byte[] bArr3 = new byte[i7];
        if (i7 > 0) {
            this.f.nextBytes(bArr3);
            java.lang.System.arraycopy(bArr3, 0, bArr2, i4, i7);
        }
        byte[] bArrC = c(bArr2);
        int length = bArrC.length + i6;
        byte[] bArr4 = new byte[length];
        java.lang.System.arraycopy(bArr2, 0, bArr4, 0, i6);
        java.lang.System.arraycopy(bArrC, 0, bArr4, i6, bArrC.length);
        byte[] bArr5 = new byte[length];
        java.lang.System.arraycopy(bArr4, 0, bArr5, 0, length);
        int iB = length / this.a.b();
        if (length % this.a.b() != 0) {
            throw new java.lang.IllegalStateException("Not multiple of block length");
        }
        this.a.init(true, this.c);
        for (int i8 = 0; i8 < iB; i8++) {
            int iB2 = this.a.b() * i8;
            this.a.c(bArr5, iB2, bArr5, iB2);
        }
        byte[] bArr6 = this.d;
        int length2 = bArr6.length + length;
        byte[] bArr7 = new byte[length2];
        java.lang.System.arraycopy(bArr6, 0, bArr7, 0, bArr6.length);
        java.lang.System.arraycopy(bArr5, 0, bArr7, this.d.length, length);
        byte[] bArr8 = new byte[length2];
        int i9 = 0;
        while (i9 < length2) {
            int i10 = i9 + 1;
            bArr8[i9] = bArr7[length2 - i10];
            i9 = i10;
        }
        this.a.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(this.b, i));
        for (int i11 = 0; i11 < iB + 1; i11++) {
            int iB3 = this.a.b() * i11;
            this.a.c(bArr8, iB3, bArr8, iB3);
        }
        return bArr8;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] b(byte[] bArr, int i2, int i3) 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");
        }
        if (i3 % this.a.b() != 0) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Ciphertext not multiple of " + this.a.b());
        }
        this.a.init(false, new org.bouncycastle.crypto.params.ParametersWithIV(this.b, i));
        byte[] bArr2 = new byte[i3];
        java.lang.System.arraycopy(bArr, i2, bArr2, 0, i3);
        for (int i4 = 0; i4 < i3 / this.a.b(); i4++) {
            int iB = this.a.b() * i4;
            this.a.c(bArr2, iB, bArr2, iB);
        }
        byte[] bArr3 = new byte[i3];
        int i5 = 0;
        while (i5 < i3) {
            int i6 = i5 + 1;
            bArr3[i5] = bArr2[i3 - i6];
            i5 = i6;
        }
        byte[] bArr4 = new byte[8];
        this.d = bArr4;
        int i7 = i3 - 8;
        byte[] bArr5 = new byte[i7];
        java.lang.System.arraycopy(bArr3, 0, bArr4, 0, 8);
        java.lang.System.arraycopy(bArr3, 8, bArr5, 0, i7);
        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[] bArr6 = new byte[i7];
        java.lang.System.arraycopy(bArr5, 0, bArr6, 0, i7);
        for (int i8 = 0; i8 < i7 / this.a.b(); i8++) {
            int iB2 = this.a.b() * i8;
            this.a.c(bArr6, iB2, bArr6, iB2);
        }
        int i9 = i3 - 16;
        byte[] bArr7 = new byte[i9];
        byte[] bArr8 = new byte[8];
        java.lang.System.arraycopy(bArr6, 0, bArr7, 0, i9);
        java.lang.System.arraycopy(bArr6, i9, bArr8, 0, 8);
        if (!d(bArr7, bArr8)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Checksum inside ciphertext is corrupted");
        }
        int i10 = bArr7[0];
        if (i9 - ((i10 & 255) + 1) <= 7) {
            byte[] bArr9 = new byte[i10];
            java.lang.System.arraycopy(bArr7, 1, bArr9, 0, i10);
            return bArr9;
        }
        throw new org.bouncycastle.crypto.InvalidCipherTextException("too many pad bytes (" + (i9 - ((bArr7[0] & 255) + 1)) + ")");
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public java.lang.String getAlgorithmName() {
        return "RC2";
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.e = z;
        this.a = new org.bouncycastle.crypto.modes.CBCBlockCipher(new org.bouncycastle.crypto.engines.RC2Engine());
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            this.f = parametersWithRandom.b();
            cipherParameters = parametersWithRandom.a();
        } else {
            this.f = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
        }
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            this.b = cipherParameters;
            if (this.e) {
                byte[] bArr = new byte[8];
                this.d = bArr;
                this.f.nextBytes(bArr);
                this.c = new org.bouncycastle.crypto.params.ParametersWithIV(this.b, this.d);
                return;
            }
            return;
        }
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
        this.c = parametersWithIV;
        this.d = parametersWithIV.a();
        this.b = 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");
        }
    }
}
