From 3ebb880f90478acdf807c19588c0d625838720b9 Mon Sep 17 00:00:00 2001 From: Tom Cosgrove Date: Wed, 29 May 2024 10:29:39 +0100 Subject: [PATCH] Hook the new psa_sim_crypto_{client,server} into the build and tests - smoke test client.c becomes a trivial call to psa_crypto_init() - server.c now uses psa_sim_crypto_server.c's psa_crypto_call() - Makefile is updated to build all the modules, and allow a different MAIN - all.sh's test_psasim now tests the simulation of psa_hash_compute() too Signed-off-by: Tom Cosgrove --- tests/psa-client-server/psasim/Makefile | 10 +- .../psasim/src/aut_psa_hash_compute.c | 112 ++++++++++++++++++ tests/psa-client-server/psasim/src/client.c | 41 +------ tests/psa-client-server/psasim/src/server.c | 10 +- tests/scripts/all.sh | 9 ++ 5 files changed, 135 insertions(+), 47 deletions(-) create mode 100644 tests/psa-client-server/psasim/src/aut_psa_hash_compute.c diff --git a/tests/psa-client-server/psasim/Makefile b/tests/psa-client-server/psasim/Makefile index db0c4127f..45ec45820 100644 --- a/tests/psa-client-server/psasim/Makefile +++ b/tests/psa-client-server/psasim/Makefile @@ -1,3 +1,5 @@ +MAIN ?= client.c + CFLAGS += -Wall -Werror -std=c99 -D_XOPEN_SOURCE=1 -D_POSIX_C_SOURCE=200809L ifeq ($(DEBUG),1) @@ -18,12 +20,16 @@ GENERATED_H_FILES = include/psa_manifest/manifest.h \ include/psa_manifest/sid.h PSA_CLIENT_SRC = src/psa_ff_client.c \ - src/client.c + src/$(MAIN) \ + src/psa_sim_crypto_client.c \ + src/psa_sim_serialise.c PARTITION_SERVER_BOOTSTRAP = src/psa_ff_bootstrap_TEST_PARTITION.c PSA_SERVER_SRC = $(PARTITION_SERVER_BOOTSTRAP) \ - src/psa_ff_server.c + src/psa_ff_server.c \ + src/psa_sim_crypto_server.c \ + src/psa_sim_serialise.c .PHONY: all clean libpsaclient libpsaserver diff --git a/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c b/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c new file mode 100644 index 000000000..519c0721f --- /dev/null +++ b/tests/psa-client-server/psasim/src/aut_psa_hash_compute.c @@ -0,0 +1,112 @@ +/* + * API(s) under test: psa_hash_compute() + * + * Taken from programs/psa/psa_hash.c, and calls to all hash APIs + * but psa_hash_compute() removed. + * + * 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(). + * + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + */ + +#include "psa/crypto.h" +#include +#include +#include + +#include "mbedtls/build_info.h" +#include "mbedtls/platform.h" + +/* 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 + * + * The algorithm used by this demo is SHA 256. + * Please see include/psa/crypto_values.h to see the other + * algorithms that are supported by Mbed TLS. + * If you switch to a different algorithm you will need to update + * the hash data in the EXAMPLE_HASH_VALUE macro below. */ + +#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; +} +#else + +#define HASH_ALG PSA_ALG_SHA_256 + +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; + +#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 \ +} + +const uint8_t expected_hash[] = EXPECTED_HASH_VALUE; +const size_t expected_hash_len = sizeof(expected_hash); + +int main(void) +{ + psa_status_t status; + uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)]; + size_t hash_length; + + mbedtls_printf("PSA Crypto API: SHA-256 example\n\n"); + + status = psa_crypto_init(); + if (status != PSA_SUCCESS) { + mbedtls_printf("psa_crypto_init failed\n"); + return EXIT_FAILURE; + } + + /* Clear local variables prior to one-shot hash demo */ + memset(hash, 0, sizeof(hash)); + hash_length = 0; + + /* Compute hash using one-shot function call */ + status = psa_hash_compute(HASH_ALG, + sample_message, sample_message_length, + hash, sizeof(hash), + &hash_length); + if (status != PSA_SUCCESS) { + mbedtls_printf("psa_hash_compute failed\n"); + goto cleanup; + } + + if (hash_length != expected_hash_len || + (memcmp(hash, expected_hash, expected_hash_len) != 0)) { + mbedtls_printf("One-shot hash operation gave the wrong result!\n\n"); + goto cleanup; + } + + mbedtls_printf("One-shot hash operation successful!\n\n"); + + /* Print out result */ + mbedtls_printf("The SHA-256( '%s' ) is: ", sample_message); + + for (size_t j = 0; j < expected_hash_len; j++) { + mbedtls_printf("%02x", hash[j]); + } + + mbedtls_printf("\n"); + + mbedtls_psa_crypto_free(); + return EXIT_SUCCESS; + +cleanup: + return EXIT_FAILURE; +} +#endif /* !MBEDTLS_PSA_CRYPTO_C || !PSA_WANT_ALG_SHA_256 */ diff --git a/tests/psa-client-server/psasim/src/client.c b/tests/psa-client-server/psasim/src/client.c index 550a6e869..a8c9e08f3 100644 --- a/tests/psa-client-server/psasim/src/client.c +++ b/tests/psa-client-server/psasim/src/client.c @@ -5,50 +5,17 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ -#include -#include - -/* Includes from psasim */ -#include -#include -#include "psa_manifest/sid.h" -#include "psa_functions_codes.h" - /* Includes from mbedtls */ -#include "mbedtls/version.h" #include "psa/crypto.h" -#define CLIENT_PRINT(fmt, ...) \ - PRINT("Client: " fmt, ##__VA_ARGS__) - int main() { - char mbedtls_version[18]; - // psa_invec invecs[1]; - // psa_outvec outvecs[1]; - psa_status_t status; - - mbedtls_version_get_string_full(mbedtls_version); - CLIENT_PRINT("%s", mbedtls_version); - - CLIENT_PRINT("My PID: %d", getpid()); - - CLIENT_PRINT("PSA version: %u", psa_version(PSA_SID_CRYPTO_SID)); - psa_handle_t h = psa_connect(PSA_SID_CRYPTO_SID, 1); - - if (h < 0) { - CLIENT_PRINT("Couldn't connect %d", h); - return 1; - } - - status = psa_call(h, PSA_CRYPTO_INIT, NULL, 0, NULL, 0); - CLIENT_PRINT("PSA_CRYPTO_INIT returned: %d", status); - - CLIENT_PRINT("Closing handle"); - psa_close(h); - + /* psa_crypto_init() connects to the server */ + psa_status_t status = psa_crypto_init(); if (status != PSA_SUCCESS) { return 1; } + + mbedtls_psa_crypto_free(); return 0; } diff --git a/tests/psa-client-server/psasim/src/server.c b/tests/psa-client-server/psasim/src/server.c index 21b65c709..77ce2694e 100644 --- a/tests/psa-client-server/psasim/src/server.c +++ b/tests/psa-client-server/psasim/src/server.c @@ -53,6 +53,7 @@ int psa_server_main(int argc, char *argv[]) const int magic_num = 66; int client_disconnected = 0; char mbedtls_version[18]; + extern psa_status_t psa_crypto_call(psa_msg_t msg); mbedtls_version_get_string_full(mbedtls_version); SERVER_PRINT("%s", mbedtls_version); @@ -83,14 +84,7 @@ int psa_server_main(int argc, char *argv[]) break; default: SERVER_PRINT("Got an IPC call of type %d", msg.type); - switch (msg.type) { - case PSA_CRYPTO_INIT: - ret = psa_crypto_init(); - break; - default: - SERVER_PRINT("Unknown PSA function code"); - break; - } + ret = psa_crypto_call(msg); SERVER_PRINT("Internal function call returned %d", ret); if (msg.client_id > 0) { diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 4f0e9bba1..e0901711d 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -6228,6 +6228,15 @@ component_test_psasim() { msg "test psasim" tests/psa-client-server/psasim/test/run_test.sh + msg "build psasim to test psa_hash_compute" + # Delete the executable to ensure we build using the right MAIN + rm tests/psa-client-server/psasim/test/psa_client + # API under test: psa_hash_compute() + make -C tests/psa-client-server/psasim CFLAGS="$ASAN_CFLAGS" LDFLAGS="$ASAN_CFLAGS" MAIN="aut_psa_hash_compute.c" + + msg "test psasim running psa_hash_compute" + tests/psa-client-server/psasim/test/run_test.sh + msg "clean psasim" make -C tests/psa-client-server/psasim clean }