Try Encrypting and Decrypting Strings with TPM from PowerShell
While reading a book about zero-trust, I came across a section discussing the use of TPM for device authentication.
TPM is a hardware embedded in devices like Windows PCs that securely handles encryption and decryption.
It’s used in everyday scenarios like Windows Hello.
There are both external and built-in types of TPM.
Using TPM allows generation of a private key and public key, enabling data encryption and decryption.
However, the private key is stored within the TPM and cannot be accessed by anyone.
Therefore, as a user, you only need to request encryption or decryption from the TPM, and you will never know the private key stored inside it.
This is the most important aspect of TPM: you cannot know the private key.
Let’s Try It
Let’s try encrypting and decrypting some data using TPM with PowerShell.
First, check if my PC can use TPM.
Get-Tpm
If it displays the following, it’s available.
TpmPresent : True #=> True if TPM is installed
TpmReady : True #=> True if TPM is usable
(以下略)
Try Encrypting
Create a data object containing a string.
$data = [Text.Encoding]::UTF8.GetBytes("Hello TPM My Name is Shin Hiroe. This is Top Secret.")
Create an X.509 certificate object
$cert = New-SelfSignedCertificate `
-Subject "CN=TPM-Test" `
-Type Custom `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-Provider "Microsoft Platform Crypto Provider" `
-KeyExportPolicy NonExportable `
-CertStoreLocation "Cert:\CurrentUser\My"
Get the public key generated by TPM
$public = $cert.GetRSAPublicKey()
Encrypt the data using the retrieved public key
$encrypted = $public.Encrypt($data, [System.Security.Cryptography.RSAEncryptionPadding]::OaepSHA256)
Output the encrypted data as a Base64 string for verification
[Convert]::ToBase64String($encrypted)
n9G6rhReXWxy+XbRlsQh/1z8fV+fC6ZZovSNOsuOO0Lc/G7MT9U5oepyRXNcCc+sQrwSmb2nZZ09Y1c0rr9KI22ko3ImilG1RJ2szinL6gsznLaM/dQXyx4tZmbLI0RxRhmm1Z4zzzctmonBIRLgvZFvg1PB/cC2cZPzHw7iuSNnjY0FJCeJLemJjMQhRBqrIgG/Z5mASwzU0/t07Fmk+ZWyXNvBZqsb1btoxwg+9LGb0CRwPQLG/8KsT1XYnzVwL8HFmJxmUOqz+evlOOdcHi5jKpCg0QbShMSK5VoMngSCksbF1tk9FDojqO6bUbztYhFmg3TlZUZlPm8WZR8wfQ==
It’s properly encrypted.
Try Decrypting
Now, let’s try decrypting the data we encrypted earlier.
First, retrieve the private key object.
However, what we retrieve here is not the actual private key. It’s an abstracted object of the private key, and during actual use, the TPM is requested for encryption/decryption internally.
$private = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
$private is a private key object, not the actual private key.
Use this to decrypt.
$decrypted = $private.Decrypt($encrypted, [System.Security.Cryptography.RSAEncryptionPadding]::OaepSHA256)
The data decrypted using the private key within the TPM is stored in $decrypted.
Let’s check the decrypted data.
[Text.Encoding]::UTF8.GetString($decrypted)
Hello TPM My Name is Shin Hiroe. This is Top Secret.
It’s properly decrypted.
Keys created with ssh-keygen are stored in files, which carry a risk of leakage, but TPM has a very low risk of leakage, even if not absolutely zero.
Additionally, since TPM is a hardware implementation, data encrypted with the public key can only be decrypted on “this PC”, which is the biggest advantage of TPM.
Although I still don’t fully understand it, I plan to try using TPM for SSH key generation or certificate authentication for Wi-Fi. TPM is interesting and can be tested on my own PC, so I’ll try these and write an article if possible.
Comments
No comments yet.