blob: a56e0f8813bc653fa4c6c7e0335fcf2d17f77bf8 [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001From 9a83c32964ee2b1ecb7b36b4c08466202efd3bf2 Mon Sep 17 00:00:00 2001
2From: Julian Hall <julian.hall@arm.com>
3Date: Fri, 11 Feb 2022 14:19:26 +0000
4Subject: [PATCH] Add defence against uninitialised multi-part transaction
5
6Adds checks for the condition where there is an attempt to
7setup a multi-part transaction without first initialising
8transaction state.
9
10Signed-off-by: Julian Hall <julian.hall@arm.com>
11Change-Id: I754479260fed0490d8f32b41a077d26028dc9903
12
13Upstream-Status: Pending [Not submitted to upstream yet]
14Signed-off-by: Emekcan Aras <Emekcan.Aras@arm.com>
15
16
17---
18 components/service/crypto/client/psa/psa_cipher.c | 14 +++++++++++++-
19 components/service/crypto/client/psa/psa_hash.c | 8 +++++++-
20 components/service/crypto/client/psa/psa_mac.c | 10 ++++++++--
21 3 files changed, 28 insertions(+), 4 deletions(-)
22
23diff --git a/components/service/crypto/client/psa/psa_cipher.c b/components/service/crypto/client/psa/psa_cipher.c
24index 70836ea6..3ab8ea21 100644
25--- a/components/service/crypto/client/psa/psa_cipher.c
26+++ b/components/service/crypto/client/psa/psa_cipher.c
27@@ -1,5 +1,5 @@
28 /*
29- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
30+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
31 *
32 * SPDX-License-Identifier: BSD-3-Clause
33 */
34@@ -13,6 +13,12 @@ psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
35 psa_key_id_t key,
36 psa_algorithm_t alg)
37 {
38+ if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
39+ return psa_crypto_client_instance.init_status;
40+
41+ if (operation->handle)
42+ return PSA_ERROR_BAD_STATE;
43+
44 return crypto_caller_cipher_encrypt_setup(&psa_crypto_client_instance.base,
45 &operation->handle,
46 key, alg);
47@@ -22,6 +28,12 @@ psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
48 psa_key_id_t key,
49 psa_algorithm_t alg)
50 {
51+ if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
52+ return psa_crypto_client_instance.init_status;
53+
54+ if (operation->handle)
55+ return PSA_ERROR_BAD_STATE;
56+
57 return crypto_caller_cipher_decrypt_setup(&psa_crypto_client_instance.base,
58 &operation->handle,
59 key, alg);
60diff --git a/components/service/crypto/client/psa/psa_hash.c b/components/service/crypto/client/psa/psa_hash.c
61index 7005c390..83278de6 100644
62--- a/components/service/crypto/client/psa/psa_hash.c
63+++ b/components/service/crypto/client/psa/psa_hash.c
64@@ -1,5 +1,5 @@
65 /*
66- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
67+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
68 *
69 * SPDX-License-Identifier: BSD-3-Clause
70 */
71@@ -14,6 +14,9 @@ psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
72 if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
73 return psa_crypto_client_instance.init_status;
74
75+ if (operation->handle)
76+ return PSA_ERROR_BAD_STATE;
77+
78 return crypto_caller_hash_setup(&psa_crypto_client_instance.base,
79 &operation->handle, alg);
80 }
81@@ -55,6 +58,9 @@ psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
82 psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
83 psa_hash_operation_t *target_operation)
84 {
85+ if (target_operation->handle)
86+ return PSA_ERROR_BAD_STATE;
87+
88 return crypto_caller_hash_clone(&psa_crypto_client_instance.base,
89 source_operation->handle,
90 &target_operation->handle);
91diff --git a/components/service/crypto/client/psa/psa_mac.c b/components/service/crypto/client/psa/psa_mac.c
92index 5efa1c4d..5c5eb32a 100644
93--- a/components/service/crypto/client/psa/psa_mac.c
94+++ b/components/service/crypto/client/psa/psa_mac.c
95@@ -1,5 +1,5 @@
96 /*
97- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
98+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
99 *
100 * SPDX-License-Identifier: BSD-3-Clause
101 */
102@@ -16,6 +16,9 @@ psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
103 if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
104 return psa_crypto_client_instance.init_status;
105
106+ if (operation->handle)
107+ return PSA_ERROR_BAD_STATE;
108+
109 return crypto_caller_mac_sign_setup(&psa_crypto_client_instance.base,
110 &operation->handle,
111 key, alg);
112@@ -28,7 +31,10 @@ psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
113 if (psa_crypto_client_instance.init_status != PSA_SUCCESS)
114 return psa_crypto_client_instance.init_status;
115
116- return crypto_caller_mac_sign_setup(&psa_crypto_client_instance.base,
117+ if (operation->handle)
118+ return PSA_ERROR_BAD_STATE;
119+
120+ return crypto_caller_mac_verify_setup(&psa_crypto_client_instance.base,
121 &operation->handle,
122 key, alg);
123 }