Skip to content
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

Restyle [ESP32] Handle NVS key with length > 15 #18605

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 37 additions & 0 deletions src/platform/ESP32/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,39 @@

#include "nvs.h"
#include "nvs_flash.h"
#include <crypto/CHIPCryptoPAL.h>
#include <lib/support/CodeUtils.h>

namespace chip {
namespace DeviceLayer {
namespace PersistedStorage {

namespace {
// Implementation Note: esp-idf nvs implementation cannot handle key length > 15,
// Below implementation tries to handle that case by hashing the key
// If key length is > 15 then take the SHA1 of the key and convert the first 8 bytes to hex string.
// Not sure how likely we would run into a conflict as we are only using 8 bytes out of 20

CHIP_ERROR HashIfLongKey(const char * key, char * keyHash)
{
VerifyOrReturnError(strlen(key) > (NVS_KEY_NAME_MAX_SIZE - 1), CHIP_ERROR_INCORRECT_STATE);

uint8_t hashBuffer[chip::Crypto::kSHA1_Hash_Length];
ReturnErrorOnFailure(chip::Crypto::Hash_SHA1((const uint8_t *) key, strlen(key), hashBuffer));

int i = 0, j = 0;
while (i < NVS_KEY_NAME_MAX_SIZE)
{
sprintf(keyHash + i, "%02x", hashBuffer[j]);
i += 2;
j += 1;
}
keyHash[NVS_KEY_NAME_MAX_SIZE - 1] = 0;
ChipLogDetail(DeviceLayer, "Using hash:%s for nvs key:%s", keyHash, key);
return CHIP_NO_ERROR;
}
} // namespace

KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;

CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
Expand All @@ -48,6 +75,10 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t

Internal::ScopedNvsHandle handle;
ReturnErrorOnFailure(handle.Open(kNamespace, NVS_READONLY));

char keyHash[NVS_KEY_NAME_MAX_SIZE];
VerifyOrdo(HashIfLongKey(key, keyHash) != CHIP_NO_ERROR, key = keyHash);

ReturnMappedErrorOnFailure(nvs_get_blob(handle, key, value, &value_size));

if (read_bytes_size)
Expand All @@ -65,6 +96,9 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value,
Internal::ScopedNvsHandle handle;
ReturnErrorOnFailure(handle.Open(kNamespace, NVS_READWRITE));

char keyHash[NVS_KEY_NAME_MAX_SIZE];
VerifyOrdo(HashIfLongKey(key, keyHash) != CHIP_NO_ERROR, key = keyHash);

ReturnMappedErrorOnFailure(nvs_set_blob(handle, key, value, value_size));

// Commit the value to the persistent store.
Expand All @@ -79,6 +113,9 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)

ReturnErrorOnFailure(handle.Open(kNamespace, NVS_READWRITE));

char keyHash[NVS_KEY_NAME_MAX_SIZE];
VerifyOrdo(HashIfLongKey(key, keyHash) != CHIP_NO_ERROR, key = keyHash);

ReturnMappedErrorOnFailure(nvs_erase_key(handle, key));

// Commit the value to the persistent store.
Expand Down