2023-07-18 14:46:10 +02:00
|
|
|
/*
|
|
|
|
* Example computing a SHA-256 hash using the PSA Crypto API
|
|
|
|
*
|
|
|
|
* The example computes the SHA-256 hash of a test string using the
|
|
|
|
* one-shot API call psa_hash_compute() and the using multi-part
|
|
|
|
* operation, which requires psa_hash_setup(), psa_hash_update() and
|
|
|
|
* psa_hash_finish(). The multi-part operation is popular on embedded
|
|
|
|
* devices where a rolling hash needs to be computed.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2023-07-18 14:46:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "mbedtls/build_info.h"
|
2023-07-24 13:18:40 +02:00
|
|
|
#include "mbedtls/platform.h"
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-10-10 17:50:49 +02:00
|
|
|
/* Information about hashing with the PSA API can be
|
|
|
|
* found here:
|
|
|
|
* https://arm-software.github.io/psa-api/crypto/1.1/api/ops/hashes.html
|
2023-10-11 11:04:54 +02:00
|
|
|
*
|
2023-10-10 17:50:49 +02:00
|
|
|
* The algorithm used by this demo is SHA 256.
|
2023-07-28 16:57:10 +02:00
|
|
|
* Please see include/psa/crypto_values.h to see the other
|
2023-10-10 17:50:49 +02:00
|
|
|
* algorithms that are supported by Mbed TLS.
|
|
|
|
* If you switch to a different algorithm you will need to update
|
2023-10-12 11:46:43 +02:00
|
|
|
* the hash data in the EXAMPLE_HASH_VALUE macro below. */
|
2023-07-28 16:57:10 +02:00
|
|
|
|
2023-10-12 11:46:43 +02:00
|
|
|
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256)
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256"
|
|
|
|
"not defined.\r\n");
|
|
|
|
return EXIT_SUCCESS;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
2023-10-12 11:46:43 +02:00
|
|
|
#else
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-10-12 11:46:43 +02:00
|
|
|
#define HASH_ALG PSA_ALG_SHA_256
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-10-11 16:19:38 +02:00
|
|
|
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;
|
|
|
|
|
2023-10-12 11:46:43 +02:00
|
|
|
#define EXPECTED_HASH_VALUE { \
|
|
|
|
0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \
|
|
|
|
0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, \
|
|
|
|
0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
2023-10-12 11:46:43 +02:00
|
|
|
|
|
|
|
const uint8_t expected_hash[] = EXPECTED_HASH_VALUE;
|
|
|
|
const size_t expected_hash_len = sizeof(expected_hash);
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-18 15:59:45 +02:00
|
|
|
int main(void)
|
2023-07-18 14:46:10 +02:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
2023-07-24 17:49:14 +02:00
|
|
|
uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)];
|
2023-07-28 15:31:06 +02:00
|
|
|
size_t hash_length;
|
2023-07-28 15:44:25 +02:00
|
|
|
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_hash_operation_t cloned_hash_operation = PSA_HASH_OPERATION_INIT;
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("PSA Crypto API: SHA-256 example\n\n");
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-18 15:59:45 +02:00
|
|
|
status = psa_crypto_init();
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_crypto_init failed\n");
|
2023-07-18 15:59:45 +02:00
|
|
|
return EXIT_FAILURE;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute hash using multi-part operation */
|
2023-07-28 15:44:25 +02:00
|
|
|
status = psa_hash_setup(&hash_operation, HASH_ALG);
|
2023-10-12 11:46:43 +02:00
|
|
|
if (status == PSA_ERROR_NOT_SUPPORTED) {
|
2023-10-10 18:38:53 +02:00
|
|
|
mbedtls_printf("unknown hash algorithm supplied\n");
|
|
|
|
return EXIT_FAILURE;
|
2023-10-12 11:46:43 +02:00
|
|
|
} else if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_hash_setup failed\n");
|
2023-07-18 15:59:45 +02:00
|
|
|
return EXIT_FAILURE;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-10-11 16:19:38 +02:00
|
|
|
status = psa_hash_update(&hash_operation, sample_message, sample_message_length);
|
2023-07-18 15:59:45 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_hash_update failed\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:44:25 +02:00
|
|
|
status = psa_hash_clone(&hash_operation, &cloned_hash_operation);
|
2023-07-18 15:59:45 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-10-11 16:28:13 +02:00
|
|
|
mbedtls_printf("PSA hash clone failed\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:44:25 +02:00
|
|
|
status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length);
|
2023-07-18 15:59:45 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_hash_finish failed\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 17:14:20 +02:00
|
|
|
/* Check the result of the operation against the sample */
|
2023-10-12 11:46:43 +02:00
|
|
|
if (hash_length != expected_hash_len ||
|
|
|
|
(memcmp(hash, expected_hash, expected_hash_len) != 0)) {
|
2023-07-28 17:14:20 +02:00
|
|
|
mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-28 17:14:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:59:45 +02:00
|
|
|
status =
|
2023-10-12 11:46:43 +02:00
|
|
|
psa_hash_verify(&cloned_hash_operation, expected_hash,
|
|
|
|
expected_hash_len);
|
2023-07-18 15:59:45 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_hash_verify failed\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 15:59:45 +02:00
|
|
|
} else {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("Multi-part hash operation successful!\n");
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:47:47 +02:00
|
|
|
/* Clear local variables prior to one-shot hash demo */
|
2023-07-18 15:59:45 +02:00
|
|
|
memset(hash, 0, sizeof(hash));
|
2023-07-28 15:31:06 +02:00
|
|
|
hash_length = 0;
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-28 15:47:47 +02:00
|
|
|
/* Compute hash using one-shot function call */
|
2023-07-24 17:49:14 +02:00
|
|
|
status = psa_hash_compute(HASH_ALG,
|
2023-10-11 16:19:38 +02:00
|
|
|
sample_message, sample_message_length,
|
2023-07-18 15:59:45 +02:00
|
|
|
hash, sizeof(hash),
|
2023-07-28 15:31:06 +02:00
|
|
|
&hash_length);
|
2023-07-18 15:59:45 +02:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("psa_hash_compute failed\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-10-12 11:46:43 +02:00
|
|
|
if (hash_length != expected_hash_len ||
|
|
|
|
(memcmp(hash, expected_hash, expected_hash_len) != 0)) {
|
2023-07-28 16:21:46 +02:00
|
|
|
mbedtls_printf("One-shot hash operation gave the wrong result!\n\n");
|
2023-08-03 17:03:30 +02:00
|
|
|
goto cleanup;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("One-shot hash operation successful!\n\n");
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-28 16:23:06 +02:00
|
|
|
/* Print out result */
|
2023-07-28 16:57:10 +02:00
|
|
|
mbedtls_printf("The SHA-256( '%s' ) is: ", sample_message);
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-10-12 11:46:43 +02:00
|
|
|
for (size_t j = 0; j < expected_hash_len; j++) {
|
2023-07-28 16:07:19 +02:00
|
|
|
mbedtls_printf("%02x", hash[j]);
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 13:18:40 +02:00
|
|
|
mbedtls_printf("\n");
|
2023-07-18 14:46:10 +02:00
|
|
|
|
2023-07-18 15:59:45 +02:00
|
|
|
mbedtls_psa_crypto_free();
|
|
|
|
return EXIT_SUCCESS;
|
2023-08-03 17:03:30 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
psa_hash_abort(&hash_operation);
|
|
|
|
psa_hash_abort(&cloned_hash_operation);
|
|
|
|
return EXIT_FAILURE;
|
2023-07-18 14:46:10 +02:00
|
|
|
}
|
2023-10-12 11:46:43 +02:00
|
|
|
#endif /* !MBEDTLS_PSA_CRYPTO_C || !PSA_WANT_ALG_SHA_256 */
|