blob: bdafcead084a2039f15be275913d4c1b1762fb9e [file] [log] [blame]
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001From 07277e2ab4b54e5844c28f0cb33e64a91aa5f492 Mon Sep 17 00:00:00 2001
2From: Julian Hall <julian.hall@arm.com>
3Date: Wed, 16 Feb 2022 10:37:04 +0000
4Subject: [PATCH] Fix multi-part termination on error
5
6For multi-part operations, the PSA Crypto API specifies that if
7the final operation does not return PSA_SUCCESS, the abort
8operaion must be called by a client to clean-up the operation.
9This change modifies behaviour in-line with the API definition.
10
11Signed-off-by: Julian Hall <julian.hall@arm.com>
12Change-Id: Ia3d3ec004164647a7ab5988cac45c39c22e76e9a
13
14Upstream-Status: Pending [Not submitted to upstream yet]
15Signed-off-by: Emekcan Aras <Emekcan.Aras@arm.com>
16
17
18---
19 components/service/crypto/client/psa/psa_aead.c | 8 ++++++++
20 components/service/crypto/client/psa/psa_cipher.c | 4 ++++
21 components/service/crypto/client/psa/psa_hash.c | 10 ++++++++++
22 components/service/crypto/client/psa/psa_mac.c | 10 ++++++++++
23 .../crypto/provider/extension/aead/aead_provider.c | 10 +++++-----
24 .../provider/extension/cipher/cipher_provider.c | 6 +++---
25 .../crypto/provider/extension/hash/hash_provider.c | 6 +++---
26 .../crypto/provider/extension/mac/mac_provider.c | 11 +++++++----
27 8 files changed, 50 insertions(+), 15 deletions(-)
28
29diff --git a/components/service/crypto/client/psa/psa_aead.c b/components/service/crypto/client/psa/psa_aead.c
30index e4579e63..559eb6a3 100644
31--- a/components/service/crypto/client/psa/psa_aead.c
32+++ b/components/service/crypto/client/psa/psa_aead.c
33@@ -241,6 +241,10 @@ psa_status_t psa_aead_encrypt(psa_key_id_t key,
34
35 *aeadtext_length = bytes_output + remaining_aead_len + tag_len;
36 }
37+ else {
38+
39+ psa_aead_abort(&operation);
40+ }
41 }
42 else {
43
44@@ -292,6 +296,10 @@ psa_status_t psa_aead_decrypt(psa_key_id_t key,
45
46 *plaintext_length = bytes_output + remaining_plaintext_len;
47 }
48+ else {
49+
50+ psa_aead_abort(&operation);
51+ }
52 }
53 else {
54
55diff --git a/components/service/crypto/client/psa/psa_cipher.c b/components/service/crypto/client/psa/psa_cipher.c
56index 111af829..4e4264b6 100644
57--- a/components/service/crypto/client/psa/psa_cipher.c
58+++ b/components/service/crypto/client/psa/psa_cipher.c
59@@ -146,6 +146,10 @@ static psa_status_t multi_cipher_update(psa_cipher_operation_t *operation,
60
61 *output_length = bytes_output + finish_output_len;
62 }
63+ else {
64+
65+ psa_cipher_abort(operation);
66+ }
67 }
68 else {
69
70diff --git a/components/service/crypto/client/psa/psa_hash.c b/components/service/crypto/client/psa/psa_hash.c
71index 83278de6..e5dd0030 100644
72--- a/components/service/crypto/client/psa/psa_hash.c
73+++ b/components/service/crypto/client/psa/psa_hash.c
74@@ -137,6 +137,11 @@ psa_status_t psa_hash_compare(psa_algorithm_t alg,
75 if (psa_status == PSA_SUCCESS) {
76
77 psa_status = psa_hash_verify(&operation, hash, hash_length);
78+
79+ if (psa_status != PSA_SUCCESS) {
80+
81+ psa_hash_abort(&operation);
82+ }
83 }
84
85 return psa_status;
86@@ -155,6 +160,11 @@ psa_status_t psa_hash_compute(psa_algorithm_t alg,
87 if (psa_status == PSA_SUCCESS) {
88
89 psa_status = psa_hash_finish(&operation, hash, hash_size, hash_length);
90+
91+ if (psa_status != PSA_SUCCESS) {
92+
93+ psa_hash_abort(&operation);
94+ }
95 }
96
97 return psa_status;
98diff --git a/components/service/crypto/client/psa/psa_mac.c b/components/service/crypto/client/psa/psa_mac.c
99index 5c5eb32a..a3db8644 100644
100--- a/components/service/crypto/client/psa/psa_mac.c
101+++ b/components/service/crypto/client/psa/psa_mac.c
102@@ -129,6 +129,11 @@ psa_status_t psa_mac_verify(psa_key_id_t key,
103 if (psa_status == PSA_SUCCESS) {
104
105 psa_status = psa_mac_verify_finish(&operation, mac, mac_length);
106+
107+ if (psa_status != PSA_SUCCESS) {
108+
109+ psa_mac_abort(&operation);
110+ }
111 }
112
113 return psa_status;
114@@ -153,6 +158,11 @@ psa_status_t psa_mac_compute(psa_key_id_t key,
115 if (psa_status == PSA_SUCCESS) {
116
117 psa_status = psa_mac_sign_finish(&operation, mac, mac_size, mac_length);
118+
119+ if (psa_status != PSA_SUCCESS) {
120+
121+ psa_mac_abort(&operation);
122+ }
123 }
124
125 return psa_status;
126diff --git a/components/service/crypto/provider/extension/aead/aead_provider.c b/components/service/crypto/provider/extension/aead/aead_provider.c
127index f4e81a03..14a25436 100644
128--- a/components/service/crypto/provider/extension/aead/aead_provider.c
129+++ b/components/service/crypto/provider/extension/aead/aead_provider.c
130@@ -1,5 +1,5 @@
131 /*
132- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
133+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
134 *
135 * SPDX-License-Identifier: BSD-3-Clause
136 */
137@@ -369,9 +369,9 @@ static rpc_status_t aead_finish_handler(void *context, struct call_req *req)
138 rpc_status = serializer->serialize_aead_finish_resp(resp_buf,
139 ciphertext, ciphertext_len,
140 tag, tag_len);
141- }
142
143- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
144+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
145+ }
146 }
147
148 call_req_set_opstatus(req, psa_status);
149@@ -418,9 +418,9 @@ static rpc_status_t aead_verify_handler(void *context, struct call_req *req)
150 struct call_param_buf *resp_buf = call_req_get_resp_buf(req);
151 rpc_status = serializer->serialize_aead_verify_resp(resp_buf,
152 plaintext, plaintext_len);
153- }
154
155- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
156+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
157+ }
158 }
159
160 call_req_set_opstatus(req, psa_status);
161diff --git a/components/service/crypto/provider/extension/cipher/cipher_provider.c b/components/service/crypto/provider/extension/cipher/cipher_provider.c
162index 8e7a86de..a5dd0371 100644
163--- a/components/service/crypto/provider/extension/cipher/cipher_provider.c
164+++ b/components/service/crypto/provider/extension/cipher/cipher_provider.c
165@@ -1,5 +1,5 @@
166 /*
167- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
168+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
169 *
170 * SPDX-License-Identifier: BSD-3-Clause
171 */
172@@ -283,9 +283,9 @@ static rpc_status_t cipher_finish_handler(void *context, struct call_req* req)
173
174 struct call_param_buf *resp_buf = call_req_get_resp_buf(req);
175 rpc_status = serializer->serialize_cipher_finish_resp(resp_buf, output, output_len);
176- }
177
178- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
179+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
180+ }
181 }
182
183 call_req_set_opstatus(req, psa_status);
184diff --git a/components/service/crypto/provider/extension/hash/hash_provider.c b/components/service/crypto/provider/extension/hash/hash_provider.c
185index 2c560513..fd39d440 100644
186--- a/components/service/crypto/provider/extension/hash/hash_provider.c
187+++ b/components/service/crypto/provider/extension/hash/hash_provider.c
188@@ -1,5 +1,5 @@
189 /*
190- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
191+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
192 *
193 * SPDX-License-Identifier: BSD-3-Clause
194 */
195@@ -179,9 +179,9 @@ static rpc_status_t hash_finish_handler(void *context, struct call_req* req)
196
197 struct call_param_buf *resp_buf = call_req_get_resp_buf(req);
198 rpc_status = serializer->serialize_hash_finish_resp(resp_buf, hash, hash_len);
199- }
200
201- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
202+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
203+ }
204 }
205
206 call_req_set_opstatus(req, psa_status);
207diff --git a/components/service/crypto/provider/extension/mac/mac_provider.c b/components/service/crypto/provider/extension/mac/mac_provider.c
208index 96fe4cf3..eef55586 100644
209--- a/components/service/crypto/provider/extension/mac/mac_provider.c
210+++ b/components/service/crypto/provider/extension/mac/mac_provider.c
211@@ -1,5 +1,5 @@
212 /*
213- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
214+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
215 *
216 * SPDX-License-Identifier: BSD-3-Clause
217 */
218@@ -181,9 +181,9 @@ static rpc_status_t mac_sign_finish_handler(void *context, struct call_req* req)
219
220 struct call_param_buf *resp_buf = call_req_get_resp_buf(req);
221 rpc_status = serializer->serialize_mac_sign_finish_resp(resp_buf, mac, mac_len);
222- }
223
224- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
225+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
226+ }
227 }
228
229 call_req_set_opstatus(req, psa_status);
230@@ -220,7 +220,10 @@ static rpc_status_t mac_verify_finish_handler(void *context, struct call_req* re
231
232 psa_status = psa_mac_verify_finish(&crypto_context->op.mac, mac, mac_len);
233
234- crypto_context_pool_free(&this_instance->context_pool, crypto_context);
235+ if (psa_status == PSA_SUCCESS) {
236+
237+ crypto_context_pool_free(&this_instance->context_pool, crypto_context);
238+ }
239 }
240
241 call_req_set_opstatus(req, psa_status);