Separate sign/verify message and hash operations

Previous versions of mbedtls didn't distinguish between
asymmetric sign and verify operations on a hash or message.
They are now treated as separate operations from a usage
control perspective. This change makes the corresponding
hash/message sepration in client and service provider
components.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I134286d66c3943090055171adfdf16270f395aa3
diff --git a/components/service/crypto/client/caller/packed-c/crypto_caller_sign_hash.h b/components/service/crypto/client/caller/packed-c/crypto_caller_sign_hash.h
index e807773..4a9ed20 100644
--- a/components/service/crypto/client/caller/packed-c/crypto_caller_sign_hash.h
+++ b/components/service/crypto/client/caller/packed-c/crypto_caller_sign_hash.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -20,7 +20,8 @@
 extern "C" {
 #endif
 
-static inline psa_status_t crypto_caller_sign_hash(struct service_client *context,
+static inline psa_status_t crypto_caller_asym_sign_commom(struct service_client *context,
+	uint32_t opcode,
 	psa_key_id_t id,
 	psa_algorithm_t alg,
 	const uint8_t *hash, size_t hash_length,
@@ -60,7 +61,7 @@
 
 		context->rpc_status =
 			rpc_caller_invoke(context->caller, call_handle,
-						TS_CRYPTO_OPCODE_SIGN_HASH, &opstatus, &resp_buf, &resp_len);
+						opcode, &opstatus, &resp_buf, &resp_len);
 
 		if (context->rpc_status == TS_RPC_CALL_ACCEPTED) {
 
@@ -98,6 +99,28 @@
 	return psa_status;
 }
 
+static inline psa_status_t crypto_caller_sign_hash(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *hash, size_t hash_length,
+	uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
+	return crypto_caller_asym_sign_commom(context, TS_CRYPTO_OPCODE_SIGN_HASH,
+		id, alg, hash, hash_length,
+		signature, signature_size, signature_length);
+}
+
+static inline psa_status_t crypto_caller_sign_message(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *hash, size_t hash_length,
+	uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
+	return crypto_caller_asym_sign_commom(context, TS_CRYPTO_OPCODE_SIGN_MESSAGE,
+		id, alg, hash, hash_length,
+		signature, signature_size, signature_length);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/components/service/crypto/client/caller/packed-c/crypto_caller_verify_hash.h b/components/service/crypto/client/caller/packed-c/crypto_caller_verify_hash.h
index 4715294..daa1133 100644
--- a/components/service/crypto/client/caller/packed-c/crypto_caller_verify_hash.h
+++ b/components/service/crypto/client/caller/packed-c/crypto_caller_verify_hash.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -20,7 +20,8 @@
 extern "C" {
 #endif
 
-static inline psa_status_t crypto_caller_verify_hash(struct service_client *context,
+static inline psa_status_t crypto_caller_asym_verify_common(struct service_client *context,
+	uint32_t opcode,
 	psa_key_id_t id,
 	psa_algorithm_t alg,
 	const uint8_t *hash, size_t hash_length,
@@ -65,7 +66,7 @@
 
 		context->rpc_status =
 			rpc_caller_invoke(context->caller, call_handle,
-					TS_CRYPTO_OPCODE_VERIFY_HASH, &opstatus, &resp_buf, &resp_len);
+					opcode, &opstatus, &resp_buf, &resp_len);
 
 		if (context->rpc_status == TS_RPC_CALL_ACCEPTED) psa_status = opstatus;
 
@@ -75,6 +76,32 @@
 	return psa_status;
 }
 
+static inline psa_status_t crypto_caller_verify_hash(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *hash, size_t hash_length,
+	const uint8_t *signature, size_t signature_length)
+{
+	return crypto_caller_asym_verify_common(context,
+		TS_CRYPTO_OPCODE_VERIFY_HASH,
+		id, alg,
+		hash, hash_length,
+		signature, signature_length);
+}
+
+static inline psa_status_t crypto_caller_verify_message(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *input, size_t input_length,
+	const uint8_t *signature, size_t signature_length)
+{
+	return crypto_caller_asym_verify_common(context,
+		TS_CRYPTO_OPCODE_VERIFY_MESSAGE,
+		id, alg,
+		input, input_length,
+		signature, signature_length);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/components/service/crypto/client/caller/stub/crypto_caller_sign_hash.h b/components/service/crypto/client/caller/stub/crypto_caller_sign_hash.h
index d09369a..09049f5 100644
--- a/components/service/crypto/client/caller/stub/crypto_caller_sign_hash.h
+++ b/components/service/crypto/client/caller/stub/crypto_caller_sign_hash.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -23,6 +23,15 @@
 	return PSA_ERROR_NOT_SUPPORTED;
 }
 
+static inline psa_status_t crypto_caller_sign_message(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *hash, size_t hash_length,
+	uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
+	return PSA_ERROR_NOT_SUPPORTED;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/components/service/crypto/client/caller/stub/crypto_caller_verify_hash.h b/components/service/crypto/client/caller/stub/crypto_caller_verify_hash.h
index 20d11dc..3f3eb87 100644
--- a/components/service/crypto/client/caller/stub/crypto_caller_verify_hash.h
+++ b/components/service/crypto/client/caller/stub/crypto_caller_verify_hash.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -23,6 +23,15 @@
 	return PSA_ERROR_NOT_SUPPORTED;
 }
 
+static inline psa_status_t crypto_caller_verify_message(struct service_client *context,
+	psa_key_id_t id,
+	psa_algorithm_t alg,
+	const uint8_t *input, size_t input_length,
+	const uint8_t *signature, size_t signature_length)
+{
+	return PSA_ERROR_NOT_SUPPORTED;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/components/service/crypto/client/cpp/crypto_client.h b/components/service/crypto/client/cpp/crypto_client.h
index 2a5e5b9..ccb0714 100644
--- a/components/service/crypto/client/cpp/crypto_client.h
+++ b/components/service/crypto/client/cpp/crypto_client.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -57,7 +57,7 @@
 		psa_key_id_t id,
 		uint8_t *data, size_t data_size, size_t *data_length) = 0;
 
-	/* Sign/verify methods */
+	/* Sign/verify hash methods */
 	virtual psa_status_t sign_hash(
 		psa_key_id_t id,
 		psa_algorithm_t alg,
@@ -70,6 +70,19 @@
 		const uint8_t *hash, size_t hash_length,
 		const uint8_t *signature, size_t signature_length) = 0;
 
+	/* Sign/verify message methods */
+	virtual psa_status_t sign_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		uint8_t *signature, size_t signature_size, size_t *signature_length) = 0;
+
+	virtual psa_status_t verify_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		const uint8_t *signature, size_t signature_length) = 0;
+
 	/* Asymmetric encrypt/decrypt */
 	virtual psa_status_t asymmetric_encrypt(
 		psa_key_id_t id,
diff --git a/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.cpp b/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.cpp
index 4d9d8f4..4e10f9b 100644
--- a/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.cpp
+++ b/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -107,6 +107,26 @@
 		signature, signature_length);
 }
 
+psa_status_t packedc_crypto_client::sign_message(
+	psa_key_id_t id, psa_algorithm_t alg,
+	const uint8_t *message, size_t message_length,
+	uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
+	return crypto_caller_sign_message(&m_client, id, alg,
+		message, message_length,
+		signature, signature_size, signature_length);
+}
+
+psa_status_t packedc_crypto_client::verify_message(
+	psa_key_id_t id, psa_algorithm_t alg,
+	const uint8_t *message, size_t message_length,
+	const uint8_t *signature, size_t signature_length)
+{
+	return crypto_caller_verify_message(&m_client, id, alg,
+		message, message_length,
+		signature, signature_length);
+}
+
 psa_status_t packedc_crypto_client::asymmetric_encrypt(
 	psa_key_id_t id, psa_algorithm_t alg,
 	const uint8_t *input, size_t input_length,
diff --git a/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.h b/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.h
index 377b51d..d74ba60 100644
--- a/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.h
+++ b/components/service/crypto/client/cpp/protocol/packed-c/packedc_crypto_client.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -54,7 +54,7 @@
 		psa_key_id_t id,
 		uint8_t *data, size_t data_size, size_t *data_length);
 
-	/* Sign/verify methods */
+	/* Sign/verify hash methods */
 	psa_status_t sign_hash(
 		psa_key_id_t id,
 		psa_algorithm_t alg,
@@ -67,6 +67,19 @@
 		const uint8_t *hash, size_t hash_length,
 		const uint8_t *signature, size_t signature_length);
 
+	/* Sign/verify message methods */
+	psa_status_t sign_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		uint8_t *signature, size_t signature_size, size_t *signature_length);
+
+	psa_status_t verify_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		const uint8_t *signature, size_t signature_length);
+
 	/* Asymmetric encrypt/decrypt */
 	psa_status_t asymmetric_encrypt(
 		psa_key_id_t id,
diff --git a/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.cpp b/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.cpp
index 9712ca0..845c9fa 100644
--- a/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.cpp
+++ b/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.cpp
@@ -387,6 +387,25 @@
 							const uint8_t *hash, size_t hash_length,
 							uint8_t *signature, size_t signature_size, size_t *signature_length)
 {
+	return asym_sign(ts_crypto_Opcode_SIGN_HASH, id, alg,
+				hash, hash_length,
+				signature, signature_size, signature_length);
+}
+
+psa_status_t protobuf_crypto_client::sign_message(psa_key_id_t id, psa_algorithm_t alg,
+							const uint8_t *message, size_t message_length,
+							uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
+	return asym_sign(ts_crypto_Opcode_SIGN_MESSAGE, id, alg,
+				message, message_length,
+				signature, signature_size, signature_length);
+}
+
+psa_status_t protobuf_crypto_client::asym_sign(uint32_t opcode,
+							psa_key_id_t id, psa_algorithm_t alg,
+							const uint8_t *hash, size_t hash_length,
+							uint8_t *signature, size_t signature_size, size_t *signature_length)
+{
 	size_t req_len;
 	pb_bytes_array_t *hash_byte_array =
 		pb_malloc_byte_array_containing_bytes(hash, hash_length);
@@ -416,7 +435,7 @@
 			pb_encode(&ostream, ts_crypto_SignHashIn_fields, &req_msg);
 
 			m_client.rpc_status = rpc_caller_invoke(m_client.caller, call_handle,
-						ts_crypto_Opcode_SIGN_HASH, &opstatus, &resp_buf, &resp_len);
+						opcode, &opstatus, &resp_buf, &resp_len);
 
 			if (m_client.rpc_status == TS_RPC_CALL_ACCEPTED) {
 
@@ -462,11 +481,29 @@
 	return psa_status;
 }
 
-
 psa_status_t protobuf_crypto_client::verify_hash(psa_key_id_t id, psa_algorithm_t alg,
 						const uint8_t *hash, size_t hash_length,
 						const uint8_t *signature, size_t signature_length)
 {
+	return asym_verify(ts_crypto_Opcode_VERIFY_HASH, id, alg,
+				hash, hash_length,
+				signature, signature_length);
+}
+
+psa_status_t protobuf_crypto_client::verify_message(psa_key_id_t id, psa_algorithm_t alg,
+						const uint8_t *message, size_t message_length,
+						const uint8_t *signature, size_t signature_length)
+{
+	return asym_verify(ts_crypto_Opcode_VERIFY_MESSAGE, id, alg,
+				message, message_length,
+				signature, signature_length);
+}
+
+psa_status_t protobuf_crypto_client::asym_verify(uint32_t opcode,
+						psa_key_id_t id, psa_algorithm_t alg,
+						const uint8_t *hash, size_t hash_length,
+						const uint8_t *signature, size_t signature_length)
+{
 	size_t req_len;
 	pb_bytes_array_t *hash_byte_array =
 		pb_malloc_byte_array_containing_bytes(hash, hash_length);
@@ -497,7 +534,7 @@
 			pb_encode(&ostream, ts_crypto_VerifyHashIn_fields, &req_msg);
 
 			m_client.rpc_status = rpc_caller_invoke(m_client.caller, call_handle,
-						ts_crypto_Opcode_VERIFY_HASH, &opstatus, &resp_buf, &resp_len);
+						opcode, &opstatus, &resp_buf, &resp_len);
 
 			if (m_client.rpc_status == TS_RPC_CALL_ACCEPTED) psa_status = opstatus;
 
diff --git a/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.h b/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.h
index 085d9cf..abe4439 100644
--- a/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.h
+++ b/components/service/crypto/client/cpp/protocol/protobuf/protobuf_crypto_client.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -54,7 +54,7 @@
 		psa_key_id_t id,
 		uint8_t *data, size_t data_size, size_t *data_length);
 
-	/* Sign/verify methods */
+	/* Sign/verify hash methods */
 	psa_status_t sign_hash(
 		psa_key_id_t id,
 		psa_algorithm_t alg,
@@ -67,6 +67,19 @@
 		const uint8_t *hash, size_t hash_length,
 		const uint8_t *signature, size_t signature_length);
 
+	/* Sign/verify message methods */
+	psa_status_t sign_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		uint8_t *signature, size_t signature_size, size_t *signature_length);
+
+	psa_status_t verify_message(
+		psa_key_id_t id,
+		psa_algorithm_t alg,
+		const uint8_t *message, size_t message_length,
+		const uint8_t *signature, size_t signature_length);
+
 	/* Asymmetric encrypt/decrypt */
 	psa_status_t asymmetric_encrypt(
 		psa_key_id_t id,
@@ -221,6 +234,16 @@
 
 private:
 
+	psa_status_t asym_sign(uint32_t opcode,
+		psa_key_id_t id, psa_algorithm_t alg,
+		const uint8_t *hash, size_t hash_length,
+		uint8_t *signature, size_t signature_size, size_t *signature_length);
+
+	psa_status_t asym_verify(uint32_t opcode,
+		psa_key_id_t id, psa_algorithm_t alg,
+		const uint8_t *hash, size_t hash_length,
+		const uint8_t *signature, size_t signature_length);
+
 	void translate_key_attributes(
 		ts_crypto_KeyAttributes &proto_attributes,
 		const psa_key_attributes_t &psa_attributes);
diff --git a/components/service/crypto/client/psa/psa_sign_message.c b/components/service/crypto/client/psa/psa_sign_message.c
index dc2f7e8..b644625 100644
--- a/components/service/crypto/client/psa/psa_sign_message.c
+++ b/components/service/crypto/client/psa/psa_sign_message.c
@@ -1,13 +1,15 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <psa/crypto.h>
+#include "psa_crypto_client.h"
+#include "crypto_caller_selector.h"
 
 psa_status_t psa_sign_message(
-	psa_key_id_t key,
+	psa_key_id_t id,
 	psa_algorithm_t alg,
 	const uint8_t *input,
 	size_t input_length,
@@ -15,19 +17,11 @@
 	size_t signature_size,
 	size_t *signature_length)
 {
-	size_t hash_len;
-	uint8_t hash[PSA_HASH_MAX_SIZE];
+	if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
+		return psa_crypto_client_instance.init_status;
 
-	psa_status_t psa_status = psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg),
+	return crypto_caller_sign_message(&psa_crypto_client_instance.base,
+		id, alg,
 		input, input_length,
-		hash, sizeof(hash), &hash_len);
-
-	if (psa_status == PSA_SUCCESS) {
-
-		psa_status = psa_sign_hash(key, alg,
-			hash, hash_len,
-			signature, signature_size, signature_length);
-	}
-
-	return psa_status;
+		signature, signature_size, signature_length);
 }
diff --git a/components/service/crypto/client/psa/psa_verify_message.c b/components/service/crypto/client/psa/psa_verify_message.c
index d0fbc7c..57c2c5e 100644
--- a/components/service/crypto/client/psa/psa_verify_message.c
+++ b/components/service/crypto/client/psa/psa_verify_message.c
@@ -1,32 +1,26 @@
 /*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <psa/crypto.h>
+#include "psa_crypto_client.h"
+#include "crypto_caller_selector.h"
 
 psa_status_t psa_verify_message(
-	psa_key_id_t key,
+	psa_key_id_t id,
 	psa_algorithm_t alg,
 	const uint8_t *input,
 	size_t input_length,
 	const uint8_t * signature,
 	size_t signature_length)
 {
-	size_t hash_len;
-	uint8_t hash[PSA_HASH_MAX_SIZE];
+	if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
+		return psa_crypto_client_instance.init_status;
 
-	psa_status_t psa_status = psa_hash_compute(PSA_ALG_SIGN_GET_HASH(alg),
+	return crypto_caller_verify_message(&psa_crypto_client_instance.base,
+		id, alg,
 		input, input_length,
-		hash, sizeof(hash), &hash_len);
-
-	if (psa_status == PSA_SUCCESS) {
-
-		psa_status = psa_verify_hash(key, alg,
-			hash, hash_len,
-			signature, signature_length);
-	}
-
-	return psa_status;
+		signature, signature_length);
 }