Implement cleanup label

Signed-off-by: Thomas Daubney <thomas.daubney@arm.com>
This commit is contained in:
Thomas Daubney 2023-08-03 16:03:30 +01:00
parent 21fbe4c90e
commit 5c2dcbd250

View File

@ -90,33 +90,25 @@ int main(void)
status = psa_hash_update(&hash_operation, sample_message, sizeof(sample_message) - 1); status = psa_hash_update(&hash_operation, sample_message, sizeof(sample_message) - 1);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("psa_hash_update failed\n"); mbedtls_printf("psa_hash_update failed\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
status = psa_hash_clone(&hash_operation, &cloned_hash_operation); status = psa_hash_clone(&hash_operation, &cloned_hash_operation);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("PSA hash clone failed"); mbedtls_printf("PSA hash clone failed");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length); status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("psa_hash_finish failed\n"); mbedtls_printf("psa_hash_finish failed\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
/* Check the result of the operation against the sample */ /* Check the result of the operation against the sample */
if ((memcmp(hash, sample_hash, sample_hash_len) != 0) || hash_length != sample_hash_len) { if ((memcmp(hash, sample_hash, sample_hash_len) != 0) || hash_length != sample_hash_len) {
mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n"); mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
status = status =
@ -124,9 +116,7 @@ int main(void)
sample_hash_len); sample_hash_len);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("psa_hash_verify failed\n"); mbedtls_printf("psa_hash_verify failed\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} else { } else {
mbedtls_printf("Multi-part hash operation successful!\n"); mbedtls_printf("Multi-part hash operation successful!\n");
} }
@ -142,16 +132,12 @@ int main(void)
&hash_length); &hash_length);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
mbedtls_printf("psa_hash_compute failed\n"); mbedtls_printf("psa_hash_compute failed\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
if (memcmp(hash, sample_hash, sample_hash_len) != 0 || hash_length != sample_hash_len) { if (memcmp(hash, sample_hash, sample_hash_len) != 0 || hash_length != sample_hash_len) {
mbedtls_printf("One-shot hash operation gave the wrong result!\n\n"); mbedtls_printf("One-shot hash operation gave the wrong result!\n\n");
psa_hash_abort(&hash_operation); goto cleanup;
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
mbedtls_printf("One-shot hash operation successful!\n\n"); mbedtls_printf("One-shot hash operation successful!\n\n");
@ -167,5 +153,10 @@ int main(void)
mbedtls_psa_crypto_free(); mbedtls_psa_crypto_free();
return EXIT_SUCCESS; return EXIT_SUCCESS;
cleanup:
psa_hash_abort(&hash_operation);
psa_hash_abort(&cloned_hash_operation);
return EXIT_FAILURE;
} }
#endif /* MBEDTLS_PSA_CRYPTO_C && PSA_WANT_ALG_SHA_256 */ #endif /* MBEDTLS_PSA_CRYPTO_C && PSA_WANT_ALG_SHA_256 */