Exemplo n.º 1
0
        public static void ApplySerpentSBox(ref Word x0, ref Word x1, ref Word x2, ref Word x3, int round)
        {
            for (int i = 0; i < x0.Size; i++)
            {
                Word input = new Word(4);
                input.SetBit(x0.GetBit(i), 0);
                input.SetBit(x1.GetBit(i), 1);
                input.SetBit(x2.GetBit(i), 2);
                input.SetBit(x3.GetBit(i), 3);
                Word output = SerpentSBox(round, input);
                x0.SetBit(output.GetBit(0), i);
                x1.SetBit(output.GetBit(1), i);
                x2.SetBit(output.GetBit(2), i);
                x3.SetBit(output.GetBit(3), i);
            }

            /* ESKİ
             * for (int i = 0; i < max; i++)
             * {
             *   x0.SetNibble(SerpentSBox(round, x0.GetNibble(i)), i);
             *   x1.SetNibble(SerpentSBox(round, x1.GetNibble(i)), i);
             *   x2.SetNibble(SerpentSBox(round, x2.GetNibble(i)), i);
             *   x3.SetNibble(SerpentSBox(round, x3.GetNibble(i)), i);
             * }
             */
        }
Exemplo n.º 2
0
        public static void ApplySerpentSBox(ref Word x0, ref Word x1, ref Word x2, ref Word x3, int round)
        {
            for (int i = 0; i < x0.Size; i++)
            {
                Word input = new Word(4);
                input.SetBit(x0.GetBit(i), 0);
                input.SetBit(x1.GetBit(i), 1);
                input.SetBit(x2.GetBit(i), 2);
                input.SetBit(x3.GetBit(i), 3);
                Word output = SerpentSBox(round, input);
                x0.SetBit(output.GetBit(0), i);
                x1.SetBit(output.GetBit(1), i);
                x2.SetBit(output.GetBit(2), i);
                x3.SetBit(output.GetBit(3), i);
            }

            /* ESKİ
             for (int i = 0; i < max; i++)
             {
                 x0.SetNibble(SerpentSBox(round, x0.GetNibble(i)), i);
                 x1.SetNibble(SerpentSBox(round, x1.GetNibble(i)), i);
                 x2.SetNibble(SerpentSBox(round, x2.GetNibble(i)), i);
                 x3.SetNibble(SerpentSBox(round, x3.GetNibble(i)), i);
             }
             */
        }
Exemplo n.º 3
0
 public static void ApplySerpentInvSBox(ref Word x0, ref Word x1, ref Word x2, ref Word x3, int round)
 {
     for (int i = 0; i < x0.Size; i++)
     {
         Word output = new Word(4);
         output.SetBit(x0.GetBit(i), 0);
         output.SetBit(x1.GetBit(i), 1);
         output.SetBit(x2.GetBit(i), 2);
         output.SetBit(x3.GetBit(i), 3);
         Word input = SerpentInvSBox(round, output);
         x0.SetBit(input.GetBit(0), i);
         x1.SetBit(input.GetBit(1), i);
         x2.SetBit(input.GetBit(2), i);
         x3.SetBit(input.GetBit(3), i);
     }
 }
Exemplo n.º 4
0
        public static void SetBitSlice(ref Word[] w, Word slice, int column)
        {
            if (w.Length != 4 || slice.Size != 4)
            {
                throw new Exception("Word Array Size should be 4 and Slice length should be 4");
            }

            for (int i = 0; i < 4; i++)
            {
                w[i].SetBit(slice.GetBit(i), column);
            }
        }
Exemplo n.º 5
0
        public void SetNibble(Word nb, int block)
        {
            if (nb.Size != 4)
            {
                throw new Exception("Nibble size should be 4!");
            }


            for (int i = 0; i < 4; i++)
            {
                _bits[i + (block * 4)] = nb.GetBit(i);
            }
        }
Exemplo n.º 6
0
 public static void ApplySerpentInvSBox(ref Word x0, ref Word x1, ref Word x2, ref Word x3, int round)
 {
     for (int i = 0; i < x0.Size; i++)
     {
         Word output = new Word(4);
         output.SetBit(x0.GetBit(i), 0);
         output.SetBit(x1.GetBit(i), 1);
         output.SetBit(x2.GetBit(i), 2);
         output.SetBit(x3.GetBit(i), 3);
         Word input = SerpentInvSBox(round, output);
         x0.SetBit(input.GetBit(0), i);
         x1.SetBit(input.GetBit(1), i);
         x2.SetBit(input.GetBit(2), i);
         x3.SetBit(input.GetBit(3), i);
     }
 }
Exemplo n.º 7
0
        public bool IsEqualTo(string s)
        {
            Word w = Word.Parse(s.Replace(" ", ""));

            if (_size != w.Size)
            {
                return(false);
            }

            for (int i = 0; i < _size; i++)
            {
                if (w.GetBit(i) != _bits[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 8
0
        public static void SetBitSlice(ref Word[] w, Word slice, int column)
        {
            if (w.Length != 4 || slice.Size != 4)
                throw new Exception("Word Array Size should be 4 and Slice length should be 4");

            for (int i = 0; i < 4; i++)
                w[i].SetBit(slice.GetBit(i), column);
        }
Exemplo n.º 9
0
        public void SetNibble(Word nb, int block)
        {
            if (nb.Size != 4)
                throw new Exception("Nibble size should be 4!");


            for (int i = 0; i < 4; i++)
                _bits[i + (block * 4)] = nb.GetBit(i);
        }
Exemplo n.º 10
0
        public static void OutputToInputProbability(Word output, Word input, int sbox, out int probNumerator, out int probDenominator)
        {
            if (input.Size != 4 || output.Size != 4)
            {
                throw new Exception("Input and output word sizes should be 4!");
            }

            Word tmpIn  = Word.Parse(input.ToString());
            Word tmpOut = Word.Parse(output.ToString());

            int[] inQ  = tmpIn.QuestionMarkPositions();
            int[] outQ = tmpOut.QuestionMarkPositions();

            int[] inputValues  = new int[0];
            int[] outputValues = new int[0];

            for (int i = 0; i < (1 << inQ.Length); i++)
            {
                Word w1 = i.ToWord(inQ.Length);
                for (int j = 0; j < inQ.Length; j++)
                {
                    tmpIn.SetBit(w1.GetBit(j), inQ[j]);
                }

                int t = tmpIn.ToInt32();
                Array.Resize(ref inputValues, inputValues.Length + 1);
                inputValues[inputValues.Length - 1] = t;
            }

            for (int i = 0; i < (1 << outQ.Length); i++)
            {
                Word w1 = i.ToWord(outQ.Length);
                for (int j = 0; j < outQ.Length; j++)
                {
                    tmpOut.SetBit(w1.GetBit(j), outQ[j]);
                }

                int t = tmpOut.ToInt32();
                Array.Resize(ref outputValues, outputValues.Length + 1);
                outputValues[outputValues.Length - 1] = t;
            }

            uint[,] ddt = XORTable(SBox[sbox], 4, 4);

            probNumerator   = 0;
            probDenominator = 0;

            // Pay: Olası her bir output value için sadece olası input değerlerinin toplamı
            for (int i = 0; i < outputValues.Length; i++)
            {
                for (int j = 0; j < inputValues.Length; j++)
                {
                    probNumerator += (int)ddt[inputValues[j], outputValues[i]];
                }
            }

            // Payda: Olası her bir outputValue için tüm input değerlerinin toplamı
            for (int i = 0; i < outputValues.Length; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    probDenominator += (int)ddt[j, outputValues[i]];
                }
            }

            return;
        }