Skip to content

[C33] Kvstore additions #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
636 changes: 636 additions & 0 deletions libraries/KVStore/FileSystemStore.cpp

Large diffs are not rendered by default.

271 changes: 271 additions & 0 deletions libraries/KVStore/FileSystemStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_FILE_SYSTEM_STORE_H
#define MBED_FILE_SYSTEM_STORE_H

#include "KVStore.h"
#include "FileSystem.h"

namespace mbed {

/** FileSystemStore for Secure Store.
* This class implements the KVStore interface to
* create a key value store over FileSystem.
*
* @code
* ...
* @endcode
*/
class FileSystemStore : public KVStore {

public:
/** Create FileSystemStore - A Key Value API on top of FS
*
* @param fs File system (FAT/LITTLE) on top of which FileSystemStore is adding KV API
*/
FileSystemStore(FileSystem *fs);

/** Destroy FileSystemStore instance
*
*/
virtual ~FileSystemStore() {}

/**
* @brief Initialize FileSystemStore, checking validity of
* KVStore writing folder and if it doesn't exist, creating it.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
*/
virtual int init();

/**
* @brief Deinitialize FileSystemStore, release and free resources.
*
* @returns KVSTORE_SUCCESS Success.
*/
virtual int deinit();

/**
* @brief Reset FileSystemStore contents (clear all keys)
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
*/
virtual int reset();

/**
* @brief Set one FileSystemStore item, given key and value.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] size Value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
*/
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);

/**
* @brief Get one FileSystemStore item by given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] buffer_size Value data buffer size.
* @param[out] actual_size Actual read size.
* @param[in] offset Offset to read from in data.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_INVALID_DATA_DETECTED Data is corrupted.
* KVSTORE_ERROR_ITEM_NOT_FOUND No such key.
*/
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0);

/**
* @brief Get information of a given key. The returned info contains size and flags
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[out] info Returned information structure.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_INVALID_DATA_DETECTED Data is corrupted.
* KVSTORE_ERROR_ITEM_NOT_FOUND No such key.
*/
virtual int get_info(const char *key, info_t *info);

/**
* @brief Remove a FileSystemStore item by given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_ITEM_NOT_FOUND No such key.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
*/
virtual int remove(const char *key);

/**
* @brief Start an incremental FileSystemStore set sequence. This operation is blocking other operations.
* Any get/set/remove/iterator operation will be blocked until set_finalize is called.
*
* @param[out] handle Returned incremental set handle.
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] final_data_size Final value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
*/
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);

/**
* @brief Add data to incremental FileSystemStore set sequence. This operation is blocking other operations.
* Any get/set/remove operation will be blocked until set_finalize is called.
*
* @param[in] handle Incremental set handle.
* @param[in] value_data Value data to add.
* @param[in] data_size Value data size.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
*/
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);

/**
* @brief Finalize an incremental FileSystemStore set sequence.
*
* @param[in] handle Incremental set handle.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_FAILED_OPERATION Underlying file system failed operation.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
*/
virtual int set_finalize(set_handle_t handle);

/**
* @brief Start an iteration over FileSystemStore keys.
* There are no issues with any other operations while iterator is open.
*
* @param[out] it Returned iterator handle.
* @param[in] prefix Key prefix (null for all keys).
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
*/
virtual int iterator_open(iterator_t *it, const char *prefix = NULL);

/**
* @brief Get next key in iteration.
* There are no issues with any other operations while iterator is open.
*
* @param[in] it Iterator handle.
* @param[in] key Buffer for returned key.
* @param[in] key_size Key buffer size.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_ITEM_NOT_FOUND No more keys found.
*/
virtual int iterator_next(iterator_t it, char *key, size_t key_size);

/**
* @brief Close iteration.
*
* @param[in] it Iterator handle.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
*/
virtual int iterator_close(iterator_t it);

#if !defined(DOXYGEN_ONLY)
private:

// Key metadata
typedef struct {
uint32_t magic;
uint16_t metadata_size;
uint16_t revision;
uint32_t user_flags;
} key_metadata_t;

/**
* @brief Build Full name class member from Key, as a combination of FSST folder and key name
*
* @param[in] key_src key file name
*
* @returns 0 on success or a negative error code on failure
*/
int _build_full_path_key(const char *key_src);

/**
* @brief Verify Key file metadata validity and open it if valid
*
* @param[in] key In validated key file name.
* @param[in] key_metadata Returned key file metadata.
* @param[in] kv_file Opened KV file handle (unless file doesn't exist)
*
* @returns 0 on success or a negative error code on failure
*/
int _verify_key_file(const char *key, key_metadata_t *key_metadata, File *kv_file);

FileSystem *_fs;
PlatformMutex _mutex;
PlatformMutex _inc_data_add_mutex;

bool _is_initialized;
char *_cfg_fs_path; /* FileSystemStore path name on FileSystem */
size_t _cfg_fs_path_size; /* Size of configured FileSystemStore path name on FileSystem */
char *_full_path_key; /* Full name of Key file currently working on */
size_t _cur_inc_data_size; /* Amount of data added to Key file so far, during incremental add data */
set_handle_t _cur_inc_set_handle; /* handle of currently key file under incremental set process */
#endif
};


} //namespace mbed
#endif //MBED_FILE_SYSTEM_STORE_H
229 changes: 229 additions & 0 deletions libraries/KVStore/KVStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBED_KVSTORE_H
#define MBED_KVSTORE_H

#include <stdint.h>
#include <string.h>

#define KVSTORE_SUCCESS 0
#define KVSTORE_ERROR_READ_FAILED 283
#define KVSTORE_ERROR_WRITE_FAILED 284
#define KVSTORE_ERROR_INVALID_DATA_DETECTED 258
#define KVSTORE_ERROR_INVALID_SIZE 261
#define KVSTORE_ERROR_INVALID_ARGUMENT 257
#define KVSTORE_ERROR_ITEM_NOT_FOUND 263
#define KVSTORE_ERROR_MEDIA_FULL 267
#define KVSTORE_ERROR_WRITE_PROTECTED 274
#define KVSTORE_ERROR_OUT_OF_RESOURCES 288
#define KVSTORE_ERROR_NOT_READY 270
#define KVSTORE_ERROR_FAILED_OPERATION 271

namespace mbed {

/** KVStore class
*
* Interface class for Key Value Storage
*/
class KVStore {
public:
enum create_flags {
WRITE_ONCE_FLAG = (1 << 0),
REQUIRE_CONFIDENTIALITY_FLAG = (1 << 1),
RESERVED_FLAG = (1 << 2),
REQUIRE_REPLAY_PROTECTION_FLAG = (1 << 3),
};

static const uint32_t MAX_KEY_SIZE = 128;

typedef struct _opaque_set_handle *set_handle_t;

typedef struct _opaque_key_iterator *iterator_t;

/**
* Holds key information
*/
typedef struct info {
/**
* The key size
*/
size_t size;
/*
* The Key flags, possible flags combination:
* WRITE_ONCE_FLAG,
* REQUIRE_CONFIDENTIALITY_FLAG,
* REQUIRE_REPLAY_PROTECTION_FLAG
*/
uint32_t flags;
} info_t;

virtual ~KVStore() {};

/**
* @brief Initialize KVStore
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int init() = 0;

/**
* @brief Deinitialize KVStore
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int deinit() = 0;


/**
* @brief Reset KVStore contents (clear all keys)
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int reset() = 0;

/**
* @brief Set one KVStore item, given key and value.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] size Value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags) = 0;

/**
* @brief Get one KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] buffer_size Value data buffer size.
* @param[out] actual_size Actual read size (NULL to pass nothing).
* @param[in] offset Offset to read from in data.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0) = 0;

/**
* @brief Get information of a given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[out] info Returned information structure (NULL to pass nothing).
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int get_info(const char *key, info_t *info = NULL) = 0;

/**
* @brief Remove a KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int remove(const char *key) = 0;


/**
* @brief Start an incremental KVStore set sequence.
*
* @param[out] handle Returned incremental set handle.
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] final_data_size Final value data size.
* @param[in] create_flags Flag mask.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags) = 0;

/**
* @brief Add data to incremental KVStore set sequence.
*
* @param[in] handle Incremental set handle.
* @param[in] value_data Value data to add.
* @param[in] data_size Value data size.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size) = 0;

/**
* @brief Finalize an incremental KVStore set sequence.
*
* @param[in] handle Incremental set handle.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int set_finalize(set_handle_t handle) = 0;

/**
* @brief Start an iteration over KVStore keys.
*
* @param[out] it Returned iterator handle.
* @param[in] prefix Key prefix (null for all keys).
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_open(iterator_t *it, const char *prefix = NULL) = 0;

/**
* @brief Get next key in iteration.
*
* @param[in] it Iterator handle.
* @param[in] key Buffer for returned key.
* @param[in] key_size Key buffer size.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_next(iterator_t it, char *key, size_t key_size) = 0;

/**
* @brief Close iteration.
*
* @param[in] it Iterator handle.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
virtual int iterator_close(iterator_t it) = 0;

/** Convenience function for checking key validity.
* Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @param[in] key Key buffer.
*
* @returns KVSTORE_SUCCESS on success or an error code on failure
*/
bool is_valid_key(const char *key) const
{
if (!key || !strlen(key) || (strlen(key) > MAX_KEY_SIZE)) {
return false;
}

if (strpbrk(key, " */?:;\"|<>\\")) {
return false;
}
return true;
}

};
/** @}*/

} // namespace mbed

#endif
892 changes: 892 additions & 0 deletions libraries/KVStore/MbedCRC.h

Large diffs are not rendered by default.

891 changes: 891 additions & 0 deletions libraries/KVStore/SecureStore.cpp

Large diffs are not rendered by default.

304 changes: 304 additions & 0 deletions libraries/KVStore/SecureStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBED_SECURESTORE_H
#define MBED_SECURESTORE_H

#if !defined(MBEDTLS_CONFIG_FILE)
// #include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

// #include "device_key/DeviceKey.h"

#define SECURESTORE_ENABLED 1

// Whole class is not supported if entropy, device key or required mbed TLS features are not enabled
#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CIPHER_MODE_CTR) || !defined(MBEDTLS_CMAC_C) || !DEVICEKEY_ENABLED
#undef SECURESTORE_ENABLED
#define SECURESTORE_ENABLED 0
#endif

#if SECURESTORE_ENABLED || defined(DOXYGEN_ONLY)

#include <stdint.h>
#include "KVStore.h"

// Forward declarations
struct mbedtls_entropy_context;

// namespace mbed {

/** TDBStore class
*
* Lightweight Key Value storage over a block device
*/

class SecureStore : public KVStore {
public:

/**
* @brief Class constructor
*
* @param[in] underlying_kv KVStore that will hold the data.
* @param[in] rbp_kv Additional KVStore used for rollback protection.
*
* @returns none
*/
SecureStore(KVStore *underlying_kv, KVStore *rbp_kv = 0);

/**
* @brief Class destructor
*
* @returns none
*/
virtual ~SecureStore();

/**
* @brief Initialize SecureStore class. It will also initialize
* the underlying KVStore and the rollback protection KVStore.
*
* @returns KVSTORE_SUCCESS Success.
* or any other error from underlying KVStore instances.
*/
virtual int init();

/**
* @brief Deinitialize SecureStore class, free handles and memory allocations.
*
* @returns KVSTORE_SUCCESS Success.
* or any other error from underlying KVStore instances.
*/
virtual int deinit();


/**
* @brief Reset KVStore contents (clear all keys)
* Warning: This function is not thread safe.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* or any other error from underlying KVStore instances.
*/
virtual int reset();

/**
* @brief Set one KVStore item, given key and value.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] size Value data size.
* @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
* REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_READ_FAILED Unable to read from media.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* or any other error from underlying KVStore instances.
*/
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags);

/**
* @brief Get one KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] buffer_size Value data buffer size.
* @param[out] actual_size Actual read size.
* @param[in] offset Offset to read from in data.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_READ_FAILED Unable to read from media.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* KVSTORE_ERROR_ITEM_NOT_FOUND No such key.
* KVSTORE_ERROR_AUTHENTICATION_FAILED Data authentication failed.
* KVSTORE_ERROR_AUTHENTICATION_RBP_FAILED
* Rollback protection data authentication failed.
* or any other error from underlying KVStore instances.
*/
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
size_t offset = 0);

/**
* @brief Get information of a given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[out] info Returned information structure containing size and flags.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_READ_FAILED Unable to read from media.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* KVSTORE_ERROR_ITEM_NOT_FOUND No such key.
* KVSTORE_ERROR_AUTHENTICATION_FAILED Data authentication failed.
* KVSTORE_ERROR_AUTHENTICATION_RBP_FAILED
* Rollback protection data authentication failed.
* or any other error from underlying KVStore instances.
*/
virtual int get_info(const char *key, info_t *info);

/**
* @brief Remove a KVStore item, given key.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_READ_FAILED Unable to read from media.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* or any other error from underlying KVStore instances.
*/
virtual int remove(const char *key);


/**
* @brief Start an incremental KVStore set sequence. This operation is blocking other operations.
* Any get/set/remove/iterator operation will be blocked until set_finalize is called.
*
* @param[out] handle Returned incremental set handle.
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] final_data_size Final value data size.
* @param[in] create_flags Flag mask - WRITE_ONCE_FLAG|REQUIRE_CONFIDENTIALITY_FLAG|
* REQUIRE_INTEGRITY_FLAG|REQUIRE_REPLAY_PROTECTION_FLAG
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_READ_FAILED Unable to read from media.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_WRITE_PROTECTED Already stored with "write once" flag.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* or any other error from underlying KVStore instances.
*/
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags);

/**
* @brief Add data to incremental KVStore set sequence. This operation is blocking other operations.
* Any get/set/remove operation will be blocked until set_finalize is called.
*
* @param[in] handle Incremental set handle.
* @param[in] value_data value data to add.
* @param[in] data_size value data size.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* or any other error from underlying KVStore instances.
*/
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size);

/**
* @brief Finalize an incremental KVStore set sequence.
*
* @param[in] handle Incremental set handle.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* KVSTORE_ERROR_INVALID_SIZE Invalid size given in function arguments.
* KVSTORE_ERROR_FAILED_OPERATION Internal error.
* or any other error from underlying KVStore instances.
*/
virtual int set_finalize(set_handle_t handle);

/**
* @brief Start an iteration over KVStore keys.
* There are no issue with any other operation while iterator is open.
*
* @param[out] it Returned iterator handle.
* @param[in] prefix Key prefix (null for all keys).
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* or any other error from underlying KVStore instances.
*/
virtual int iterator_open(iterator_t *it, const char *prefix = NULL);

/**
* @brief Get next key in iteration.
* There are no issue with any other operation while iterator is open.
*
* @param[in] it Iterator handle.
* @param[in] key Buffer for returned key.
* @param[in] key_size Key buffer size.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* or any other error from underlying KVStore instances.
*/
virtual int iterator_next(iterator_t it, char *key, size_t key_size);

/**
* @brief Close iteration.
*
* @returns KVSTORE_SUCCESS Success.
* KVSTORE_ERROR_NOT_READY Not initialized.
* KVSTORE_ERROR_INVALID_ARGUMENT Invalid argument given in function arguments.
* or any other error from underlying KVStore instances.
*
* @returns 0 on success or a negative error code on failure
*/
virtual int iterator_close(iterator_t it);

#if !defined(DOXYGEN_ONLY)
private:
// Forward declaration
struct inc_set_handle_t;

PlatformMutex _mutex;
bool _is_initialized;
KVStore *_underlying_kv, *_rbp_kv;
mbedtls_entropy_context *_entropy;
inc_set_handle_t *_ih;
uint8_t *_scratch_buf;

/**
* @brief Actual get function, serving get and get_info APIs.
*
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
* @param[in] buffer Value data buffer.
* @param[in] buffer_size Value data buffer size.
* @param[out] actual_size Actual read size.
* @param[in] offset Offset to read from in data.
* @param[out] info Returned information structure.
*
* @returns 0 on success or a negative error code on failure
*/
int do_get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL,
size_t offset = 0, info_t *info = 0);
#endif
};
/** @}*/

// } // namespace mbed

#endif
#endif
1,512 changes: 1,512 additions & 0 deletions libraries/KVStore/TDBStore.cpp

Large diffs are not rendered by default.

548 changes: 548 additions & 0 deletions libraries/KVStore/TDBStore.h

Large diffs are not rendered by default.