Reject NULL original_output with non-NULL output

If we have a copy buffer but no original to copy back to, there is not
much sensible we can do. The psa_crypto_buffer_copy_t state is invalid.

Signed-off-by: David Horstmann <david.horstmann@arm.com>
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index f0599d7..a3283ab 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -5603,6 +5603,12 @@
 psa_status_t psa_crypto_copy_and_free(psa_crypto_buffer_copy_t *buffers)
 {
     if (buffers->output != NULL) {
+        if (buffers->output_original == NULL) {
+            /* Output is non-NULL but original output is NULL. The argument
+             * buffers is invalid. Return an error as we have no original to
+             * copy back to. */
+            return PSA_ERROR_INVALID_ARGUMENT;
+        }
         memcpy(buffers->output_original, buffers->output, buffers->output_len);
     }
 
diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data
index 7e4069e..fc89693 100644
--- a/tests/suites/test_suite_psa_crypto.data
+++ b/tests/suites/test_suite_psa_crypto.data
@@ -4080,3 +4080,9 @@
 
 PSA buffers copy and free, null output
 psa_crypto_copy_and_free:0:20:1:0:0:PSA_SUCCESS
+
+PSA buffers copy and free, null output_original
+psa_crypto_copy_and_free:0:20:0:20:1:PSA_ERROR_INVALID_ARGUMENT
+
+PSA buffers copy and free, null output_original and null output
+psa_crypto_copy_and_free:0:20:1:0:1:PSA_SUCCESS