blob: 106917e9bc234f0d4857b49fbfe4ddd7f243afcd [file] [log] [blame]
Ashutosh Singhf4d88672017-11-29 13:35:43 +00001/*
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08002 * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
Ashutosh Singhf4d88672017-11-29 13:35:43 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Xinyu Zhangade2e0a2021-03-18 16:20:54 +08008#include "psa/client.h"
Jamie Foxcc31d402019-01-28 17:13:52 +00009#include "psa/protected_storage.h"
Edison Aib892dfe2019-06-21 11:28:11 +080010#include "psa_manifest/sid.h"
Xinyu Zhangade2e0a2021-03-18 16:20:54 +080011#include "tfm_ns_interface.h"
Mate Toth-Pal261df462018-08-07 12:02:42 +020012
Galanakis, Minosecc9de82019-11-20 14:29:44 +000013psa_status_t psa_ps_set(psa_storage_uid_t uid,
14 size_t data_length,
15 const void *p_data,
16 psa_storage_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000017{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000018 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080019 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010020
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000021 psa_invec in_vec[] = {
22 { .base = &uid, .len = sizeof(uid) },
23 { .base = p_data, .len = data_length },
24 { .base = &create_flags, .len = sizeof(create_flags) }
25 };
26
Kevin Pengc6d74502020-03-04 16:55:37 +080027 handle = psa_connect(TFM_PS_SET_SID, TFM_PS_SET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080028 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000029 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080030 }
31
Galanakis, Minosecc9de82019-11-20 14:29:44 +000032 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec),
33 NULL, 0);
Summer Qindb1448b2019-02-26 11:20:52 +080034
35 psa_close(handle);
36
Galanakis, Minosecc9de82019-11-20 14:29:44 +000037 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000038}
39
Galanakis, Minosecc9de82019-11-20 14:29:44 +000040psa_status_t psa_ps_get(psa_storage_uid_t uid,
41 size_t data_offset,
42 size_t data_size,
43 void *p_data,
44 size_t *p_data_length)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000045{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000046 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080047 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010048
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000049 psa_invec in_vec[] = {
50 { .base = &uid, .len = sizeof(uid) },
51 { .base = &data_offset, .len = sizeof(data_offset) }
52 };
53
54 psa_outvec out_vec[] = {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000055 { .base = p_data, .len = data_size }
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000056 };
57
Galanakis, Minosecc9de82019-11-20 14:29:44 +000058 if (p_data_length == NULL) {
59 return PSA_ERROR_INVALID_ARGUMENT;
60 }
61
Kevin Pengc6d74502020-03-04 16:55:37 +080062 handle = psa_connect(TFM_PS_GET_SID, TFM_PS_GET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080063 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000064 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080065 }
66
Summer Qin4b1d03b2019-07-02 14:56:08 +080067 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080068 IOVEC_LEN(out_vec));
69
70 psa_close(handle);
71
Galanakis, Minosecc9de82019-11-20 14:29:44 +000072 *p_data_length = out_vec[0].len;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010073
Galanakis, Minosecc9de82019-11-20 14:29:44 +000074 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000075}
76
Galanakis, Minosecc9de82019-11-20 14:29:44 +000077psa_status_t psa_ps_get_info(psa_storage_uid_t uid,
78 struct psa_storage_info_t *p_info)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +010079{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000080 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080081 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010082
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000083 psa_invec in_vec[] = {
84 { .base = &uid, .len = sizeof(uid) }
85 };
86
87 psa_outvec out_vec[] = {
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000088 { .base = p_info, .len = sizeof(*p_info) }
89 };
90
Kevin Pengc6d74502020-03-04 16:55:37 +080091 handle = psa_connect(TFM_PS_GET_INFO_SID, TFM_PS_GET_INFO_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080092 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000093 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080094 }
95
Summer Qin4b1d03b2019-07-02 14:56:08 +080096 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080097 IOVEC_LEN(out_vec));
98
99 psa_close(handle);
100
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000101 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100102}
103
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000104psa_status_t psa_ps_remove(psa_storage_uid_t uid)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100105{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000106 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +0800107 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100108
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000109 psa_invec in_vec[] = {
110 { .base = &uid, .len = sizeof(uid) }
111 };
112
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000113
Kevin Pengc6d74502020-03-04 16:55:37 +0800114 handle = psa_connect(TFM_PS_REMOVE_SID, TFM_PS_REMOVE_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800115 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000116 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +0800117 }
118
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000119 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec),
120 NULL, 0);
Summer Qindb1448b2019-02-26 11:20:52 +0800121
122 psa_close(handle);
123
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000124 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100125}
126
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000127psa_status_t psa_ps_create(psa_storage_uid_t uid, size_t size,
128 psa_storage_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000129{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000130 (void)uid;
131 (void)size;
132 (void)create_flags;
133
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000134 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000135}
136
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000137psa_status_t psa_ps_set_extended(psa_storage_uid_t uid, size_t data_offset,
138 size_t data_length, const void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000139{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000140 (void)uid;
141 (void)data_offset;
142 (void)data_length;
143 (void)p_data;
144
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000145 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000146}
147
Jamie Foxb93da8b2018-12-13 18:27:30 +0000148uint32_t psa_ps_get_support(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000149{
Jamie Foxb93da8b2018-12-13 18:27:30 +0000150 /* Initialise support_flags to a sensible default, to avoid returning an
151 * uninitialised value in case the secure function fails.
152 */
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000153 uint32_t support_flags = 0;
Summer Qindb1448b2019-02-26 11:20:52 +0800154 psa_handle_t handle;
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000155
156 psa_outvec out_vec[] = {
157 { .base = &support_flags, .len = sizeof(support_flags) }
158 };
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100159
Jamie Foxb93da8b2018-12-13 18:27:30 +0000160 /* The PSA API does not return an error, so any error from TF-M is
161 * ignored.
162 */
Kevin Pengc6d74502020-03-04 16:55:37 +0800163 handle = psa_connect(TFM_PS_GET_SUPPORT_SID, TFM_PS_GET_SUPPORT_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800164 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800165 return support_flags;
166 }
167
Summer Qin4b1d03b2019-07-02 14:56:08 +0800168 (void)psa_call(handle, PSA_IPC_CALL, NULL, 0, out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800169
170 psa_close(handle);
Jamie Foxb93da8b2018-12-13 18:27:30 +0000171
172 return support_flags;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000173}