From a5dfd26d450c5ce826a701f12a76f9efb51b38b4 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Sat, 13 Jan 2024 12:12:14 -0500 Subject: [PATCH] tpm2: NVMarshal: To read an OBJECT from NVRAM use NvReadObject not NvRead NvReadObject knows how to read an OBJECT from NVRAM, so use it instead of the low-level NvRead(). This prepares the code for possibly larger OBJECTs in NVRAM in the future that may have been marshalled before they were written into NVRAM (such as RSA 4096 keys or possibly keys from QC if they will be stored in OBJECTs). The call to NvReadObject() requires that the ref in NvReadObject points to the handle of an NV_ENTRY_HEADER. An NV_ENTRY_HEADER looks like this: typedef struct { UINT32 size; TPM_HANDLE handle; } NV_ENTRY_HEADER; Signed-off-by: Stefan Berger --- src/tpm2/NVMarshal.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/tpm2/NVMarshal.c b/src/tpm2/NVMarshal.c index e01f24c0..6fa06c04 100644 --- a/src/tpm2/NVMarshal.c +++ b/src/tpm2/NVMarshal.c @@ -4561,11 +4561,9 @@ USER_NVRAM_Display(const char *msg) break; case TPM_HT_PERSISTENT: fprintf(stderr, " (PERSISTENT)"); - offset += sizeof(handle); - NvRead(&obj, entryRef + offset, sizeof(obj)); - offset += sizeof(obj); - fprintf(stderr, " sizeof(obj): %zu\n", sizeof(obj)); + NvReadObject(entryRef + offset, &obj); + fprintf(stderr, " sizeof(obj): %zu entrysize: %u\n", sizeof(obj), entrysize); break; default: TPMLIB_LogTPM2Error("USER_NVRAM: Corrupted handle: %08x\n", handle); @@ -4637,10 +4635,7 @@ USER_NVRAM_Marshal(BYTE **buffer, INT32 *size) } break; case TPM_HT_PERSISTENT: - offset += sizeof(handle); - - NvRead(&obj, entryRef + offset, sizeof(obj)); - offset += sizeof(obj); + NvReadObject(entryRef + offset, &obj); written += ANY_OBJECT_Marshal(&obj, buffer, size); break; default: