SBOX_TESTTOOL
最近给一个比赛写的,不过有不少地方没处理好感觉
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestProject { class TestTool { private UInt32[] SBOX; public TestTool(UInt32[] Sbox) { SBOX = new UInt32[256]; try { Sbox.CopyTo(SBOX, 0); } catch { throw new Exception("Data Copy Error"); } } public class NonlinearDegree { UInt32[] Table = new UInt32[256]; void SetTable() { UInt32 flag; for (UInt32 i = 0; i < 256; i++) { flag = i; for (int j = 0; j < 8; j++) { if (((flag >> j) & 0x01) != 0) { Table[i]++; } } } } public UInt32 Test(UInt32[] SBOX) { UInt32 U, A, x, b, temp, nonlin = 256; SetTable(); for (U = 1; U < 256; U++) { for (A = 1; A < 256; A++) { for (b = 0; b < 2; b++) { temp = 0; for (x = 0; x < 256; x++) { if (((Table[x & A] + b) % 2) != (Table[SBOX[x] & U] % 2)) { temp++; } } if (nonlin > temp) { nonlin = temp; } } } } return nonlin; } } public class FixedPoint { public UInt32 Test(UInt32[] SBOX) { UInt32 count = 0; for (int i = 0; i < 256; i++) { if (SBOX[i] == i) { count++; } } return count; } } public class DifferentialUniform { UInt32[,] DTable = new UInt32[256, 256]; public UInt32 Test(UInt32[] SBOX) { UInt32 differ = 0; DTable[0, 0] = 256; for (int alpha = 1; alpha < 256; alpha++) { for (int x = 0; x < 256; x++) DTable[alpha, SBOX[x] ^ SBOX[x ^ alpha]]++; for (int x = 0; x < 256; x++) if (DTable[alpha, x] > differ) differ = DTable[alpha, x]; } return differ; } } public class AlgebraDegree { UInt32[] Table = new UInt32[256]; void SetTable() { int flag; for (int i = 0; i < 256; i++) { flag = i; for (int j = 0; j < 8; j++) { if (((flag >> j) & 0x01) != 0) { Table[i]++; } } } } public UInt32 Test(UInt32[] SBOX) { UInt32 Degree = 0; int[,] function = new int[8, 256]; SetTable(); for (int flag = 0; flag < 8; flag++) { int[] term = new int[256]; UInt32 degree = 0; for (int X = 0; X < 256; X++) { for (int a = 0; a < 256; a++) { if (((SBOX[a] >> flag) & 0x01) != 0) { int temp = 0x01; for (int x = 0; x < 8; x++) { if (((~X >> x) & 0x01) != 0) { if (((a >> x) & 0x01) != 0) { temp = 0x00; break; } } } term[X] ^= temp; } } function[flag, X] = term[X]; } for (int i = 0; i < 256; i++) { if ((function[flag, i] == 0x01) && (Table[i] > degree)) degree = Table[i]; if (Degree < degree) Degree = degree; } } return Degree; } } public class IterativePeriod { public UInt32[] Test(UInt32[] SBOX) { UInt32 temp; int[] js = new int[8]; UInt32[] ot = new UInt32[256]; for (int i = 0; i < 256; i++) { temp = SBOX[i]; do { temp = SBOX[temp]; ot[i]++; } while (temp != SBOX[i]); } return ot; } } public class Robust { UInt32[,] DTable = new UInt32[256, 256]; public double Test(UInt32[] SBOX) { int num = 0; UInt32 max = 0; double robustdegree = 0; DTable[0, 0] = 256; for (int alpha = 1; alpha < 256; alpha++) { for (int x = 0; x < 256; x++) DTable[alpha, SBOX[x] ^ SBOX[x ^ alpha]]++; for (int x = 0; x < 256; x++) if (DTable[alpha, x] > max) max = DTable[alpha, x]; } for (int i = 0; i < 256; i++) if (DTable[i, 0] != 0) num++; robustdegree = ((1 - (num + 0.0) / 256) * (1 - (max + 0.0) / 256)); return robustdegree; } } public class Balance { UInt32[] Table = new UInt32[256]; void SetTable() { int flag; for (int i = 0; i < 256; i++) { flag = i; for (int j = 0; j < 8; j++) { if (((flag >> j) & 0x01) != 0) { Table[i]++; } } } } public bool Test(UInt32[] SBOX) { SetTable(); int testnum = 0xFF; UInt32 count = 0; for (int i = 0; i < 256; i++) count += Table[testnum & SBOX[i]]; if (count - 1024 != 0) return false; else return true; } } public class StrictAvalancheCriterion { public double[,] sac = new double[8, 8]; public void Test(UInt32[] SBOX) { int n = 8; int i, j, k, bt, nN, sx, sy; //double[,] sac = new double[8, 8]; int[] js = new int[8]; bt = 1; nN = (int)Math.Pow(2, n); for (i = 0; i < n; i++) { for (k = 0; k < n; k++) js[k] = 0; for (j = 0; j < nN; j++) { sx = (int)SBOX[j]; sy = (int)SBOX[j ^ bt]; for (k = 0; k < n; k++) if ((sx >> k & 1) != (sy >> k & 1)) js[k]++; } bt <<= 1; for (k = 0; k < n; k++) sac[i, k] = (js[k] * 1.0) / 256; } } } } }