blob: 7cc3a63fffce09cbb40e5bb70f96ce9bb74c7cef [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 /* A parameter with a buffer pointer pointer that has data length longer
40 * than maximum permitted is treated as a secure violation.
41 * TF-M framework rejects the request with TFM_ERROR_INVALID_PARAMETER.
42 */
43 if (status == (psa_status_t)TFM_ERROR_INVALID_PARAMETER) {
44 return PSA_ERROR_INVALID_ARGUMENT;
Summer Qindb1448b2019-02-26 11:20:52 +080045 }
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010046
Galanakis, Minosecc9de82019-11-20 14:29:44 +000047 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000048}
49
Galanakis, Minosecc9de82019-11-20 14:29:44 +000050psa_status_t psa_ps_get(psa_storage_uid_t uid,
51 size_t data_offset,
52 size_t data_size,
53 void *p_data,
54 size_t *p_data_length)
Ashutosh Singhf4d88672017-11-29 13:35:43 +000055{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000056 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080057 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010058
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000059 psa_invec in_vec[] = {
60 { .base = &uid, .len = sizeof(uid) },
61 { .base = &data_offset, .len = sizeof(data_offset) }
62 };
63
64 psa_outvec out_vec[] = {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000065 { .base = p_data, .len = data_size }
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000066 };
67
Galanakis, Minosecc9de82019-11-20 14:29:44 +000068 if (p_data_length == NULL) {
69 return PSA_ERROR_INVALID_ARGUMENT;
70 }
71
Kevin Pengc6d74502020-03-04 16:55:37 +080072 handle = psa_connect(TFM_PS_GET_SID, TFM_PS_GET_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +080073 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +000074 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +080075 }
76
Summer Qin4b1d03b2019-07-02 14:56:08 +080077 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +080078 IOVEC_LEN(out_vec));
79
80 psa_close(handle);
81
Galanakis, Minosecc9de82019-11-20 14:29:44 +000082 *p_data_length = out_vec[0].len;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010083
Galanakis, Minosecc9de82019-11-20 14:29:44 +000084 return status;
Ashutosh Singhf4d88672017-11-29 13:35:43 +000085}
86
Galanakis, Minosecc9de82019-11-20 14:29:44 +000087psa_status_t psa_ps_get_info(psa_storage_uid_t uid,
88 struct psa_storage_info_t *p_info)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +010089{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000090 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +080091 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +010092
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000093 psa_invec in_vec[] = {
94 { .base = &uid, .len = sizeof(uid) }
95 };
96
97 psa_outvec out_vec[] = {
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +000098 { .base = p_info, .len = sizeof(*p_info) }
99 };
100
Kevin Pengc6d74502020-03-04 16:55:37 +0800101 handle = psa_connect(TFM_PS_GET_INFO_SID, TFM_PS_GET_INFO_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800102 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000103 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +0800104 }
105
Summer Qin4b1d03b2019-07-02 14:56:08 +0800106 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec), out_vec,
Summer Qindb1448b2019-02-26 11:20:52 +0800107 IOVEC_LEN(out_vec));
108
109 psa_close(handle);
110
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000111 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100112}
113
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000114psa_status_t psa_ps_remove(psa_storage_uid_t uid)
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100115{
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000116 psa_status_t status;
Summer Qindb1448b2019-02-26 11:20:52 +0800117 psa_handle_t handle;
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100118
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000119 psa_invec in_vec[] = {
120 { .base = &uid, .len = sizeof(uid) }
121 };
122
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000123
Kevin Pengc6d74502020-03-04 16:55:37 +0800124 handle = psa_connect(TFM_PS_REMOVE_SID, TFM_PS_REMOVE_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800125 if (!PSA_HANDLE_IS_VALID(handle)) {
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000126 return PSA_ERROR_GENERIC_ERROR;
Summer Qindb1448b2019-02-26 11:20:52 +0800127 }
128
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000129 status = psa_call(handle, PSA_IPC_CALL, in_vec, IOVEC_LEN(in_vec),
130 NULL, 0);
Summer Qindb1448b2019-02-26 11:20:52 +0800131
132 psa_close(handle);
133
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000134 return status;
Marc Moreno Berengue51af9512018-06-14 18:28:14 +0100135}
136
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000137psa_status_t psa_ps_create(psa_storage_uid_t uid, size_t size,
138 psa_storage_create_flags_t create_flags)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000139{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000140 (void)uid;
141 (void)size;
142 (void)create_flags;
143
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000144 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000145}
146
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000147psa_status_t psa_ps_set_extended(psa_storage_uid_t uid, size_t data_offset,
148 size_t data_length, const void *p_data)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000149{
Hugues de Valoned5d01a2019-02-19 14:41:38 +0000150 (void)uid;
151 (void)data_offset;
152 (void)data_length;
153 (void)p_data;
154
Galanakis, Minosecc9de82019-11-20 14:29:44 +0000155 return PSA_ERROR_NOT_SUPPORTED;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000156}
157
Jamie Foxb93da8b2018-12-13 18:27:30 +0000158uint32_t psa_ps_get_support(void)
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000159{
Jamie Foxb93da8b2018-12-13 18:27:30 +0000160 /* Initialise support_flags to a sensible default, to avoid returning an
161 * uninitialised value in case the secure function fails.
162 */
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000163 uint32_t support_flags = 0;
Summer Qindb1448b2019-02-26 11:20:52 +0800164 psa_handle_t handle;
Marc Moreno Berengue684f61e2019-01-25 13:29:52 +0000165
166 psa_outvec out_vec[] = {
167 { .base = &support_flags, .len = sizeof(support_flags) }
168 };
Marc Moreno Berengue10d0d362018-06-18 14:15:56 +0100169
Jamie Foxb93da8b2018-12-13 18:27:30 +0000170 /* The PSA API does not return an error, so any error from TF-M is
171 * ignored.
172 */
Kevin Pengc6d74502020-03-04 16:55:37 +0800173 handle = psa_connect(TFM_PS_GET_SUPPORT_SID, TFM_PS_GET_SUPPORT_VERSION);
Summer Qinba48ccd2019-07-03 16:31:29 +0800174 if (!PSA_HANDLE_IS_VALID(handle)) {
Summer Qindb1448b2019-02-26 11:20:52 +0800175 return support_flags;
176 }
177
Summer Qin4b1d03b2019-07-02 14:56:08 +0800178 (void)psa_call(handle, PSA_IPC_CALL, NULL, 0, out_vec, IOVEC_LEN(out_vec));
Summer Qindb1448b2019-02-26 11:20:52 +0800179
180 psa_close(handle);
Jamie Foxb93da8b2018-12-13 18:27:30 +0000181
182 return support_flags;
Ashutosh Singhf4d88672017-11-29 13:35:43 +0000183}