package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSTU7624WrapEngine implements org.bouncycastle.crypto.Wrapper {
    private boolean a;
    private org.bouncycastle.crypto.engines.DSTU7624Engine b;
    private byte[] c;
    private byte[] d;
    private byte[] e;
    private byte[] f;
    private java.util.ArrayList g;

    public DSTU7624WrapEngine(int i) {
        org.bouncycastle.crypto.engines.DSTU7624Engine dSTU7624Engine = new org.bouncycastle.crypto.engines.DSTU7624Engine(i);
        this.b = dSTU7624Engine;
        this.c = new byte[dSTU7624Engine.b() / 2];
        this.e = new byte[this.b.b()];
        this.f = new byte[this.b.b()];
        this.g = new java.util.ArrayList();
        this.d = new byte[4];
    }

    private void c(int i, byte[] bArr, int i2) {
        bArr[i2 + 3] = (byte) (i >> 24);
        bArr[i2 + 2] = (byte) (i >> 16);
        bArr[i2 + 1] = (byte) (i >> 8);
        bArr[i2] = (byte) i;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] a(byte[] bArr, int i, int i2) {
        if (!this.a) {
            throw new java.lang.IllegalStateException("not set for wrapping");
        }
        if (i2 % this.b.b() != 0) {
            throw new org.bouncycastle.crypto.DataLengthException("wrap data must be a multiple of " + this.b.b() + " bytes");
        }
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        int iB = ((i2 / this.b.b()) + 1) * 2;
        int i3 = iB - 1;
        int i4 = i3 * 6;
        int iB2 = this.b.b() + i2;
        byte[] bArr2 = new byte[iB2];
        java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
        java.lang.System.arraycopy(bArr2, 0, this.c, 0, this.b.b() / 2);
        this.g.clear();
        int iB3 = iB2 - (this.b.b() / 2);
        int iB4 = this.b.b() / 2;
        while (iB3 != 0) {
            byte[] bArr3 = new byte[this.b.b() / 2];
            java.lang.System.arraycopy(bArr2, iB4, bArr3, 0, this.b.b() / 2);
            this.g.add(bArr3);
            iB3 -= this.b.b() / 2;
            iB4 += this.b.b() / 2;
        }
        int i5 = 0;
        while (i5 < i4) {
            java.lang.System.arraycopy(this.c, 0, bArr2, 0, this.b.b() / 2);
            java.lang.System.arraycopy(this.g.get(0), 0, bArr2, this.b.b() / 2, this.b.b() / 2);
            this.b.c(bArr2, 0, bArr2, 0);
            i5++;
            c(i5, this.d, 0);
            for (int i6 = 0; i6 < 4; i6++) {
                int iB5 = (this.b.b() / 2) + i6;
                bArr2[iB5] = (byte) (bArr2[iB5] ^ this.d[i6]);
            }
            java.lang.System.arraycopy(bArr2, this.b.b() / 2, this.c, 0, this.b.b() / 2);
            for (int i7 = 2; i7 < iB; i7++) {
                java.lang.System.arraycopy(this.g.get(i7 - 1), 0, this.g.get(i7 - 2), 0, this.b.b() / 2);
            }
            java.lang.System.arraycopy(bArr2, 0, this.g.get(iB - 2), 0, this.b.b() / 2);
        }
        java.lang.System.arraycopy(this.c, 0, bArr2, 0, this.b.b() / 2);
        int iB6 = this.b.b() / 2;
        for (int i8 = 0; i8 < i3; i8++) {
            java.lang.System.arraycopy(this.g.get(i8), 0, bArr2, iB6, this.b.b() / 2);
            iB6 += this.b.b() / 2;
        }
        return bArr2;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public byte[] b(byte[] bArr, int i, int i2) throws org.bouncycastle.crypto.InvalidCipherTextException {
        if (this.a) {
            throw new java.lang.IllegalStateException("not set for unwrapping");
        }
        if (i2 % this.b.b() != 0) {
            throw new org.bouncycastle.crypto.DataLengthException("unwrap data must be a multiple of " + this.b.b() + " bytes");
        }
        int iB = (i2 * 2) / this.b.b();
        int i3 = iB - 1;
        int i4 = i3 * 6;
        byte[] bArr2 = new byte[i2];
        java.lang.System.arraycopy(bArr, i, bArr2, 0, i2);
        byte[] bArr3 = new byte[this.b.b() / 2];
        java.lang.System.arraycopy(bArr2, 0, bArr3, 0, this.b.b() / 2);
        this.g.clear();
        int iB2 = i2 - (this.b.b() / 2);
        int iB3 = this.b.b() / 2;
        while (iB2 != 0) {
            byte[] bArr4 = new byte[this.b.b() / 2];
            java.lang.System.arraycopy(bArr2, iB3, bArr4, 0, this.b.b() / 2);
            this.g.add(bArr4);
            iB2 -= this.b.b() / 2;
            iB3 += this.b.b() / 2;
        }
        for (int i5 = 0; i5 < i4; i5++) {
            java.lang.System.arraycopy(this.g.get(iB - 2), 0, bArr2, 0, this.b.b() / 2);
            java.lang.System.arraycopy(bArr3, 0, bArr2, this.b.b() / 2, this.b.b() / 2);
            c(i4 - i5, this.d, 0);
            for (int i6 = 0; i6 < 4; i6++) {
                int iB4 = (this.b.b() / 2) + i6;
                bArr2[iB4] = (byte) (bArr2[iB4] ^ this.d[i6]);
            }
            this.b.c(bArr2, 0, bArr2, 0);
            java.lang.System.arraycopy(bArr2, 0, bArr3, 0, this.b.b() / 2);
            for (int i7 = 2; i7 < iB; i7++) {
                int i8 = iB - i7;
                java.lang.System.arraycopy(this.g.get(i8 - 1), 0, this.g.get(i8), 0, this.b.b() / 2);
            }
            java.lang.System.arraycopy(bArr2, this.b.b() / 2, this.g.get(0), 0, this.b.b() / 2);
        }
        java.lang.System.arraycopy(bArr3, 0, bArr2, 0, this.b.b() / 2);
        int iB5 = this.b.b() / 2;
        for (int i9 = 0; i9 < i3; i9++) {
            java.lang.System.arraycopy(this.g.get(i9), 0, bArr2, iB5, this.b.b() / 2);
            iB5 += this.b.b() / 2;
        }
        java.lang.System.arraycopy(bArr2, i2 - this.b.b(), this.e, 0, this.b.b());
        byte[] bArr5 = new byte[i2 - this.b.b()];
        if (!org.bouncycastle.util.Arrays.d(this.e, this.f)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("checksum failed");
        }
        java.lang.System.arraycopy(bArr2, 0, bArr5, 0, i2 - this.b.b());
        return bArr5;
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public java.lang.String getAlgorithmName() {
        return "DSTU7624WrapEngine";
    }

    @Override // org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            cipherParameters = ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a();
        }
        this.a = z;
        if (!(cipherParameters instanceof org.bouncycastle.crypto.params.KeyParameter)) {
            throw new java.lang.IllegalArgumentException("invalid parameters passed to DSTU7624WrapEngine");
        }
        this.b.init(z, cipherParameters);
    }
}
