PCR numbers are naturally unsigned values. Further, they are 32 bits, even on 64-bit machines. This change eliminates the need for negative value and overflow tests. The parameter name is changed from j and idx to pcrHandle, which is more decriptive and is the parameter name used in the TPM 2.0 specification. Signed-off-by: Ken Goldman <kgoldman@xxxxxxxxxx> --- src/evmctl.c | 9 +++++---- src/pcr.h | 2 +- src/pcr_tss.c | 4 ++-- src/pcr_tsspcrread.c | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/evmctl.c b/src/evmctl.c index 1815f55..b056a1e 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -1895,7 +1895,8 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank) { int tpm_enabled = 0; char *errmsg = NULL; - int i, j; + int i; + uint32_t pcrHandle; int err; /* If --pcrs was specified, read only from the specified file(s) */ @@ -1915,9 +1916,9 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank) /* Read PCRs from multiple TPM 2.0 banks */ for (i = 0; i < num_banks; i++) { err = 0; - for (j = 0; j < NUM_PCRS && !err; j++) { - err = tpm2_pcr_read(bank[i].algo_name, j, - bank[i].pcr[j], bank[i].digest_size, + for (pcrHandle = 0; pcrHandle < NUM_PCRS && !err; pcrHandle++) { + err = tpm2_pcr_read(bank[i].algo_name, pcrHandle, + bank[i].pcr[pcrHandle], bank[i].digest_size, &errmsg); if (err) { log_debug("Failed to read %s PCRs: (%s)\n", diff --git a/src/pcr.h b/src/pcr.h index 79547bd..dd8311a 100644 --- a/src/pcr.h +++ b/src/pcr.h @@ -1,3 +1,3 @@ int tpm2_pcr_supported(void); -int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, +int tpm2_pcr_read(const char *algo_name, uint32_t pcrHandle, uint8_t *hwpcr, int len, char **errmsg); diff --git a/src/pcr_tss.c b/src/pcr_tss.c index feb1ff7..5e00524 100644 --- a/src/pcr_tss.c +++ b/src/pcr_tss.c @@ -106,7 +106,7 @@ static TPM2_ALG_ID algo_to_tss2(const char *algo_name) return TPM2_ALG_ERROR; } -int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, +int tpm2_pcr_read(const char *algo_name, uint32_t pcrHandle, uint8_t *hwpcr, int len, char **errmsg) { TSS2_ABI_VERSION abi_version = { @@ -140,7 +140,7 @@ int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, } }; - pcr_select_in.pcrSelections[0].pcrSelect[idx / 8] = (1 << (idx % 8)); + pcr_select_in.pcrSelections[0].pcrSelect[pcrHandle / 8] = (1 << (pcrHandle % 8)); ret = Esys_Initialize(&ctx, NULL, &abi_version); if (ret != TPM2_RC_SUCCESS) { diff --git a/src/pcr_tsspcrread.c b/src/pcr_tsspcrread.c index 462f270..118c7d2 100644 --- a/src/pcr_tsspcrread.c +++ b/src/pcr_tsspcrread.c @@ -68,7 +68,7 @@ int tpm2_pcr_supported(void) return 1; } -int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, +int tpm2_pcr_read(const char *algo_name, uint32_t pcrHandle, uint8_t *hwpcr, int len, char **errmsg) { FILE *fp; @@ -77,7 +77,7 @@ int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, int ret; sprintf(cmd, "%s -halg %s -ha %d -ns 2> /dev/null", - path, algo_name, idx); + path, algo_name, pcrHandle); fp = popen(cmd, "r"); if (!fp) { ret = asprintf(errmsg, "popen failed: %s", strerror(errno)); -- 2.25.1