Been trying to write and read my own keys from the TPM (AES 256) using c# with .NET 4.8
After starting to test my code i noticed that defining space after multiple (specifically 5) successful writes in a NvSpace exception.
I would like to know if there’s any known limit to the user writing on the TPM.
relevent code (assuming i have a handler representing a free space)
public static void LoadKeyToTPM(byte[] key, uint nvIndexHandle)
{
Tpm2Device tpmDevice = new TbsDevice();
tpmDevice.Connect();
// Create a TPM context
Tpm2 tpm = new Tpm2(tpmDevice);
// Check if an available NV space handle was found
if (nvIndexHandle != 0)
{
// Define NV public area with the necessary attributes
NvPublic nvPublic = new NvPublic(
new TpmHandle(nvIndexHandle), // NV Index handle
TpmAlgId.Sha256, // Name algorithm
NvAttr.Authwrite | NvAttr.Authread, // NV attributes
ComputeHMAC(secretKey), // Authorization policy
512 // Data size
);
TpmHandle ownerAuth = new TpmHandle(TpmRh.Owner);
// Define the NV space, crushes here after 5 successful attemps
tpm.NvDefineSpace(ownerAuth, ComputeHMAC(secretKey), nvPublic);
// Write data to NV memory
WriteNV(tpm, nvIndexHandle, key, secretKey);
}
else
{
Console.WriteLine("No available NV space found.");
}
}
Solutions i’ve tried:
- Write on different parts of the NV space (not consecutive slots)
- Write in different sessions to see if there’s some time gating
- Write the same key over and over and even a random string just to verify the problem isnt the data being written.
- Define more space than needed on each define attemp
- Checking the manufacturer for some types of limitations on space or slot consumption