1 /* Copyright (c) 2024 Nordic Semiconductor
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 #ifndef PSA_INTERNAL_TRUSTED_STORAGE_H
5 #define PSA_INTERNAL_TRUSTED_STORAGE_H
6 
7 /** @file psa/internal_trusted_storage.h The PSA Internal Trusted Storage (ITS) API.
8  * @ingroup psa_secure_storage
9  * For more information on the ITS, see [The Internal Trusted Storage API](https://arm-software.github.io/psa-api/storage/1.0/overview/architecture.html#the-internal-trusted-storage-api).
10  */
11 
12 /** @cond INTERNAL_HIDDEN */
13 #include "../internal/zephyr/secure_storage/its.h"
14 #ifdef BUILDING_MBEDTLS_CRYPTO
15 #define ITS_CALLER_ID SECURE_STORAGE_ITS_CALLER_MBEDTLS
16 #else
17 #define ITS_CALLER_ID SECURE_STORAGE_ITS_CALLER_PSA_ITS
18 #endif
19 #define ITS_UID (secure_storage_its_uid_t){.uid = uid, .caller_id = ITS_CALLER_ID}
20 /** @endcond */
21 
22 #include <psa/storage_common.h>
23 
24 #define PSA_ITS_API_VERSION_MAJOR 1
25 #define PSA_ITS_API_VERSION_MINOR 0
26 
27 /**
28  * @brief Creates a new or modifies an existing entry.
29  *
30  * Stores data in the internal storage.
31  *
32  * @param uid          The identifier of the data. Must be nonzero.
33  * @param data_length  The size in bytes of the data in `p_data` to store.
34  * @param p_data       A buffer containing the data to store.
35  * @param create_flags Flags indicating the properties of the entry.
36  *
37  * @retval PSA_SUCCESS                    The operation completed successfully.
38  * @retval PSA_ERROR_NOT_PERMITTED        An entry associated with the provided `uid` already
39  *                                        exists and was created with `PSA_STORAGE_FLAG_WRITE_ONCE`.
40  * @retval PSA_ERROR_NOT_SUPPORTED        One or more of the flags provided in `create_flags`
41  *                                        are not supported or invalid.
42  * @retval PSA_ERROR_INVALID_ARGUMENT     One or more arguments other than `create_flags` are
43  *                                        invalid.
44  * @retval PSA_ERROR_INSUFFICIENT_STORAGE There is insufficient space on the storage medium.
45  * @retval PSA_ERROR_STORAGE_FAILURE      The physical storage has failed (fatal error).
46  */
47 /** @cond INTERNAL_HIDDEN */
48 static ALWAYS_INLINE
49 /** @endcond  */
psa_its_set(psa_storage_uid_t uid,size_t data_length,const void * p_data,psa_storage_create_flags_t create_flags)50 psa_status_t psa_its_set(psa_storage_uid_t uid, size_t data_length,
51 			 const void *p_data, psa_storage_create_flags_t create_flags)
52 {
53 	return secure_storage_its_set(ITS_UID, data_length, p_data, create_flags);
54 }
55 
56 /**
57  * @brief Retrieves data associated with the provided `uid`.
58  *
59  * @param[in]  uid           The identifier of the data.
60  * @param[in]  data_offset   The offset, in bytes, from which to start reading the data.
61  * @param[in]  data_size     The number of bytes to read.
62  * @param[out] p_data        The buffer where the data will be placed on success.
63  *                           Must be at least `data_size` bytes long.
64  * @param[out] p_data_length On success, the number of bytes placed in `p_data`.
65  *
66  * @retval PSA_SUCCESS                The operation completed successfully.
67  * @retval PSA_ERROR_INVALID_ARGUMENT One or more of the arguments are invalid. This can also
68  *                                    happen if `data_offset` is larger than the size of the data
69  *                                    associated with `uid`.
70  * @retval PSA_ERROR_DOES_NOT_EXIST   The provided `uid` was not found in the storage.
71  * @retval PSA_ERROR_STORAGE_FAILURE  The physical storage has failed (fatal error).
72  */
73 /** @cond INTERNAL_HIDDEN */
74 static ALWAYS_INLINE
75 /** @endcond  */
psa_its_get(psa_storage_uid_t uid,size_t data_offset,size_t data_size,void * p_data,size_t * p_data_length)76 psa_status_t psa_its_get(psa_storage_uid_t uid, size_t data_offset,
77 			 size_t data_size, void *p_data, size_t *p_data_length)
78 {
79 	return secure_storage_its_get(ITS_UID, data_offset, data_size, p_data, p_data_length);
80 }
81 
82 /**
83  * @brief Retrieves the metadata of a given entry.
84  *
85  * @param[in]  uid    The identifier of the entry.
86  * @param[out] p_info A pointer to a `psa_storage_info_t` struct that will
87  *                    be populated with the metadata on success.
88  *
89  * @retval PSA_SUCCESS                The operation completed successfully.
90  * @retval PSA_ERROR_INVALID_ARGUMENT One or more of the arguments are invalid.
91  * @retval PSA_ERROR_DOES_NOT_EXIST   The provided `uid` was not found in the storage.
92  * @retval PSA_ERROR_STORAGE_FAILURE  The physical storage has failed (fatal error).
93  */
94 /** @cond INTERNAL_HIDDEN */
95 static ALWAYS_INLINE
96 /** @endcond  */
psa_its_get_info(psa_storage_uid_t uid,struct psa_storage_info_t * p_info)97 psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
98 {
99 	return secure_storage_its_get_info(ITS_UID, p_info);
100 }
101 
102 /**
103  * @brief Removes the provided `uid` and its associated data.
104  *
105  * Deletes all the data associated with the entry from internal storage.
106  *
107  * @param uid The identifier of the entry to remove.
108  *
109  * @retval PSA_SUCCESS                The operation completed successfully.
110  * @retval PSA_ERROR_NOT_PERMITTED    The entry was created with `PSA_STORAGE_FLAG_WRITE_ONCE`.
111  * @retval PSA_ERROR_INVALID_ARGUMENT `uid` is invalid.
112  * @retval PSA_ERROR_DOES_NOT_EXIST   The provided `uid` was not found in the storage.
113  * @retval PSA_ERROR_STORAGE_FAILURE  The physical storage has failed (fatal error).
114  */
115 /** @cond INTERNAL_HIDDEN */
116 static ALWAYS_INLINE
117 /** @endcond  */
psa_its_remove(psa_storage_uid_t uid)118 psa_status_t psa_its_remove(psa_storage_uid_t uid)
119 {
120 	return secure_storage_its_remove(ITS_UID);
121 }
122 
123 #undef ITS_UID
124 #undef ITS_CALLER_ID
125 
126 #endif
127