Esempio n. 1
0
        static void GeneratePermutationTable(
            PermutationTable table,
            string entityName       = "ENTITY_NAME",
            string architectureName = "ARCHITECTURE_NAME")
        {
            switch (table)
            {
            case PermutationTable.IP:
                var ip       = TableValues.IP();
                var ipResult = InitPermutationTableHeader(64, 64, entityName, architectureName);
                ipResult = GeneratePermutationTableAssignments(ip, ipResult);
                ipResult.Add($"end {architectureName};");
                File.WriteAllLines(_path + "IP.vhd", ipResult);
                break;

            case PermutationTable.IPInv:
                var ipInv       = TableValues.IPInv();
                var ipInvResult = InitPermutationTableHeader(64, 64, entityName, architectureName);
                ipInvResult = GeneratePermutationTableAssignments(ipInv, ipInvResult);
                ipInvResult.Add($"end {architectureName};");
                File.WriteAllLines(_path + "IPInv.vhd", ipInvResult);
                break;

            case PermutationTable.P:
                var p       = TableValues.P();
                var pResult = InitPermutationTableHeader(32, 32, entityName, architectureName);
                pResult = GeneratePermutationTableAssignments(p, pResult);
                pResult.Add($"end {architectureName};");
                File.WriteAllLines(_path + "P.vhd", pResult);
                break;

            case PermutationTable.PC1:
                var pc1       = TableValues.PC1();
                var pc1Result = InitPermutationTableHeader(64, 56, entityName, architectureName);
                pc1Result = GeneratePermutationTableAssignments(pc1, pc1Result);
                pc1Result.Add($"end {architectureName};");
                File.WriteAllLines(_path + "PC1.vhd", pc1Result);
                break;

            case PermutationTable.PC2:
                var pc2       = TableValues.PC2();
                var pc2Result = InitPermutationTableHeader(56, 48, entityName, architectureName);
                pc2Result = GeneratePermutationTableAssignments(pc2, pc2Result);
                pc2Result.Add($"end {architectureName};");
                File.WriteAllLines(_path + "PC2.vhd", pc2Result);
                break;
            }
        }
Esempio n. 2
0
        static void GenerateSubstitutionTable(
            SBox name,
            string switchVariable   = "INPUT",
            string assignVariable   = "OUTPUT",
            string entityName       = "ENTITY_NAME",
            string architectureName = "ARCHITECTURE_NAME")
        {
            byte mask    = 15; // 0b00001111
            var  result  = InitSBoxHeader(switchVariable, entityName, architectureName);
            int  counter = 0;

            switch (name)
            {
            case SBox.AesSbox:
                var aesSbox = TableValues.AesSBox();
                foreach (var row in aesSbox)
                {
                    foreach (var number in row)
                    {
                        int    columnIndex  = counter & mask;
                        int    rowIndex     = (counter >> 4) & mask;
                        string newValue     = GetBinaryValue(aesSbox[rowIndex][columnIndex], 8);
                        string counterValue = GetBinaryValue(counter++, 8);
                        result.Add($"\t\twhen \"{counterValue}\" => {assignVariable} <= \"{newValue}\";");
                    }
                }

                result.Add("end case;");
                result.Add("end process;");
                result.Add($"end {architectureName};");
                File.WriteAllLines(_path + "AesSbox.vhd", result);
                break;

            case SBox.AesSboxInv:
                var aesSboxInv = TableValues.AesSBoxInv();
                foreach (var row in aesSboxInv)
                {
                    foreach (var number in row)
                    {
                        int    columnIndex  = counter & mask;
                        int    rowIndex     = (counter >> 4) & mask;
                        string newValue     = GetBinaryValue(aesSboxInv[rowIndex][columnIndex], 8);
                        string counterValue = GetBinaryValue(counter++, 8);
                        result.Add($"\twhen \"{counterValue}\" => {assignVariable} <= \"{newValue}\";");
                    }
                }

                result.Add("end case;");
                result.Add("end process;");
                result.Add($"end {architectureName};");
                File.WriteAllLines(_path + "AesSboxInv.vhd", result);
                break;

            case SBox.Des1:
                var des1 = TableValues.DesSBox(1);
                GenerateDesSbox(des1, "Des1", assignVariable, entityName, architectureName: architectureName);
                break;

            case SBox.Des2:
                var des2 = TableValues.DesSBox(2);
                GenerateDesSbox(des2, "Des2", assignVariable, architectureName);
                break;

            case SBox.Des3:
                var des3 = TableValues.DesSBox(3);
                GenerateDesSbox(des3, "Des3", assignVariable, architectureName);
                break;

            case SBox.Des4:
                var des4 = TableValues.DesSBox(4);
                GenerateDesSbox(des4, "Des4", assignVariable, architectureName);
                break;

            case SBox.Des5:
                var des5 = TableValues.DesSBox(5);
                GenerateDesSbox(des5, "Des5", assignVariable, architectureName);
                break;

            case SBox.Des6:
                var des6 = TableValues.DesSBox(6);
                GenerateDesSbox(des6, "Des6", assignVariable, architectureName);
                break;

            case SBox.Des7:
                var des7 = TableValues.DesSBox(7);
                GenerateDesSbox(des7, "Des7", assignVariable, architectureName);
                break;

            case SBox.Des8:
                var des8 = TableValues.DesSBox(8);
                GenerateDesSbox(des8, "Des8", assignVariable, entityName, architectureName: architectureName);
                break;
            }
        }