예제 #1
0
파일: Word.cs 프로젝트: hktaskin/Serpent
 public Word GetNibble(int block)
 {
     Word n = new Word(4);
     for (int i = 0; i < 4; i++)
         n.SetBit(_bits[i + (block * 4)], i);
     return n;
 }
예제 #2
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        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);
             }
             */
        }
예제 #3
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
 public static void ApplySerpentPermutation(ref Word x0, ref Word x1, ref Word x2, ref Word x3)
 {
     x0.RotateLeft(13);
     x2.RotateLeft(3);
     x1 = x1 ^ x0 ^ x2;
     x3 = x3 ^ x2 ^ (x0 << 3);
     x1.RotateLeft(1);
     x3.RotateLeft(7);
     x0 = x0 ^ x1 ^ x3;
     x2 = x2 ^ x3 ^ (x1 << 7);
     x0.RotateLeft(5);
     x2.RotateLeft(22);
 }
예제 #4
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
 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);
     }
 }
예제 #5
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        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);
        }
예제 #6
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        public static Word SerpentSBox(int round, Word input)
        {
            if (input.Contains('?'))
                return Word.Parse("????");

            if (input.IsEqualTo("0000"))
                return Word.Parse("0000");

            int SBoxID = round % 8;

            switch (SBoxID)
            {
                case 0:
                    // Input: [02] 0010 -> Output: 1???
                    if (input.IsEqualTo("0010"))
                        return Word.Parse("1???");
                    // Input: [04] 0100 -> Output: 1???
                    if (input.IsEqualTo("0100"))
                        return Word.Parse("1???");
                    // Input: [06] 0110 -> Output: 0???
                    if (input.IsEqualTo("0110"))
                        return Word.Parse("0???");
                    break;
                case 1:
                    // Input: [04] 0100 -> Output: ?1??
                    if (input.IsEqualTo("0100"))
                        return Word.Parse("?1??");
                    // Input: [08] 1000 -> Output: ?1??
                    if (input.IsEqualTo("1000"))
                        return Word.Parse("?1??");
                    // Input: [0C] 1100 -> Output: ?0??
                    if (input.IsEqualTo("1100"))
                        return Word.Parse("?0??");
                    break;
                case 2:
                    // Input: [02] 0010 -> Output: ???1
                    if (input.IsEqualTo("0010"))
                        return Word.Parse("???1");
                    // Input: [08] 1000 -> Output: ???1
                    if (input.IsEqualTo("1000"))
                        return Word.Parse("???1");
                    // Input: [0A] 1010 -> Output: ???0
                    if (input.IsEqualTo("1010"))
                        return Word.Parse("???0");
                    break;
                case 3:
                    break;
                case 4:
                case 5:
                    // Input: [04] 0100 -> Output: ???1
                    if (input.IsEqualTo("0100"))
                        return Word.Parse("???1");
                    // Input: [0B] 1011 -> Output: ???1
                    if (input.IsEqualTo("1011"))
                        return Word.Parse("???1");
                    // Input: [0F] 1111 -> Output: ???0
                    if (input.IsEqualTo("1111"))
                        return Word.Parse("???0");
                    break;
                case 6:
                    // Input: [02] 0010 -> Output: ??1?
                    if (input.IsEqualTo("0010"))
                        return Word.Parse("??1?");
                    // Input: [04] 0100 -> Output: ??1?
                    if (input.IsEqualTo("0100"))
                        return Word.Parse("??1?");
                    // Input: [06] 0110 -> Output: ??0?
                    if (input.IsEqualTo("0110"))
                        return Word.Parse("??0?");
                    break;
                case 7:
                    break;
            }

            return Word.Parse("????");
        }
예제 #7
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        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;
        }
예제 #8
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        public static int[] NonZeroColumnPositions(Word[] w)
        {
            int[] konum = new int[0];

            if (w.Length != 4)
                throw new Exception("Word list size should be 4!");

            for (int i = 0; i < w[0].Size; i++)
            {
                Word t = Serpent.GetBitSlice(w, i);
                if (t.Contains('?') || t.Contains('1'))
                {
                    Array.Resize(ref konum, konum.Length + 1);
                    konum[konum.Length - 1] = i;
                }
            }

            return konum;
        }
예제 #9
0
파일: Word.cs 프로젝트: hktaskin/Serpent
 public static Word Parse(char[] c)
 {
     Word w = new Word(c.Length);
     for (int i = 0; i < c.Length; i++)
         w.SetBit(c[i], i);
     return w;
 }
예제 #10
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        public static bool Impossible(Word[] c1, Word[] c2)
        {
            if ((c1[0].Size != c2[0].Size) || (c1.Length != c2.Length))
                throw new Exception("Sizes for Words in arrays should be same!");

            for (int i = 0; i < c1.Length; i++)
            {
                for (int j = 0; j < c1[i].Size; j++)
                {
                    if ((c1[i].GetBit(j) == '1' && c2[i].GetBit(j) == '0') ||
                        (c1[i].GetBit(j) == '0' && c2[i].GetBit(j) == '1'))
                        return true;
                }
            }
            return false;
        }
예제 #11
0
파일: Form1.cs 프로젝트: hktaskin/Serpent
 void FillW(Word[] w)
 {
     txtW0.Text = w[0].ToString();
     txtW1.Text = w[1].ToString();
     txtW2.Text = w[2].ToString();
     txtW3.Text = w[3].ToString();
 }
예제 #12
0
파일: Form1.cs 프로젝트: hktaskin/Serpent
 void FillO(Word[] o)
 {
     txtO0.Text = o[0].ToString();
     txtO1.Text = o[1].ToString();
     txtO2.Text = o[2].ToString();
     txtO3.Text = o[3].ToString();
 }
예제 #13
0
파일: Form1.cs 프로젝트: hktaskin/Serpent
        private void btnTest1_Click(object sender, EventArgs e)
        {
            dbg.ClearDebug();
            dbg.WriteLine("In Serpent We have broken!\r\n");

            Word[] tmpW = new Word[4];

            int[] a = new int[] { 2, 4, 5, 9, 10, 14 };
            int[] b = new int[] { 5, 7, 12, 14 };
            int konuma = 25, konumb = 22;

            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < b.Length; j++)
                {
                    for (int t = 0; t < 4; t++)
                        tmpW[t] = Word.Parse("0000 0000 0000 0000 0000 0000 0000 0000");

                    Word worda = a[i].ToWord(4);
                    Word wordb = b[j].ToWord(4);

                    Serpent.SetBitSlice(ref tmpW, worda, konuma);
                    Serpent.SetBitSlice(ref tmpW, wordb, konumb);

                    Serpent.ApplySerpentInvPermutation(ref tmpW[0], ref tmpW[1], ref tmpW[2], ref tmpW[3]);

                    int[] nzcpos = Serpent.NonZeroColumnPositions(tmpW);

                    string pos = string.Join("-", nzcpos);

                    dbg.WriteLine(string.Format("a: {0} b: {1} Number of Non-Zero Columns: {2}\r\nPositions: {3}\r\n", a[i], b[j], nzcpos.Length, pos));

                }

            }

            MessageBox.Show("Check Log Screen!");
        }
예제 #14
0
파일: Word.cs 프로젝트: hktaskin/Serpent
        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);
        }
예제 #15
0
파일: Word.cs 프로젝트: hktaskin/Serpent
        public static Word operator ^(Word w1, Word w2)
        {
            if (w1.Size != w2.Size)
                throw new Exception("Word sizes should be same!");

            Word r = new Word(w1.Size);

            for (int i = 0; i < w1.Size; i++)
            {
                if ((w1.GetBit(i) == '0' && w2.GetBit(i) == '0') ||
                    (w1.GetBit(i) == '1' && w2.GetBit(i) == '1'))
                    r.SetBit('0', i);
                else if ((w1.GetBit(i) == '0' && w2.GetBit(i) == '1') ||
                         (w1.GetBit(i) == '1' && w2.GetBit(i) == '0'))
                    r.SetBit('1', i);
                else
                    r.SetBit('?', i);
            }
            return r;
        }
예제 #16
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        public static int NonZeroColumnNumber(Word[] w)
        {
            if (w.Length != 4)
                throw new Exception("Word list size should be 4!");
            int sayac = 0;

            for (int i = 0; i < w[0].Size; i++)
            {
                if (w[0].GetBit(i) == '0' && w[1].GetBit(i) == '0' && w[2].GetBit(i) == '0' && w[3].GetBit(i) == '0')
                    sayac++;
            }

            return 32 - sayac;
        }
예제 #17
0
파일: Serpent.cs 프로젝트: hktaskin/Serpent
        public static Word GetBitSlice(Word[] w, int column)
        {
            if (w.Length != 4)
                throw new Exception("Word Array Size should be 4 and Slice length should be 4");

            Word slice = new Word(4);

            for (int i = 0; i < 4; i++)
                slice.SetBit(w[i].GetBit(column), i);

            return slice;
        }
예제 #18
0
파일: Word.cs 프로젝트: hktaskin/Serpent
 /// <param name="s">LSB value is on the right</param>
 public static Word Parse(string str)
 {
     string s = CleanInputData(str); //str.Replace(" ", "");
     Word w = new Word(s.Length);
     for (int i = 0; i < s.Length; i++)
         w.SetBit(s[s.Length - i - 1], i);
     return w;
 }