[SeaBIOS] [PATCH v3 1/2] tcgbios: Use table to convert hash to buffer size

Stefan Berger posted 2 patches 5 years, 4 months ago
[SeaBIOS] [PATCH v3 1/2] tcgbios: Use table to convert hash to buffer size
Posted by Stefan Berger 5 years, 4 months ago
Use a table to convert the hash to the buffer size it needs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 src/tcgbios.c | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/tcgbios.c b/src/tcgbios.c
index 24846d3..0eabc60 100644
--- a/src/tcgbios.c
+++ b/src/tcgbios.c
@@ -161,23 +161,38 @@ struct tpm_log_entry {
            + SHA512_BUFSIZE + SM3_256_BUFSIZE];
 } PACKED;
 
+static const struct hash_parameters {
+    u16 hashalg;
+    u8  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(u16 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;
 }
 
 // Add an entry at the start of the log describing digest formats
-- 
2.17.2
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: [PATCH v3 1/2] tcgbios: Use table to convert hash to buffer size
Posted by Paul Menzel 5 years, 3 months ago
Dear Stefan,


On 30.01.19 20:06, Stefan Berger wrote:
> Use a table to convert the hash to the buffer size it needs.

Could you please add a reasoning for this change to the commit message, 
that means, why is a table preferred?

[…]


Kind regards,

Paul
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org