package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CryptoProWrapEngine extends org.bouncycastle.crypto.engines.GOST28147WrapEngine {
    private static boolean c(byte b, int i) {
        return (b & (1 << i)) != 0;
    }

    private static byte[] d(byte[] bArr, byte[] bArr2, byte[] bArr3) {
        for (int i = 0; i != 8; i++) {
            int i2 = 0;
            int i3 = 0;
            for (int i4 = 0; i4 != 8; i4++) {
                int iQ = org.bouncycastle.util.Pack.q(bArr, i4 * 4);
                if (c(bArr2[i], i4)) {
                    i2 += iQ;
                } else {
                    i3 += iQ;
                }
            }
            byte[] bArr4 = new byte[8];
            org.bouncycastle.util.Pack.l(i2, bArr4, 0);
            org.bouncycastle.util.Pack.l(i3, bArr4, 4);
            org.bouncycastle.crypto.modes.GCFBBlockCipher gCFBBlockCipher = new org.bouncycastle.crypto.modes.GCFBBlockCipher(new org.bouncycastle.crypto.engines.GOST28147Engine());
            gCFBBlockCipher.init(true, new org.bouncycastle.crypto.params.ParametersWithIV(new org.bouncycastle.crypto.params.ParametersWithSBox(new org.bouncycastle.crypto.params.KeyParameter(bArr), bArr3), bArr4));
            gCFBBlockCipher.c(bArr, 0, bArr, 0);
            gCFBBlockCipher.c(bArr, 8, bArr, 8);
            gCFBBlockCipher.c(bArr, 16, bArr, 16);
            gCFBBlockCipher.c(bArr, 24, bArr, 24);
        }
        return bArr;
    }

    @Override // org.bouncycastle.crypto.engines.GOST28147WrapEngine, org.bouncycastle.crypto.Wrapper
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.KeyParameter keyParameter;
        byte[] bArrB;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            cipherParameters = ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a();
        }
        org.bouncycastle.crypto.params.ParametersWithUKM parametersWithUKM = (org.bouncycastle.crypto.params.ParametersWithUKM) cipherParameters;
        if (parametersWithUKM.a() instanceof org.bouncycastle.crypto.params.ParametersWithSBox) {
            keyParameter = (org.bouncycastle.crypto.params.KeyParameter) ((org.bouncycastle.crypto.params.ParametersWithSBox) parametersWithUKM.a()).a();
            bArrB = ((org.bouncycastle.crypto.params.ParametersWithSBox) parametersWithUKM.a()).b();
        } else {
            keyParameter = (org.bouncycastle.crypto.params.KeyParameter) parametersWithUKM.a();
            bArrB = null;
        }
        org.bouncycastle.crypto.params.KeyParameter keyParameter2 = new org.bouncycastle.crypto.params.KeyParameter(d(keyParameter.b(), parametersWithUKM.b(), bArrB));
        super.init(z, bArrB != null ? new org.bouncycastle.crypto.params.ParametersWithUKM(new org.bouncycastle.crypto.params.ParametersWithSBox(keyParameter2, bArrB), parametersWithUKM.b()) : new org.bouncycastle.crypto.params.ParametersWithUKM(keyParameter2, parametersWithUKM.b()));
    }
}
