Skip to content

Commit

Permalink
[Group][Transport] Add Random first value group counter (#26610)
Browse files Browse the repository at this point in the history
* Add Random first value group counter

* fix CI
  • Loading branch information
jepenven-silabs authored May 24, 2023
1 parent 849cd66 commit 5181fc9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/transport/GroupPeerMessageCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <transport/GroupPeerMessageCounter.h>

#include <crypto/RandUtils.h>

namespace chip {
namespace Transport {

Expand Down Expand Up @@ -268,7 +270,6 @@ CHIP_ERROR GroupOutgoingCounters::Init(chip::PersistentStorageDelegate * storage
return CHIP_ERROR_INVALID_ARGUMENT;
}

// TODO Implement Logic for first time use / factory reset to be random
// Spec 4.5.1.3
mStorage = storage_delegate;
uint16_t size = static_cast<uint16_t>(sizeof(uint32_t));
Expand All @@ -277,9 +278,8 @@ CHIP_ERROR GroupOutgoingCounters::Init(chip::PersistentStorageDelegate * storage
err = mStorage->SyncGetKeyValue(DefaultStorageKeyAllocator::GroupControlCounter().KeyName(), &temp, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
// might be the first time we retrieve the value
// TODO handle this case
mGroupControlCounter = 0; // TODO should be random
// First time retrieving the counter
mGroupControlCounter = (chip::Crypto::GetRandU32() & kMessageCounterRandomInitMask) + 1;
}
else if (err != CHIP_NO_ERROR)
{
Expand All @@ -293,9 +293,8 @@ CHIP_ERROR GroupOutgoingCounters::Init(chip::PersistentStorageDelegate * storage
err = mStorage->SyncGetKeyValue(DefaultStorageKeyAllocator::GroupDataCounter().KeyName(), &temp, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
// might be the first time we retrieve the value
// TODO handle this case
mGroupDataCounter = 0; // TODO should be random
// First time retrieving the counter
mGroupDataCounter = (chip::Crypto::GetRandU32() & kMessageCounterRandomInitMask) + 1;
}
else if (err != CHIP_NO_ERROR)
{
Expand Down
2 changes: 2 additions & 0 deletions src/transport/GroupPeerMessageCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class GroupPeerTable
class GroupOutgoingCounters
{
public:
static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask

GroupOutgoingCounters(){};
GroupOutgoingCounters(chip::PersistentStorageDelegate * storage_delegate);
CHIP_ERROR Init(chip::PersistentStorageDelegate * storage_delegate);
Expand Down
13 changes: 8 additions & 5 deletions src/transport/tests/TestGroupMessageCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,18 @@ void GroupMessageCounterTest(nlTestSuite * inSuite, void * inContext)

chip::TestPersistentStorageDelegate delegate;
TestGroupOutgoingCounters groupCientCounter;
uint32_t controlCounter = 0, dataCounter = 0;
CHIP_ERROR err = groupCientCounter.Init(&delegate);

NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Start Test with Control counter
NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(true) == 0);
// Counter should be random
controlCounter = groupCientCounter.GetCounter(true);
dataCounter = groupCientCounter.GetCounter(false);
groupCientCounter.IncrementCounter(true);

NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(true) == 1);
NL_TEST_ASSERT(inSuite, (groupCientCounter.GetCounter(true) - controlCounter) == 1);

groupCientCounter.SetCounter(true, UINT32_MAX - GROUP_MSG_COUNTER_MIN_INCREMENT);
NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(true) == UINT32_MAX - GROUP_MSG_COUNTER_MIN_INCREMENT);
Expand All @@ -421,7 +424,7 @@ void GroupMessageCounterTest(nlTestSuite * inSuite, void * inContext)
TestGroupOutgoingCounters groupCientCounter2(&delegate);

NL_TEST_ASSERT(inSuite, groupCientCounter2.GetCounter(true) == UINT32_MAX);
NL_TEST_ASSERT(inSuite, groupCientCounter2.GetCounter(false) == GROUP_MSG_COUNTER_MIN_INCREMENT);
NL_TEST_ASSERT(inSuite, (groupCientCounter2.GetCounter(false) - dataCounter) == GROUP_MSG_COUNTER_MIN_INCREMENT);

// Test Roll over
groupCientCounter2.IncrementCounter(true);
Expand All @@ -433,9 +436,9 @@ void GroupMessageCounterTest(nlTestSuite * inSuite, void * inContext)
// Redo the test with the second counter

// Start Test with Control counter
NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(false) == 0);
dataCounter = groupCientCounter.GetCounter(false);
groupCientCounter.IncrementCounter(false);
NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(false) == 1);
NL_TEST_ASSERT(inSuite, (groupCientCounter.GetCounter(false) - dataCounter) == 1);

groupCientCounter.SetCounter(false, UINT32_MAX - GROUP_MSG_COUNTER_MIN_INCREMENT);
NL_TEST_ASSERT(inSuite, groupCientCounter.GetCounter(false) == UINT32_MAX - GROUP_MSG_COUNTER_MIN_INCREMENT);
Expand Down

0 comments on commit 5181fc9

Please sign in to comment.