Exemplo n.º 1
0
        /// <summary>
        /// The function returns the black box output bit.
        /// </summary>
        /// <param name="v">Public variables.</param>
        /// <param name="x">Secret variables.</param>
        /// <returns>Returns the black box output bit, either 0 or 1.</returns>
        public int Blackbox(int[] v, int[] x)
        {
            int result = 0;

            try
            {
                result = CubeattackBlackbox.GenerateBlackboxOutputBit(v, x, indexOutputBit);
            }
            catch (Exception ex)
            {
                stop = true;
                CubeAttack_LogMessage("Error: " + ex, NotificationLevel.Error);
            }
            return(result);
        }
Exemplo n.º 2
0
        }     //End PreprocessingPhase

        /// <summary>
        /// Online phase of the cube attack.
        /// </summary>
        public void OnlinePhase()
        {
            if (settings.ReadSuperpolysFromFile)
            {
                if (File.Exists(settings.OpenFilename))
                {
                    CubeAttack_LogMessage("Read superpolys from file !", NotificationLevel.Info);
                    superpolyMatrix = new Matrix(settings.SecretVar, settings.SecretVar + 1);
                    listCubeIndexes = new List <List <int> >();
                    outputBitIndex  = new int[settings.SecretVar];

                    int i = 0;
                    foreach (string sLine in File.ReadAllLines(settings.OpenFilename))
                    {
                        string[] allValues = sLine.Split(' ');
                        string[] variables = allValues[0].Split('+');
                        string[] cubeIndex = allValues[1].Split(',');

                        List <string> variablesList = new List <string>(variables); // Copy to List

                        for (int j = 0; j < variablesList.Count; j++)
                        {
                            if (variablesList[j].Substring(0, 1) == "1")
                            {
                                superpolyMatrix[i, 0] = 1;
                                variablesList.Remove(variablesList[j]);
                            }
                        }
                        for (int j = 0; j < variablesList.Count; j++)
                        {
                            if (variablesList[j].Substring(0, 1) == "x")
                            {
                                variablesList[j] = variablesList[j].Substring(1);
                            }
                        }

                        List <int> superpoly = new List <int>();
                        for (int j = 0; j < variablesList.Count; j++)
                        {
                            superpoly.Add(Convert.ToInt32(variablesList[j]));
                        }
                        for (int j = 0; j < superpoly.Count; j++)
                        {
                            superpolyMatrix[i, superpoly[j] + 1] = 1;
                        }

                        List <int> maxterm = new List <int>();
                        foreach (string cube in cubeIndex)
                        {
                            maxterm.Add(Convert.ToInt32(cube));
                        }
                        listCubeIndexes.Add(maxterm);

                        outputBitIndex[i] = Convert.ToInt32(allValues[2]);
                        i++;
                        // Save number of input superpolys
                        countSuperpoly = i;
                    }
                }
                else
                {
                    CubeAttack_LogMessage("Please input a File", NotificationLevel.Error);
                    return;
                }
            }
            if (superpolyMatrix == null || listCubeIndexes == null)
            {
                CubeAttack_LogMessage("Preprocessing phase has to be executed first", NotificationLevel.Error);
            }
            else
            {
                CubeAttack_LogMessage("Start online phase", NotificationLevel.Info);
                outputSuperpoly = string.Empty;
                int[] pubVarElement = new int[settings.PublicVar];

                if (pubVarGlob != null)
                {
                    for (int i = 0; i < settings.PublicVar; i++)
                    {
                        pubVarElement[i] = pubVarGlob[i];
                    }
                }
                Vector b = new Vector(settings.SecretVar);

                for (int i = 0; i < listCubeIndexes.Count; i++)
                {
                    List <int> superpoly = new List <int>();
                    for (int j = 1; j < superpolyMatrix.Cols; j++)
                    {
                        superpoly.Add(superpolyMatrix[i, j]);
                    }

                    CubeAttack_LogMessage("Compute value of superpoly " + SuperpolyAsString(superpoly), NotificationLevel.Info);

                    for (ulong k = 0; k < Math.Pow(2, listCubeIndexes[i].Count); k++)
                    {
                        if (stop)
                        {
                            return;
                        }
                        for (int l = 0; l < listCubeIndexes[i].Count; l++)
                        {
                            pubVarElement[listCubeIndexes[i][l]] = (k & ((ulong)1 << l)) > 0 ? 1 : 0;
                        }
                        try
                        {
                            b[i] ^= CubeattackBlackbox.GenerateBlackboxOutputBit(pubVarElement, null, outputBitIndex[i]);
                        }
                        catch (Exception ex)
                        {
                            CubeAttack_LogMessage("Error: " + ex, NotificationLevel.Error);
                        }
                    }
                    for (int j = 0; j < settings.PublicVar; j++)
                    {
                        pubVarElement[j] = 0;
                    }

                    outputSuperpoly += GetLogMessage(listCubeIndexes[i], superpoly, outputBitIndex[i], b[i]);
                    OnPropertyChanged("OutputSuperpoly");

                    ProgressChanged((double)i / (double)listCubeIndexes.Count, 1.0);
                    outputSuperpoly = string.Empty;
                }
                if (listCubeIndexes.Count == settings.SecretVar)
                {
                    CubeAttack_LogMessage("Solve system of equations", NotificationLevel.Info);
                    for (int i = 0; i < settings.SecretVar; i++)
                    {
                        b[i] ^= superpolyMatrix[i, 0];
                    }
                    // Delete first column and invert
                    OutputKey(superpolyMatrix.DeleteFirstColumn().Inverse() * b);
                    OnPropertyChanged("OutputKeyBits");
                    CubeAttack_LogMessage("Key bits successfully discovered, online phase completed", NotificationLevel.Info);
                }
                else
                {
                    CubeAttack_LogMessage("Not enough linearly independent superpolys have been found in the preprocessing to discover all secret bits !", NotificationLevel.Info);
                }
            }
        }