Example #1
0
        internal static TpmClient CreateDeviceClient()
        {
            Tpm2Device tpmDevice = new TbsDevice();

            tpmDevice.Connect();

            var tpm = new Tpm2(tpmDevice);

            TpmClient client = new TpmClient(tpmDevice, tpm);

            return(client);
        }
Example #2
0
        internal static TpmClient CreateSimulatorClient()
        {
            Tpm2Device tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);

            tpmDevice.Connect();

            var tpm = new Tpm2(tpmDevice);

            tpmDevice.PowerCycle();
            tpm.Startup(Su.Clear);

            TpmClient client = new TpmClient(tpmDevice, tpm);

            return(client);
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            using (var client = TpmClient.CreateSimulatorClient())
            {
                var helloWorld = Encoding.UTF8.GetBytes("Hello World");

                var cipher   = RsaExamples.RsaEncrypt(client.Tpm, helloWorld);
                var decipher = RsaExamples.RsaDecrypt(client.Tpm, cipher);

                var helloWorld2 = Encoding.UTF8.GetString(decipher);

                return;



                Examples.PrintCommandss(client.Tpm);
                Examples.CreateTwoPrimaries(client.Tpm);

                Examples.EncryptDecrypt(client.Tpm);

                return;



                var r = client.Tpm.GetRandom(10);
                Console.WriteLine(client.Tpm._GetUnderlyingDevice().HasRM());
                CreateRsaPrimaryKey(client.Tpm, false);
                Examples.NVReadWrite(client.Tpm);
                Examples.NVCounter(client.Tpm);

                Examples.AvailablePCRBanks(client.Tpm);
                Examples.PrintAlgorithms(client.Tpm);
            }

            return;

            using (var client = TpmClient.CreateDeviceClient())
            {
                Examples.AvailablePCRBanks(client.Tpm);
                Examples.PrintAlgorithms(client.Tpm);
                Examples.PrintCommandss(client.Tpm);
            }

            return;



            Sign();

            return;

            var data = Encoding.UTF8.GetBytes("Hello World");

            Examples.SaveValueIntoTpm(3001, data, data.Length, _authValue);
            Examples.ReadValueFromTpm(3001, data.Length, _authValue);
            return;



            Examples.GetHardwareDeviceName();
            return;

            Examples.GetDeviceId();


            return;

            Examples.ConnectLocal();
            Examples.ConnectSimulator();


            using (var device = Examples.Connect(useSimulator))
            {
                //Examples.AvailablePCRBanks(device);
            }



            return;



            ReadPcr();
            Sign();

            try
            {
                using (Tpm2Device tpmDevice = new TbsDevice())
                {
                    tpmDevice.Connect();

                    using (var tpm = new Tpm2(tpmDevice))
                    {
                        ICapabilitiesUnion caps;
                        tpm.GetCapability(Cap.Algs, 0, 1000, out caps);
                        var algsx = (AlgPropertyArray)caps;

                        Console.WriteLine("Supported algorithms:");
                        foreach (var alg in algsx.algProperties)
                        {
                            Console.WriteLine("  {0}", alg.alg.ToString());
                        }

                        Console.WriteLine("Supported commands:");
                        tpm.GetCapability(Cap.TpmProperties, (uint)Pt.TotalCommands, 1, out caps);
                        tpm.GetCapability(Cap.Commands, (uint)TpmCc.First, TpmCc.Last - TpmCc.First + 1, out caps);

                        var          commands      = (CcaArray)caps;
                        List <TpmCc> implementedCc = new List <TpmCc>();
                        foreach (var attr in commands.commandAttributes)
                        {
                            var commandCode = (TpmCc)((uint)attr & 0x0000FFFFU);
                            implementedCc.Add(commandCode);
                            Console.WriteLine("  {0}", commandCode.ToString());
                        }

                        Console.WriteLine("Commands from spec not implemented:");
                        foreach (var cc in Enum.GetValues(typeof(TpmCc)))
                        {
                            if (!implementedCc.Contains((TpmCc)cc))
                            {
                                Console.WriteLine("  {0}", cc.ToString());
                            }
                        }

                        //
                        // As an alternative: call GetCapabilities more than once to obtain all values
                        //
                        byte more;
                        var  firstCommandCode = (uint)TpmCc.First;
                        do
                        {
                            more     = tpm.GetCapability(Cap.Commands, firstCommandCode, 10, out caps);
                            commands = (CcaArray)caps;
                            //
                            // Commands are sorted; getting the last element as it will be the largest.
                            //
                            uint lastCommandCode = (uint)commands.commandAttributes[commands.commandAttributes.Length - 1] & 0x0000FFFFU;
                            firstCommandCode = lastCommandCode;
                        } while (more == 1);

                        //
                        // Read PCR attributes. Cap.Pcrs returns the list of PCRs which are supported
                        // in different PCR banks. The PCR banks are identified by the hash algorithm
                        // used to extend values into the PCRs of this bank.
                        //
                        tpm.GetCapability(Cap.Pcrs, 0, 255, out caps);
                        PcrSelection[] pcrs = ((PcrSelectionArray)caps).pcrSelections;

                        Console.WriteLine();
                        Console.WriteLine("Available PCR banks:");
                        foreach (PcrSelection pcrBank in pcrs)
                        {
                            var sb = new StringBuilder();
                            sb.AppendFormat("PCR bank for algorithm {0} has registers at index:", pcrBank.hash);
                            sb.AppendLine();
                            foreach (uint selectedPcr in pcrBank.GetSelectedPcrs())
                            {
                                sb.AppendFormat("{0},", selectedPcr);
                            }
                            Console.WriteLine(sb);
                        }

                        //
                        // Read PCR attributes. Cap.PcrProperties checks for certain properties of each PCR register.
                        //
                        tpm.GetCapability(Cap.PcrProperties, 0, 255, out caps);

                        Console.WriteLine();
                        Console.WriteLine("PCR attributes:");
                        TaggedPcrSelect[] pcrProperties = ((TaggedPcrPropertyArray)caps).pcrProperty;
                        foreach (TaggedPcrSelect pcrProperty in pcrProperties)
                        {
                            if ((PtPcr)pcrProperty.tag == PtPcr.None)
                            {
                                continue;
                            }

                            uint pcrIndex = 0;
                            var  sb       = new StringBuilder();
                            sb.AppendFormat("PCR property {0} supported by these registers: ", (PtPcr)pcrProperty.tag);
                            sb.AppendLine();
                            foreach (byte pcrBitmap in pcrProperty.pcrSelect)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    if ((pcrBitmap & (1 << i)) != 0)
                                    {
                                        sb.AppendFormat("{0},", pcrIndex);
                                    }
                                    pcrIndex++;
                                }
                            }
                            Console.WriteLine(sb);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.ReadKey();
        }