From 499dd4322e4b1ffd9fa0bf52bb5f2e6b54a7fdaa Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 13:38:12 +0100 Subject: [PATCH 1/7] Update psasim generated files TF-PSA-Crypto no longer has `psa_can_do_hash()` (except as a temporary stub to avoid breaking the build of psasim). Signed-off-by: Gilles Peskine --- psasim/src/psa_functions_codes.h | 1 - psasim/src/psa_sim_crypto_client.c | 62 -------------------------- psasim/src/psa_sim_crypto_server.c | 71 ------------------------------ 3 files changed, 134 deletions(-) diff --git a/psasim/src/psa_functions_codes.h b/psasim/src/psa_functions_codes.h index 74746b653..7cb8ea80b 100644 --- a/psasim/src/psa_functions_codes.h +++ b/psasim/src/psa_functions_codes.h @@ -26,7 +26,6 @@ enum { PSA_AEAD_VERIFY, PSA_ASYMMETRIC_DECRYPT, PSA_ASYMMETRIC_ENCRYPT, - PSA_CAN_DO_HASH, PSA_CIPHER_ABORT, PSA_CIPHER_DECRYPT, PSA_CIPHER_DECRYPT_SETUP, diff --git a/psasim/src/psa_sim_crypto_client.c b/psasim/src/psa_sim_crypto_client.c index 9051f2053..67978b896 100644 --- a/psasim/src/psa_sim_crypto_client.c +++ b/psasim/src/psa_sim_crypto_client.c @@ -1544,68 +1544,6 @@ fail: } -int psa_can_do_hash( - psa_algorithm_t hash_alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - int value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_algorithm_t_needs(hash_alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - hash_alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CAN_DO_HASH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CAN_DO_HASH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_int( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation ) diff --git a/psasim/src/psa_sim_crypto_server.c b/psasim/src/psa_sim_crypto_server.c index bd121c543..2210afcc8 100644 --- a/psasim/src/psa_sim_crypto_server.c +++ b/psasim/src/psa_sim_crypto_server.c @@ -1705,73 +1705,6 @@ fail: return 0; // This shouldn't happen! } -// Returns 1 for success, 0 for failure -int psa_can_do_hash_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - int value = 0; - psa_algorithm_t hash_alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &hash_alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_can_do_hash( - hash_alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_int_needs(value); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_int( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - // Returns 1 for success, 0 for failure int psa_cipher_abort_wrapper( uint8_t *in_params, size_t in_params_len, @@ -8893,10 +8826,6 @@ psa_status_t psa_crypto_call(psa_msg_t msg) ok = psa_asymmetric_encrypt_wrapper(in_params, in_params_len, &out_params, &out_params_len); break; - case PSA_CAN_DO_HASH: - ok = psa_can_do_hash_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; case PSA_CIPHER_ABORT: ok = psa_cipher_abort_wrapper(in_params, in_params_len, &out_params, &out_params_len); From 4b0febffa182d17cb42ccdad14ce414c11641d52 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 13:39:03 +0100 Subject: [PATCH 2/7] psasim: Update generation script instructions for the TF-PSA-Crypto split Signed-off-by: Gilles Peskine --- psasim/README.md | 6 +++--- psasim/src/psa_sim_generate.pl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/psasim/README.md b/psasim/README.md index db49ae947..aaf66e28d 100644 --- a/psasim/README.md +++ b/psasim/README.md @@ -34,9 +34,9 @@ A significant portion of the intermediate code of PSASIM is auto-generated using These files need to be regenerated whenever some PSA Crypto API is added/deleted/modified. The procedure is as follows: - `psa_sim_serialise.[c|h]`: - - go to `/tests/psa-client-server/psasim/src/` + - go to `framework/psasim/src/` - run `./psa_sim_serialise.pl h > psa_sim_serialise.h` - run `./psa_sim_serialise.pl c > psa_sim_serialise.c` - `psa_sim_crypto_[client|server].c` and `psa_functions_codes.h`: - - go to Mbed TLS' root folder - - run `./tests/psa-client-server/psasim/src/psa_sim_generate.pl` + - go to the TF-PSA-Crypto root folder + - run `framework/psasim/src/psa_sim_generate.pl` diff --git a/psasim/src/psa_sim_generate.pl b/psasim/src/psa_sim_generate.pl index 0f4c86f81..e993f4f22 100755 --- a/psasim/src/psa_sim_generate.pl +++ b/psasim/src/psa_sim_generate.pl @@ -1052,7 +1052,7 @@ sub output_signature sub get_functions { - my $header_dir = 'tf-psa-crypto/include'; + my $header_dir = 'include'; my $src = ""; for my $header_file ('psa/crypto.h', 'psa/crypto_extra.h') { local *HEADER; From 9dca598383ad1ffb9335d894eef7c270240eeced Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 13:49:47 +0100 Subject: [PATCH 3/7] Add rules to generate wrappers during the psasim build We don't need to check those generated files into version control. This fixes the problem that the generated files have precise knowledge of the functions that exist on whatever branch of TF-PSA-Crypto is consuming the framework. Signed-off-by: Gilles Peskine --- psasim/Makefile | 17 +++++++++++++++++ psasim/README.md | 21 --------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/psasim/Makefile b/psasim/Makefile index 45fcbedcc..f50eecd5a 100644 --- a/psasim/Makefile +++ b/psasim/Makefile @@ -67,6 +67,23 @@ test/psa_server: $(PSA_SERVER_SRC) $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_H_FILES): src/manifest.json src/server.c tools/psa_autogen.py src/manifest.json +GENERATED_C_FILES = \ + src/psa_sim_serialise.h src/psa_sim_serialise.c \ + src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c + +src/psa_sim_serialise.h: src/psa_sim_serialise.pl + cd src && ./psa_sim_serialise.pl h >$(@F) + +src/psa_sim_serialise.c: src/psa_sim_serialise.pl + cd src && ./psa_sim_serialise.pl c >$(@F) + +src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c: $(TF_PSA_CRYPTO_PATH)/include/psa/crypto.h +src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c: $(TF_PSA_CRYPTO_PATH)/include/psa/crypto_extra.h +src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c: src/psa_sim_generate.pl + cd $(TF_PSA_CRYPTO_PATH) && $(abspath src/psa_sim_generate.pl) + +generated_files: $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_C_FILES) + # Build MbedTLS libraries (crypto, x509 and tls) and copy them locally to # build client/server applications. # diff --git a/psasim/README.md b/psasim/README.md index aaf66e28d..6c32ae779 100644 --- a/psasim/README.md +++ b/psasim/README.md @@ -19,24 +19,3 @@ Please refer to `tests/scripts/components-psasim.sh` for guidance on how to buil - `component_test_psasim()`: builds the server and a couple of test clients which are used to evaluate some basic PSA Crypto API commands. - `component_test_suite_with_psasim()`: builds the server and _all_ the usual test suites (those found under the `/tests/suites/*` folder) which are used by the CI and runs them. A small subset of test suites (`test_suite_constant_time_hmac`,`test_suite_lmots`,`test_suite_lms`) are being skipped, for CI turnover time optimization. They can be run locally if required. - -## How to update automatically generated files - -A significant portion of the intermediate code of PSASIM is auto-generated using Perl. In particular: - -- `psa_sim_serialise.[c|h]`: - - Generated by `psa_sim_serialise.pl`. - - These files provide the serialisation/deserialisation support that is required to pass functions' parameters between client and server. -- `psa_sim_crypto_[client|server].c` and `psa_functions_codes.h`: - - Generated by `psa_sim_generate.pl`. - - `psa_sim_crypto_[client|server].c` provide interfaces for PSA Crypto APIs on client and server sides, while `psa_functions_codes.h` simply enumerates all PSA Crypto APIs. - -These files need to be regenerated whenever some PSA Crypto API is added/deleted/modified. The procedure is as follows: - -- `psa_sim_serialise.[c|h]`: - - go to `framework/psasim/src/` - - run `./psa_sim_serialise.pl h > psa_sim_serialise.h` - - run `./psa_sim_serialise.pl c > psa_sim_serialise.c` -- `psa_sim_crypto_[client|server].c` and `psa_functions_codes.h`: - - go to the TF-PSA-Crypto root folder - - run `framework/psasim/src/psa_sim_generate.pl` From b6c23e6e0178c96351213d542b6cbae14a3672c2 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 13:55:14 +0100 Subject: [PATCH 4/7] Remove psasim generated files The files are now generated during the build. Stop having them in version control. As formerly in Mbed TLS, `make clean` doesn't remove configuration-independent target-independent generated files, but the new target `make neat` does. Signed-off-by: Gilles Peskine --- psasim/.gitignore | 5 + psasim/Makefile | 6 +- psasim/src/psa_functions_codes.h | 106 - psasim/src/psa_sim_crypto_client.c | 7844 ------------------------ psasim/src/psa_sim_crypto_server.c | 9155 ---------------------------- psasim/src/psa_sim_serialise.c | 1765 ------ psasim/src/psa_sim_serialise.h | 1432 ----- 7 files changed, 10 insertions(+), 20303 deletions(-) delete mode 100644 psasim/src/psa_functions_codes.h delete mode 100644 psasim/src/psa_sim_crypto_client.c delete mode 100644 psasim/src/psa_sim_crypto_server.c delete mode 100644 psasim/src/psa_sim_serialise.c delete mode 100644 psasim/src/psa_sim_serialise.h diff --git a/psasim/.gitignore b/psasim/.gitignore index 4065abf77..40715cafa 100644 --- a/psasim/.gitignore +++ b/psasim/.gitignore @@ -1,6 +1,11 @@ bin/* *.o *.so +/src/psa_functions_codes.h +/src/psa_sim_crypto_client.c +/src/psa_sim_crypto_server.c +/src/psa_sim_serialise.c +/src/psa_sim_serialise.h test/psa_ff_bootstrap.c test/psa_manifest/* test/client diff --git a/psasim/Makefile b/psasim/Makefile index f50eecd5a..d4c7589bc 100644 --- a/psasim/Makefile +++ b/psasim/Makefile @@ -82,7 +82,8 @@ src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server. src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c: src/psa_sim_generate.pl cd $(TF_PSA_CRYPTO_PATH) && $(abspath src/psa_sim_generate.pl) -generated_files: $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_C_FILES) +PSASIM_GENERATED_FILES = $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_C_FILES) +generated_files: $(PSASIM_GENERATED_FILES) # Build MbedTLS libraries (crypto, x509 and tls) and copy them locally to # build client/server applications. @@ -104,3 +105,6 @@ clean: clean_server_intermediate_files rm -rf client_libs server_libs rm -f test/psa_service_* test/psa_notify_* test/*.log rm -f test/seedfile + +neat: clean + rm -f $(PSASIM_GENERATED_FILES) diff --git a/psasim/src/psa_functions_codes.h b/psasim/src/psa_functions_codes.h deleted file mode 100644 index 7cb8ea80b..000000000 --- a/psasim/src/psa_functions_codes.h +++ /dev/null @@ -1,106 +0,0 @@ -/* THIS FILE WAS AUTO-GENERATED BY psa_sim_generate.pl. DO NOT EDIT!! */ - -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#ifndef _PSA_FUNCTIONS_CODES_H_ -#define _PSA_FUNCTIONS_CODES_H_ - -enum { - /* Start here to avoid overlap with PSA_IPC_CONNECT, PSA_IPC_DISCONNECT - * and VERSION_REQUEST */ - PSA_CRYPTO_INIT = 100, - PSA_AEAD_ABORT, - PSA_AEAD_DECRYPT, - PSA_AEAD_DECRYPT_SETUP, - PSA_AEAD_ENCRYPT, - PSA_AEAD_ENCRYPT_SETUP, - PSA_AEAD_FINISH, - PSA_AEAD_GENERATE_NONCE, - PSA_AEAD_SET_LENGTHS, - PSA_AEAD_SET_NONCE, - PSA_AEAD_UPDATE, - PSA_AEAD_UPDATE_AD, - PSA_AEAD_VERIFY, - PSA_ASYMMETRIC_DECRYPT, - PSA_ASYMMETRIC_ENCRYPT, - PSA_CIPHER_ABORT, - PSA_CIPHER_DECRYPT, - PSA_CIPHER_DECRYPT_SETUP, - PSA_CIPHER_ENCRYPT, - PSA_CIPHER_ENCRYPT_SETUP, - PSA_CIPHER_FINISH, - PSA_CIPHER_GENERATE_IV, - PSA_CIPHER_SET_IV, - PSA_CIPHER_UPDATE, - PSA_COPY_KEY, - PSA_DESTROY_KEY, - PSA_EXPORT_KEY, - PSA_EXPORT_PUBLIC_KEY, - PSA_EXPORT_PUBLIC_KEY_IOP_ABORT, - PSA_EXPORT_PUBLIC_KEY_IOP_COMPLETE, - PSA_EXPORT_PUBLIC_KEY_IOP_GET_NUM_OPS, - PSA_EXPORT_PUBLIC_KEY_IOP_SETUP, - PSA_GENERATE_KEY, - PSA_GENERATE_KEY_CUSTOM, - PSA_GENERATE_KEY_IOP_ABORT, - PSA_GENERATE_KEY_IOP_COMPLETE, - PSA_GENERATE_KEY_IOP_GET_NUM_OPS, - PSA_GENERATE_KEY_IOP_SETUP, - PSA_GENERATE_RANDOM, - PSA_GET_KEY_ATTRIBUTES, - PSA_HASH_ABORT, - PSA_HASH_CLONE, - PSA_HASH_COMPARE, - PSA_HASH_COMPUTE, - PSA_HASH_FINISH, - PSA_HASH_SETUP, - PSA_HASH_UPDATE, - PSA_HASH_VERIFY, - PSA_IMPORT_KEY, - PSA_INTERRUPTIBLE_GET_MAX_OPS, - PSA_INTERRUPTIBLE_SET_MAX_OPS, - PSA_KEY_AGREEMENT, - PSA_KEY_AGREEMENT_IOP_ABORT, - PSA_KEY_AGREEMENT_IOP_COMPLETE, - PSA_KEY_AGREEMENT_IOP_GET_NUM_OPS, - PSA_KEY_AGREEMENT_IOP_SETUP, - PSA_KEY_DERIVATION_ABORT, - PSA_KEY_DERIVATION_GET_CAPACITY, - PSA_KEY_DERIVATION_INPUT_BYTES, - PSA_KEY_DERIVATION_INPUT_INTEGER, - PSA_KEY_DERIVATION_INPUT_KEY, - PSA_KEY_DERIVATION_KEY_AGREEMENT, - PSA_KEY_DERIVATION_OUTPUT_BYTES, - PSA_KEY_DERIVATION_OUTPUT_KEY, - PSA_KEY_DERIVATION_OUTPUT_KEY_CUSTOM, - PSA_KEY_DERIVATION_SET_CAPACITY, - PSA_KEY_DERIVATION_SETUP, - PSA_MAC_ABORT, - PSA_MAC_COMPUTE, - PSA_MAC_SIGN_FINISH, - PSA_MAC_SIGN_SETUP, - PSA_MAC_UPDATE, - PSA_MAC_VERIFY, - PSA_MAC_VERIFY_FINISH, - PSA_MAC_VERIFY_SETUP, - PSA_PURGE_KEY, - PSA_RAW_KEY_AGREEMENT, - PSA_RESET_KEY_ATTRIBUTES, - PSA_SIGN_HASH, - PSA_SIGN_HASH_ABORT, - PSA_SIGN_HASH_COMPLETE, - PSA_SIGN_HASH_GET_NUM_OPS, - PSA_SIGN_HASH_START, - PSA_SIGN_MESSAGE, - PSA_VERIFY_HASH, - PSA_VERIFY_HASH_ABORT, - PSA_VERIFY_HASH_COMPLETE, - PSA_VERIFY_HASH_GET_NUM_OPS, - PSA_VERIFY_HASH_START, - PSA_VERIFY_MESSAGE, -}; - -#endif /* _PSA_FUNCTIONS_CODES_H_ */ diff --git a/psasim/src/psa_sim_crypto_client.c b/psasim/src/psa_sim_crypto_client.c deleted file mode 100644 index 67978b896..000000000 --- a/psasim/src/psa_sim_crypto_client.c +++ /dev/null @@ -1,7844 +0,0 @@ -/* THIS FILE WAS AUTO-GENERATED BY psa_sim_generate.pl. DO NOT EDIT!! */ - -/* client calls */ - -/* - * Copyright The Mbed TLS Contributors - * 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" -#include "psa_sim_serialise.h" - -/* Includes from mbedtls */ -#include "mbedtls/version.h" -#include "psa/crypto.h" - -#define CLIENT_PRINT(fmt, ...) \ - INFO("Client: " fmt, ##__VA_ARGS__) - -static psa_handle_t handle = -1; - -#if defined(MBEDTLS_PSA_CRYPTO_C) -#error "Error: MBEDTLS_PSA_CRYPTO_C must be disabled on client build" -#endif - -int psa_crypto_call(int function, - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - // psa_outvec outvecs[1]; - if (handle < 0) { - fprintf(stderr, "NOT CONNECTED\n"); - exit(1); - } - - psa_invec invec; - invec.base = in_params; - invec.len = in_params_len; - - size_t max_receive = 24576; - uint8_t *receive = malloc(max_receive); - if (receive == NULL) { - fprintf(stderr, "FAILED to allocate %u bytes\n", (unsigned) max_receive); - exit(1); - } - - size_t actual_received = 0; - - psa_outvec outvecs[2]; - outvecs[0].base = &actual_received; - outvecs[0].len = sizeof(actual_received); - outvecs[1].base = receive; - outvecs[1].len = max_receive; - - psa_status_t status = psa_call(handle, function, &invec, 1, outvecs, 2); - if (status != PSA_SUCCESS) { - free(receive); - return 0; - } - - *out_params = receive; - *out_params_len = actual_received; - - return 1; // success -} - -psa_status_t psa_crypto_init(void) -{ - const char *mbedtls_version; - uint8_t *result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - mbedtls_version = mbedtls_version_get_string_full(); - CLIENT_PRINT("%s", mbedtls_version); - - CLIENT_PRINT("My PID: %d", getpid()); - - CLIENT_PRINT("PSA version: %u", psa_version(PSA_SID_CRYPTO_SID)); - handle = psa_connect(PSA_SID_CRYPTO_SID, 1); - - if (handle < 0) { - CLIENT_PRINT("Couldn't connect %d", handle); - return PSA_ERROR_COMMUNICATION_FAILURE; - } - - int ok = psa_crypto_call(PSA_CRYPTO_INIT, NULL, 0, &result, &result_length); - CLIENT_PRINT("PSA_CRYPTO_INIT returned: %d", ok); - - if (!ok) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t(&rpos, &rremain, &status); - if (!ok) { - goto fail; - } - -fail: - free(result); - - return status; -} - -void mbedtls_psa_crypto_free(void) -{ - /* Do not try to close a connection that was never started.*/ - if (handle == -1) { - return; - } - - CLIENT_PRINT("Closing handle"); - psa_close(handle); - handle = -1; -} - - -psa_status_t psa_aead_abort( - psa_aead_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_decrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *nonce, size_t nonce_length, - const uint8_t *additional_data, size_t additional_data_length, - const uint8_t *ciphertext, size_t ciphertext_length, - uint8_t *plaintext, size_t plaintext_size, - size_t *plaintext_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(nonce, nonce_length) + - psasim_serialise_buffer_needs(additional_data, additional_data_length) + - psasim_serialise_buffer_needs(ciphertext, ciphertext_length) + - psasim_serialise_buffer_needs(plaintext, plaintext_size) + - psasim_serialise_size_t_needs(*plaintext_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - nonce, nonce_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - additional_data, additional_data_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - ciphertext, ciphertext_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *plaintext_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_DECRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_DECRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - plaintext_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_decrypt_setup( - psa_aead_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_DECRYPT_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_DECRYPT_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_encrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *nonce, size_t nonce_length, - const uint8_t *additional_data, size_t additional_data_length, - const uint8_t *plaintext, size_t plaintext_length, - uint8_t *ciphertext, size_t ciphertext_size, - size_t *ciphertext_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(nonce, nonce_length) + - psasim_serialise_buffer_needs(additional_data, additional_data_length) + - psasim_serialise_buffer_needs(plaintext, plaintext_length) + - psasim_serialise_buffer_needs(ciphertext, ciphertext_size) + - psasim_serialise_size_t_needs(*ciphertext_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - nonce, nonce_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - additional_data, additional_data_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - plaintext, plaintext_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *ciphertext_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_ENCRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_ENCRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - ciphertext_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_encrypt_setup( - psa_aead_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_ENCRYPT_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_ENCRYPT_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_finish( - psa_aead_operation_t *operation, - uint8_t *ciphertext, size_t ciphertext_size, - size_t *ciphertext_length, - uint8_t *tag, size_t tag_size, - size_t *tag_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(ciphertext, ciphertext_size) + - psasim_serialise_size_t_needs(*ciphertext_length) + - psasim_serialise_buffer_needs(tag, tag_size) + - psasim_serialise_size_t_needs(*tag_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *ciphertext_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - tag, tag_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *tag_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_FINISH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_FINISH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - ciphertext_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - tag, tag_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - tag_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_generate_nonce( - psa_aead_operation_t *operation, - uint8_t *nonce, size_t nonce_size, - size_t *nonce_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(nonce, nonce_size) + - psasim_serialise_size_t_needs(*nonce_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - nonce, nonce_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *nonce_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_GENERATE_NONCE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_GENERATE_NONCE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - nonce, nonce_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - nonce_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_set_lengths( - psa_aead_operation_t *operation, - size_t ad_length, - size_t plaintext_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_size_t_needs(ad_length) + - psasim_serialise_size_t_needs(plaintext_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - ad_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - plaintext_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_SET_LENGTHS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_SET_LENGTHS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_set_nonce( - psa_aead_operation_t *operation, - const uint8_t *nonce, size_t nonce_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(nonce, nonce_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - nonce, nonce_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_SET_NONCE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_SET_NONCE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_update( - psa_aead_operation_t *operation, - const uint8_t *input, size_t input_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_UPDATE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_UPDATE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_update_ad( - psa_aead_operation_t *operation, - const uint8_t *input, size_t input_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(input, input_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_UPDATE_AD, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_UPDATE_AD server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_aead_verify( - psa_aead_operation_t *operation, - uint8_t *plaintext, size_t plaintext_size, - size_t *plaintext_length, - const uint8_t *tag, size_t tag_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_aead_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(plaintext, plaintext_size) + - psasim_serialise_size_t_needs(*plaintext_length) + - psasim_serialise_buffer_needs(tag, tag_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_aead_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *plaintext_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - tag, tag_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_AEAD_VERIFY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_AEAD_VERIFY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_aead_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - plaintext_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_asymmetric_decrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - const uint8_t *salt, size_t salt_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(salt, salt_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - salt, salt_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_ASYMMETRIC_DECRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_ASYMMETRIC_DECRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_asymmetric_encrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - const uint8_t *salt, size_t salt_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(salt, salt_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - salt, salt_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_ASYMMETRIC_ENCRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_ASYMMETRIC_ENCRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_abort( - psa_cipher_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_decrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_DECRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_DECRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_decrypt_setup( - psa_cipher_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_DECRYPT_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_DECRYPT_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_encrypt( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_ENCRYPT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_ENCRYPT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_encrypt_setup( - psa_cipher_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_ENCRYPT_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_ENCRYPT_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_finish( - psa_cipher_operation_t *operation, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_FINISH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_FINISH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_generate_iv( - psa_cipher_operation_t *operation, - uint8_t *iv, size_t iv_size, - size_t *iv_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(iv, iv_size) + - psasim_serialise_size_t_needs(*iv_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - iv, iv_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *iv_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_GENERATE_IV, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_GENERATE_IV server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - iv, iv_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - iv_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_set_iv( - psa_cipher_operation_t *operation, - const uint8_t *iv, size_t iv_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(iv, iv_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - iv, iv_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_SET_IV, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_SET_IV server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_cipher_update( - psa_cipher_operation_t *operation, - const uint8_t *input, size_t input_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_cipher_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_cipher_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_CIPHER_UPDATE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_CIPHER_UPDATE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_cipher_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_copy_key( - mbedtls_svc_key_id_t source_key, - const psa_key_attributes_t *attributes, - mbedtls_svc_key_id_t *target_key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(source_key) + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*target_key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - source_key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *target_key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_COPY_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_COPY_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - target_key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_destroy_key( - mbedtls_svc_key_id_t key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_DESTROY_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_DESTROY_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_export_key( - mbedtls_svc_key_id_t key, - uint8_t *data, size_t data_size, - size_t *data_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(*data_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - data, data_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *data_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_export_public_key( - mbedtls_svc_key_id_t key, - uint8_t *data, size_t data_size, - size_t *data_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(*data_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - data, data_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *data_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_PUBLIC_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_PUBLIC_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_export_public_key_iop_abort( - psa_export_public_key_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_export_public_key_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_export_public_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_PUBLIC_KEY_IOP_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_PUBLIC_KEY_IOP_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_export_public_key_iop_complete( - psa_export_public_key_iop_t *operation, - uint8_t *data, size_t data_size, - size_t *data_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_export_public_key_iop_t_needs(*operation) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(*data_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_export_public_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - data, data_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *data_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_PUBLIC_KEY_IOP_COMPLETE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_PUBLIC_KEY_IOP_COMPLETE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_export_public_key_iop_get_num_ops( - psa_export_public_key_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_export_public_key_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_export_public_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_PUBLIC_KEY_IOP_GET_NUM_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_PUBLIC_KEY_IOP_GET_NUM_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -psa_status_t psa_export_public_key_iop_setup( - psa_export_public_key_iop_t *operation, - mbedtls_svc_key_id_t key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_export_public_key_iop_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_export_public_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_EXPORT_PUBLIC_KEY_IOP_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_EXPORT_PUBLIC_KEY_IOP_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_generate_key( - const psa_key_attributes_t *attributes, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_generate_key_custom( - const psa_key_attributes_t *attributes, - const psa_custom_key_parameters_t *custom, - const uint8_t *custom_data, size_t custom_data_length, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_psa_custom_key_parameters_t_needs(*custom) + - psasim_serialise_buffer_needs(custom_data, custom_data_length) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_custom_key_parameters_t( - &pos, &remaining, - *custom); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - custom_data, custom_data_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY_CUSTOM, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY_CUSTOM server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_generate_key_iop_abort( - psa_generate_key_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_generate_key_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_generate_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY_IOP_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY_IOP_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_generate_key_iop_complete( - psa_generate_key_iop_t *operation, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_generate_key_iop_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_generate_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY_IOP_COMPLETE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY_IOP_COMPLETE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_generate_key_iop_get_num_ops( - psa_generate_key_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_generate_key_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_generate_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY_IOP_GET_NUM_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY_IOP_GET_NUM_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -psa_status_t psa_generate_key_iop_setup( - psa_generate_key_iop_t *operation, - const psa_key_attributes_t *attributes - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_generate_key_iop_t_needs(*operation) + - psasim_serialise_psa_key_attributes_t_needs(*attributes); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_generate_key_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_KEY_IOP_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_KEY_IOP_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_generate_random( - uint8_t *output, size_t output_size - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_buffer_needs(output, output_size); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GENERATE_RANDOM, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GENERATE_RANDOM server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_get_key_attributes( - mbedtls_svc_key_id_t key, - psa_key_attributes_t *attributes - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_key_attributes_t_needs(*attributes); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_GET_KEY_ATTRIBUTES, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_GET_KEY_ATTRIBUTES server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &rpos, &rremain, - attributes); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_abort( - psa_hash_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_clone( - const psa_hash_operation_t *source_operation, - psa_hash_operation_t *target_operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*source_operation) + - psasim_serialise_psa_hash_operation_t_needs(*target_operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *source_operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *target_operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_CLONE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_CLONE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - target_operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_compare( - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - const uint8_t *hash, size_t hash_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(hash, hash_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_COMPARE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_COMPARE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_compute( - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - uint8_t *hash, size_t hash_size, - size_t *hash_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(hash, hash_size) + - psasim_serialise_size_t_needs(*hash_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *hash_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_COMPUTE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_COMPUTE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - hash, hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - hash_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_finish( - psa_hash_operation_t *operation, - uint8_t *hash, size_t hash_size, - size_t *hash_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(hash, hash_size) + - psasim_serialise_size_t_needs(*hash_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *hash_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_FINISH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_FINISH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - hash, hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - hash_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_setup( - psa_hash_operation_t *operation, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*operation) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_update( - psa_hash_operation_t *operation, - const uint8_t *input, size_t input_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(input, input_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_UPDATE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_UPDATE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_hash_verify( - psa_hash_operation_t *operation, - const uint8_t *hash, size_t hash_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_hash_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(hash, hash_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_hash_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_HASH_VERIFY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_HASH_VERIFY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_hash_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_import_key( - const psa_key_attributes_t *attributes, - const uint8_t *data, size_t data_length, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_buffer_needs(data, data_length) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - data, data_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_IMPORT_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_IMPORT_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_interruptible_get_max_ops( - void - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - 0; - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_INTERRUPTIBLE_GET_MAX_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_INTERRUPTIBLE_GET_MAX_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -void psa_interruptible_set_max_ops( - uint32_t max_ops - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(max_ops); - - ser_params = malloc(needed); - if (ser_params == NULL) { - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_uint32_t( - &pos, &remaining, - max_ops); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_INTERRUPTIBLE_SET_MAX_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_INTERRUPTIBLE_SET_MAX_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); -} - - -psa_status_t psa_key_agreement( - mbedtls_svc_key_id_t private_key, - const uint8_t *peer_key, size_t peer_key_length, - psa_algorithm_t alg, - const psa_key_attributes_t *attributes, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(private_key) + - psasim_serialise_buffer_needs(peer_key, peer_key_length) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - private_key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - peer_key, peer_key_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_AGREEMENT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_AGREEMENT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_agreement_iop_abort( - psa_key_agreement_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_agreement_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_agreement_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_AGREEMENT_IOP_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_AGREEMENT_IOP_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_agreement_iop_complete( - psa_key_agreement_iop_t *operation, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_agreement_iop_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_agreement_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_AGREEMENT_IOP_COMPLETE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_AGREEMENT_IOP_COMPLETE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_key_agreement_iop_get_num_ops( - psa_key_agreement_iop_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_agreement_iop_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_agreement_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_AGREEMENT_IOP_GET_NUM_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_AGREEMENT_IOP_GET_NUM_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -psa_status_t psa_key_agreement_iop_setup( - psa_key_agreement_iop_t *operation, - mbedtls_svc_key_id_t private_key, - const uint8_t *peer_key, size_t peer_key_length, - psa_algorithm_t alg, - const psa_key_attributes_t *attributes - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_agreement_iop_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(private_key) + - psasim_serialise_buffer_needs(peer_key, peer_key_length) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_psa_key_attributes_t_needs(*attributes); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_agreement_iop_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - private_key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - peer_key, peer_key_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_AGREEMENT_IOP_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_AGREEMENT_IOP_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_abort( - psa_key_derivation_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_get_capacity( - const psa_key_derivation_operation_t *operation, - size_t *capacity - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_size_t_needs(*capacity); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *capacity); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_GET_CAPACITY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_GET_CAPACITY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - capacity); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_input_bytes( - psa_key_derivation_operation_t *operation, - psa_key_derivation_step_t step, - const uint8_t *data, size_t data_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_key_derivation_step_t_needs(step) + - psasim_serialise_buffer_needs(data, data_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_step_t( - &pos, &remaining, - step); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - data, data_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_INPUT_BYTES, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_INPUT_BYTES server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_input_integer( - psa_key_derivation_operation_t *operation, - psa_key_derivation_step_t step, - uint64_t value - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_key_derivation_step_t_needs(step) + - psasim_serialise_uint64_t_needs(value); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_step_t( - &pos, &remaining, - step); - if (!ok) { - goto fail; - } - ok = psasim_serialise_uint64_t( - &pos, &remaining, - value); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_INPUT_INTEGER, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_INPUT_INTEGER server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_input_key( - psa_key_derivation_operation_t *operation, - psa_key_derivation_step_t step, - mbedtls_svc_key_id_t key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_key_derivation_step_t_needs(step) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_step_t( - &pos, &remaining, - step); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_INPUT_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_INPUT_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_key_agreement( - psa_key_derivation_operation_t *operation, - psa_key_derivation_step_t step, - mbedtls_svc_key_id_t private_key, - const uint8_t *peer_key, size_t peer_key_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_key_derivation_step_t_needs(step) + - psasim_serialise_mbedtls_svc_key_id_t_needs(private_key) + - psasim_serialise_buffer_needs(peer_key, peer_key_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_step_t( - &pos, &remaining, - step); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - private_key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - peer_key, peer_key_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_KEY_AGREEMENT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_KEY_AGREEMENT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_output_bytes( - psa_key_derivation_operation_t *operation, - uint8_t *output, size_t output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(output, output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_OUTPUT_BYTES, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_OUTPUT_BYTES server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_output_key( - const psa_key_attributes_t *attributes, - psa_key_derivation_operation_t *operation, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_OUTPUT_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_OUTPUT_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_output_key_custom( - const psa_key_attributes_t *attributes, - psa_key_derivation_operation_t *operation, - const psa_custom_key_parameters_t *custom, - const uint8_t *custom_data, size_t custom_data_length, - mbedtls_svc_key_id_t *key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes) + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_custom_key_parameters_t_needs(*custom) + - psasim_serialise_buffer_needs(custom_data, custom_data_length) + - psasim_serialise_mbedtls_svc_key_id_t_needs(*key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_custom_key_parameters_t( - &pos, &remaining, - *custom); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - custom_data, custom_data_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - *key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_OUTPUT_KEY_CUSTOM, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_OUTPUT_KEY_CUSTOM server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_set_capacity( - psa_key_derivation_operation_t *operation, - size_t capacity - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_size_t_needs(capacity); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - capacity); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_SET_CAPACITY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_SET_CAPACITY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_key_derivation_setup( - psa_key_derivation_operation_t *operation, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_derivation_operation_t_needs(*operation) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_derivation_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_KEY_DERIVATION_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_KEY_DERIVATION_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_abort( - psa_mac_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_compute( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - uint8_t *mac, size_t mac_size, - size_t *mac_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(mac, mac_size) + - psasim_serialise_size_t_needs(*mac_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - mac, mac_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *mac_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_COMPUTE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_COMPUTE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - mac, mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - mac_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_sign_finish( - psa_mac_operation_t *operation, - uint8_t *mac, size_t mac_size, - size_t *mac_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(mac, mac_size) + - psasim_serialise_size_t_needs(*mac_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - mac, mac_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *mac_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_SIGN_FINISH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_SIGN_FINISH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - mac, mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - mac_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_sign_setup( - psa_mac_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_SIGN_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_SIGN_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_update( - psa_mac_operation_t *operation, - const uint8_t *input, size_t input_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(input, input_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_UPDATE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_UPDATE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_verify( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - const uint8_t *mac, size_t mac_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(mac, mac_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - mac, mac_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_VERIFY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_VERIFY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_verify_finish( - psa_mac_operation_t *operation, - const uint8_t *mac, size_t mac_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(mac, mac_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - mac, mac_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_VERIFY_FINISH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_VERIFY_FINISH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_mac_verify_setup( - psa_mac_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_mac_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_mac_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_MAC_VERIFY_SETUP, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_MAC_VERIFY_SETUP server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_mac_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_purge_key( - mbedtls_svc_key_id_t key - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_PURGE_KEY, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_PURGE_KEY server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_raw_key_agreement( - psa_algorithm_t alg, - mbedtls_svc_key_id_t private_key, - const uint8_t *peer_key, size_t peer_key_length, - uint8_t *output, size_t output_size, - size_t *output_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_mbedtls_svc_key_id_t_needs(private_key) + - psasim_serialise_buffer_needs(peer_key, peer_key_length) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(*output_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - private_key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - peer_key, peer_key_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - output, output_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *output_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_RAW_KEY_AGREEMENT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_RAW_KEY_AGREEMENT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -void psa_reset_key_attributes( - psa_key_attributes_t *attributes - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(*attributes); - - ser_params = malloc(needed); - if (ser_params == NULL) { - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_key_attributes_t( - &pos, &remaining, - *attributes); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_RESET_KEY_ATTRIBUTES, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_RESET_KEY_ATTRIBUTES server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &rpos, &rremain, - attributes); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); -} - - -psa_status_t psa_sign_hash( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *hash, size_t hash_length, - uint8_t *signature, size_t signature_size, - size_t *signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(hash, hash_length) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(*signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_HASH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_HASH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_sign_hash_abort( - psa_sign_hash_interruptible_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_sign_hash_interruptible_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_HASH_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_HASH_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_sign_hash_complete( - psa_sign_hash_interruptible_operation_t *operation, - uint8_t *signature, size_t signature_size, - size_t *signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_sign_hash_interruptible_operation_t_needs(*operation) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(*signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_HASH_COMPLETE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_HASH_COMPLETE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_sign_hash_get_num_ops( - const psa_sign_hash_interruptible_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_sign_hash_interruptible_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_HASH_GET_NUM_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_HASH_GET_NUM_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -psa_status_t psa_sign_hash_start( - psa_sign_hash_interruptible_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *hash, size_t hash_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_sign_hash_interruptible_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(hash, hash_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_HASH_START, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_HASH_START server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_sign_message( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - uint8_t *signature, size_t signature_size, - size_t *signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(*signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_size); - if (!ok) { - goto fail; - } - ok = psasim_serialise_size_t( - &pos, &remaining, - *signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_SIGN_MESSAGE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_SIGN_MESSAGE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_return_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_verify_hash( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *hash, size_t hash_length, - const uint8_t *signature, size_t signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(hash, hash_length) + - psasim_serialise_buffer_needs(signature, signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_HASH, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_HASH server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_verify_hash_abort( - psa_verify_hash_interruptible_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_verify_hash_interruptible_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_HASH_ABORT, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_HASH_ABORT server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_verify_hash_complete( - psa_verify_hash_interruptible_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_verify_hash_interruptible_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_HASH_COMPLETE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_HASH_COMPLETE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -uint32_t psa_verify_hash_get_num_ops( - const psa_verify_hash_interruptible_operation_t *operation - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - uint32_t value = 0; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_verify_hash_interruptible_operation_t_needs(*operation); - - ser_params = malloc(needed); - if (ser_params == NULL) { - value = 0; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_HASH_GET_NUM_OPS, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_HASH_GET_NUM_OPS server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &rpos, &rremain, - &value); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return value; -} - - -psa_status_t psa_verify_hash_start( - psa_verify_hash_interruptible_operation_t *operation, - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *hash, size_t hash_length, - const uint8_t *signature, size_t signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_psa_verify_hash_interruptible_operation_t_needs(*operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(hash, hash_length) + - psasim_serialise_buffer_needs(signature, signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - *operation); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - hash, hash_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_HASH_START, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_HASH_START server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} - - -psa_status_t psa_verify_message( - mbedtls_svc_key_id_t key, - psa_algorithm_t alg, - const uint8_t *input, size_t input_length, - const uint8_t *signature, size_t signature_length - ) -{ - uint8_t *ser_params = NULL; - uint8_t *ser_result = NULL; - size_t result_length; - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - size_t needed = - psasim_serialise_begin_needs() + - psasim_serialise_mbedtls_svc_key_id_t_needs(key) + - psasim_serialise_psa_algorithm_t_needs(alg) + - psasim_serialise_buffer_needs(input, input_length) + - psasim_serialise_buffer_needs(signature, signature_length); - - ser_params = malloc(needed); - if (ser_params == NULL) { - status = PSA_ERROR_INSUFFICIENT_MEMORY; - goto fail; - } - - uint8_t *pos = ser_params; - size_t remaining = needed; - int ok; - ok = psasim_serialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - ok = psasim_serialise_mbedtls_svc_key_id_t( - &pos, &remaining, - key); - if (!ok) { - goto fail; - } - ok = psasim_serialise_psa_algorithm_t( - &pos, &remaining, - alg); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - input, input_length); - if (!ok) { - goto fail; - } - ok = psasim_serialise_buffer( - &pos, &remaining, - signature, signature_length); - if (!ok) { - goto fail; - } - - ok = psa_crypto_call(PSA_VERIFY_MESSAGE, - ser_params, (size_t) (pos - ser_params), &ser_result, &result_length); - if (!ok) { - printf("PSA_VERIFY_MESSAGE server call failed\n"); - goto fail; - } - - uint8_t *rpos = ser_result; - size_t rremain = result_length; - - ok = psasim_deserialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_status_t( - &rpos, &rremain, - &status); - if (!ok) { - goto fail; - } - -fail: - free(ser_params); - free(ser_result); - - return status; -} diff --git a/psasim/src/psa_sim_crypto_server.c b/psasim/src/psa_sim_crypto_server.c deleted file mode 100644 index 2210afcc8..000000000 --- a/psasim/src/psa_sim_crypto_server.c +++ /dev/null @@ -1,9155 +0,0 @@ -/* THIS FILE WAS AUTO-GENERATED BY psa_sim_generate.pl. DO NOT EDIT!! */ - -/* server implementations */ - -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#include -#include - -#include - -#include "psa_functions_codes.h" -#include "psa_sim_serialise.h" - -#include "service.h" - -#if !defined(MBEDTLS_PSA_CRYPTO_C) -#error "Error: MBEDTLS_PSA_CRYPTO_C must be enabled on server build" -#endif - -#if defined(MBEDTLS_TEST_HOOKS) -void (*mbedtls_test_hook_error_add)(int, int, const char *, int); -#endif - -// Returns 1 for success, 0 for failure -int psa_crypto_init_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - - uint8_t *result = NULL; - int ok; - - // Now we call the actual target function - - status = psa_crypto_init( - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_decrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *nonce = NULL; - size_t nonce_length; - uint8_t *additional_data = NULL; - size_t additional_data_length; - uint8_t *ciphertext = NULL; - size_t ciphertext_length; - uint8_t *plaintext = NULL; - size_t plaintext_size; - size_t plaintext_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &nonce, &nonce_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &additional_data, &additional_data_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &ciphertext, &ciphertext_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &plaintext, &plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &plaintext_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_decrypt( - key, - alg, - nonce, nonce_length, - additional_data, additional_data_length, - ciphertext, ciphertext_length, - plaintext, plaintext_size, - &plaintext_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(plaintext, plaintext_size) + - psasim_serialise_size_t_needs(plaintext_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - plaintext_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(nonce); - free(additional_data); - free(ciphertext); - free(plaintext); - - return 1; // success - -fail: - free(result); - - free(nonce); - free(additional_data); - free(ciphertext); - free(plaintext); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_decrypt_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_decrypt_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_encrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *nonce = NULL; - size_t nonce_length; - uint8_t *additional_data = NULL; - size_t additional_data_length; - uint8_t *plaintext = NULL; - size_t plaintext_length; - uint8_t *ciphertext = NULL; - size_t ciphertext_size; - size_t ciphertext_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &nonce, &nonce_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &additional_data, &additional_data_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &plaintext, &plaintext_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &ciphertext, &ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &ciphertext_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_encrypt( - key, - alg, - nonce, nonce_length, - additional_data, additional_data_length, - plaintext, plaintext_length, - ciphertext, ciphertext_size, - &ciphertext_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(ciphertext, ciphertext_size) + - psasim_serialise_size_t_needs(ciphertext_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - ciphertext_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(nonce); - free(additional_data); - free(plaintext); - free(ciphertext); - - return 1; // success - -fail: - free(result); - - free(nonce); - free(additional_data); - free(plaintext); - free(ciphertext); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_encrypt_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_encrypt_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_finish_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *ciphertext = NULL; - size_t ciphertext_size; - size_t ciphertext_length; - uint8_t *tag = NULL; - size_t tag_size; - size_t tag_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &ciphertext, &ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &ciphertext_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &tag, &tag_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &tag_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_finish( - operation, - ciphertext, ciphertext_size, - &ciphertext_length, - tag, tag_size, - &tag_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation) + - psasim_serialise_buffer_needs(ciphertext, ciphertext_size) + - psasim_serialise_size_t_needs(ciphertext_length) + - psasim_serialise_buffer_needs(tag, tag_size) + - psasim_serialise_size_t_needs(tag_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - ciphertext, ciphertext_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - ciphertext_length); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - tag, tag_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - tag_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(ciphertext); - free(tag); - - return 1; // success - -fail: - free(result); - - free(ciphertext); - free(tag); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_generate_nonce_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *nonce = NULL; - size_t nonce_size; - size_t nonce_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &nonce, &nonce_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &nonce_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_generate_nonce( - operation, - nonce, nonce_size, - &nonce_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation) + - psasim_serialise_buffer_needs(nonce, nonce_size) + - psasim_serialise_size_t_needs(nonce_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - nonce, nonce_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - nonce_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(nonce); - - return 1; // success - -fail: - free(result); - - free(nonce); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_set_lengths_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - size_t ad_length; - size_t plaintext_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &ad_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &plaintext_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_set_lengths( - operation, - ad_length, - plaintext_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_set_nonce_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *nonce = NULL; - size_t nonce_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &nonce, &nonce_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_set_nonce( - operation, - nonce, nonce_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(nonce); - - return 1; // success - -fail: - free(result); - - free(nonce); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_update_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *input = NULL; - size_t input_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_update( - operation, - input, input_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_update_ad_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *input = NULL; - size_t input_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_update_ad( - operation, - input, input_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - - return 1; // success - -fail: - free(result); - - free(input); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_aead_verify_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_aead_operation_t *operation; - uint8_t *plaintext = NULL; - size_t plaintext_size; - size_t plaintext_length; - uint8_t *tag = NULL; - size_t tag_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_aead_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &plaintext, &plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &plaintext_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &tag, &tag_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_aead_verify( - operation, - plaintext, plaintext_size, - &plaintext_length, - tag, tag_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_aead_operation_t_needs(operation) + - psasim_serialise_buffer_needs(plaintext, plaintext_size) + - psasim_serialise_size_t_needs(plaintext_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_aead_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - plaintext, plaintext_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - plaintext_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(plaintext); - free(tag); - - return 1; // success - -fail: - free(result); - - free(plaintext); - free(tag); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_asymmetric_decrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *salt = NULL; - size_t salt_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &salt, &salt_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_asymmetric_decrypt( - key, - alg, - input, input_length, - salt, salt_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(salt); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(salt); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_asymmetric_encrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *salt = NULL; - size_t salt_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &salt, &salt_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_asymmetric_encrypt( - key, - alg, - input, input_length, - salt, salt_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(salt); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(salt); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_decrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_decrypt( - key, - alg, - input, input_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_decrypt_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_decrypt_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_encrypt_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_encrypt( - key, - alg, - input, input_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_encrypt_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_encrypt_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_finish_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_finish( - operation, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(output); - - return 1; // success - -fail: - free(result); - - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_generate_iv_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - uint8_t *iv = NULL; - size_t iv_size; - size_t iv_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &iv, &iv_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &iv_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_generate_iv( - operation, - iv, iv_size, - &iv_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation) + - psasim_serialise_buffer_needs(iv, iv_size) + - psasim_serialise_size_t_needs(iv_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - iv, iv_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - iv_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(iv); - - return 1; // success - -fail: - free(result); - - free(iv); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_set_iv_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - uint8_t *iv = NULL; - size_t iv_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &iv, &iv_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_set_iv( - operation, - iv, iv_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(iv); - - return 1; // success - -fail: - free(result); - - free(iv); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_cipher_update_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_cipher_operation_t *operation; - uint8_t *input = NULL; - size_t input_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_cipher_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_cipher_update( - operation, - input, input_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_cipher_operation_t_needs(operation) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_cipher_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(output); - - return 1; // success - -fail: - free(result); - - free(input); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_copy_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t source_key; - psa_key_attributes_t attributes; - mbedtls_svc_key_id_t target_key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &source_key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &target_key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_copy_key( - source_key, - &attributes, - &target_key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_mbedtls_svc_key_id_t_needs(target_key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - target_key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_destroy_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_destroy_key( - key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - uint8_t *data = NULL; - size_t data_size; - size_t data_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &data, &data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &data_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_export_key( - key, - data, data_size, - &data_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(data_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(data); - - return 1; // success - -fail: - free(result); - - free(data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_public_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - uint8_t *data = NULL; - size_t data_size; - size_t data_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &data, &data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &data_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_export_public_key( - key, - data, data_size, - &data_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(data_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(data); - - return 1; // success - -fail: - free(result); - - free(data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_public_key_iop_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_export_public_key_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_export_public_key_iop_abort( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_export_public_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_public_key_iop_complete_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_export_public_key_iop_t operation; - uint8_t *data = NULL; - size_t data_size; - size_t data_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &data, &data_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &data_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_export_public_key_iop_complete( - &operation, - data, data_size, - &data_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_export_public_key_iop_t_needs(operation) + - psasim_serialise_buffer_needs(data, data_size) + - psasim_serialise_size_t_needs(data_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - data, data_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - data_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(data); - - return 1; // success - -fail: - free(result); - - free(data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_public_key_iop_get_num_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - psa_export_public_key_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_export_public_key_iop_get_num_ops( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value) + - psasim_serialise_psa_export_public_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_export_public_key_iop_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_export_public_key_iop_t operation; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_export_public_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_export_public_key_iop_setup( - &operation, - key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_export_public_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_export_public_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_attributes_t attributes; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_key( - &attributes, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_custom_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_attributes_t attributes; - psa_custom_key_parameters_t custom; - uint8_t *custom_data = NULL; - size_t custom_data_length; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_custom_key_parameters_t( - &pos, &remaining, - &custom); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &custom_data, &custom_data_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_key_custom( - &attributes, - &custom, - custom_data, custom_data_length, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(custom_data); - - return 1; // success - -fail: - free(result); - - free(custom_data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_iop_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_generate_key_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_key_iop_abort( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_generate_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_iop_complete_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_generate_key_iop_t operation; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_key_iop_complete( - &operation, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_generate_key_iop_t_needs(operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_iop_get_num_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - psa_generate_key_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_generate_key_iop_get_num_ops( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value) + - psasim_serialise_psa_generate_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_key_iop_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_generate_key_iop_t operation; - psa_key_attributes_t attributes; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_generate_key_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_key_iop_setup( - &operation, - &attributes - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_generate_key_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_generate_key_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_generate_random_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - uint8_t *output = NULL; - size_t output_size; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_generate_random( - output, output_size - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(output); - - return 1; // success - -fail: - free(result); - - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_get_key_attributes_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_key_attributes_t attributes; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_get_key_attributes( - key, - &attributes - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_key_attributes_t_needs(attributes); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_attributes_t( - &rpos, &rremain, - attributes); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_clone_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *source_operation; - psa_hash_operation_t *target_operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &source_operation); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &target_operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_clone( - source_operation, - target_operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(target_operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - target_operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_compare_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *hash = NULL; - size_t hash_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_compare( - alg, - input, input_length, - hash, hash_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(hash); - - return 1; // success - -fail: - free(result); - - free(input); - free(hash); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_compute_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *hash = NULL; - size_t hash_size; - size_t hash_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &hash_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_compute( - alg, - input, input_length, - hash, hash_size, - &hash_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(hash, hash_size) + - psasim_serialise_size_t_needs(hash_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - hash, hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - hash_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(hash); - - return 1; // success - -fail: - free(result); - - free(input); - free(hash); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_finish_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *operation; - uint8_t *hash = NULL; - size_t hash_size; - size_t hash_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &hash_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_finish( - operation, - hash, hash_size, - &hash_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(operation) + - psasim_serialise_buffer_needs(hash, hash_size) + - psasim_serialise_size_t_needs(hash_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - hash, hash_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - hash_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - - return 1; // success - -fail: - free(result); - - free(hash); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *operation; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_setup( - operation, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_update_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *operation; - uint8_t *input = NULL; - size_t input_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_update( - operation, - input, input_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - - return 1; // success - -fail: - free(result); - - free(input); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_hash_verify_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_hash_operation_t *operation; - uint8_t *hash = NULL; - size_t hash_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_hash_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_hash_verify( - operation, - hash, hash_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_hash_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_hash_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - - return 1; // success - -fail: - free(result); - - free(hash); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_import_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_attributes_t attributes; - uint8_t *data = NULL; - size_t data_length; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &data, &data_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_import_key( - &attributes, - data, data_length, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(data); - - return 1; // success - -fail: - free(result); - - free(data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_interruptible_get_max_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - - uint8_t *result = NULL; - int ok; - - // Now we call the actual target function - - value = psa_interruptible_get_max_ops( - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_interruptible_set_max_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t max_ops; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint32_t( - &pos, &remaining, - &max_ops); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - psa_interruptible_set_max_ops( - max_ops - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs(); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_agreement_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t private_key; - uint8_t *peer_key = NULL; - size_t peer_key_length; - psa_algorithm_t alg; - psa_key_attributes_t attributes; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &private_key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &peer_key, &peer_key_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_agreement( - private_key, - peer_key, peer_key_length, - alg, - &attributes, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(peer_key); - - return 1; // success - -fail: - free(result); - - free(peer_key); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_agreement_iop_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_agreement_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_agreement_iop_abort( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_key_agreement_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_agreement_iop_complete_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_agreement_iop_t operation; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_agreement_iop_complete( - &operation, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_key_agreement_iop_t_needs(operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_agreement_iop_get_num_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - psa_key_agreement_iop_t operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_key_agreement_iop_get_num_ops( - &operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value) + - psasim_serialise_psa_key_agreement_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_agreement_iop_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_agreement_iop_t operation; - mbedtls_svc_key_id_t private_key; - uint8_t *peer_key = NULL; - size_t peer_key_length; - psa_algorithm_t alg; - psa_key_attributes_t attributes; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_agreement_iop_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &private_key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &peer_key, &peer_key_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_agreement_iop_setup( - &operation, - private_key, - peer_key, peer_key_length, - alg, - &attributes - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_psa_key_agreement_iop_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_agreement_iop_t( - &rpos, &rremain, - operation); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(peer_key); - - return 1; // success - -fail: - free(result); - - free(peer_key); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_get_capacity_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - size_t capacity; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &capacity); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_get_capacity( - operation, - &capacity - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_size_t_needs(capacity); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - capacity); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_input_bytes_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - psa_key_derivation_step_t step; - uint8_t *data = NULL; - size_t data_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_step_t( - &pos, &remaining, - &step); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &data, &data_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_input_bytes( - operation, - step, - data, data_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(data); - - return 1; // success - -fail: - free(result); - - free(data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_input_integer_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - psa_key_derivation_step_t step; - uint64_t value; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_step_t( - &pos, &remaining, - &step); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_uint64_t( - &pos, &remaining, - &value); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_input_integer( - operation, - step, - value - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_input_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - psa_key_derivation_step_t step; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_step_t( - &pos, &remaining, - &step); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_input_key( - operation, - step, - key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_key_agreement_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - psa_key_derivation_step_t step; - mbedtls_svc_key_id_t private_key; - uint8_t *peer_key = NULL; - size_t peer_key_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_derivation_step_t( - &pos, &remaining, - &step); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &private_key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &peer_key, &peer_key_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_key_agreement( - operation, - step, - private_key, - peer_key, peer_key_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(peer_key); - - return 1; // success - -fail: - free(result); - - free(peer_key); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_output_bytes_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - uint8_t *output = NULL; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_output_bytes( - operation, - output, output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation) + - psasim_serialise_buffer_needs(output, output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(output); - - return 1; // success - -fail: - free(result); - - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_output_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_attributes_t attributes; - psa_key_derivation_operation_t *operation; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_output_key( - &attributes, - operation, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_output_key_custom_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_attributes_t attributes; - psa_key_derivation_operation_t *operation; - psa_custom_key_parameters_t custom; - uint8_t *custom_data = NULL; - size_t custom_data_length; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_custom_key_parameters_t( - &pos, &remaining, - &custom); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &custom_data, &custom_data_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_output_key_custom( - &attributes, - operation, - &custom, - custom_data, custom_data_length, - &key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation) + - psasim_serialise_mbedtls_svc_key_id_t_needs(key); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_mbedtls_svc_key_id_t( - &rpos, &rremain, - key); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(custom_data); - - return 1; // success - -fail: - free(result); - - free(custom_data); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_set_capacity_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - size_t capacity; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &capacity); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_set_capacity( - operation, - capacity - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_key_derivation_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_key_derivation_operation_t *operation; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_key_derivation_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_key_derivation_setup( - operation, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_key_derivation_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_key_derivation_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_compute_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *mac = NULL; - size_t mac_size; - size_t mac_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &mac, &mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &mac_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_compute( - key, - alg, - input, input_length, - mac, mac_size, - &mac_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(mac, mac_size) + - psasim_serialise_size_t_needs(mac_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - mac, mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - mac_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(mac); - - return 1; // success - -fail: - free(result); - - free(input); - free(mac); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_sign_finish_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - uint8_t *mac = NULL; - size_t mac_size; - size_t mac_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &mac, &mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &mac_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_sign_finish( - operation, - mac, mac_size, - &mac_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation) + - psasim_serialise_buffer_needs(mac, mac_size) + - psasim_serialise_size_t_needs(mac_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - mac, mac_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - mac_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(mac); - - return 1; // success - -fail: - free(result); - - free(mac); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_sign_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_sign_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_update_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - uint8_t *input = NULL; - size_t input_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_update( - operation, - input, input_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - - return 1; // success - -fail: - free(result); - - free(input); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_verify_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *mac = NULL; - size_t mac_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &mac, &mac_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_verify( - key, - alg, - input, input_length, - mac, mac_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(mac); - - return 1; // success - -fail: - free(result); - - free(input); - free(mac); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_verify_finish_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - uint8_t *mac = NULL; - size_t mac_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &mac, &mac_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_verify_finish( - operation, - mac, mac_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(mac); - - return 1; // success - -fail: - free(result); - - free(mac); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_mac_verify_setup_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_mac_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_mac_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_mac_verify_setup( - operation, - key, - alg - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_mac_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_mac_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_purge_key_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_purge_key( - key - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_raw_key_agreement_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_algorithm_t alg; - mbedtls_svc_key_id_t private_key; - uint8_t *peer_key = NULL; - size_t peer_key_length; - uint8_t *output = NULL; - size_t output_size; - size_t output_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &private_key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &peer_key, &peer_key_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &output, &output_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &output_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_raw_key_agreement( - alg, - private_key, - peer_key, peer_key_length, - output, output_size, - &output_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(output, output_size) + - psasim_serialise_size_t_needs(output_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - output, output_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - output_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(peer_key); - free(output); - - return 1; // success - -fail: - free(result); - - free(peer_key); - free(output); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_reset_key_attributes_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_key_attributes_t attributes; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_key_attributes_t( - &pos, &remaining, - &attributes); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - psa_reset_key_attributes( - &attributes - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_key_attributes_t_needs(attributes); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_key_attributes_t( - &rpos, &rremain, - attributes); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_hash_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *hash = NULL; - size_t hash_length; - uint8_t *signature = NULL; - size_t signature_size; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_sign_hash( - key, - alg, - hash, hash_length, - signature, signature_size, - &signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(signature_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - free(signature); - - return 1; // success - -fail: - free(result); - - free(hash); - free(signature); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_hash_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_sign_hash_interruptible_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_sign_hash_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_sign_hash_interruptible_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_hash_complete_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_sign_hash_interruptible_operation_t *operation; - uint8_t *signature = NULL; - size_t signature_size; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_sign_hash_complete( - operation, - signature, signature_size, - &signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_sign_hash_interruptible_operation_t_needs(operation) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(signature_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(signature); - - return 1; // success - -fail: - free(result); - - free(signature); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_hash_get_num_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - psa_sign_hash_interruptible_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_sign_hash_get_num_ops( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_hash_start_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_sign_hash_interruptible_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *hash = NULL; - size_t hash_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_sign_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_sign_hash_start( - operation, - key, - alg, - hash, hash_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_sign_hash_interruptible_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_sign_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - - return 1; // success - -fail: - free(result); - - free(hash); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_sign_message_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *signature = NULL; - size_t signature_size; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_size_t( - &pos, &remaining, - &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_sign_message( - key, - alg, - input, input_length, - signature, signature_size, - &signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_serialise_buffer_needs(signature, signature_size) + - psasim_serialise_size_t_needs(signature_length); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_buffer( - &rpos, &rremain, - signature, signature_size); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_size_t( - &rpos, &rremain, - signature_length); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(signature); - - return 1; // success - -fail: - free(result); - - free(input); - free(signature); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_hash_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *hash = NULL; - size_t hash_length; - uint8_t *signature = NULL; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_verify_hash( - key, - alg, - hash, hash_length, - signature, signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - free(signature); - - return 1; // success - -fail: - free(result); - - free(hash); - free(signature); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_hash_abort_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_verify_hash_interruptible_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_verify_hash_abort( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_verify_hash_interruptible_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 1); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_hash_complete_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_verify_hash_interruptible_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_verify_hash_complete( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_verify_hash_interruptible_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_hash_get_num_ops_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - uint32_t value = 0; - psa_verify_hash_interruptible_operation_t *operation; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - value = psa_verify_hash_get_num_ops( - operation - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_uint32_t_needs(value); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_uint32_t( - &rpos, &rremain, - value); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - return 1; // success - -fail: - free(result); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_hash_start_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - psa_verify_hash_interruptible_operation_t *operation; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *hash = NULL; - size_t hash_length; - uint8_t *signature = NULL; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_server_deserialise_psa_verify_hash_interruptible_operation_t( - &pos, &remaining, - &operation); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &hash, &hash_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_verify_hash_start( - operation, - key, - alg, - hash, hash_length, - signature, signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status) + - psasim_server_serialise_psa_verify_hash_interruptible_operation_t_needs(operation); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - ok = psasim_server_serialise_psa_verify_hash_interruptible_operation_t( - &rpos, &rremain, - operation, 0); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(hash); - free(signature); - - return 1; // success - -fail: - free(result); - - free(hash); - free(signature); - - return 0; // This shouldn't happen! -} - -// Returns 1 for success, 0 for failure -int psa_verify_message_wrapper( - uint8_t *in_params, size_t in_params_len, - uint8_t **out_params, size_t *out_params_len) -{ - psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; - mbedtls_svc_key_id_t key; - psa_algorithm_t alg; - uint8_t *input = NULL; - size_t input_length; - uint8_t *signature = NULL; - size_t signature_length; - - uint8_t *pos = in_params; - size_t remaining = in_params_len; - uint8_t *result = NULL; - int ok; - - ok = psasim_deserialise_begin(&pos, &remaining); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_mbedtls_svc_key_id_t( - &pos, &remaining, - &key); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_psa_algorithm_t( - &pos, &remaining, - &alg); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &input, &input_length); - if (!ok) { - goto fail; - } - - ok = psasim_deserialise_buffer( - &pos, &remaining, - &signature, &signature_length); - if (!ok) { - goto fail; - } - - // Now we call the actual target function - - status = psa_verify_message( - key, - alg, - input, input_length, - signature, signature_length - ); - - // NOTE: Should really check there is no overflow as we go along. - size_t result_size = - psasim_serialise_begin_needs() + - psasim_serialise_psa_status_t_needs(status); - - result = malloc(result_size); - if (result == NULL) { - goto fail; - } - - uint8_t *rpos = result; - size_t rremain = result_size; - - ok = psasim_serialise_begin(&rpos, &rremain); - if (!ok) { - goto fail; - } - - ok = psasim_serialise_psa_status_t( - &rpos, &rremain, - status); - if (!ok) { - goto fail; - } - - *out_params = result; - *out_params_len = result_size; - - free(input); - free(signature); - - return 1; // success - -fail: - free(result); - - free(input); - free(signature); - - return 0; // This shouldn't happen! -} - -psa_status_t psa_crypto_call(psa_msg_t msg) -{ - int ok = 0; - - int func = msg.type; - - /* We only expect a single input buffer, with everything serialised in it */ - if (msg.in_size[1] != 0 || msg.in_size[2] != 0 || msg.in_size[3] != 0) { - return PSA_ERROR_INVALID_ARGUMENT; - } - - /* We expect exactly 2 output buffers, one for size, the other for data */ - if (msg.out_size[0] != sizeof(size_t) || msg.out_size[1] == 0 || - msg.out_size[2] != 0 || msg.out_size[3] != 0) { - return PSA_ERROR_INVALID_ARGUMENT; - } - - uint8_t *in_params = NULL; - size_t in_params_len = 0; - uint8_t *out_params = NULL; - size_t out_params_len = 0; - - in_params_len = msg.in_size[0]; - in_params = malloc(in_params_len); - if (in_params == NULL) { - return PSA_ERROR_INSUFFICIENT_MEMORY; - } - - /* Read the bytes from the client */ - size_t actual = psa_read(msg.handle, 0, in_params, in_params_len); - if (actual != in_params_len) { - free(in_params); - return PSA_ERROR_CORRUPTION_DETECTED; - } - - switch (func) { - case PSA_CRYPTO_INIT: - ok = psa_crypto_init_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_ABORT: - ok = psa_aead_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_DECRYPT: - ok = psa_aead_decrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_DECRYPT_SETUP: - ok = psa_aead_decrypt_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_ENCRYPT: - ok = psa_aead_encrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_ENCRYPT_SETUP: - ok = psa_aead_encrypt_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_FINISH: - ok = psa_aead_finish_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_GENERATE_NONCE: - ok = psa_aead_generate_nonce_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_SET_LENGTHS: - ok = psa_aead_set_lengths_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_SET_NONCE: - ok = psa_aead_set_nonce_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_UPDATE: - ok = psa_aead_update_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_UPDATE_AD: - ok = psa_aead_update_ad_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_AEAD_VERIFY: - ok = psa_aead_verify_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_ASYMMETRIC_DECRYPT: - ok = psa_asymmetric_decrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_ASYMMETRIC_ENCRYPT: - ok = psa_asymmetric_encrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_ABORT: - ok = psa_cipher_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_DECRYPT: - ok = psa_cipher_decrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_DECRYPT_SETUP: - ok = psa_cipher_decrypt_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_ENCRYPT: - ok = psa_cipher_encrypt_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_ENCRYPT_SETUP: - ok = psa_cipher_encrypt_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_FINISH: - ok = psa_cipher_finish_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_GENERATE_IV: - ok = psa_cipher_generate_iv_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_SET_IV: - ok = psa_cipher_set_iv_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_CIPHER_UPDATE: - ok = psa_cipher_update_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_COPY_KEY: - ok = psa_copy_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_DESTROY_KEY: - ok = psa_destroy_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_KEY: - ok = psa_export_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_PUBLIC_KEY: - ok = psa_export_public_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_PUBLIC_KEY_IOP_ABORT: - ok = psa_export_public_key_iop_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_PUBLIC_KEY_IOP_COMPLETE: - ok = psa_export_public_key_iop_complete_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_PUBLIC_KEY_IOP_GET_NUM_OPS: - ok = psa_export_public_key_iop_get_num_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_EXPORT_PUBLIC_KEY_IOP_SETUP: - ok = psa_export_public_key_iop_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY: - ok = psa_generate_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY_CUSTOM: - ok = psa_generate_key_custom_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY_IOP_ABORT: - ok = psa_generate_key_iop_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY_IOP_COMPLETE: - ok = psa_generate_key_iop_complete_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY_IOP_GET_NUM_OPS: - ok = psa_generate_key_iop_get_num_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_KEY_IOP_SETUP: - ok = psa_generate_key_iop_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GENERATE_RANDOM: - ok = psa_generate_random_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_GET_KEY_ATTRIBUTES: - ok = psa_get_key_attributes_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_ABORT: - ok = psa_hash_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_CLONE: - ok = psa_hash_clone_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_COMPARE: - ok = psa_hash_compare_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_COMPUTE: - ok = psa_hash_compute_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_FINISH: - ok = psa_hash_finish_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_SETUP: - ok = psa_hash_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_UPDATE: - ok = psa_hash_update_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_HASH_VERIFY: - ok = psa_hash_verify_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_IMPORT_KEY: - ok = psa_import_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_INTERRUPTIBLE_GET_MAX_OPS: - ok = psa_interruptible_get_max_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_INTERRUPTIBLE_SET_MAX_OPS: - ok = psa_interruptible_set_max_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_AGREEMENT: - ok = psa_key_agreement_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_AGREEMENT_IOP_ABORT: - ok = psa_key_agreement_iop_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_AGREEMENT_IOP_COMPLETE: - ok = psa_key_agreement_iop_complete_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_AGREEMENT_IOP_GET_NUM_OPS: - ok = psa_key_agreement_iop_get_num_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_AGREEMENT_IOP_SETUP: - ok = psa_key_agreement_iop_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_ABORT: - ok = psa_key_derivation_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_GET_CAPACITY: - ok = psa_key_derivation_get_capacity_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_INPUT_BYTES: - ok = psa_key_derivation_input_bytes_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_INPUT_INTEGER: - ok = psa_key_derivation_input_integer_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_INPUT_KEY: - ok = psa_key_derivation_input_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_KEY_AGREEMENT: - ok = psa_key_derivation_key_agreement_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_OUTPUT_BYTES: - ok = psa_key_derivation_output_bytes_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_OUTPUT_KEY: - ok = psa_key_derivation_output_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_OUTPUT_KEY_CUSTOM: - ok = psa_key_derivation_output_key_custom_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_SET_CAPACITY: - ok = psa_key_derivation_set_capacity_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_KEY_DERIVATION_SETUP: - ok = psa_key_derivation_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_ABORT: - ok = psa_mac_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_COMPUTE: - ok = psa_mac_compute_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_SIGN_FINISH: - ok = psa_mac_sign_finish_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_SIGN_SETUP: - ok = psa_mac_sign_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_UPDATE: - ok = psa_mac_update_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_VERIFY: - ok = psa_mac_verify_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_VERIFY_FINISH: - ok = psa_mac_verify_finish_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_MAC_VERIFY_SETUP: - ok = psa_mac_verify_setup_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_PURGE_KEY: - ok = psa_purge_key_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_RAW_KEY_AGREEMENT: - ok = psa_raw_key_agreement_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_RESET_KEY_ATTRIBUTES: - ok = psa_reset_key_attributes_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_HASH: - ok = psa_sign_hash_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_HASH_ABORT: - ok = psa_sign_hash_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_HASH_COMPLETE: - ok = psa_sign_hash_complete_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_HASH_GET_NUM_OPS: - ok = psa_sign_hash_get_num_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_HASH_START: - ok = psa_sign_hash_start_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_SIGN_MESSAGE: - ok = psa_sign_message_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_HASH: - ok = psa_verify_hash_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_HASH_ABORT: - ok = psa_verify_hash_abort_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_HASH_COMPLETE: - ok = psa_verify_hash_complete_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_HASH_GET_NUM_OPS: - ok = psa_verify_hash_get_num_ops_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_HASH_START: - ok = psa_verify_hash_start_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - case PSA_VERIFY_MESSAGE: - ok = psa_verify_message_wrapper(in_params, in_params_len, - &out_params, &out_params_len); - break; - } - - free(in_params); - - if (out_params_len > msg.out_size[1]) { - fprintf(stderr, "unable to write %zu bytes into buffer of %zu bytes\n", - out_params_len, msg.out_size[1]); - exit(1); - } - - /* Write the exact amount of data we're returning */ - psa_write(msg.handle, 0, &out_params_len, sizeof(out_params_len)); - - /* And write the data itself */ - if (out_params_len) { - psa_write(msg.handle, 1, out_params, out_params_len); - } - - free(out_params); - - return ok ? PSA_SUCCESS : PSA_ERROR_GENERIC_ERROR; -} - -void psa_crypto_close(void) -{ - psa_sim_serialize_reset(); -} diff --git a/psasim/src/psa_sim_serialise.c b/psasim/src/psa_sim_serialise.c deleted file mode 100644 index 0dde934ad..000000000 --- a/psasim/src/psa_sim_serialise.c +++ /dev/null @@ -1,1765 +0,0 @@ -/** - * \file psa_sim_serialise.c - * - * \brief Rough-and-ready serialisation and deserialisation for the PSA Crypto simulator - */ - -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#include "psa_sim_serialise.h" -#include "util.h" -#include -#include - -/* Basic idea: - * - * All arguments to a function will be serialised into a single buffer to - * be sent to the server with the PSA crypto function to be called. - * - * All returned data (the function's return value and any values returned - * via `out` parameters) will similarly be serialised into a buffer to be - * sent back to the client from the server. - * - * For each data type foo (e.g. int, size_t, psa_algorithm_t, but also "buffer" - * where "buffer" is a (uint8_t *, size_t) pair, we have a pair of functions, - * psasim_serialise_foo() and psasim_deserialise_foo(). - * - * We also have psasim_serialise_foo_needs() functions, which return a - * size_t giving the number of bytes that serialising that instance of that - * type will need. This allows callers to size buffers for serialisation. - * - * Each serialised buffer starts with a version byte, bytes that indicate - * the size of basic C types, and four bytes that indicate the endianness - * (to avoid incompatibilities if we ever run this over a network - we are - * not aiming for universality, just for correctness and simplicity). - * - * Most types are serialised as a fixed-size (per type) octet string, with - * no type indication. This is acceptable as (a) this is for the test PSA crypto - * simulator only, not production, and (b) these functions are called by - * code that itself is written by script. - * - * We also want to keep serialised data reasonably compact as communication - * between client and server goes in messages of less than 200 bytes each. - * - * Many serialisation functions can be created by a script; an exemplar Perl - * script is included. It is not hooked into the build and so must be run - * manually, but is expected to be replaced by a Python script in due course. - * Types that can have their functions created by script include plain old C - * data types (e.g. int), types typedef'd to those, and even structures that - * don't contain pointers. - */ - -/* include/psa/crypto_platform.h:typedef uint32_t mbedtls_psa_client_handle_t; - * but we don't get it on server builds, so redefine it here with a unique type name - */ -typedef uint32_t psasim_client_handle_t; - -typedef struct psasim_operation_s { - psasim_client_handle_t handle; -} psasim_operation_t; - -#define MAX_LIVE_HANDLES_PER_CLASS 100 /* this many slots */ - -static psa_hash_operation_t hash_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t hash_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_hash_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_hash_operation_slot(void) -{ - psasim_client_handle_t handle = next_hash_operation_handle++; - if (next_hash_operation_handle == 0) { /* wrapped around */ - FATAL("Hash operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (hash_operation_handles[i] == 0) { - hash_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_hash_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (hash_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_aead_operation_t aead_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t aead_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_aead_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_aead_operation_slot(void) -{ - psasim_client_handle_t handle = next_aead_operation_handle++; - if (next_aead_operation_handle == 0) { /* wrapped around */ - FATAL("Aead operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (aead_operation_handles[i] == 0) { - aead_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_aead_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (aead_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_mac_operation_t mac_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t mac_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_mac_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_mac_operation_slot(void) -{ - psasim_client_handle_t handle = next_mac_operation_handle++; - if (next_mac_operation_handle == 0) { /* wrapped around */ - FATAL("Mac operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (mac_operation_handles[i] == 0) { - mac_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_mac_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (mac_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_cipher_operation_t cipher_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t cipher_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_cipher_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_cipher_operation_slot(void) -{ - psasim_client_handle_t handle = next_cipher_operation_handle++; - if (next_cipher_operation_handle == 0) { /* wrapped around */ - FATAL("Cipher operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (cipher_operation_handles[i] == 0) { - cipher_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_cipher_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (cipher_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_key_derivation_operation_t key_derivation_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t key_derivation_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_key_derivation_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_key_derivation_operation_slot(void) -{ - psasim_client_handle_t handle = next_key_derivation_operation_handle++; - if (next_key_derivation_operation_handle == 0) { /* wrapped around */ - FATAL("Key_derivation operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (key_derivation_operation_handles[i] == 0) { - key_derivation_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_key_derivation_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (key_derivation_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_sign_hash_interruptible_operation_t sign_hash_interruptible_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t sign_hash_interruptible_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_sign_hash_interruptible_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_sign_hash_interruptible_operation_slot(void) -{ - psasim_client_handle_t handle = next_sign_hash_interruptible_operation_handle++; - if (next_sign_hash_interruptible_operation_handle == 0) { /* wrapped around */ - FATAL("Sign_hash_interruptible operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (sign_hash_interruptible_operation_handles[i] == 0) { - sign_hash_interruptible_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_sign_hash_interruptible_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (sign_hash_interruptible_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -static psa_verify_hash_interruptible_operation_t verify_hash_interruptible_operations[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t verify_hash_interruptible_operation_handles[ - MAX_LIVE_HANDLES_PER_CLASS]; -static psasim_client_handle_t next_verify_hash_interruptible_operation_handle = 1; - -/* Get a free slot */ -static ssize_t allocate_verify_hash_interruptible_operation_slot(void) -{ - psasim_client_handle_t handle = next_verify_hash_interruptible_operation_handle++; - if (next_verify_hash_interruptible_operation_handle == 0) { /* wrapped around */ - FATAL("Verify_hash_interruptible operation handle wrapped"); - } - - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (verify_hash_interruptible_operation_handles[i] == 0) { - verify_hash_interruptible_operation_handles[i] = handle; - return i; - } - } - - ERROR("All slots are currently used. Unable to allocate a new one."); - - return -1; /* all in use */ -} - -/* Find the slot given the handle */ -static ssize_t find_verify_hash_interruptible_slot_by_handle(psasim_client_handle_t handle) -{ - for (ssize_t i = 0; i < MAX_LIVE_HANDLES_PER_CLASS; i++) { - if (verify_hash_interruptible_operation_handles[i] == handle) { - return i; - } - } - - ERROR("Unable to find slot by handle %u", handle); - - return -1; /* not found */ -} - -size_t psasim_serialise_begin_needs(void) -{ - /* The serialisation buffer will - * start with a byte of 0 to indicate version 0, - * then have 1 byte each for length of int, long, void *, - * then have 4 bytes to indicate endianness. */ - return 4 + sizeof(uint32_t); -} - -int psasim_serialise_begin(uint8_t **pos, size_t *remaining) -{ - uint32_t endian = 0x1234; - - if (*remaining < 4 + sizeof(endian)) { - return 0; - } - - *(*pos)++ = 0; /* version */ - *(*pos)++ = (uint8_t) sizeof(int); - *(*pos)++ = (uint8_t) sizeof(long); - *(*pos)++ = (uint8_t) sizeof(void *); - - memcpy(*pos, &endian, sizeof(endian)); - - *pos += sizeof(endian); - - return 1; -} - -int psasim_deserialise_begin(uint8_t **pos, size_t *remaining) -{ - uint8_t version = 255; - uint8_t int_size = 0; - uint8_t long_size = 0; - uint8_t ptr_size = 0; - uint32_t endian; - - if (*remaining < 4 + sizeof(endian)) { - return 0; - } - - memcpy(&version, (*pos)++, sizeof(version)); - if (version != 0) { - return 0; - } - - memcpy(&int_size, (*pos)++, sizeof(int_size)); - if (int_size != sizeof(int)) { - return 0; - } - - memcpy(&long_size, (*pos)++, sizeof(long_size)); - if (long_size != sizeof(long)) { - return 0; - } - - memcpy(&ptr_size, (*pos)++, sizeof(ptr_size)); - if (ptr_size != sizeof(void *)) { - return 0; - } - - *remaining -= 4; - - memcpy(&endian, *pos, sizeof(endian)); - if (endian != 0x1234) { - return 0; - } - - *pos += sizeof(endian); - *remaining -= sizeof(endian); - - return 1; -} - -size_t psasim_serialise_unsigned_int_needs( - unsigned int value) -{ - return sizeof(value); -} - -int psasim_serialise_unsigned_int(uint8_t **pos, - size_t *remaining, - unsigned int value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_unsigned_int(uint8_t **pos, - size_t *remaining, - unsigned int *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_int_needs( - int value) -{ - return sizeof(value); -} - -int psasim_serialise_int(uint8_t **pos, - size_t *remaining, - int value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_int(uint8_t **pos, - size_t *remaining, - int *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_size_t_needs( - size_t value) -{ - return sizeof(value); -} - -int psasim_serialise_size_t(uint8_t **pos, - size_t *remaining, - size_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_size_t(uint8_t **pos, - size_t *remaining, - size_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_uint16_t_needs( - uint16_t value) -{ - return sizeof(value); -} - -int psasim_serialise_uint16_t(uint8_t **pos, - size_t *remaining, - uint16_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_uint16_t(uint8_t **pos, - size_t *remaining, - uint16_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_uint32_t_needs( - uint32_t value) -{ - return sizeof(value); -} - -int psasim_serialise_uint32_t(uint8_t **pos, - size_t *remaining, - uint32_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_uint32_t(uint8_t **pos, - size_t *remaining, - uint32_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_uint64_t_needs( - uint64_t value) -{ - return sizeof(value); -} - -int psasim_serialise_uint64_t(uint8_t **pos, - size_t *remaining, - uint64_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_uint64_t(uint8_t **pos, - size_t *remaining, - uint64_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_buffer_needs(const uint8_t *buffer, size_t buffer_size) -{ - (void) buffer; - return sizeof(buffer_size) + buffer_size; -} - -int psasim_serialise_buffer(uint8_t **pos, - size_t *remaining, - const uint8_t *buffer, - size_t buffer_length) -{ - if (*remaining < sizeof(buffer_length) + buffer_length) { - return 0; - } - - memcpy(*pos, &buffer_length, sizeof(buffer_length)); - *pos += sizeof(buffer_length); - - if (buffer_length > 0) { // To be able to serialise (NULL, 0) - memcpy(*pos, buffer, buffer_length); - *pos += buffer_length; - } - - return 1; -} - -int psasim_deserialise_buffer(uint8_t **pos, - size_t *remaining, - uint8_t **buffer, - size_t *buffer_length) -{ - if (*remaining < sizeof(*buffer_length)) { - return 0; - } - - memcpy(buffer_length, *pos, sizeof(*buffer_length)); - - *pos += sizeof(buffer_length); - *remaining -= sizeof(buffer_length); - - if (*buffer_length == 0) { // Deserialise (NULL, 0) - *buffer = NULL; - return 1; - } - - if (*remaining < *buffer_length) { - return 0; - } - - uint8_t *data = malloc(*buffer_length); - if (data == NULL) { - return 0; - } - - memcpy(data, *pos, *buffer_length); - *pos += *buffer_length; - *remaining -= *buffer_length; - - *buffer = data; - - return 1; -} - -/* When the client is deserialising a buffer returned from the server, it needs - * to use this function to deserialised the returned buffer. It should use the - * usual \c psasim_serialise_buffer() function to serialise the outbound - * buffer. */ -int psasim_deserialise_return_buffer(uint8_t **pos, - size_t *remaining, - uint8_t *buffer, - size_t buffer_length) -{ - if (*remaining < sizeof(buffer_length)) { - return 0; - } - - size_t length_check; - - memcpy(&length_check, *pos, sizeof(buffer_length)); - - *pos += sizeof(buffer_length); - *remaining -= sizeof(buffer_length); - - if (buffer_length != length_check) { // Make sure we're sent back the same we sent to the server - return 0; - } - - if (length_check == 0) { // Deserialise (NULL, 0) - return 1; - } - - if (*remaining < buffer_length) { - return 0; - } - - memcpy(buffer, *pos, buffer_length); - *pos += buffer_length; - *remaining -= buffer_length; - - return 1; -} - -size_t psasim_serialise_psa_custom_key_parameters_t_needs( - psa_custom_key_parameters_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_custom_key_parameters_t(uint8_t **pos, - size_t *remaining, - psa_custom_key_parameters_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_custom_key_parameters_t(uint8_t **pos, - size_t *remaining, - psa_custom_key_parameters_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_psa_status_t_needs( - psa_status_t value) -{ - return psasim_serialise_int_needs(value); -} - -int psasim_serialise_psa_status_t(uint8_t **pos, - size_t *remaining, - psa_status_t value) -{ - return psasim_serialise_int(pos, remaining, value); -} - -int psasim_deserialise_psa_status_t(uint8_t **pos, - size_t *remaining, - psa_status_t *value) -{ - return psasim_deserialise_int(pos, remaining, value); -} - -size_t psasim_serialise_psa_algorithm_t_needs( - psa_algorithm_t value) -{ - return psasim_serialise_unsigned_int_needs(value); -} - -int psasim_serialise_psa_algorithm_t(uint8_t **pos, - size_t *remaining, - psa_algorithm_t value) -{ - return psasim_serialise_unsigned_int(pos, remaining, value); -} - -int psasim_deserialise_psa_algorithm_t(uint8_t **pos, - size_t *remaining, - psa_algorithm_t *value) -{ - return psasim_deserialise_unsigned_int(pos, remaining, value); -} - -size_t psasim_serialise_psa_key_derivation_step_t_needs( - psa_key_derivation_step_t value) -{ - return psasim_serialise_uint16_t_needs(value); -} - -int psasim_serialise_psa_key_derivation_step_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_step_t value) -{ - return psasim_serialise_uint16_t(pos, remaining, value); -} - -int psasim_deserialise_psa_key_derivation_step_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_step_t *value) -{ - return psasim_deserialise_uint16_t(pos, remaining, value); -} - -size_t psasim_serialise_psa_hash_operation_t_needs( - psa_hash_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_hash_operation_t_needs( - psa_hash_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - hash_operations; - - if (completed) { - memset(&hash_operations[slot], - 0, - sizeof(psa_hash_operation_t)); - hash_operation_handles[slot] = 0; - } - - client_operation.handle = hash_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_hash_operation_slot(); - } else { - slot = find_hash_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &hash_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_aead_operation_t_needs( - psa_aead_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_aead_operation_t_needs( - psa_aead_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - aead_operations; - - if (completed) { - memset(&aead_operations[slot], - 0, - sizeof(psa_aead_operation_t)); - aead_operation_handles[slot] = 0; - } - - client_operation.handle = aead_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_aead_operation_slot(); - } else { - slot = find_aead_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &aead_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_key_attributes_t_needs( - psa_key_attributes_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_key_attributes_t(uint8_t **pos, - size_t *remaining, - psa_key_attributes_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_key_attributes_t(uint8_t **pos, - size_t *remaining, - psa_key_attributes_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_psa_mac_operation_t_needs( - psa_mac_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_mac_operation_t_needs( - psa_mac_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - mac_operations; - - if (completed) { - memset(&mac_operations[slot], - 0, - sizeof(psa_mac_operation_t)); - mac_operation_handles[slot] = 0; - } - - client_operation.handle = mac_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_mac_operation_slot(); - } else { - slot = find_mac_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &mac_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_cipher_operation_t_needs( - psa_cipher_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_cipher_operation_t_needs( - psa_cipher_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - cipher_operations; - - if (completed) { - memset(&cipher_operations[slot], - 0, - sizeof(psa_cipher_operation_t)); - cipher_operation_handles[slot] = 0; - } - - client_operation.handle = cipher_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_cipher_operation_slot(); - } else { - slot = find_cipher_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &cipher_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_key_derivation_operation_t_needs( - psa_key_derivation_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_key_derivation_operation_t_needs( - psa_key_derivation_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - key_derivation_operations; - - if (completed) { - memset(&key_derivation_operations[slot], - 0, - sizeof(psa_key_derivation_operation_t)); - key_derivation_operation_handles[slot] = 0; - } - - client_operation.handle = key_derivation_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_key_derivation_operation_slot(); - } else { - slot = find_key_derivation_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &key_derivation_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_sign_hash_interruptible_operation_t_needs( - psa_sign_hash_interruptible_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_sign_hash_interruptible_operation_t_needs( - psa_sign_hash_interruptible_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - sign_hash_interruptible_operations; - - if (completed) { - memset(&sign_hash_interruptible_operations[slot], - 0, - sizeof(psa_sign_hash_interruptible_operation_t)); - sign_hash_interruptible_operation_handles[slot] = 0; - } - - client_operation.handle = sign_hash_interruptible_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_sign_hash_interruptible_operation_slot(); - } else { - slot = find_sign_hash_interruptible_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &sign_hash_interruptible_operations[slot]; - - return 1; -} - -size_t psasim_serialise_psa_verify_hash_interruptible_operation_t_needs( - psa_verify_hash_interruptible_operation_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_server_serialise_psa_verify_hash_interruptible_operation_t_needs( - psa_verify_hash_interruptible_operation_t *operation) -{ - (void) operation; - - /* We will actually return a handle */ - return sizeof(psasim_operation_t); -} - -int psasim_server_serialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t *operation, - int completed) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(client_operation)) { - return 0; - } - - ssize_t slot = operation - verify_hash_interruptible_operations; - - if (completed) { - memset(&verify_hash_interruptible_operations[slot], - 0, - sizeof(psa_verify_hash_interruptible_operation_t)); - verify_hash_interruptible_operation_handles[slot] = 0; - } - - client_operation.handle = verify_hash_interruptible_operation_handles[slot]; - - memcpy(*pos, &client_operation, sizeof(client_operation)); - *pos += sizeof(client_operation); - - return 1; -} - -int psasim_server_deserialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t **operation) -{ - psasim_operation_t client_operation; - - if (*remaining < sizeof(psasim_operation_t)) { - return 0; - } - - memcpy(&client_operation, *pos, sizeof(psasim_operation_t)); - *pos += sizeof(psasim_operation_t); - *remaining -= sizeof(psasim_operation_t); - - ssize_t slot; - if (client_operation.handle == 0) { /* We need a new handle */ - slot = allocate_verify_hash_interruptible_operation_slot(); - } else { - slot = find_verify_hash_interruptible_slot_by_handle(client_operation.handle); - } - - if (slot < 0) { - return 0; - } - - *operation = &verify_hash_interruptible_operations[slot]; - - return 1; -} - -size_t psasim_serialise_mbedtls_svc_key_id_t_needs( - mbedtls_svc_key_id_t value) -{ - return sizeof(value); -} - -int psasim_serialise_mbedtls_svc_key_id_t(uint8_t **pos, - size_t *remaining, - mbedtls_svc_key_id_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_mbedtls_svc_key_id_t(uint8_t **pos, - size_t *remaining, - mbedtls_svc_key_id_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_psa_key_agreement_iop_t_needs( - psa_key_agreement_iop_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_key_agreement_iop_t(uint8_t **pos, - size_t *remaining, - psa_key_agreement_iop_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_key_agreement_iop_t(uint8_t **pos, - size_t *remaining, - psa_key_agreement_iop_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_psa_generate_key_iop_t_needs( - psa_generate_key_iop_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_generate_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_generate_key_iop_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_generate_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_generate_key_iop_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -size_t psasim_serialise_psa_export_public_key_iop_t_needs( - psa_export_public_key_iop_t value) -{ - return sizeof(value); -} - -int psasim_serialise_psa_export_public_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_export_public_key_iop_t value) -{ - if (*remaining < sizeof(value)) { - return 0; - } - - memcpy(*pos, &value, sizeof(value)); - *pos += sizeof(value); - - return 1; -} - -int psasim_deserialise_psa_export_public_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_export_public_key_iop_t *value) -{ - if (*remaining < sizeof(*value)) { - return 0; - } - - memcpy(value, *pos, sizeof(*value)); - - *pos += sizeof(*value); - *remaining -= sizeof(*value); - - return 1; -} - -void psa_sim_serialize_reset(void) -{ - memset(hash_operation_handles, 0, - sizeof(hash_operation_handles)); - memset(hash_operations, 0, - sizeof(hash_operations)); - memset(aead_operation_handles, 0, - sizeof(aead_operation_handles)); - memset(aead_operations, 0, - sizeof(aead_operations)); - memset(mac_operation_handles, 0, - sizeof(mac_operation_handles)); - memset(mac_operations, 0, - sizeof(mac_operations)); - memset(cipher_operation_handles, 0, - sizeof(cipher_operation_handles)); - memset(cipher_operations, 0, - sizeof(cipher_operations)); - memset(key_derivation_operation_handles, 0, - sizeof(key_derivation_operation_handles)); - memset(key_derivation_operations, 0, - sizeof(key_derivation_operations)); - memset(sign_hash_interruptible_operation_handles, 0, - sizeof(sign_hash_interruptible_operation_handles)); - memset(sign_hash_interruptible_operations, 0, - sizeof(sign_hash_interruptible_operations)); - memset(verify_hash_interruptible_operation_handles, 0, - sizeof(verify_hash_interruptible_operation_handles)); - memset(verify_hash_interruptible_operations, 0, - sizeof(verify_hash_interruptible_operations)); -} diff --git a/psasim/src/psa_sim_serialise.h b/psasim/src/psa_sim_serialise.h deleted file mode 100644 index 3b6f08e19..000000000 --- a/psasim/src/psa_sim_serialise.h +++ /dev/null @@ -1,1432 +0,0 @@ -/** - * \file psa_sim_serialise.h - * - * \brief Rough-and-ready serialisation and deserialisation for the PSA Crypto simulator - */ - -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#include -#include - -#include "psa/crypto.h" -#include "psa/crypto_types.h" -#include "psa/crypto_values.h" - -/* Basic idea: - * - * All arguments to a function will be serialised into a single buffer to - * be sent to the server with the PSA crypto function to be called. - * - * All returned data (the function's return value and any values returned - * via `out` parameters) will similarly be serialised into a buffer to be - * sent back to the client from the server. - * - * For each data type foo (e.g. int, size_t, psa_algorithm_t, but also "buffer" - * where "buffer" is a (uint8_t *, size_t) pair, we have a pair of functions, - * psasim_serialise_foo() and psasim_deserialise_foo(). - * - * We also have psasim_serialise_foo_needs() functions, which return a - * size_t giving the number of bytes that serialising that instance of that - * type will need. This allows callers to size buffers for serialisation. - * - * Each serialised buffer starts with a version byte, bytes that indicate - * the size of basic C types, and four bytes that indicate the endianness - * (to avoid incompatibilities if we ever run this over a network - we are - * not aiming for universality, just for correctness and simplicity). - * - * Most types are serialised as a fixed-size (per type) octet string, with - * no type indication. This is acceptable as (a) this is for the test PSA crypto - * simulator only, not production, and (b) these functions are called by - * code that itself is written by script. - * - * We also want to keep serialised data reasonably compact as communication - * between client and server goes in messages of less than 200 bytes each. - * - * Many serialisation functions can be created by a script; an exemplar Perl - * script is included. It is not hooked into the build and so must be run - * manually, but is expected to be replaced by a Python script in due course. - * Types that can have their functions created by script include plain old C - * data types (e.g. int), types typedef'd to those, and even structures that - * don't contain pointers. - */ - -/** Reset all operation slots. - * - * Should be called when all clients have disconnected. - */ -void psa_sim_serialize_reset(void); - -/** Return how much buffer space is needed by \c psasim_serialise_begin(). - * - * \return The number of bytes needed in the buffer for - * \c psasim_serialise_begin()'s output. - */ -size_t psasim_serialise_begin_needs(void); - -/** Begin serialisation into a buffer. - * - * This must be the first serialisation API called - * on a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error (likely - * no space). - */ -int psasim_serialise_begin(uint8_t **pos, size_t *remaining); - -/** Begin deserialisation of a buffer. - * - * This must be the first deserialisation API called - * on a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_begin(uint8_t **pos, size_t *remaining); - -/** Return how much buffer space is needed by \c psasim_serialise_unsigned_int() - * to serialise an `unsigned int`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_unsigned_int() to serialise - * the given value. - */ -size_t psasim_serialise_unsigned_int_needs( - unsigned int value); - -/** Serialise an `unsigned int` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_unsigned_int(uint8_t **pos, - size_t *remaining, - unsigned int value); - -/** Deserialise an `unsigned int` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to an `unsigned int` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_unsigned_int(uint8_t **pos, - size_t *remaining, - unsigned int *value); - -/** Return how much buffer space is needed by \c psasim_serialise_int() - * to serialise an `int`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_int() to serialise - * the given value. - */ -size_t psasim_serialise_int_needs( - int value); - -/** Serialise an `int` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_int(uint8_t **pos, - size_t *remaining, - int value); - -/** Deserialise an `int` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to an `int` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_int(uint8_t **pos, - size_t *remaining, - int *value); - -/** Return how much buffer space is needed by \c psasim_serialise_size_t() - * to serialise a `size_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_size_t() to serialise - * the given value. - */ -size_t psasim_serialise_size_t_needs( - size_t value); - -/** Serialise a `size_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_size_t(uint8_t **pos, - size_t *remaining, - size_t value); - -/** Deserialise a `size_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `size_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_size_t(uint8_t **pos, - size_t *remaining, - size_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_uint16_t() - * to serialise an `uint16_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_uint16_t() to serialise - * the given value. - */ -size_t psasim_serialise_uint16_t_needs( - uint16_t value); - -/** Serialise an `uint16_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_uint16_t(uint8_t **pos, - size_t *remaining, - uint16_t value); - -/** Deserialise an `uint16_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to an `uint16_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_uint16_t(uint8_t **pos, - size_t *remaining, - uint16_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_uint32_t() - * to serialise an `uint32_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_uint32_t() to serialise - * the given value. - */ -size_t psasim_serialise_uint32_t_needs( - uint32_t value); - -/** Serialise an `uint32_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_uint32_t(uint8_t **pos, - size_t *remaining, - uint32_t value); - -/** Deserialise an `uint32_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to an `uint32_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_uint32_t(uint8_t **pos, - size_t *remaining, - uint32_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_uint64_t() - * to serialise an `uint64_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_uint64_t() to serialise - * the given value. - */ -size_t psasim_serialise_uint64_t_needs( - uint64_t value); - -/** Serialise an `uint64_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_uint64_t(uint8_t **pos, - size_t *remaining, - uint64_t value); - -/** Deserialise an `uint64_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to an `uint64_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_uint64_t(uint8_t **pos, - size_t *remaining, - uint64_t *value); - -/** Return how much space is needed by \c psasim_serialise_buffer() - * to serialise a buffer: a (`uint8_t *`, `size_t`) pair. - * - * \param buffer Pointer to the buffer to be serialised - * (needed in case some serialisations are value- - * dependent). - * \param buffer_size Number of bytes in the buffer to be serialised. - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_buffer() to serialise - * the specified buffer. - */ -size_t psasim_serialise_buffer_needs(const uint8_t *buffer, size_t buffer_size); - -/** Serialise a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param buffer Pointer to the buffer to be serialised. - * \param buffer_length Number of bytes in the buffer to be serialised. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_buffer(uint8_t **pos, size_t *remaining, - const uint8_t *buffer, size_t buffer_length); - -/** Deserialise a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the serialisation buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the serialisation buffer. - * \param buffer Pointer to a `uint8_t *` to receive the address - * of a newly-allocated buffer, which the caller - * must `free()`. - * \param buffer_length Pointer to a `size_t` to receive the number of - * bytes in the deserialised buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_buffer(uint8_t **pos, size_t *remaining, - uint8_t **buffer, size_t *buffer_length); - -/** Deserialise a buffer returned from the server. - * - * When the client is deserialising a buffer returned from the server, it needs - * to use this function to deserialised the returned buffer. It should use the - * usual \c psasim_serialise_buffer() function to serialise the outbound - * buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the serialisation buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the serialisation buffer. - * \param buffer Pointer to a `uint8_t *` to receive the address - * of a newly-allocated buffer, which the caller - * must `free()`. - * \param buffer_length Pointer to a `size_t` to receive the number of - * bytes in the deserialised buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_return_buffer(uint8_t **pos, size_t *remaining, - uint8_t *buffer, size_t buffer_length); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_custom_key_parameters_t() - * to serialise a `psa_custom_key_parameters_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_custom_key_parameters_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_custom_key_parameters_t_needs( - psa_custom_key_parameters_t value); - -/** Serialise a `psa_custom_key_parameters_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_custom_key_parameters_t(uint8_t **pos, - size_t *remaining, - psa_custom_key_parameters_t value); - -/** Deserialise a `psa_custom_key_parameters_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_custom_key_parameters_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_custom_key_parameters_t(uint8_t **pos, - size_t *remaining, - psa_custom_key_parameters_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_status_t() - * to serialise a `psa_status_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_status_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_status_t_needs( - psa_status_t value); - -/** Serialise a `psa_status_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_status_t(uint8_t **pos, - size_t *remaining, - psa_status_t value); - -/** Deserialise a `psa_status_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_status_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_status_t(uint8_t **pos, - size_t *remaining, - psa_status_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_algorithm_t() - * to serialise a `psa_algorithm_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_algorithm_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_algorithm_t_needs( - psa_algorithm_t value); - -/** Serialise a `psa_algorithm_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_algorithm_t(uint8_t **pos, - size_t *remaining, - psa_algorithm_t value); - -/** Deserialise a `psa_algorithm_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_algorithm_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_algorithm_t(uint8_t **pos, - size_t *remaining, - psa_algorithm_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_key_derivation_step_t() - * to serialise a `psa_key_derivation_step_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_key_derivation_step_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_key_derivation_step_t_needs( - psa_key_derivation_step_t value); - -/** Serialise a `psa_key_derivation_step_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_key_derivation_step_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_step_t value); - -/** Deserialise a `psa_key_derivation_step_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_key_derivation_step_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_key_derivation_step_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_step_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_hash_operation_t() - * to serialise a `psa_hash_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_hash_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_hash_operation_t_needs( - psa_hash_operation_t value); - -/** Serialise a `psa_hash_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t value); - -/** Deserialise a `psa_hash_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_hash_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_hash_operation_t() - * to serialise a `psa_hash_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_hash_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_hash_operation_t_needs( - psa_hash_operation_t *value); - -/** Serialise a `psa_hash_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t *value, - int completed); - -/** Deserialise a `psa_hash_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_hash_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_hash_operation_t(uint8_t **pos, - size_t *remaining, - psa_hash_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_aead_operation_t() - * to serialise a `psa_aead_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_aead_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_aead_operation_t_needs( - psa_aead_operation_t value); - -/** Serialise a `psa_aead_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t value); - -/** Deserialise a `psa_aead_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_aead_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_aead_operation_t() - * to serialise a `psa_aead_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_aead_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_aead_operation_t_needs( - psa_aead_operation_t *value); - -/** Serialise a `psa_aead_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t *value, - int completed); - -/** Deserialise a `psa_aead_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_aead_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_aead_operation_t(uint8_t **pos, - size_t *remaining, - psa_aead_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_key_attributes_t() - * to serialise a `psa_key_attributes_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_key_attributes_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_key_attributes_t_needs( - psa_key_attributes_t value); - -/** Serialise a `psa_key_attributes_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_key_attributes_t(uint8_t **pos, - size_t *remaining, - psa_key_attributes_t value); - -/** Deserialise a `psa_key_attributes_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_key_attributes_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_key_attributes_t(uint8_t **pos, - size_t *remaining, - psa_key_attributes_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_mac_operation_t() - * to serialise a `psa_mac_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_mac_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_mac_operation_t_needs( - psa_mac_operation_t value); - -/** Serialise a `psa_mac_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t value); - -/** Deserialise a `psa_mac_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_mac_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_mac_operation_t() - * to serialise a `psa_mac_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_mac_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_mac_operation_t_needs( - psa_mac_operation_t *value); - -/** Serialise a `psa_mac_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t *value, - int completed); - -/** Deserialise a `psa_mac_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_mac_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_mac_operation_t(uint8_t **pos, - size_t *remaining, - psa_mac_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_cipher_operation_t() - * to serialise a `psa_cipher_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_cipher_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_cipher_operation_t_needs( - psa_cipher_operation_t value); - -/** Serialise a `psa_cipher_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t value); - -/** Deserialise a `psa_cipher_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_cipher_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_cipher_operation_t() - * to serialise a `psa_cipher_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_cipher_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_cipher_operation_t_needs( - psa_cipher_operation_t *value); - -/** Serialise a `psa_cipher_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t *value, - int completed); - -/** Deserialise a `psa_cipher_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_cipher_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_cipher_operation_t(uint8_t **pos, - size_t *remaining, - psa_cipher_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_key_derivation_operation_t() - * to serialise a `psa_key_derivation_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_key_derivation_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_key_derivation_operation_t_needs( - psa_key_derivation_operation_t value); - -/** Serialise a `psa_key_derivation_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t value); - -/** Deserialise a `psa_key_derivation_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_key_derivation_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_key_derivation_operation_t() - * to serialise a `psa_key_derivation_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_key_derivation_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_key_derivation_operation_t_needs( - psa_key_derivation_operation_t *value); - -/** Serialise a `psa_key_derivation_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t *value, - int completed); - -/** Deserialise a `psa_key_derivation_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_key_derivation_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_key_derivation_operation_t(uint8_t **pos, - size_t *remaining, - psa_key_derivation_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_sign_hash_interruptible_operation_t() - * to serialise a `psa_sign_hash_interruptible_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_sign_hash_interruptible_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_sign_hash_interruptible_operation_t_needs( - psa_sign_hash_interruptible_operation_t value); - -/** Serialise a `psa_sign_hash_interruptible_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t value); - -/** Deserialise a `psa_sign_hash_interruptible_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_sign_hash_interruptible_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_sign_hash_interruptible_operation_t() - * to serialise a `psa_sign_hash_interruptible_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_sign_hash_interruptible_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_sign_hash_interruptible_operation_t_needs( - psa_sign_hash_interruptible_operation_t *value); - -/** Serialise a `psa_sign_hash_interruptible_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t *value, - int completed); - -/** Deserialise a `psa_sign_hash_interruptible_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_sign_hash_interruptible_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_sign_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_sign_hash_interruptible_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_verify_hash_interruptible_operation_t() - * to serialise a `psa_verify_hash_interruptible_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_verify_hash_interruptible_operation_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_verify_hash_interruptible_operation_t_needs( - psa_verify_hash_interruptible_operation_t value); - -/** Serialise a `psa_verify_hash_interruptible_operation_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t value); - -/** Deserialise a `psa_verify_hash_interruptible_operation_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_verify_hash_interruptible_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t *value); - -/** Return how much buffer space is needed by \c psasim_server_serialise_psa_verify_hash_interruptible_operation_t() - * to serialise a `psa_verify_hash_interruptible_operation_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_verify_hash_interruptible_operation_t() to serialise - * the given value. - */ -size_t psasim_server_serialise_psa_verify_hash_interruptible_operation_t_needs( - psa_verify_hash_interruptible_operation_t *value); - -/** Serialise a `psa_verify_hash_interruptible_operation_t` into a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * \param completed Non-zero if the operation is now completed (set by - * finish and abort calls). - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_serialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t *value, - int completed); - -/** Deserialise a `psa_verify_hash_interruptible_operation_t` from a buffer on the server side. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_verify_hash_interruptible_operation_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_server_deserialise_psa_verify_hash_interruptible_operation_t(uint8_t **pos, - size_t *remaining, - psa_verify_hash_interruptible_operation_t **value); - -/** Return how much buffer space is needed by \c psasim_serialise_mbedtls_svc_key_id_t() - * to serialise a `mbedtls_svc_key_id_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_mbedtls_svc_key_id_t() to serialise - * the given value. - */ -size_t psasim_serialise_mbedtls_svc_key_id_t_needs( - mbedtls_svc_key_id_t value); - -/** Serialise a `mbedtls_svc_key_id_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_mbedtls_svc_key_id_t(uint8_t **pos, - size_t *remaining, - mbedtls_svc_key_id_t value); - -/** Deserialise a `mbedtls_svc_key_id_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `mbedtls_svc_key_id_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_mbedtls_svc_key_id_t(uint8_t **pos, - size_t *remaining, - mbedtls_svc_key_id_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_key_agreement_iop_t() - * to serialise a `psa_key_agreement_iop_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_key_agreement_iop_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_key_agreement_iop_t_needs( - psa_key_agreement_iop_t value); - -/** Serialise a `psa_key_agreement_iop_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_key_agreement_iop_t(uint8_t **pos, - size_t *remaining, - psa_key_agreement_iop_t value); - -/** Deserialise a `psa_key_agreement_iop_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_key_agreement_iop_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_key_agreement_iop_t(uint8_t **pos, - size_t *remaining, - psa_key_agreement_iop_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_generate_key_iop_t() - * to serialise a `psa_generate_key_iop_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_generate_key_iop_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_generate_key_iop_t_needs( - psa_generate_key_iop_t value); - -/** Serialise a `psa_generate_key_iop_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_generate_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_generate_key_iop_t value); - -/** Deserialise a `psa_generate_key_iop_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_generate_key_iop_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_generate_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_generate_key_iop_t *value); - -/** Return how much buffer space is needed by \c psasim_serialise_psa_export_public_key_iop_t() - * to serialise a `psa_export_public_key_iop_t`. - * - * \param value The value that will be serialised into the buffer - * (needed in case some serialisations are value- - * dependent). - * - * \return The number of bytes needed in the buffer by - * \c psasim_serialise_psa_export_public_key_iop_t() to serialise - * the given value. - */ -size_t psasim_serialise_psa_export_public_key_iop_t_needs( - psa_export_public_key_iop_t value); - -/** Serialise a `psa_export_public_key_iop_t` into a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value The value to serialise into the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_serialise_psa_export_public_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_export_public_key_iop_t value); - -/** Deserialise a `psa_export_public_key_iop_t` from a buffer. - * - * \param pos[in,out] Pointer to a `uint8_t *` holding current position - * in the buffer. - * \param remaining[in,out] Pointer to a `size_t` holding number of bytes - * remaining in the buffer. - * \param value Pointer to a `psa_export_public_key_iop_t` to receive the value - * deserialised from the buffer. - * - * \return \c 1 on success ("okay"), \c 0 on error. - */ -int psasim_deserialise_psa_export_public_key_iop_t(uint8_t **pos, - size_t *remaining, - psa_export_public_key_iop_t *value); From 3a69598ade6596d86d031ef74dd4afbfb61cb2a1 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 14:03:57 +0100 Subject: [PATCH 5/7] Clean up psasim/.gitignore Add generated files that are no longer checked in. Remove obsolete things (including things now covered at the framework root). Signed-off-by: Gilles Peskine --- psasim/.gitignore | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/psasim/.gitignore b/psasim/.gitignore index 40715cafa..2e2994106 100644 --- a/psasim/.gitignore +++ b/psasim/.gitignore @@ -1,17 +1,20 @@ -bin/* -*.o -*.so +# Intermediate generated files +/include/psa_manifest/manifest.h +/include/psa_manifest/pid.h +/include/psa_manifest/sid.h /src/psa_functions_codes.h /src/psa_sim_crypto_client.c /src/psa_sim_crypto_server.c /src/psa_sim_serialise.c /src/psa_sim_serialise.h -test/psa_ff_bootstrap.c -test/psa_manifest/* -test/client -test/partition -cscope.out -*.orig + +# Build products +/test/psa_manifest/* +/test/client +/test/partition +*.so +*psa_ff_bootstrap_* + +# Local files from editors and indexing tools *.swp *.DS_Store -*psa_ff_bootstrap_* From 2bc58125fe2e747ffa9b408142f5320b3bfe0051 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Feb 2026 22:23:52 +0100 Subject: [PATCH 6/7] Add missing dependencies on generated headers Without those dependencies, the headers don't get generated. Signed-off-by: Gilles Peskine --- psasim/Makefile | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/psasim/Makefile b/psasim/Makefile index d4c7589bc..8bb9849c0 100644 --- a/psasim/Makefile +++ b/psasim/Makefile @@ -20,9 +20,22 @@ SERVER_LIBS := -Lserver_libs -lmbedcrypto COMMON_INCLUDE := -I./include -I$(MBEDTLS_PATH)/include \ $(TF_PSA_CRYPTO_LIBRARY_PUBLIC_INCLUDE) -GENERATED_H_FILES = include/psa_manifest/manifest.h \ - include/psa_manifest/pid.h \ - include/psa_manifest/sid.h +GENERATED_MANIFEST_H_FILES = \ + include/psa_manifest/manifest.h \ + include/psa_manifest/pid.h \ + include/psa_manifest/sid.h + +GENERATED_WRAPPER_H_FILES = \ + src/psa_sim_serialise.h \ + src/psa_functions_codes.h + +GENERATED_H_FILES = $(GENERATED_MANIFEST_H_FILES) $(GENERATED_WRAPPER_H_FILES) + +GENERATED_C_FILES = \ + src/psa_sim_serialise.c \ + src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c + +PSASIM_GENERATED_FILES = $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_C_FILES) LIBPSACLIENT_SRC = src/psa_ff_client.c \ src/psa_sim_crypto_client.c \ @@ -45,6 +58,8 @@ PSA_SERVER_SRC = $(PARTITION_SERVER_BOOTSTRAP) \ all: +generated_files: $(PSASIM_GENERATED_FILES) + test/seedfile: dd if=/dev/urandom of=./test/seedfile bs=64 count=1 @@ -64,13 +79,9 @@ test/psa_client_full: $(PSA_CLIENT_FULL_SRC) $(GENERATED_H_FILES) test/seedfile test/psa_server: $(PSA_SERVER_SRC) $(GENERATED_H_FILES) $(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_SERVER_SRC) $(SERVER_LIBS) $(LDFLAGS) -o $@ -$(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_H_FILES): src/manifest.json src/server.c +$(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_MANIFEST_H_FILES): src/manifest.json src/server.c tools/psa_autogen.py src/manifest.json -GENERATED_C_FILES = \ - src/psa_sim_serialise.h src/psa_sim_serialise.c \ - src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c - src/psa_sim_serialise.h: src/psa_sim_serialise.pl cd src && ./psa_sim_serialise.pl h >$(@F) @@ -82,9 +93,6 @@ src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server. src/psa_functions_codes.h src/psa_sim_crypto_client.c src/psa_sim_crypto_server.c: src/psa_sim_generate.pl cd $(TF_PSA_CRYPTO_PATH) && $(abspath src/psa_sim_generate.pl) -PSASIM_GENERATED_FILES = $(GENERATED_H_FILES) $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_C_FILES) -generated_files: $(PSASIM_GENERATED_FILES) - # Build MbedTLS libraries (crypto, x509 and tls) and copy them locally to # build client/server applications. # From a9bb80e6998488deca8123b25fd9607564b4ec83 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 9 Feb 2026 15:34:29 +0100 Subject: [PATCH 7/7] Document the psasim build Signed-off-by: Gilles Peskine --- psasim/README.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/psasim/README.md b/psasim/README.md index 6c32ae779..e148a2c88 100644 --- a/psasim/README.md +++ b/psasim/README.md @@ -4,6 +4,7 @@ PSASIM holds necessary C source and header files which allows to test Mbed TLS i In practical terms it means that this allow to build PSASIM with Mbed TLS sources and get 2 Linux applications, a client and a server, which are connected through Linux's shared memeory, and in which the client relies on the server to perform all PSA Crypto operations. The goal of PSASIM is _not_ to provide a ready-to-use solution for anyone looking to implement the pure crypto client structure (see [Limitations](#limitations) for details), but to provide an example of TF-PSA-Crypto RPC (Remote Procedure Call) implementation using Mbed TLS. + ## Limitations In the current implementation: @@ -13,9 +14,33 @@ In the current implementation: - Shared memory is the only communication medium allowed. Others can be implemented (ex: net sockets), but in terms of simulation speed shared memory proved to be the fastest. - Server is not secure at all: keys and operation structs are stored on the RAM, so they can easily be dumped. -## Testing +## Building -Please refer to `tests/scripts/components-psasim.sh` for guidance on how to build & test PSASIM: +### Build tooling + +Building PSASIM requires the following tools: + +* GNU make. +* A C compiler. +* Perl. +* The JSON package for Perl (`cpain -i JSON` or `apt install libjson-perl`). + +### Build instructions + +The build instructions are in `framework/psasim/Makefile`, with the assistance of `scripts/crypto-common.make` in TF-PSA-Crypto. The main targets are: + +* `client_libs`: builds object files to be linked with a client. The client code is expected to include TF-PSA-Crypto with `MBEDTLS_PSA_CRYPTO_CLIENT` enabled and `MBEDTLS_PSA_CRYPTO_C` disabled, with no local cryptographic primitives. +* `test/psa_server`: builds a server, including the crypto partition. This requires TF-PSA-Crypto compiled with the PSA core (`MBEDTLS_PSA_CRYPTO_C`) and cryptographic primitives. + +Note in particular that the client and the server require different builds of `libtfpsacrypto`, since they must have different configurations. + +Note that at the time of writing, building PSASIM only officially works from an Mbed TLS tree. It might not work from a standalone TF-PSA-Crypto tree. + +Some C files are generated from JSON data and from parsing TF-PSA-Crypto header files, using the scripts `src/psa_sim_generate.pl` and `src/psa_sim_serialise.pl`. They are not committed into version control, but rather generated during the build of the client or the server, according to the instructions in the makefile. + +### Testing + +Please refer to [`tests/scripts/components-psasim.sh` in Mbed TLS](https://github.com/Mbed-TLS/mbedtls/blob/development/tests/scripts/components-psasim.sh) and the `helper_psasim_xxx` auxiliary functions in [`framework/scripts/all-helpers.sh`](../scripts/all-helpers.sh) for guidance on how to build & test PSASIM: - `component_test_psasim()`: builds the server and a couple of test clients which are used to evaluate some basic PSA Crypto API commands. - `component_test_suite_with_psasim()`: builds the server and _all_ the usual test suites (those found under the `/tests/suites/*` folder) which are used by the CI and runs them. A small subset of test suites (`test_suite_constant_time_hmac`,`test_suite_lmots`,`test_suite_lms`) are being skipped, for CI turnover time optimization. They can be run locally if required.