package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RFC3211WrapEngine implements org.bouncycastle.crypto.Wrapper {
    private org.bouncycastle.crypto.modes.CBCBlockCipher a;
    private org.bouncycastle.crypto.params.ParametersWithIV b;
    private boolean c;
    private java.security.SecureRandom d;

    public RFC3211WrapEngine(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.a = new org.bouncycastle.crypto.modes.CBCBlockCipher(blockCipher);
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] a(byte[] bArr, int i, int i2) {
        if (!this.c) {
            throw new java.lang.IllegalStateException("not set for wrapping");
        }
        if (i2 > 255 || i2 < 0) {
            throw new java.lang.IllegalArgumentException("input must be from 0 to 255 bytes");
        }
        this.a.init(true, this.b);
        int iB = this.a.b();
        int i3 = i2 + 4;
        int i4 = iB * 2;
        if (i3 >= i4) {
            i4 = i3 % iB == 0 ? i3 : ((i3 / iB) + 1) * iB;
        }
        byte[] bArr2 = new byte[i4];
        bArr2[0] = (byte) i2;
        java.lang.System.arraycopy(bArr, i, bArr2, 4, i2);
        int length = bArr2.length - i3;
        byte[] bArr3 = new byte[length];
        this.d.nextBytes(bArr3);
        java.lang.System.arraycopy(bArr3, 0, bArr2, i3, length);
        bArr2[1] = (byte) (~bArr2[4]);
        bArr2[2] = (byte) (~bArr2[5]);
        bArr2[3] = (byte) (~bArr2[6]);
        for (int i5 = 0; i5 < bArr2.length; i5 += iB) {
            this.a.c(bArr2, i5, bArr2, i5);
        }
        for (int i6 = 0; i6 < bArr2.length; i6 += iB) {
            this.a.c(bArr2, i6, bArr2, i6);
        }
        return bArr2;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] b(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        if (this.c) {
            throw new java.lang.IllegalStateException("not set for unwrapping");
        }
        int iB = this.a.b();
        if (i2 < iB * 2) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("input too short");
        }
        byte[] bArr2 = new byte[i2];
        byte[] bArr3 = new byte[iB];
        java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
        java.lang.System.arraycopy(bArr, i, bArr3, 0, iB);
        this.a.init(false, new org.bouncycastle.crypto.params.ParametersWithIV(this.b.b(), bArr3));
        for (int i3 = iB; i3 < i2; i3 += iB) {
            this.a.c(bArr2, i3, bArr2, i3);
        }
        java.lang.System.arraycopy(bArr2, i2 - iB, bArr3, 0, iB);
        this.a.init(false, new org.bouncycastle.crypto.params.ParametersWithIV(this.b.b(), bArr3));
        this.a.c(bArr2, 0, bArr2, 0);
        this.a.init(false, this.b);
        for (int i4 = 0; i4 < i2; i4 += iB) {
            this.a.c(bArr2, i4, bArr2, i4);
        }
        int i5 = bArr2[0];
        int i6 = i2 - 4;
        boolean z = (i5 & 255) > i6;
        byte[] bArr4 = z ? new byte[i6] : new byte[i5 & 255];
        java.lang.System.arraycopy(bArr2, 4, bArr4, 0, bArr4.length);
        int i7 = 0;
        int i8 = 0;
        while (i7 != 3) {
            int i9 = i7 + 1;
            i8 |= bArr2[i7 + 4] ^ ((byte) (~bArr2[i9]));
            i7 = i9;
        }
        org.bouncycastle.util.Arrays.i(bArr2);
        if (!z && !(i8 != 0)) {
            return bArr4;
        }
        throw new org.bouncycastle.crypto.InvalidCipherTextException("wrapped key corrupted");
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public java.lang.String getAlgorithmName() {
        return this.a.getUnderlyingCipher().getAlgorithmName() + "/RFC3211Wrap";
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.c = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            this.d = parametersWithRandom.b();
            if (!(parametersWithRandom.a() instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("RFC3211Wrap requires an IV");
            }
            this.b = (org.bouncycastle.crypto.params.ParametersWithIV) parametersWithRandom.a();
            return;
        }
        if (z) {
            this.d = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
        }
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
            throw new java.lang.IllegalArgumentException("RFC3211Wrap requires an IV");
        }
        this.b = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
    }
}
