Example #1
0
        public static void SaveValueIntoTpm(int address, byte[] data, int length)
        {
            Tpm2Device tpmDevice;

            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                tpmDevice = new TbsDevice();
            }
            else
            {
                tpmDevice = new LinuxTpmDevice();
            }
            tpmDevice.Connect();

            var tpm = new Tpm2(tpmDevice);

            var       ownerAuth = new AuthValue();
            TpmHandle nvHandle  = TpmHandle.NV(address);

            tpm[ownerAuth]._AllowErrors().NvUndefineSpace(TpmHandle.RhOwner, nvHandle);

            AuthValue nvAuth   = authValue;
            var       nvPublic = new NvPublic(nvHandle, TpmAlgId.Sha1, NvAttr.Authwrite | NvAttr.Authread, new byte[0], (ushort)length);

            tpm[ownerAuth].NvDefineSpace(TpmHandle.RhOwner, nvAuth, nvPublic);

            tpm[nvAuth].NvWrite(nvHandle, nvHandle, data, 0);
            tpm.Dispose();
        }
        public override void ExecuteInternal()
        {
            Tpm2Device?tpmDevice = null;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                tpmDevice = new TbsDevice();
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                tpmDevice = new LinuxTpmDevice();
            }

            if (tpmDevice is Tpm2Device)
            {
                //
            }

            tpmDevice?.Dispose();
        }
Example #3
0
        public static byte[] ReadValueFromTpm(int address, int length)
        {
            Tpm2Device tpmDevice;

            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                tpmDevice = new TbsDevice();
            }
            else
            {
                tpmDevice = new LinuxTpmDevice();
            }
            tpmDevice.Connect();
            var       tpm      = new Tpm2(tpmDevice);
            TpmHandle nvHandle = TpmHandle.NV(address);
            AuthValue nvAuth   = authValue;

            byte[] newData = tpm[nvAuth].NvRead(nvHandle, nvHandle, (ushort)length, 0);
            tpm.Dispose();
            return(newData);
        }
Example #4
0
        public static byte[] ReadData(int tailleData)
        {
            Tpm2Device tpmDevice;

            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                tpmDevice = new TbsDevice();
            }
            else
            {
                tpmDevice = new LinuxTpmDevice();
            }
            tpmDevice.Connect();
            var tpm = new Tpm2(tpmDevice);

            TpmHandle nvHandle = TpmHandle.NV(indexTMPSlot);

            AuthValue nvAuth = nvAuthValue;

            byte[] newData = tpm[nvAuth].NvRead(nvHandle, nvHandle, (ushort)tailleData, 0);

            tpm.Dispose();
            return(newData);
        }
Example #5
0
        /// <summary>
        /// Executes the hashing functionality. After parsing arguments, the
        /// function connects to the selected TPM device and invokes the TPM
        /// commands on that connection.
        /// </summary>
        /// <param name="args">Arguments to this program.</param>
        static void Main(string[] args)
        {
            //
            // Parse the program arguments. If the wrong arguments are given or
            // are malformed, then instructions for usage are displayed and
            // the program terminates.
            //
            string tpmDeviceName = ParseArguments(args);

            if (tpmDeviceName == null)
            {
                return;
            }

            try
            {
                //
                // Create the device according to the selected connection.
                //
                Tpm2Device tpmDevice;
                switch (tpmDeviceName)
                {
                case DeviceSim:
                    tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);
                    break;

                case DeviceWinTbs:
                    tpmDevice = new TbsDevice();
                    break;

                case DeviceLinux:
                    tpmDevice = new LinuxTpmDevice();
                    break;

                default:
                    throw new Exception("Unknown device selected.");
                }

                //
                // Connect to the TPM device. This function actually establishes the
                // connection.
                //
                tpmDevice.Connect();

                //
                // Pass the device object used for communication to the TPM 2.0 object
                // which provides the command interface.
                //
                var tpm = new Tpm2(tpmDevice);
                if (tpmDevice is TcpTpmDevice)
                {
                    //
                    // If we are using the simulator, we have to do a few things the
                    // firmware would usually do. These actions have to occur after
                    // the connection has been established.
                    //
                    tpmDevice.PowerCycle();
                    tpm.Startup(Su.Clear);
                }

                Pcrs(tpm);
                QuotePcrs(tpm);
                StorageRootKey(tpm);
                //
                // Need a synchronization event to avoid disposing TPM object before
                // asynchronous method completed.
                //
                var sync = new AutoResetEvent(false);
                Console.WriteLine("Calling asynchronous method.");
                PrimarySigningKeyAsync(tpm, sync);

                Console.WriteLine("Waiting for asynchronous method to complete.");
                sync.WaitOne();

                //
                // Clean up.
                //
                tpm.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred: {0}", e.Message);
            }

            Console.WriteLine("Press Any Key to continue.");
            Console.ReadLine();
        }
Example #6
0
    /// <summary>
    /// After parsing the arguments for the TPM device, the program executes a read
    /// of the prior initialized NV index (3001). The program then outputs the 8 bytes
    /// previously stored at that index.
    /// </summary>
    /// <param name="args">Arguments to this program.</param>
    static void Main(string[] args)
    {
        //
        // Parse the program arguments. If the wrong arguments are given or
        // are malformed, then instructions for usage are displayed and
        // the program terminates.
        //
        string tpmDeviceName;

        if (!ParseArguments(args, out tpmDeviceName))
        {
            WriteUsage();
            return;
        }

        try
        {
            Tpm2Device tpmDevice;
            switch (tpmDeviceName)
            {
            case DeviceSimulator:
                tpmDevice = new TcpTpmDevice(DefaultSimulatorName, DefaultSimulatorPort);
                break;

            case DeviceWinTbs:
                tpmDevice = new TbsDevice();
                break;

            case DeviceLinux:
                tpmDevice = new LinuxTpmDevice();
                break;

            default:
                throw new Exception("Unknown device selected.");
            }

            tpmDevice.Connect();

            var tpm = new Tpm2(tpmDevice);
            if (tpmDevice is TcpTpmDevice)
            {
                //
                // If we are using the simulator, we have to do a few things the
                // firmware would usually do. These actions have to occur after
                // the connection has been established.
                //
                tpmDevice.PowerCycle();
                tpm.Startup(Su.Clear);
            }

            NVReadOnly(tpm);

            // TPM clean up procedure
            tpm.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception occurred: {0}", e.Message);
        }

        Console.WriteLine("Press Any Key to continue.");
        Console.ReadLine();
    }