blob: af06424e38e271f37f8e9f3bb1f1fddbc0604bea [file] [log] [blame]
Ashutosh Singhf4d88672017-11-29 13:35:43 +00001/*
Jamie Foxb93da8b2018-12-13 18:27:30 +00002 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Ashutosh Singhf4d88672017-11-29 13:35:43 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Jamie Foxcc31d402019-01-28 17:13:52 +00008#include "psa/protected_storage.h"
Jamie Foxb93da8b2018-12-13 18:27:30 +00009
Ashutosh Singhf4d88672017-11-29 13:35:43 +000010#include "tfm_ns_lock.h"
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000011#include "tfm_veneers.h"
Edison Aib892dfe2019-06-21 11:28:11 +080012#ifdef TFM_PSA_API
13#include "psa_manifest/sid.h"
14#endif
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000015
16#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
Summer Qindb1448b2019-02-26 11:20:52 +080017/* FixMe: Check if PSA framework header would provide similar macro. */
18#define TFM_PSA_HANDLE_IS_VALID(handle) ((handle) > (psa_handle_t)0)
Mate Toth-Pal261df462018-08-07 12:02:42 +020019
Jamie Foxb93da8b2018-12-13 18:27:30 +000020psa_ps_status_t psa_ps_set(psa_ps_uid_t uid,
21 uint32_t data_length,
22 const void *p_data,
23 psa_ps_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000024{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000025 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000026 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +080027#ifdef TFM_PSA_API
28 psa_handle_t handle;
29#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010030
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000031 psa_invec in_vec[] = {
32 { .base = &uid, .len = sizeof(uid) },
33 { .base = p_data, .len = data_length },
34 { .base = &create_flags, .len = sizeof(create_flags) }
35 };
36
37 psa_outvec out_vec[] = {
38 { .base = &err , .len = sizeof(err) }
39 };
40
Summer Qindb1448b2019-02-26 11:20:52 +080041#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +080042 handle = psa_connect(TFM_SST_SET_SID, TFM_SST_SET_VERSION);
Summer Qindb1448b2019-02-26 11:20:52 +080043 if (!TFM_PSA_HANDLE_IS_VALID(handle)) {
44 return PSA_PS_ERROR_OPERATION_FAILED;
45 }
46
47 status = psa_call(handle, in_vec, IOVEC_LEN(in_vec), out_vec,
48 IOVEC_LEN(out_vec));
49
50 psa_close(handle);
51
52 if (status != PSA_SUCCESS) {
53 return PSA_PS_ERROR_OPERATION_FAILED;
54 }
55#else
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000056 status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_set_req_veneer,
57 (uint32_t)in_vec, IOVEC_LEN(in_vec),
58 (uint32_t)out_vec, IOVEC_LEN(out_vec));
59 if (status != PSA_SUCCESS) {
60 return PSA_PS_ERROR_OPERATION_FAILED;
61 }
Summer Qindb1448b2019-02-26 11:20:52 +080062#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010063
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000064 return err;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000065}
66
Jamie Foxb93da8b2018-12-13 18:27:30 +000067psa_ps_status_t psa_ps_get(psa_ps_uid_t uid,
68 uint32_t data_offset,
69 uint32_t data_length,
70 void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000071{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000072 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000073 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +080074#ifdef TFM_PSA_API
75 psa_handle_t handle;
76#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010077
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000078 psa_invec in_vec[] = {
79 { .base = &uid, .len = sizeof(uid) },
80 { .base = &data_offset, .len = sizeof(data_offset) }
81 };
82
83 psa_outvec out_vec[] = {
84 { .base = &err, .len = sizeof(err) },
85 { .base = p_data, .len = data_length }
86 };
87
Summer Qindb1448b2019-02-26 11:20:52 +080088#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +080089 handle = psa_connect(TFM_SST_GET_SID, TFM_SST_GET_VERSION);
Summer Qindb1448b2019-02-26 11:20:52 +080090 if (!TFM_PSA_HANDLE_IS_VALID(handle)) {
91 return PSA_PS_ERROR_OPERATION_FAILED;
92 }
93
94 status = psa_call(handle, in_vec, IOVEC_LEN(in_vec), out_vec,
95 IOVEC_LEN(out_vec));
96
97 psa_close(handle);
98
99 if (status != PSA_SUCCESS) {
100 return PSA_PS_ERROR_OPERATION_FAILED;
101 }
102#else
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000103 status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_req_veneer,
104 (uint32_t)in_vec, IOVEC_LEN(in_vec),
105 (uint32_t)out_vec, IOVEC_LEN(out_vec));
106
107 if (status != PSA_SUCCESS) {
108 return PSA_PS_ERROR_OPERATION_FAILED;
109 }
Summer Qindb1448b2019-02-26 11:20:52 +0800110#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100111
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000112 return err;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000113}
114
Jamie Foxb93da8b2018-12-13 18:27:30 +0000115psa_ps_status_t psa_ps_get_info(psa_ps_uid_t uid, struct psa_ps_info_t *p_info)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100116{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000117 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000118 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +0800119#ifdef TFM_PSA_API
120 psa_handle_t handle;
121#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100122
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000123 psa_invec in_vec[] = {
124 { .base = &uid, .len = sizeof(uid) }
125 };
126
127 psa_outvec out_vec[] = {
128 { .base = &err, .len = sizeof(err) },
129 { .base = p_info, .len = sizeof(*p_info) }
130 };
131
Summer Qindb1448b2019-02-26 11:20:52 +0800132#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +0800133 handle = psa_connect(TFM_SST_GET_INFO_SID, TFM_SST_GET_INFO_VERSION);
Summer Qindb1448b2019-02-26 11:20:52 +0800134 if (!TFM_PSA_HANDLE_IS_VALID(handle)) {
135 return PSA_PS_ERROR_OPERATION_FAILED;
136 }
137
138 status = psa_call(handle, in_vec, IOVEC_LEN(in_vec), out_vec,
139 IOVEC_LEN(out_vec));
140
141 psa_close(handle);
142
143 if (status != PSA_SUCCESS) {
144 return PSA_PS_ERROR_OPERATION_FAILED;
145 }
146#else
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000147 status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_info_req_veneer,
148 (uint32_t)in_vec, IOVEC_LEN(in_vec),
149 (uint32_t)out_vec, IOVEC_LEN(out_vec));
150
151 if (status != PSA_SUCCESS) {
152 return PSA_PS_ERROR_OPERATION_FAILED;
153 }
Summer Qindb1448b2019-02-26 11:20:52 +0800154#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100155
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000156 return err;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100157}
158
Jamie Foxb93da8b2018-12-13 18:27:30 +0000159psa_ps_status_t psa_ps_remove(psa_ps_uid_t uid)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100160{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000161 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000162 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +0800163#ifdef TFM_PSA_API
164 psa_handle_t handle;
165#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100166
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000167 psa_invec in_vec[] = {
168 { .base = &uid, .len = sizeof(uid) }
169 };
170
171 psa_outvec out_vec[] = {
172 { .base = &err, .len = sizeof(err) }
173 };
174
Summer Qindb1448b2019-02-26 11:20:52 +0800175#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +0800176 handle = psa_connect(TFM_SST_REMOVE_SID, TFM_SST_REMOVE_VERSION);
Summer Qindb1448b2019-02-26 11:20:52 +0800177 if (!TFM_PSA_HANDLE_IS_VALID(handle)) {
178 return PSA_PS_ERROR_OPERATION_FAILED;
179 }
180
181 status = psa_call(handle, in_vec, IOVEC_LEN(in_vec), out_vec,
182 IOVEC_LEN(out_vec));
183
184 psa_close(handle);
185
186 if (status != PSA_SUCCESS) {
187 return PSA_PS_ERROR_OPERATION_FAILED;
188 }
189#else
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000190 status = tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_remove_req_veneer,
191 (uint32_t)in_vec, IOVEC_LEN(in_vec),
192 (uint32_t)out_vec, IOVEC_LEN(out_vec));
193
194 if (status != PSA_SUCCESS) {
195 return PSA_PS_ERROR_OPERATION_FAILED;
196 }
Summer Qindb1448b2019-02-26 11:20:52 +0800197#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100198
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000199 return err;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100200}
201
Jamie Foxb93da8b2018-12-13 18:27:30 +0000202psa_ps_status_t psa_ps_create(psa_ps_uid_t uid, uint32_t size,
203 psa_ps_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000204{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000205 (void)uid;
206 (void)size;
207 (void)create_flags;
208
Jamie Foxb93da8b2018-12-13 18:27:30 +0000209 return PSA_PS_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000210}
211
Jamie Foxb93da8b2018-12-13 18:27:30 +0000212psa_ps_status_t psa_ps_set_extended(psa_ps_uid_t uid, uint32_t data_offset,
213 uint32_t data_length, const void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000214{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000215 (void)uid;
216 (void)data_offset;
217 (void)data_length;
218 (void)p_data;
219
Jamie Foxb93da8b2018-12-13 18:27:30 +0000220 return PSA_PS_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000221}
222
Jamie Foxb93da8b2018-12-13 18:27:30 +0000223uint32_t psa_ps_get_support(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000224{
Jamie Foxb93da8b2018-12-13 18:27:30 +0000225 /* Initialise support_flags to a sensible default, to avoid returning an
226 * uninitialised value in case the secure function fails.
227 */
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000228 uint32_t support_flags = 0;
Summer Qindb1448b2019-02-26 11:20:52 +0800229#ifdef TFM_PSA_API
230 psa_handle_t handle;
231#endif
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000232
233 psa_outvec out_vec[] = {
234 { .base = &support_flags, .len = sizeof(support_flags) }
235 };
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100236
Jamie Foxb93da8b2018-12-13 18:27:30 +0000237 /* The PSA API does not return an error, so any error from TF-M is
238 * ignored.
239 */
Summer Qindb1448b2019-02-26 11:20:52 +0800240#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +0800241 handle = psa_connect(TFM_SST_GET_SUPPORT_SID, TFM_SST_GET_SUPPORT_VERSION);
Summer Qindb1448b2019-02-26 11:20:52 +0800242 if (!TFM_PSA_HANDLE_IS_VALID(handle)) {
243 return support_flags;
244 }
245
246 (void)psa_call(handle, NULL, 0, out_vec, IOVEC_LEN(out_vec));
247
248 psa_close(handle);
249#else
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000250 (void)tfm_ns_lock_dispatch((veneer_fn)tfm_tfm_sst_get_support_req_veneer,
251 (uint32_t)NULL, 0,
252 (uint32_t)out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800253#endif
Jamie Foxb93da8b2018-12-13 18:27:30 +0000254
255 return support_flags;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000256}