2

I am going through TPM(Trusted Platform Module) and trying to do some task. How can i store data to TPM(Trusted Platform Module) chip and also how to read that data. Can anyone help me on this?

Arshit Singh
  • 103
  • 4
  • 14
  • 1
    I do not think you would use a TPM to store data directly. You might instead ask the TPM to encrypt/decrypt a key that you use to encrypt your data.You might want to have a look at : https://github.com/Microsoft/TSS.MSR and https://stackoverflow.com/questions/28862767/how-to-encrypt-bytes-using-the-tpm-trusted-platform-module – JonasH Jun 26 '20 at 13:45

1 Answers1

1

This is done using the NV storage commands. Using TSS.MSR, from their samples:

static void NVReadWrite(Tpm2 tpm)
{
    //
    // AuthValue encapsulates an authorization value: essentially a byte-array.
    // OwnerAuth is the owner authorization value of the TPM-under-test.  We
    // assume that it (and other) auths are set to the default (null) value.
    // If running on a real TPM, which has been provisioned by Windows, this
    // value will be different. An administrator can retrieve the owner
    // authorization value from the registry.
    //
    var ownerAuth = new AuthValue();
    TpmHandle nvHandle = TpmHandle.NV(3001);

    //
    // Clean up any slot that was left over from an earlier run
    // 
    tpm._AllowErrors()
       .NvUndefineSpace(TpmRh.Owner, nvHandle);
    //
    // Scenario 1 - write and read a 32-byte NV-slot
    // 
    AuthValue nvAuth = AuthValue.FromRandom(8);
    tpm.NvDefineSpace(TpmRh.Owner, nvAuth,
                      new NvPublic(nvHandle, TpmAlgId.Sha1,
                                   NvAttr.Authread | NvAttr.Authwrite,
                                   null, 32));

    //
    // Write some data
    // 
    var nvData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    tpm.NvWrite(nvHandle, nvHandle, nvData, 0);
    //
    // And read it back
    // 
    byte[] nvRead = tpm.NvRead(nvHandle, nvHandle, (ushort)nvData.Length, 0);

    //
    // Is it correct?
    // 
    bool correct = nvData.SequenceEqual(nvRead);
    if (!correct)
    {
        throw new Exception("NV data was incorrect.");
    }

    Console.WriteLine("NV data written and read.");

    //
    // And clean up
    // 
    tpm.NvUndefineSpace(TpmRh.Owner, nvHandle);
}
mnistic
  • 10,866
  • 2
  • 19
  • 33