Add variable for message length

Add variable to store message length to increase
clarity in what the program is doing.

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
This commit is contained in:
Thomas Daubney 2023-10-11 15:19:38 +01:00
parent 34500874ce
commit 1c2378b8b1

View File

@ -51,11 +51,15 @@
0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \ 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \
} }
const uint8_t sample_message[] = "Hello World!";
const uint8_t sample_hash[] = SAMPLE_HASH_DATA; const uint8_t sample_hash[] = SAMPLE_HASH_DATA;
const size_t sample_hash_len = sizeof(sample_hash); const size_t sample_hash_len = sizeof(sample_hash);
const uint8_t sample_message[] = "Hello World!";
/* sample_message is terminated with a null byte which is not part of
* the message itself so we make sure to subtract it in order to get
* the message length. */
const size_t sample_message_length = sizeof(sample_message) - 1;
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256) #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256)
int main(void) int main(void)
{ {
@ -95,7 +99,7 @@ int main(void)
/* Note: Here we use sizeof(sample_message) - 1 since we don't wish to /* Note: Here we use sizeof(sample_message) - 1 since we don't wish to
* include the null byte in the hash computation */ * include the null byte in the hash computation */
status = psa_hash_update(&hash_operation, sample_message, sizeof(sample_message) - 1); status = psa_hash_update(&hash_operation, sample_message, sample_message_length);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("psa_hash_update failed\n"); mbedtls_printf("psa_hash_update failed\n");
goto cleanup; goto cleanup;
@ -135,7 +139,7 @@ int main(void)
/* Compute hash using one-shot function call */ /* Compute hash using one-shot function call */
status = psa_hash_compute(HASH_ALG, status = psa_hash_compute(HASH_ALG,
sample_message, sizeof(sample_message) - 1, sample_message, sample_message_length,
hash, sizeof(hash), hash, sizeof(hash),
&hash_length); &hash_length);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {