1 /*
2  * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "psa/client.h"
9 #include "psa/protected_storage.h"
10 #include "psa_manifest/sid.h"
11 #include "tfm_ps_defs.h"
12 
psa_ps_set(psa_storage_uid_t uid,size_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)13 psa_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)
17 {
18     psa_status_t status;
19 
20     psa_invec in_vec[] = {
21         { .base = &uid,   .len = sizeof(uid) },
22         { .base = p_data, .len = data_length },
23         { .base = &create_flags, .len = sizeof(create_flags) }
24     };
25 
26     status = psa_call(TFM_PROTECTED_STORAGE_SERVICE_HANDLE, TFM_PS_SET, in_vec,
27                       IOVEC_LEN(in_vec), NULL, 0);
28 
29     return status;
30 }
31 
psa_ps_get(psa_storage_uid_t uid,size_t data_offset,size_t data_size,void * p_data,size_t * p_data_length)32 psa_status_t psa_ps_get(psa_storage_uid_t uid,
33                         size_t data_offset,
34                         size_t data_size,
35                         void *p_data,
36                         size_t *p_data_length)
37 {
38     psa_status_t status;
39 
40     psa_invec in_vec[] = {
41         { .base = &uid, .len = sizeof(uid) },
42         { .base = &data_offset, .len = sizeof(data_offset) }
43     };
44 
45     psa_outvec out_vec[] = {
46         { .base = p_data, .len = data_size }
47     };
48 
49     if (p_data_length == NULL) {
50         return PSA_ERROR_INVALID_ARGUMENT;
51     }
52 
53     status = psa_call(TFM_PROTECTED_STORAGE_SERVICE_HANDLE, TFM_PS_GET, in_vec,
54                       IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
55 
56     *p_data_length = out_vec[0].len;
57 
58     return status;
59 }
60 
psa_ps_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)61 psa_status_t psa_ps_get_info(psa_storage_uid_t uid,
62                              struct psa_storage_info_t *p_info)
63 {
64     psa_status_t status;
65 
66     psa_invec in_vec[] = {
67         { .base = &uid, .len = sizeof(uid) }
68     };
69 
70     psa_outvec out_vec[] = {
71         { .base = p_info, .len = sizeof(*p_info) }
72     };
73 
74     status = psa_call(TFM_PROTECTED_STORAGE_SERVICE_HANDLE, TFM_PS_GET_INFO,
75                       in_vec, IOVEC_LEN(in_vec), out_vec, IOVEC_LEN(out_vec));
76 
77     return status;
78 }
79 
psa_ps_remove(psa_storage_uid_t uid)80 psa_status_t psa_ps_remove(psa_storage_uid_t uid)
81 {
82     psa_status_t status;
83 
84     psa_invec in_vec[] = {
85         { .base = &uid, .len = sizeof(uid) }
86     };
87 
88     status = psa_call(TFM_PROTECTED_STORAGE_SERVICE_HANDLE, TFM_PS_REMOVE,
89                       in_vec, IOVEC_LEN(in_vec), NULL, 0);
90 
91     return status;
92 }
93 
psa_ps_create(psa_storage_uid_t uid,size_t size,psa_storage_create_flags_t create_flags)94 psa_status_t psa_ps_create(psa_storage_uid_t uid, size_t size,
95                            psa_storage_create_flags_t create_flags)
96 {
97     (void)uid;
98     (void)size;
99     (void)create_flags;
100 
101     return PSA_ERROR_NOT_SUPPORTED;
102 }
103 
psa_ps_set_extended(psa_storage_uid_t uid,size_t data_offset,size_t data_length,const void * p_data)104 psa_status_t psa_ps_set_extended(psa_storage_uid_t uid, size_t data_offset,
105                                  size_t data_length, const void *p_data)
106 {
107     (void)uid;
108     (void)data_offset;
109     (void)data_length;
110     (void)p_data;
111 
112     return PSA_ERROR_NOT_SUPPORTED;
113 }
114 
psa_ps_get_support(void)115 uint32_t psa_ps_get_support(void)
116 {
117     /* Initialise support_flags to a sensible default, to avoid returning an
118      * uninitialised value in case the secure function fails.
119      */
120     uint32_t support_flags = 0;
121 
122     psa_outvec out_vec[] = {
123         { .base = &support_flags, .len = sizeof(support_flags) }
124     };
125 
126     /* The PSA API does not return an error, so any error from TF-M is
127      * ignored.
128      */
129     (void)psa_call(TFM_PROTECTED_STORAGE_SERVICE_HANDLE, TFM_PS_GET_SUPPORT,
130                    NULL, 0, out_vec, IOVEC_LEN(out_vec));
131 
132     return support_flags;
133 }
134