1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  * Copyright (c) 2024 Cypress Semiconductor Corporation (an Infineon company)
4  * or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  */
9 
10 #ifndef __PS_OBJECT_TABLE_H__
11 #define __PS_OBJECT_TABLE_H__
12 
13 #include <stdint.h>
14 
15 #include "psa/protected_storage.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*!
22  * \struct ps_obj_table_info_t
23  *
24  * \brief Object table information structure.
25  */
26 struct ps_obj_table_info_t {
27     uint32_t fid;        /*!< File ID in the file system */
28 #ifdef PS_ENCRYPTION
29     uint8_t *tag;        /*!< Pointer to the MAC value of AEAD object */
30 #if PS_AES_KEY_USAGE_LIMIT != 0
31     uint32_t num_blocks; /*!< blocks encrypted/decrypted with current key */
32 #endif
33 #else
34     uint32_t version;  /*!< Object version */
35 #endif
36 };
37 
38 /**
39  * \brief Creates object table.
40  *
41  * \return Returns error code as specified in \ref psa_status_t
42  */
43 psa_status_t ps_object_table_create(void);
44 
45 /**
46  * \brief Initializes object table.
47  *
48  * \param[in,out] obj_data  Pointer to the static object data allocated
49  *                          in other to reuse that memory to allocated a
50  *                          temporary object table.
51  *
52  * \return Returns error code as specified in \ref psa_status_t
53  */
54 psa_status_t ps_object_table_init(uint8_t *obj_data);
55 
56 /**
57  * \brief Checks if there is an entry in the table for the provided UID and
58  *        client ID pair.
59  *
60  * \param[in] uid        Identifier for the data
61  * \param[in] client_id  Identifier of the asset’s owner (client)
62  *
63  * \return Returns error code as specified in \ref psa_status_t
64  *
65  * \retval PSA_SUCCESS                 If there is a table entry for the object
66  * \retval PSA_ERROR_DOES_NOT_EXIST    If no table entry exists for the object
67  */
68 psa_status_t ps_object_table_obj_exist(psa_storage_uid_t uid,
69                                        int32_t client_id);
70 
71 /**
72  * \brief Gets a not in use file ID.
73  *
74  * \param[in] fid_num Amount of file IDs that the function will check are
75  *                    free before returning one. 0 is an invalid input and
76  *                    will error. Note that this function will only ever
77  *                    return 1 file ID.
78  * \param[out] p_fid  Pointer to the location to store the file ID
79  *
80  * \return Returns PSA_SUCCESS if the fid is valid and fid_num - 1 entries
81  *         are still free in the table. Otherwise, it returns an error code as
82  *         specified in \ref psa_status_t
83  */
84 psa_status_t ps_object_table_get_free_fid(uint32_t fid_num, uint32_t *p_fid);
85 
86 #if PS_AES_KEY_USAGE_LIMIT != 0
87 /**
88  * \brief Get the generation number to use for key generation
89  *
90  * \return Returns the current key generation
91  */
92 uint32_t ps_object_table_current_gen(void);
93 
94 /**
95  * \brief Set the generation number to use for key generation
96  *
97  * \param [in] new_gen  The new key generation
98  */
99 void ps_object_table_set_gen(uint32_t new_gen);
100 #endif
101 
102 /**
103  * \brief Sets object table information in the object table and stores it
104  *        persistently, for the provided UID and client ID pair.
105  *
106  * \param[in] uid           Identifier for the data.
107  * \param[in] client_id     Identifier of the asset’s owner (client)
108  * \param[in] obj_tbl_info  Pointer to the location to store object table
109  *                          information \ref ps_obj_table_info_t
110  *
111  * \note  A call to this function results in writing the table to the
112  *        file system.
113  *
114  * \return Returns error code as specified in \ref psa_status_t
115  */
116 psa_status_t ps_object_table_set_obj_tbl_info(psa_storage_uid_t uid,
117                                               int32_t client_id,
118                                 const struct ps_obj_table_info_t *obj_tbl_info);
119 
120 /**
121  * \brief Gets object table information from the object table for the provided
122  *        UID and client ID pair.
123  *
124  * \param[in]  uid           Identifier for the data.
125  * \param[in]  client_id     Identifier of the asset’s owner (client)
126  * \param[out] obj_tbl_info  Pointer to the location to store object table
127  *                           information
128  *
129  * \return Returns PSA_SUCCESS if the object exists. Otherwise, it
130  *         returns PSA_ERROR_DOES_NOT_EXIST.
131  */
132 psa_status_t ps_object_table_get_obj_tbl_info(psa_storage_uid_t uid,
133                                               int32_t client_id,
134                                       struct ps_obj_table_info_t *obj_tbl_info);
135 
136 /**
137  * \brief Deletes the table entry for the provided UID and client ID pair.
138  *
139  * \param[in]  uid        Identifier for the data.
140  * \param[in]  client_id  Identifier of the asset’s owner (client)
141  *
142  * \return Returns error code as specified in \ref psa_status_t
143  */
144 psa_status_t ps_object_table_delete_object(psa_storage_uid_t uid,
145                                            int32_t client_id);
146 
147 /**
148  * \brief Deletes old object table from the persistent area.
149  *
150  * \return Returns error code as specified in \ref psa_status_t
151  */
152 psa_status_t ps_object_table_delete_old_table(void);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif /* __PS_OBJECT_TABLE_H__ */
159