[SLOF] [PATCH v4 31/33] tpm2: Use a table to convert the hash to the buffer size it needs.

Stefan Berger stefanb at linux.vnet.ibm.com
Thu Dec 12 07:27:26 AEDT 2019


Signed-off-by: Stefan Berger <stefanb at linux.ibm.com>
---
 include/helpers.h    |  1 +
 lib/libtpm/tcgbios.c | 41 ++++++++++++++++++++++++++++-------------
 2 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/include/helpers.h b/include/helpers.h
index 4353b3b..c6a2ccd 100644
--- a/include/helpers.h
+++ b/include/helpers.h
@@ -49,5 +49,6 @@ extern int SLOF_get_keystroke(void);
 #define container_of(ptr, type, member) ({                      \
 			const typeof(((type *)0)->member)* struct_ptr = (ptr); \
 			(type *)((char *)struct_ptr - offset_of(type, member)); })
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 
 #endif
diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
index db1e4f0..d0a4c9c 100644
--- a/lib/libtpm/tcgbios.c
+++ b/lib/libtpm/tcgbios.c
@@ -137,23 +137,38 @@ struct tpm_log_entry {
 	   + SHA512_BUFSIZE + SM3_256_BUFSIZE];
 } __attribute__((packed));
 
+static const struct hash_parameters {
+	uint16_t hashalg;
+	uint8_t  hash_buffersize;
+} hash_parameters[] = {
+	{
+		.hashalg = TPM2_ALG_SHA1,
+		.hash_buffersize = SHA1_BUFSIZE,
+	}, {
+		.hashalg = TPM2_ALG_SHA256,
+		.hash_buffersize = SHA256_BUFSIZE,
+	}, {
+		.hashalg = TPM2_ALG_SHA384,
+		.hash_buffersize = SHA384_BUFSIZE,
+	}, {
+		.hashalg = TPM2_ALG_SHA512,
+		.hash_buffersize = SHA512_BUFSIZE,
+	}, {
+		.hashalg = TPM2_ALG_SM3_256,
+		.hash_buffersize = SM3_256_BUFSIZE,
+	}
+};
+
 static int
 tpm20_get_hash_buffersize(uint16_t hashAlg)
 {
-	switch (hashAlg) {
-	case TPM2_ALG_SHA1:
-		return SHA1_BUFSIZE;
-	case TPM2_ALG_SHA256:
-		return SHA256_BUFSIZE;
-	case TPM2_ALG_SHA384:
-		return SHA384_BUFSIZE;
-	case TPM2_ALG_SHA512:
-		return SHA512_BUFSIZE;
-	case TPM2_ALG_SM3_256:
-		return SM3_256_BUFSIZE;
-	default:
-		return -1;
+	unsigned i;
+
+	for (i = 0; i < ARRAY_SIZE(hash_parameters); i++) {
+		if (hash_parameters[i].hashalg == hashAlg)
+			return hash_parameters[i].hash_buffersize;
 	}
+	return -1;
 }
 
 /*
-- 
2.17.1



More information about the SLOF mailing list