blob: 4f937db2d03f55348a5d9da1c4bdab3408febca2 [file] [log] [blame]
Ashutosh Singhf4d88672017-11-29 13:35:43 +00001/*
Galanakis, Minosecc9de82019-11-20 14:29:44 +00002 * Copyright (c) 2017-2020, 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"
Edison Aib892dfe2019-06-21 11:28:11 +080011#include "psa_manifest/sid.h"
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000012
13#define IOVEC_LEN(x) (uint32_t)(sizeof(x)/sizeof(x[0]))
Mate Toth-Pal261df462018-08-07 12:02:42 +020014
Galanakis, Minosecc9de82019-11-20 14:29:44 +000015psa_status_t psa_ps_set(psa_storage_uid_t uid,
16 size_t data_length,
17 const void *p_data,
18 psa_storage_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000019{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000020 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080021 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010022
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000023 psa_invec in_vec[] = {
24 { .base = &uid, .len = sizeof(uid) },
25 { .base = p_data, .len = data_length },
26 { .base = &create_flags, .len = sizeof(create_flags) }
27 };
28
Kevin Pengc6d74502020-03-04 16:55:37 +080029 handle = psa_connect(TFM_PS_SET_SID, TFM_PS_SET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080030 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000031 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080032 }
33
Galanakis, Minosecc9de82019-11-20 14:29:44 +000034 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec),
35 NULL, 0);
Summer Qindb1448b2019-02-26 11:20:52 +080036
37 psa_close(handle);
38
Galanakis, Minosecc9de82019-11-20 14:29:44 +000039 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000040}
41
Galanakis, Minosecc9de82019-11-20 14:29:44 +000042psa_status_t psa_ps_get(psa_storage_uid_t uid,
43 size_t data_offset,
44 size_t data_size,
45 void *p_data,
46 size_t *p_data_length)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000047{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000048 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080049 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010050
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000051 psa_invec in_vec[] = {
52 { .base = &uid, .len = sizeof(uid) },
53 { .base = &data_offset, .len = sizeof(data_offset) }
54 };
55
56 psa_outvec out_vec[] = {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000057 { .base = p_data, .len = data_size }
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000058 };
59
Galanakis, Minosecc9de82019-11-20 14:29:44 +000060 if (p_data_length == NULL) {
61 return PSA_ERROR_INVALID_ARGUMENT;
62 }
63
Kevin Pengc6d74502020-03-04 16:55:37 +080064 handle = psa_connect(TFM_PS_GET_SID, TFM_PS_GET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080065 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000066 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080067 }
68
Summer Qin4b1d03b2019-07-02 14:56:08 +080069 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080070 IOVEC_LEN(out_vec));
71
72 psa_close(handle);
73
Galanakis, Minosecc9de82019-11-20 14:29:44 +000074 *p_data_length = out_vec[0].len;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010075
Galanakis, Minosecc9de82019-11-20 14:29:44 +000076 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000077}
78
Galanakis, Minosecc9de82019-11-20 14:29:44 +000079psa_status_t psa_ps_get_info(psa_storage_uid_t uid,
80 struct psa_storage_info_t *p_info)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +010081{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000082 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080083 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010084
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000085 psa_invec in_vec[] = {
86 { .base = &uid, .len = sizeof(uid) }
87 };
88
89 psa_outvec out_vec[] = {
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000090 { .base = p_info, .len = sizeof(*p_info) }
91 };
92
Kevin Pengc6d74502020-03-04 16:55:37 +080093 handle = psa_connect(TFM_PS_GET_INFO_SID, TFM_PS_GET_INFO_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080094 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000095 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080096 }
97
Summer Qin4b1d03b2019-07-02 14:56:08 +080098 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080099 IOVEC_LEN(out_vec));
100
101 psa_close(handle);
102
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000103 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100104}
105
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000106psa_status_t psa_ps_remove(psa_storage_uid_t uid)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100107{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000108 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +0800109 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100110
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000111 psa_invec in_vec[] = {
112 { .base = &uid, .len = sizeof(uid) }
113 };
114
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000115
Kevin Pengc6d74502020-03-04 16:55:37 +0800116 handle = psa_connect(TFM_PS_REMOVE_SID, TFM_PS_REMOVE_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800117 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000118 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +0800119 }
120
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000121 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec),
122 NULL, 0);
Summer Qindb1448b2019-02-26 11:20:52 +0800123
124 psa_close(handle);
125
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000126 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100127}
128
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000129psa_status_t psa_ps_create(psa_storage_uid_t uid, size_t size,
130 psa_storage_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000131{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000132 (void)uid;
133 (void)size;
134 (void)create_flags;
135
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000136 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000137}
138
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000139psa_status_t psa_ps_set_extended(psa_storage_uid_t uid, size_t data_offset,
140 size_t data_length, const void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000141{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000142 (void)uid;
143 (void)data_offset;
144 (void)data_length;
145 (void)p_data;
146
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000147 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000148}
149
Jamie Foxb93da8b2018-12-13 18:27:30 +0000150uint32_t psa_ps_get_support(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000151{
Jamie Foxb93da8b2018-12-13 18:27:30 +0000152 /* Initialise support_flags to a sensible default, to avoid returning an
153 * uninitialised value in case the secure function fails.
154 */
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000155 uint32_t support_flags = 0;
Summer Qindb1448b2019-02-26 11:20:52 +0800156 psa_handle_t handle;
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000157
158 psa_outvec out_vec[] = {
159 { .base = &support_flags, .len = sizeof(support_flags) }
160 };
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100161
Jamie Foxb93da8b2018-12-13 18:27:30 +0000162 /* The PSA API does not return an error, so any error from TF-M is
163 * ignored.
164 */
Kevin Pengc6d74502020-03-04 16:55:37 +0800165 handle = psa_connect(TFM_PS_GET_SUPPORT_SID, TFM_PS_GET_SUPPORT_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800166 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800167 return support_flags;
168 }
169
Summer Qin4b1d03b2019-07-02 14:56:08 +0800170 (void)psa_call(handle, PSA_IPC_CALL, NULL, 0, out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800171
172 psa_close(handle);
Jamie Foxb93da8b2018-12-13 18:27:30 +0000173
174 return support_flags;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000175}