diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index 7fc0d9677f..bcda6d689d 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -111,63 +111,90 @@ mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
return PSA_ERROR_BAD_STATE;
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
-/* Substitute an input buffer for a local copy of itself.
+
+/* Declare a local copy of an input buffer.
+ *
+ * Note: This macro must be called before any operations which may jump to
+ * the exit label, so that the local input copy object is safe to be freed.
+ *
+ * Assumptions:
+ * - input is the name of a pointer to the buffer to be copied
+ * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope
+ */
+#define LOCAL_INPUT_DECLARE(input) \
+ psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT;
+
+/* Allocate a copy of the buffer input and create a pointer with the name
+ * input_copy_name that points to the start of the copy.
+ *
* Assumptions:
* - psa_status_t status exists
* - An exit label is declared
- * - The name _copy is not used for the given value of
+ * - input is the name of a pointer to the buffer to be copied
+ * - LOCAL_INPUT_DECLARE(input) has previously been called
+ * - input_copy_name is a name that is not used in the current scope
*/
-#define SWAP_FOR_LOCAL_INPUT(input, length) \
- psa_crypto_local_input_t input##_copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
- status = psa_crypto_local_input_alloc(input, length, &input##_copy); \
+#define LOCAL_INPUT_ALLOC(input, length, input_copy_name) \
+ status = psa_crypto_local_input_alloc(input, length, \
+ &LOCAL_INPUT_COPY_OF_##input); \
if (status != PSA_SUCCESS) { \
goto exit; \
} \
- input = input##_copy.buffer;
+ const uint8_t *input_copy_name = LOCAL_INPUT_COPY_OF_##input.buffer;
-/* Free the substituted input buffer copy created by SWAP_FOR_LOCAL_INPUT().
- * Note that this does not restore the pointer to the original buffer.
+/* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC()
+ *
* Assumptions:
- * - psa_crypto_local_input_t _copy exists, for the given value of
- *
- * - _copy was previously allocated by psa_crypto_local_input_alloc()
- * - points to _copy.buffer
+ * - input_copy_name is the name of the input copy created by LOCAL_INPUT_ALLOC()
+ * - input is the name of the original buffer that was copied
*/
-#define FREE_LOCAL_INPUT(input) \
- input = NULL; \
- psa_crypto_local_input_free(&input##_copy);
+#define LOCAL_INPUT_FREE(input_copy_name, input) \
+ input_copy_name = NULL; \
+ psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input);
-/* Substitute an output buffer for a local copy of itself.
+/* Declare a local copy of an output buffer.
+ *
+ * Note: This macro must be called before any operations which may jump to
+ * the exit label, so that the local output copy object is safe to be freed.
+ *
+ * Assumptions:
+ * - output is the name of a pointer to the buffer to be copied
+ * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope
+ */
+#define LOCAL_OUTPUT_DECLARE(output) \
+ psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
+
+/* Allocate a copy of the buffer output and create a pointer with the name
+ * output_copy_name that points to the start of the copy.
+ *
* Assumptions:
* - psa_status_t status exists
* - An exit label is declared
- * - The name