Primer
All that follows is about TPM 1.2. Keep in mind that Microsoft requires a TPM 2.0 for all future Windows versions. The 2.0 generation is fundamentally different to the 1.2
There is no one-line solution because of TPM design principles. Think of the TPM as a microcontroller with limited resources. It main design goal was to be cheap, while still secure. So the TPM was ripped of all logic which was not necessary for a secure operation. Thus a TPM is only working when you have at least some more or less fat software, issuing a lot of commands in the correct order. And those sequences of commands may get very complex. That’s why TCG specified the TSS with a well defined API. If you would like to go the Java way, there is even an high level Java API. I’m not aware of an similar project for C# / .net
Development
In your case I’d suggest you look at IBM’s software TPM.
- Project page
- Donwload the whole package
In the package you will find 3 very usefull components:
- a software TPM emulator
- a lightweight tpm lib
- some basic command line utilities
You don’t necessarily need the software TPM emulator, you can also connect to the machine’s HW TPM. However, you can intercept the issued commands and look at the responses, thus learning how they are assembled and how they correspond to the command specification.
High level
Prerequisites:
- TPM is activated
- TPM driver is loaded
- you have taken ownership of the TPM
In order to seal a blob, you need to do the following:
- create a key
- store the key-blob somewhere
- ensure that the key is loaded in the TPM
- seal the blob
To unseal you need to:
- obtain the key-blob
- load the key to the TPM
- unseal the sealed blob
You can store the key-blob in your data structure you use to store the protected bytes.
Most of the TPM commands you need are authorized ones. Therefore you need to establish authorization sessions where needed. AFAIR those are mostly OSAP sessions.
TPM commands
Currently I can’t run a debug version, so I can’t provide you with the exact sequence. So consider this an unordered list of commands you will have to use:
TPM_OSAPTPM_CreateWrapKeyTPM_LoadKey2TPM_Seal
If you want to read the current PCR values, too:
TPM_PCRRead