1 /*
2  *  Copyright (c) 2021, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  * @brief
32  *   This file includes platform abstraction for secure non-volatile storage of settings.
33  */
34 
35 #ifndef OPENTHREAD_POSIX_SECURE_SETTINGS_H_
36 #define OPENTHREAD_POSIX_SECURE_SETTINGS_H_
37 
38 #include <openthread/instance.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @addtogroup plat-settings
46  *
47  * @brief
48  *   This module includes the platform abstraction for secure non-volatile storage of settings.
49  *
50  * @{
51  *
52  */
53 
54 /**
55  * This function performs any initialization for the secure settings subsystem, if necessary.
56  *
57  * @param[in]  aInstance The OpenThread instance structure.
58  *
59  */
60 void otPosixSecureSettingsInit(otInstance *aInstance);
61 
62 /**
63  * This function performs any de-initialization for the secure settings subsystem, if necessary.
64  *
65  * @param[in]  aInstance The OpenThread instance structure.
66  *
67  */
68 void otPosixSecureSettingsDeinit(otInstance *aInstance);
69 
70 /**
71  * This function fetches the value of the setting identified by aKey and write it to the memory pointed to by aValue.
72  * It then writes the length to the integer pointed to by aValueLength. The initial value of aValueLength is the
73  * maximum number of bytes to be written to aValue.
74  *
75  * This function can be used to check for the existence of a key without fetching the value by setting aValue and
76  * aValueLength to NULL. You can also check the length of the setting without fetching it by setting only aValue
77  * to NULL.
78  *
79  * Note that the underlying storage implementation is not required to maintain the order of settings with multiple
80  * values. The order of such values MAY change after ANY write operation to the store.
81  *
82  * @param[in]     aInstance     The OpenThread instance structure.
83  * @param[in]     aKey          The key associated with the requested setting.
84  * @param[in]     aIndex        The index of the specific item to get.
85  * @param[out]    aValue        A pointer to where the value of the setting should be written. May be set to NULL if
86  *                              just testing for the presence or length of a setting.
87  * @param[inout]  aValueLength  A pointer to the length of the value. When called, this pointer should point to an
88  *                              integer containing the maximum value size that can be written to aValue. At return,
89  *                              the actual length of the setting is written. This may be set to NULL if performing
90  *                              a presence check.
91  *
92  * @retval OT_ERROR_NONE             The given setting was found and fetched successfully.
93  * @retval OT_ERROR_NOT_FOUND        The given setting was not found in the setting store.
94  * @retval OT_ERROR_NOT_IMPLEMENTED  This function is not implemented on this platform.
95  *
96  */
97 otError otPosixSecureSettingsGet(otInstance *aInstance,
98                                  uint16_t    aKey,
99                                  int         aIndex,
100                                  uint8_t *   aValue,
101                                  uint16_t *  aValueLength);
102 
103 /**
104  * This function sets or replaces the value of a setting identified by aKey. If there was more than one value
105  * previously associated with aKey, then they are all deleted and replaced with this single entry.
106  *
107  * Calling this function successfully may cause unrelated settings with multiple values to be reordered.
108  *
109  * @param[in]  aInstance     The OpenThread instance structure.
110  * @param[in]  aKey          The key associated with the setting to change.
111  * @param[in]  aValue        A pointer to where the new value of the setting should be read from. MUST NOT be NULL if
112  *                           aValueLength is non-zero.
113  * @param[in]  aValueLength  The length of the data pointed to by aValue. May be zero.
114  *
115  * @retval OT_ERROR_NONE             The given setting was changed or staged.
116  * @retval OT_ERROR_NOT_IMPLEMENTED  This function is not implemented on this platform.
117  * @retval OT_ERROR_NO_BUFS          No space remaining to store the given setting.
118  *
119  */
120 otError otPosixSecureSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
121 
122 /**
123  * This function adds the value to a setting identified by aKey, without replacing any existing values.
124  *
125  * Note that the underlying implementation is not required to maintain the order of the items associated with a
126  * specific key. The added value may be added to the end, the beginning, or even somewhere in the middle. The order
127  * of any pre-existing values may also change.
128  *
129  * Calling this function successfully may cause unrelated settings with multiple values to be reordered.
130  *
131  * @param[in]  aInstance     The OpenThread instance structure.
132  * @param[in]  aKey          The key associated with the setting to change.
133  * @param[in]  aValue        A pointer to where the new value of the setting should be read from. MUST NOT be NULL
134  *                           if aValueLength is non-zero.
135  * @param[in]  aValueLength  The length of the data pointed to by aValue. May be zero.
136  *
137  * @retval OT_ERROR_NONE             The given setting was added or staged to be added.
138  * @retval OT_ERROR_NOT_IMPLEMENTED  This function is not implemented on this platform.
139  * @retval OT_ERROR_NO_BUFS          No space remaining to store the given setting.
140  *
141  */
142 otError otPosixSecureSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
143 
144 /**
145  * This function deletes a specific value from the setting identified by aKey from the secure settings store.
146  *
147  * Note that the underlying implementation is not required to maintain the order of the items associated with a
148  * specific key.
149  *
150  * @param[in] aInstance  The OpenThread instance structure.
151  * @param[in] aKey       The key associated with the requested setting.
152  * @param[in] aIndex     The index of the value to be removed. If set to -1, all values for this aKey will be removed.
153  *
154  * @retval OT_ERROR_NONE             The given key and index was found and removed successfully.
155  * @retval OT_ERROR_NOT_FOUND        The given key or index was not found in the setting store.
156  * @retval OT_ERROR_NOT_IMPLEMENTED  This function is not implemented on this platform.
157  *
158  */
159 otError otPosixSecureSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex);
160 
161 /**
162  * This function deletes all settings from the secure settings store, resetting it to its initial factory state.
163  *
164  * @param[in] aInstance  The OpenThread instance structure.
165  *
166  */
167 void otPosixSecureSettingsWipe(otInstance *aInstance);
168 
169 /**
170  * @}
171  *
172  */
173 
174 #ifdef __cplusplus
175 } // extern "C"
176 #endif
177 
178 #endif // OPENTHREAD_POSIX_SECURE_SETTINGS_H_
179