How to test for TPM 1.2 vs. TPM 2.0 in C# or C++ on Windows 10

Private Coder 75 Reputation points
2023-03-15T16:00:38.5133333+00:00

My app is written in C# and runs on Windows 10. It needs to configure itself depending on whether the system has a TPM (Trusted Platform Module) and, if so, whether it's a TPM V. 1.2 or a TPM V. 2.0.

I am aware of the command, get-tpm, in PowerShell. On my machine, get-tpm has this output.

TpmPresent                : True
TpmReady                  : True
TpmEnabled                : True
TpmActivated              : True
TpmOwned                  : True
RestartPending            : False
ManufacturerId            : ...
ManufacturerIdTxt         : STM
ManufacturerVersion       : 73.64.17568.6659
ManufacturerVersionFull20 : 73.64.17568.6659
ManagedAuthLevel          : Full
OwnerAuth                 :
OwnerClearDisabled        : False
AutoProvisioning          : Enabled
LockedOut                 : False
LockoutHealTime           : 10 minutes
LockoutCount              : 0
LockoutMax                : 31
SelfTest                  : {}

I believe a PowerShell scripts simply needs to test whether ManufacturerVersionFull20 or ManufacturerVersionFull12 is among the properties provided by tpm-get. Is there anything that's analogous to that in C#? If push comes to shove, then I can always use an external script and run it from my app, of course. Ideally, I would like to avoid that.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,195 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,636 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Private Coder 75 Reputation points
    2023-03-15T16:09:27.2233333+00:00

    Now I've found this code.

    using Tpm2Lib;
    
    public static void TestTpmVersion()
    {
        try
        {
            var tpm = TpmDevice.GetTpm();
            var tpmVersion = tpm.GetCapability(Capability.TpmVersion);
            if (tpmVersion == 0x20000)
            {
                Console.WriteLine("TPM version 2.0");
            }
            else if (tpmVersion == 0x10000)
            {
                Console.WriteLine("TPM version 1.2");
            }
            else
            {
                Console.WriteLine("Unknown TPM version");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
    
    0 comments No comments