Blog
All Blog Posts | Next Post | Previous Post
Using the TPM for application integrity and licensing: what it can and cannot do
Today
The TPM will not stop a determined attacker from patching your executable, but it can make a license useless on any machine other than the one it was issued for, and it can prove to a server that a key never left that machine. That is the straightforward summary of this post.
Every Windows 11 PC ships with a TPM 2.0, so developers naturally ask whether it can protect their software: check that the binary has not been tampered with, or make sure a license cannot be copied. The answer is yes for some goals and no for others. This post separates the two, then shows three practical patterns with Delphi code built on CNG and the Microsoft Platform Crypto Provider (PCP).
What a TPM actually provides
A TPM is a small, slow security chip (or a firmware equivalent) with four capabilities that matter here:
|
Capability |
What it gives you |
Typical use |
|
Non-exportable keys |
RSA or ECC private keys generated inside the TPM; software can use them but never read them |
Bind a license or an identity to one device |
|
PCRs and measured boot |
Registers that accumulate hashes of firmware, boot loader and OS components; they can only be extended, never set |
Describe the state the machine booted into |
|
Quotes (attestation) |
A TPM-signed statement of PCR values, verifiable by a remote party |
Prove the platform state to a server |
|
Sealing |
Data encrypted so that it only decrypts when the PCRs hold given values |
Release a secret only on a known-good boot |
On Windows, the simplest door to the first capability is CNG with the Microsoft Platform Crypto Provider: keys are created with NCryptCreatePersistedKey, stay in the user's key store, and every signing or decryption operation is executed by the TPM. In practice the PCP guarantees RSA 2048 and ECDSA P-256 on every TPM 2.0; larger keys and other curves depend on the chip and on the provider.
Two things a TPM does not do: it does not measure your application by itself, and it does not run your code. It signs, decrypts and attests; deciding what to do with those answers is still the job of your software or your server.
Start with the threat model
Any decision your application takes locally can be patched away: a check if not LicenseValid then Halt becomes a jump in a disassembler, whatever cryptography sits behind it. The TPM does not change that, because the TPM answers questions; it does not force your code to ask them.
What the TPM changes is what an attacker can copy and what they can forge:
Copying a license bound to a TPM key is worthless on another machine, since the private key cannot be extracted and moved with the files.
Forging a server can verify a signature made by a TPM key it has seen before, so a cracked client cannot impersonate a licensed device online.
Withholding data encrypted to the TPM key cannot be read anywhere else, so a cracked binary on another machine still lacks the content or feature keys it needs.
So the useful question is not "can the TPM stop cracking?" but "what does the attacker still lack after cracking?". The three patterns below are ranked by how much they withhold.
Pattern 1: a license bound to the device
The license names the device by the hash of a TPM public key, and the vendor signs it; a copied license file fails on any other machine.
Activation runs once:
1. The application creates (or reopens) a non-exportable ECDSA P-256 key in the TPM and exports its public key.
2. It sends that public key, with the customer's order or serial number, to the activation server.
3. The server builds the license (product, edition, expiry, SHA-256 of the device public key) and signs it with the vendor key, which never leaves the server.
4. The application stores the license and its signature next to its data.
activation sequence · TPM, application, server
The private key never leaves the TPM; the server only ever sees the public key.
On the client side, activation is a few lines with the TPM units used throughout this post:
Crypto := TTPMCrypto.Create;
try
if Crypto.KeyExists('MyApp_Device') then
DeviceKey := Crypto.OpenKey('MyApp_Device')
else
DeviceKey := Crypto.CreateP256SigningKey('MyApp_Device');
// Sent to the activation server with the order number
DevicePublicKey := DeviceKey.ExportPublicKey;
...At startup, the application checks three things: the vendor signed the license, the license names this device, and the device key is really usable here.
function IsLicenseValid(const License, LicenseSig, VendorPublicKey: TBytes;
DeviceKey: TTPMKey): Boolean;
var
DevicePublicKey, Nonce: TBytes;
begin
// 1. Issued by the vendor (vendor public key embedded in the application)
if not TTPMCrypto.VerifyWithECDSAPublicKey(VendorPublicKey, License, LicenseSig) then
Exit(False);
// 2. Issued for this device
DevicePublicKey := DeviceKey.ExportPublicKey;
if not SameBytes(LicenseDeviceHash(License), TTPMCrypto.SHA256(DevicePublicKey)) then
Exit(False);
// 3. The private key is present: sign a fresh nonce and check it
Nonce := TTPMCrypto.RandomBytes(32);
Result := TTPMCrypto.VerifyWithECDSAPublicKey(DevicePublicKey, Nonce,
TTPMCrypto.SignWithECDSA(DeviceKey, Nonce));
end;LicenseDeviceHash and SameBytes stand for small helpers of your own (the license parser and a byte comparison). Copying the PCP key file to another PC does not help an attacker either: the file is wrapped by a key that only the original TPM holds, so it cannot be loaded elsewhere.
This pattern defeats license sharing, the most common abuse. It does not defeat a patched binary that skips IsLicenseValid; patterns 2 and 3 address that.
Pattern 2: licensed secrets encrypted to the TPM
Instead of a yes/no answer, the server sends what the licensed features need, encrypted so that only this device's TPM can decrypt it. A patched binary on another machine skips the check but still cannot read the data.
What to encrypt depends on the product: a key that decrypts a premium module or a data set, model parameters, configuration that unlocks a paid tier, or the vendor's API token for a cloud service. The scheme is a classic envelope:
The device holds an RSA 2048 key restricted to decryption, created once in the TPM.
The server, which only needs the device's public key, picks a random AES-256 key, encrypts the payload with AES-GCM, and encrypts the AES key with RSA-OAEP / SHA-256.
The application asks the TPM to recover the AES key, then decrypts the payload in software.
// Activation: the decryption-only key and its public part for the server
EnvelopeKey := Crypto.CreateEnvelopeKey('MyApp_Envelope');
ServerReceives := EnvelopeKey.ExportPublicKey;
// Server side (no TPM needed, only the public key)
Envelope := TTPMCrypto.EncryptEnvelope(FeatureKey, DevicePublicKey, amGCM);
// Client side, at each start: the TPM recovers the AES key
FeatureKey := TTPMCrypto.DecryptEnvelope(Envelope, EnvelopeKey);
In GCM mode the whole envelope header, including the encrypted AES key, is authenticated, so any modification is detected. Restricting the key to decryption also matters: with the PCP, a key created with the decrypt usage refuses to sign, so a single compromised operation cannot be turned into a forged license.
The limit is the same as any client-side secret: once decrypted, the feature key sits in process memory, and an attacker with a debugger on a licensed machine can dump it. The TPM prevents redistribution of the encrypted files, not extraction by a legitimate licensee who is determined to break the product.
Pattern 3: online challenge-response and key attestation
When the decision moves to your server, patching the client no longer grants anything: the server only serves devices that prove, with their TPM key, that they are the activated machine.
The exchange is short:
1. The client asks for a service (an update, a cloud feature, a fresh time-limited license).
2. The server answers with a random nonce of 32 bytes.
3. The client signs the nonce, plus the request, with its TPM device key.
4. The server verifies the signature against the public key stored at activation, checks the license status, and only then serves the request.
A fresh nonce defeats replay, and revocation is immediate: the server simply stops answering for that public key. The client code is one call to SignWithECDSA; the work is on the server, which keeps the device registry.
One gap remains at activation: nothing so far proves that the public key really comes from a TPM. A cracked client could generate an ordinary software key, activate it and copy it freely. Key attestation closes that gap. The TPM certifies the new key with an attestation key (TPM2_Certify), and the attestation key itself is tied to the TPM's endorsement key, whose certificate is issued by the TPM manufacturer. A server that checks this chain knows the device key was generated inside a genuine TPM and marked non-exportable. Windows exposes this machinery through the PCP; it is more work to set up, and worth it when licenses carry real value.
Application integrity: what the TPM can and cannot vouch for
The TPM vouches for how the machine booted, not for your executable. Application integrity comes from code signing and the operating system; the TPM proves that the operating system enforcing it is the genuine one.
The chain looks like this:
Code signing does the application part. Sign every executable and DLL with Authenticode. Windows checks the signature at load time, and an App Control (WDAC) policy can refuse anything unsigned or signed by someone else.
Measured boot does the platform part. Firmware, boot manager and kernel are hashed into PCRs as the machine starts; PCR 7, for instance, reflects the Secure Boot configuration. Windows records each measurement in the TCG event log.
Remote attestation ties them together. A server asks for a TPM quote of the PCRs, replays the event log against it, and learns whether Secure Boot was on and which boot components ran. Only a machine that passes receives the service.
Two tempting shortcuts do not hold:
A self-check of your own hash (the program hashes its executable and compares) is one more local decision, patched as easily as the license check.
Extending a PCR with your application's hash yourself proves nothing to a server: any process can extend a resettable PCR with whatever value it likes, and Windows does not measure user-mode applications on its own.
Sealing a secret to PCR values (typically PCR 7) is possible, but every firmware or boot manager update changes the measurements; plan a resealing path, or the secret becomes unreadable after the next BIOS update.
Operational pitfalls
A TPM-bound license is only as good as the support path for the day the key disappears; every case below happens in the field.
|
Situation |
What happens |
What to plan |
|
TPM cleared, or TPM firmware updated |
Every TPM key is destroyed; Dell, for example, requires a TPM clear before a TPM firmware update |
Self-service re-activation, limited per period, handled by the server |
|
Motherboard or PC replaced |
New TPM, new keys |
Transfer of the license to a new device public key |
|
Several Windows accounts |
PCP keys are per user by default; machine-wide keys need administrator rights |
Decide whether the license is per user or per machine |
|
Virtual machines |
The key lives in the virtual TPM; depending on the hypervisor, its state can follow a copied VM |
Treat VMs as a separate license class, or rely on online checks |
|
Algorithm support |
Only RSA 2048 and ECDSA P-256 are available everywhere through the PCP; P-384 may exist in the TPM yet be refused by the provider, P-521 is practically absent |
Stick to RSA 2048 and P-256 |
|
Speed |
A discrete TPM takes about 150 ms per signature and several seconds to generate an RSA 2048 key |
Generate keys at activation; sign once per session, not per call |
|
Blocked commands |
Windows TBS blocks some TPM commands for applications (TPM2_GetTestResult, for instance, on some machines) |
Prefer CNG/PCP over raw TPM commands, and handle TPM_E_COMMAND_BLOCKED |
|
Privacy |
The endorsement key is a permanent, unique identifier of the machine |
Use a per-application key as the device identity, never the EK itself |
Get the code with TMS Cryptography Pack
The TPM units and the two demo programs used in this post ship with TMS Cryptography Pack, in a dedicated folder (\Demo\Console\TPM). They talk to the TPM through Windows only (TBS, CNG and the Platform Crypto Provider) and are not yet part of the library's classes, so you can adopt them without changing your existing code.
They cover the whole chain shown above:
TPM diagnostics: device information, properties, firmware version, supported algorithms and curves, PCR banks and values
persistent RSA and ECDSA keys in the PCP: creation, signing, verification, public key export, RSA-OAEP
a TTPMCrypto facade: SHA-256, AES-256 in CBC or GCM, PBKDF2 key derivation, TPM-protected envelopes, RSA 2048 and ECDSA P-256 signatures
readable decoding of TPM, TBS and NCrypt error codes
full interoperability between TPM and TMS crypto functions
On the software side, TMS Cryptography Pack provides native Pascal implementations to encrypt, sign, hash and authenticate data and files, for VCL, FMX and console applications. The TPM keeps the keys on the device; the library does everything else, so a license file, a signed update or an encrypted data set can be produced and checked with the same toolkit on the server and on the client.
A free trial is available from the product page.
Conclusion
Use the TPM to make a license non-transferable and to let your server recognise a device it activated; do not expect it to stop a patched binary on its own. Combined, the three patterns raise the cost of piracy from copying a file to breaking a product on a licensed machine, which is where most commercial protection should aim.
A short checklist:
Device identity = a per-application ECDSA P-256 key created in the TPM through the PCP
License = vendor-signed data that includes the hash of the device public key
Valuable features or data delivered encrypted to a decryption-only TPM key (AES-256-GCM envelope)
Server-side decisions protected by a nonce signed with the device key
Key attestation at activation when licenses carry real value
Executables signed with Authenticode; platform state checked by remote attestation if needed
A re-activation path for TPM clears, firmware updates and hardware replacement
Bernard Roussely
This blog post has not received any comments yet.
All Blog Posts | Next Post | Previous Post