blob: c3a1504dc1945e551abf41ac19b69c1c8126def9 [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
Antonio de Angelis05b24192019-07-04 15:28:46 +010010#include "tfm_ns_interface.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]))
Mate Toth-Pal261df462018-08-07 12:02:42 +020017
Jamie Foxb93da8b2018-12-13 18:27:30 +000018psa_ps_status_t psa_ps_set(psa_ps_uid_t uid,
19 uint32_t data_length,
20 const void *p_data,
21 psa_ps_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000022{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000023 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000024 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +080025#ifdef TFM_PSA_API
26 psa_handle_t handle;
27#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010028
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000029 psa_invec in_vec[] = {
30 { .base = &uid, .len = sizeof(uid) },
31 { .base = p_data, .len = data_length },
32 { .base = &create_flags, .len = sizeof(create_flags) }
33 };
34
35 psa_outvec out_vec[] = {
36 { .base = &err , .len = sizeof(err) }
37 };
38
Summer Qindb1448b2019-02-26 11:20:52 +080039#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +080040 handle = psa_connect(TFM_SST_SET_SID, TFM_SST_SET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080041 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +080042 return PSA_PS_ERROR_OPERATION_FAILED;
43 }
44
Summer Qin4b1d03b2019-07-02 14:56:08 +080045 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080046 IOVEC_LEN(out_vec));
47
48 psa_close(handle);
49
50 if (status != PSA_SUCCESS) {
51 return PSA_PS_ERROR_OPERATION_FAILED;
52 }
53#else
Antonio de Angelis05b24192019-07-04 15:28:46 +010054 status = tfm_ns_interface_dispatch(
55 (veneer_fn)tfm_tfm_sst_set_req_veneer,
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000056 (uint32_t)in_vec, IOVEC_LEN(in_vec),
57 (uint32_t)out_vec, IOVEC_LEN(out_vec));
58 if (status != PSA_SUCCESS) {
59 return PSA_PS_ERROR_OPERATION_FAILED;
60 }
Summer Qindb1448b2019-02-26 11:20:52 +080061#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010062
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000063 return err;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000064}
65
Jamie Foxb93da8b2018-12-13 18:27:30 +000066psa_ps_status_t psa_ps_get(psa_ps_uid_t uid,
67 uint32_t data_offset,
68 uint32_t data_length,
69 void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000070{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000071 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +000072 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +080073#ifdef TFM_PSA_API
74 psa_handle_t handle;
75#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010076
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000077 psa_invec in_vec[] = {
78 { .base = &uid, .len = sizeof(uid) },
79 { .base = &data_offset, .len = sizeof(data_offset) }
80 };
81
82 psa_outvec out_vec[] = {
83 { .base = &err, .len = sizeof(err) },
84 { .base = p_data, .len = data_length }
85 };
86
Summer Qindb1448b2019-02-26 11:20:52 +080087#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +080088 handle = psa_connect(TFM_SST_GET_SID, TFM_SST_GET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080089 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +080090 return PSA_PS_ERROR_OPERATION_FAILED;
91 }
92
Summer Qin4b1d03b2019-07-02 14:56:08 +080093 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080094 IOVEC_LEN(out_vec));
95
96 psa_close(handle);
97
98 if (status != PSA_SUCCESS) {
99 return PSA_PS_ERROR_OPERATION_FAILED;
100 }
101#else
Antonio de Angelis05b24192019-07-04 15:28:46 +0100102 status = tfm_ns_interface_dispatch(
103 (veneer_fn)tfm_tfm_sst_get_req_veneer,
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000104 (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 Qinba48ccd2019-07-03 16:31:29 +0800134 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800135 return PSA_PS_ERROR_OPERATION_FAILED;
136 }
137
Summer Qin4b1d03b2019-07-02 14:56:08 +0800138 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +0800139 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
Antonio de Angelis05b24192019-07-04 15:28:46 +0100147 status = tfm_ns_interface_dispatch(
148 (veneer_fn)tfm_tfm_sst_get_info_req_veneer,
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000149 (uint32_t)in_vec, IOVEC_LEN(in_vec),
150 (uint32_t)out_vec, IOVEC_LEN(out_vec));
151
152 if (status != PSA_SUCCESS) {
153 return PSA_PS_ERROR_OPERATION_FAILED;
154 }
Summer Qindb1448b2019-02-26 11:20:52 +0800155#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100156
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000157 return err;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100158}
159
Jamie Foxb93da8b2018-12-13 18:27:30 +0000160psa_ps_status_t psa_ps_remove(psa_ps_uid_t uid)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100161{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000162 psa_status_t status;
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000163 psa_ps_status_t err;
Summer Qindb1448b2019-02-26 11:20:52 +0800164#ifdef TFM_PSA_API
165 psa_handle_t handle;
166#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100167
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000168 psa_invec in_vec[] = {
169 { .base = &uid, .len = sizeof(uid) }
170 };
171
172 psa_outvec out_vec[] = {
173 { .base = &err, .len = sizeof(err) }
174 };
175
Summer Qindb1448b2019-02-26 11:20:52 +0800176#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +0800177 handle = psa_connect(TFM_SST_REMOVE_SID, TFM_SST_REMOVE_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800178 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800179 return PSA_PS_ERROR_OPERATION_FAILED;
180 }
181
Summer Qin4b1d03b2019-07-02 14:56:08 +0800182 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +0800183 IOVEC_LEN(out_vec));
184
185 psa_close(handle);
186
187 if (status != PSA_SUCCESS) {
188 return PSA_PS_ERROR_OPERATION_FAILED;
189 }
190#else
Antonio de Angelis05b24192019-07-04 15:28:46 +0100191 status = tfm_ns_interface_dispatch(
192 (veneer_fn)tfm_tfm_sst_remove_req_veneer,
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000193 (uint32_t)in_vec, IOVEC_LEN(in_vec),
194 (uint32_t)out_vec, IOVEC_LEN(out_vec));
195
196 if (status != PSA_SUCCESS) {
197 return PSA_PS_ERROR_OPERATION_FAILED;
198 }
Summer Qindb1448b2019-02-26 11:20:52 +0800199#endif
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100200
Marc Moreno Berengue15d8a312019-01-25 14:35:52 +0000201 return err;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100202}
203
Jamie Foxb93da8b2018-12-13 18:27:30 +0000204psa_ps_status_t psa_ps_create(psa_ps_uid_t uid, uint32_t size,
205 psa_ps_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000206{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000207 (void)uid;
208 (void)size;
209 (void)create_flags;
210
Jamie Foxb93da8b2018-12-13 18:27:30 +0000211 return PSA_PS_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000212}
213
Jamie Foxb93da8b2018-12-13 18:27:30 +0000214psa_ps_status_t psa_ps_set_extended(psa_ps_uid_t uid, uint32_t data_offset,
215 uint32_t data_length, const void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000216{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000217 (void)uid;
218 (void)data_offset;
219 (void)data_length;
220 (void)p_data;
221
Jamie Foxb93da8b2018-12-13 18:27:30 +0000222 return PSA_PS_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000223}
224
Jamie Foxb93da8b2018-12-13 18:27:30 +0000225uint32_t psa_ps_get_support(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000226{
Jamie Foxb93da8b2018-12-13 18:27:30 +0000227 /* Initialise support_flags to a sensible default, to avoid returning an
228 * uninitialised value in case the secure function fails.
229 */
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000230 uint32_t support_flags = 0;
Summer Qindb1448b2019-02-26 11:20:52 +0800231#ifdef TFM_PSA_API
232 psa_handle_t handle;
233#endif
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000234
235 psa_outvec out_vec[] = {
236 { .base = &support_flags, .len = sizeof(support_flags) }
237 };
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100238
Jamie Foxb93da8b2018-12-13 18:27:30 +0000239 /* The PSA API does not return an error, so any error from TF-M is
240 * ignored.
241 */
Summer Qindb1448b2019-02-26 11:20:52 +0800242#ifdef TFM_PSA_API
Edison Aib892dfe2019-06-21 11:28:11 +0800243 handle = psa_connect(TFM_SST_GET_SUPPORT_SID, TFM_SST_GET_SUPPORT_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800244 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800245 return support_flags;
246 }
247
Summer Qin4b1d03b2019-07-02 14:56:08 +0800248 (void)psa_call(handle, PSA_IPC_CALL, NULL, 0, out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800249
250 psa_close(handle);
251#else
Antonio de Angelis05b24192019-07-04 15:28:46 +0100252 (void)tfm_ns_interface_dispatch(
253 (veneer_fn)tfm_tfm_sst_get_support_req_veneer,
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000254 (uint32_t)NULL, 0,
255 (uint32_t)out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800256#endif
Jamie Foxb93da8b2018-12-13 18:27:30 +0000257
258 return support_flags;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000259}