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); } */ }
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); * } */ }
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); }
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); } }
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); }
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); }
/// <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); }
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); }
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; }
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; }
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; }
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; }
/// <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; }
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; }