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 "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 namespace mbed {
44 
45 /** TDBStore class
46  *
47  * Lightweight Key Value storage over a block device
48  */
49 
50 class SecureStore : public KVStore {
51 public:
52 
53  /**
54  * @brief Class constructor
55  *
56  * @param[in] underlying_kv KVStore that will hold the data.
57  * @param[in] rbp_kv Additional KVStore used for rollback protection.
58  *
59  * @returns none
60  */
61  SecureStore(KVStore *underlying_kv, KVStore *rbp_kv = 0);
62 
63  /**
64  * @brief Class destructor
65  *
66  * @returns none
67  */
68  virtual ~SecureStore();
69 
70  /**
71  * @brief Initialize SecureStore class. It will also initialize
72  * the underlying KVStore and the rollback protection KVStore.
73  *
74  * @returns MBED_SUCCESS Success.
75  * or any other error from underlying KVStore instances.
76  */
77  virtual int init();
78 
79  /**
80  * @brief Deinitialize SecureStore class, free handles and memory allocations.
81  *
82  * @returns MBED_SUCCESS Success.
83  * or any other error from underlying KVStore instances.
84  */
85  virtual int deinit();
86 
87 
88  /**
89  * @brief Reset KVStore contents (clear all keys)
90  * Warning: This function is not thread safe.
91  *
92  * @returns MBED_SUCCESS Success.
93  * MBED_ERROR_NOT_READY Not initialized.
94  * or any other error from underlying KVStore instances.
95  */
96  virtual int reset();
97 
98  /**
99  * @brief Set one KVStore item, given key and value.
100  *
101  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
102  * @param[in] buffer Value data buffer.
103  * @param[in] size Value data size.
104  * @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
105  * REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
106  *
107  * @returns MBED_SUCCESS Success.
108  * MBED_ERROR_NOT_READY Not initialized.
109  * MBED_ERROR_READ_FAILED Unable to read from media.
110  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
111  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
112  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
113  * MBED_ERROR_FAILED_OPERATION Internal error.
114  * or any other error from underlying KVStore instances.
115  */
116  virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);
117 
118  /**
119  * @brief Get one KVStore item, given key.
120  *
121  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
122  * @param[in] buffer Value data buffer.
123  * @param[in] buffer_size Value data buffer size.
124  * @param[out] actual_size Actual read size.
125  * @param[in] offset Offset to read from in data.
126  *
127  * @returns MBED_SUCCESS Success.
128  * MBED_ERROR_NOT_READY Not initialized.
129  * MBED_ERROR_READ_FAILED Unable to read from media.
130  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
131  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
132  * MBED_ERROR_FAILED_OPERATION Internal error.
133  * MBED_ERROR_ITEM_NOT_FOUND No such key.
134  * MBED_ERROR_AUTHENTICATION_FAILED Data authentication failed.
135  * MBED_ERROR_AUTHENTICATION_RBP_FAILED
136  * Rollback protection data authentication failed.
137  * or any other error from underlying KVStore instances.
138  */
139  virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
140  size_t offset = 0);
141 
142  /**
143  * @brief Get information of a given key.
144  *
145  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
146  * @param[out] info Returned information structure containing size and flags.
147  *
148  * @returns MBED_SUCCESS Success.
149  * MBED_ERROR_NOT_READY Not initialized.
150  * MBED_ERROR_READ_FAILED Unable to read from media.
151  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
152  * MBED_ERROR_FAILED_OPERATION Internal error.
153  * MBED_ERROR_ITEM_NOT_FOUND No such key.
154  * MBED_ERROR_AUTHENTICATION_FAILED Data authentication failed.
155  * MBED_ERROR_AUTHENTICATION_RBP_FAILED
156  * Rollback protection data authentication failed.
157  * or any other error from underlying KVStore instances.
158  */
159  virtual int get_info(const char *key, info_t *info);
160 
161  /**
162  * @brief Remove a KVStore item, given key.
163  *
164  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
165  *
166  * @returns MBED_SUCCESS Success.
167  * MBED_ERROR_NOT_READY Not initialized.
168  * MBED_ERROR_READ_FAILED Unable to read from media.
169  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
170  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
171  * MBED_ERROR_FAILED_OPERATION Internal error.
172  * or any other error from underlying KVStore instances.
173  */
174  virtual int remove(const char *key);
175 
176 
177  /**
178  * @brief Start an incremental KVStore set sequence. This operation is blocking other operations.
179  * Any get/set/remove/iterator operation will be blocked until set_finalize is called.
180  *
181  * @param[out] handle Returned incremental set handle.
182  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
183  * @param[in] final_data_size Final value data size.
184  * @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
185  * REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
186  *
187  * @returns MBED_SUCCESS Success.
188  * MBED_ERROR_NOT_READY Not initialized.
189  * MBED_ERROR_READ_FAILED Unable to read from media.
190  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
191  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
192  * MBED_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
193  * MBED_ERROR_FAILED_OPERATION Internal error.
194  * or any other error from underlying KVStore instances.
195  */
196  virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);
197 
198  /**
199  * @brief Add data to incremental KVStore set sequence. This operation is blocking other operations.
200  * Any get/set/remove operation will be blocked until set_finalize is called.
201  *
202  * @param[in] handle Incremental set handle.
203  * @param[in] value_data value data to add.
204  * @param[in] data_size value data size.
205  *
206  * @returns MBED_SUCCESS Success.
207  * MBED_ERROR_NOT_READY Not initialized.
208  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
209  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
210  * MBED_ERROR_FAILED_OPERATION Internal error.
211  * or any other error from underlying KVStore instances.
212  */
213  virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);
214 
215  /**
216  * @brief Finalize an incremental KVStore set sequence.
217  *
218  * @param[in] handle Incremental set handle.
219  *
220  * @returns MBED_SUCCESS Success.
221  * MBED_ERROR_NOT_READY Not initialized.
222  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
223  * MBED_ERROR_INVALID_SIZE Invalid size given in function arguments.
224  * MBED_ERROR_FAILED_OPERATION Internal error.
225  * or any other error from underlying KVStore instances.
226  */
227  virtual int set_finalize(set_handle_t handle);
228 
229  /**
230  * @brief Start an iteration over KVStore keys.
231  * There are no issue with any other operation while iterator is open.
232  *
233  * @param[out] it Returned iterator handle.
234  * @param[in] prefix Key prefix (null for all keys).
235  *
236  * @returns MBED_SUCCESS Success.
237  * MBED_ERROR_NOT_READY Not initialized.
238  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
239  * or any other error from underlying KVStore instances.
240  */
241  virtual int iterator_open(iterator_t *it, const char *prefix = NULL);
242 
243  /**
244  * @brief Get next key in iteration.
245  * There are no issue with any other operation while iterator is open.
246  *
247  * @param[in] it Iterator handle.
248  * @param[in] key Buffer for returned key.
249  * @param[in] key_size Key buffer size.
250  *
251  * @returns MBED_SUCCESS Success.
252  * MBED_ERROR_NOT_READY Not initialized.
253  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
254  * or any other error from underlying KVStore instances.
255  */
256  virtual int iterator_next(iterator_t it, char *key, size_t key_size);
257 
258  /**
259  * @brief Close iteration.
260  *
261  * @returns MBED_SUCCESS Success.
262  * MBED_ERROR_NOT_READY Not initialized.
263  * MBED_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
264  * or any other error from underlying KVStore instances.
265  *
266  * @returns 0 on success or a negative error code on failure
267  */
268  virtual int iterator_close(iterator_t it);
269 
270 #if !defined(DOXYGEN_ONLY)
271 private:
272 
273  PlatformMutex _mutex;
274  bool _is_initialized;
275  KVStore *_underlying_kv, *_rbp_kv;
276  void *_entropy;
277  void *_inc_set_handle;
278  uint8_t *_scratch_buf;
279 
280  /**
281  * @brief Actual get function, serving get and get_info APIs.
282  *
283  * @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
284  * @param[in] buffer Value data buffer.
285  * @param[in] buffer_size Value data buffer size.
286  * @param[out] actual_size Actual read size.
287  * @param[in] offset Offset to read from in data.
288  * @param[out] info Returned information structure.
289  *
290  * @returns 0 on success or a negative error code on failure
291  */
292  int do_get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
293  size_t offset = 0, info_t *info = 0);
294 #endif
295 };
296 /** @}*/
297 
298 } // namespace mbed
299 
300 #endif
301 #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.
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:50
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.