Skip to contentSkip to conversation

efi_loader: simplify tcg2_create_digest()

PATCH New Ilias Apalodimas 2 participants 2 messages Apr 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 efi_loader: simplify tcg2_create_digest()

Bumping the digest list count, for all supported algorithms, can be calculated outside of the individual switch statements. So let's do that for every loop iteration instead and simplify the code a bit.

---
 lib/efi_loader/efi_tcg2.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/efi_loader/efi_tcg2.c +1 -4
index d5eca68769b0..94e8f22bbb69 100644
@@ -535,30 +535,27 @@ static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
 			sha1_starts(&ctx);
 			sha1_update(&ctx, input, length);
 			sha1_finish(&ctx, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA256:
 			sha256_starts(&ctx_256);
 			sha256_update(&ctx_256, input, length);
 			sha256_finish(&ctx_256, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA384:
 			sha384_starts(&ctx_512);
 			sha384_update(&ctx_512, input, length);
 			sha384_finish(&ctx_512, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA512:
 			sha512_starts(&ctx_512);
 			sha512_update(&ctx_512, input, length);
 			sha512_finish(&ctx_512, final);
-			digest_list->count++;
 			break;
 		default:
 			EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
 			return EFI_INVALID_PARAMETER;
 		}
+		digest_list->count++;
 		digest_list->digests[i].hash_alg = hash_alg;
 		memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg));
 	}
Signature · 1 line
2.31.0
#1 lore raw

PATCH 1/1 efi_loader: simplify tcg2_create_digest()

Quoted 5 lines · Ilias Apalodimas

Bumping the digest list count, for all supported algorithms, can be calculated outside of the individual switch statements. So let's do that for every loop iteration instead and simplify the code a bit.

Quoted 41 lines
---
 lib/efi_loader/efi_tcg2.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
lib/efi_loader/efi_tcg2.c +1 -4
index d5eca68769b0..94e8f22bbb69 100644
@@ -535,30 +535,27 @@ static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
 			sha1_starts(&ctx);
 			sha1_update(&ctx, input, length);
 			sha1_finish(&ctx, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA256:
 			sha256_starts(&ctx_256);
 			sha256_update(&ctx_256, input, length);
 			sha256_finish(&ctx_256, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA384:
 			sha384_starts(&ctx_512);
 			sha384_update(&ctx_512, input, length);
 			sha384_finish(&ctx_512, final);
-			digest_list->count++;
 			break;
 		case TPM2_ALG_SHA512:
 			sha512_starts(&ctx_512);
 			sha512_update(&ctx_512, input, length);
 			sha512_finish(&ctx_512, final);
-			digest_list->count++;
 			break;
 		default:
 			EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
 			return EFI_INVALID_PARAMETER;
 		}
+		digest_list->count++;
 		digest_list->digests[i].hash_alg = hash_alg;
 		memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg));
 	}