Mistake on this page?
Report an issue in GitHub or email us
SecureStore.h
1 /*
2  * Copyright (c) 2018 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MBED_SECURESTORE_H
18 #define MBED_SECURESTORE_H
19 
20 #if !defined(MBEDTLS_CONFIG_FILE)
21 #include "mbedtls/config.h"
22 #else
23 #include MBEDTLS_CONFIG_FILE
24 #endif
25 
26 #include "device_key/DeviceKey.h"
27 
28 #define SECURESTORE_ENABLED 1
29 
30 // Whole class is not supported if entropy, device key or required mbed TLS features are not enabled
31 #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CIPHER_MODE_CTR) || !defined(MBEDTLS_CMAC_C) || !DEVICEKEY_ENABLED
32 #undef SECURESTORE_ENABLED
33 #define SECURESTORE_ENABLED 0
34 #endif
35 
36 #if SECURESTORE_ENABLED || defined(DOXYGEN_ONLY)
37 
38 #include <stdint.h>
39 #include <stdio.h>
40 #include "KVStore.h"
41 #include "PlatformMutex.h"
42 
43 // Forward declarations
44 struct mbedtls_entropy_context;
45 
46 namespace mbed {
47 
48 /** TDBStore class
49  *
50  * Lightweight Key Value storage over a block device
51  */
52 
53 class SecureStore : public KVStore {
54 public:
55 
56  /**
57  * @brief Class constructor
58  *
59  * @param[in] underlying_kv KVStore that will hold the data.
60  * @param[in] rbp_kv Additional KVStore used for rollback protection.
61  *
62  * @returns none
63  */
64  SecureStore(KVStore *underlying_kv, KVStore *rbp_kv = 0);
65 
66  /**
67  * @brief Class destructor
68  *
69  * @returns none
70  */
71  virtual ~SecureStore();
72 
73  /**
74  * @brief Initialize SecureStore class. It will also initialize
75  * the underlying KVStore and the rollback protection KVStore.
76  *
77  * @returns MBED_SUCCESS Success.
78  * or any other error from underlying KVStore instances.
79  */
80  virtual int init();
81 
82  /**
83  * @brief Deinitialize SecureStore class, free handles and memory allocations.
84  *
85  * @returns MBED_SUCCESS Success.
86  * or any other error from underlying KVStore instances.
87  */
88  virtual int deinit();
89 
90 
91  /**
92  * @brief Reset KVStore contents (clear all keys)
93  * Warning: This function is not thread safe.
94  *
95  * @returns MBED_SUCCESS Success.
96  * MBED_ERROR_NOT_READY Not initialized.
97  * or any other error from underlying KVStore instances.
98  */
99  virtual int reset();
100 
101  /**
102  * @brief Set one KVStore item, given key and value.
103  *
104  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
105  * @param[in] buffer Value data buffer.
106  * @param[in] size Value data size.
107  * @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
108  * REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
109  *
110  * @returns MBED_SUCCESS Success.
111  * MBED_ERROR_NOT_READY Not initialized.
112  * MBED_ERROR_READ_FAILED Unable to read from media.
113  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
114  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
115  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
116  * MBED_ERROR_FAILED_OPERATION Internal error.
117  * or any other error from underlying KVStore instances.
118  */
119  virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);
120 
121  /**
122  * @brief Get one KVStore item, given key.
123  *
124  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
125  * @param[in] buffer Value data buffer.
126  * @param[in] buffer_size Value data buffer size.
127  * @param[out] actual_size Actual read size.
128  * @param[in] offset Offset to read from in data.
129  *
130  * @returns MBED_SUCCESS Success.
131  * MBED_ERROR_NOT_READY Not initialized.
132  * MBED_ERROR_READ_FAILED Unable to read from media.
133  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
134  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
135  * MBED_ERROR_FAILED_OPERATION Internal error.
136  * MBED_ERROR_ITEM_NOT_FOUND No such key.
137  * MBED_ERROR_AUTHENTICATION_FAILED Data authentication failed.
138  * MBED_ERROR_AUTHENTICATION_RBP_FAILED
139  * Rollback protection data authentication failed.
140  * or any other error from underlying KVStore instances.
141  */
142  virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
143  size_t offset = 0);
144 
145  /**
146  * @brief Get information of a given key.
147  *
148  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
149  * @param[out] info Returned information structure containing size and flags.
150  *
151  * @returns MBED_SUCCESS Success.
152  * MBED_ERROR_NOT_READY Not initialized.
153  * MBED_ERROR_READ_FAILED Unable to read from media.
154  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
155  * MBED_ERROR_FAILED_OPERATION Internal error.
156  * MBED_ERROR_ITEM_NOT_FOUND No such key.
157  * MBED_ERROR_AUTHENTICATION_FAILED Data authentication failed.
158  * MBED_ERROR_AUTHENTICATION_RBP_FAILED
159  * Rollback protection data authentication failed.
160  * or any other error from underlying KVStore instances.
161  */
162  virtual int get_info(const char *key, info_t *info);
163 
164  /**
165  * @brief Remove a KVStore item, given key.
166  *
167  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
168  *
169  * @returns MBED_SUCCESS Success.
170  * MBED_ERROR_NOT_READY Not initialized.
171  * MBED_ERROR_READ_FAILED Unable to read from media.
172  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
173  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
174  * MBED_ERROR_FAILED_OPERATION Internal error.
175  * or any other error from underlying KVStore instances.
176  */
177  virtual int remove(const char *key);
178 
179 
180  /**
181  * @brief Start an incremental KVStore set sequence. This operation is blocking other operations.
182  * Any get/set/remove/iterator operation will be blocked until set_finalize is called.
183  *
184  * @param[out] handle Returned incremental set handle.
185  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
186  * @param[in] final_data_size Final value data size.
187  * @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
188  * REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
189  *
190  * @returns MBED_SUCCESS Success.
191  * MBED_ERROR_NOT_READY Not initialized.
192  * MBED_ERROR_READ_FAILED Unable to read from media.
193  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
194  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
195  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
196  * MBED_ERROR_FAILED_OPERATION Internal error.
197  * or any other error from underlying KVStore instances.
198  */
199  virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);
200 
201  /**
202  * @brief Add data to incremental KVStore set sequence. This operation is blocking other operations.
203  * Any get/set/remove operation will be blocked until set_finalize is called.
204  *
205  * @param[in] handle Incremental set handle.
206  * @param[in] value_data value data to add.
207  * @param[in] data_size value data size.
208  *
209  * @returns MBED_SUCCESS Success.
210  * MBED_ERROR_NOT_READY Not initialized.
211  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
212  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
213  * MBED_ERROR_FAILED_OPERATION Internal error.
214  * or any other error from underlying KVStore instances.
215  */
216  virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);
217 
218  /**
219  * @brief Finalize an incremental KVStore set sequence.
220  *
221  * @param[in] handle Incremental set handle.
222  *
223  * @returns MBED_SUCCESS Success.
224  * MBED_ERROR_NOT_READY Not initialized.
225  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
226  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
227  * MBED_ERROR_FAILED_OPERATION Internal error.
228  * or any other error from underlying KVStore instances.
229  */
230  virtual int set_finalize(set_handle_t handle);
231 
232  /**
233  * @brief Start an iteration over KVStore keys.
234  * There are no issue with any other operation while iterator is open.
235  *
236  * @param[out] it Returned iterator handle.
237  * @param[in] prefix Key prefix (null for all keys).
238  *
239  * @returns MBED_SUCCESS Success.
240  * MBED_ERROR_NOT_READY Not initialized.
241  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
242  * or any other error from underlying KVStore instances.
243  */
244  virtual int iterator_open(iterator_t *it, const char *prefix = NULL);
245 
246  /**
247  * @brief Get next key in iteration.
248  * There are no issue with any other operation while iterator is open.
249  *
250  * @param[in] it Iterator handle.
251  * @param[in] key Buffer for returned key.
252  * @param[in] key_size Key buffer size.
253  *
254  * @returns MBED_SUCCESS Success.
255  * MBED_ERROR_NOT_READY Not initialized.
256  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
257  * or any other error from underlying KVStore instances.
258  */
259  virtual int iterator_next(iterator_t it, char *key, size_t key_size);
260 
261  /**
262  * @brief Close iteration.
263  *
264  * @returns MBED_SUCCESS Success.
265  * MBED_ERROR_NOT_READY Not initialized.
266  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
267  * or any other error from underlying KVStore instances.
268  *
269  * @returns 0 on success or a negative error code on failure
270  */
271  virtual int iterator_close(iterator_t it);
272 
273 #if !defined(DOXYGEN_ONLY)
274 private:
275  // Forward declaration
276  struct inc_set_handle_t;
277 
278  PlatformMutex _mutex;
279  bool _is_initialized;
280  KVStore *_underlying_kv, *_rbp_kv;
281  mbedtls_entropy_context *_entropy;
282  inc_set_handle_t *_ih;
283  uint8_t *_scratch_buf;
284 
285  /**
286  * @brief Actual get function, serving get and get_info APIs.
287  *
288  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
289  * @param[in] buffer Value data buffer.
290  * @param[in] buffer_size Value data buffer size.
291  * @param[out] actual_size Actual read size.
292  * @param[in] offset Offset to read from in data.
293  * @param[out] info Returned information structure.
294  *
295  * @returns 0 on success or a negative error code on failure
296  */
297  int do_get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
298  size_t offset = 0, info_t *info = 0);
299 #endif
300 };
301 /** @}*/
302 
303 } // namespace mbed
304 
305 #endif
306 #endif
virtual int reset()
Reset KVStore contents (clear all keys) Warning: This function is not thread safe.
virtual int init()
Initialize SecureStore class.
Holds key information.
Definition: KVStore.h:48
virtual int deinit()
Deinitialize SecureStore class, free handles and memory allocations.
virtual ~SecureStore()
Class destructor.
virtual int iterator_open(iterator_t *it, const char *prefix=NULL)
Start an iteration over KVStore keys.
virtual int get_info(const char *key, info_t *info)
Get information of a given key.
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags)
Start an incremental KVStore set sequence.
virtual int iterator_close(iterator_t it)
Close iteration.
virtual int set_finalize(set_handle_t handle)
Finalize an incremental KVStore set sequence.
KVStore class.
Definition: KVStore.h:30
virtual int iterator_next(iterator_t it, char *key, size_t key_size)
Get next key in iteration.
Definition: ATHandler.h:46
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size)
Add data to incremental KVStore set sequence.
TDBStore class.
Definition: SecureStore.h:53
SecureStore(KVStore *underlying_kv, KVStore *rbp_kv=0)
Class constructor.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.