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

[msg] Fix counter init. #12948

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/transport/MessageCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace chip {

void GlobalUnencryptedMessageCounter::Init()
{
value = Crypto::GetRandU32();
mValue = Crypto::GetRandU32();
}

CHIP_ERROR GlobalEncryptedMessageCounter::Init()
Expand Down
31 changes: 16 additions & 15 deletions src/transport/MessageCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ class MessageCounter
class GlobalUnencryptedMessageCounter : public MessageCounter
{
public:
GlobalUnencryptedMessageCounter() : value(0) {}
GlobalUnencryptedMessageCounter() : mValue(0) {}

void Init();

Type GetType() override { return GlobalUnencrypted; }
uint32_t Value() override { return value; }
uint32_t Value() override { return mValue; }
CHIP_ERROR Advance() override
{
++value;
++mValue;
return CHIP_NO_ERROR;
}

private:
uint32_t value;
uint32_t mValue;
};

class GlobalEncryptedMessageCounter : public MessageCounter
Expand All @@ -88,44 +88,45 @@ class GlobalEncryptedMessageCounter : public MessageCounter
#else
struct FakePersistedCounter
{
FakePersistedCounter() : value(0) {}
FakePersistedCounter() : mValue(0) {}
CHIP_ERROR Init(chip::Platform::PersistedStorage::Key aId, uint32_t aEpoch) { return CHIP_NO_ERROR; }

uint32_t GetValue() { return value; }
uint32_t GetValue() { return mValue; }
CHIP_ERROR Advance()
{
++value;
++mValue;
return CHIP_NO_ERROR;
}

private:
uint32_t value;
uint32_t mValue;
} persisted;
#endif
};

class LocalSessionMessageCounter : public MessageCounter
{
public:
static constexpr uint32_t kInitialValue = 1; ///< Used for initializing peer counter
static constexpr uint32_t kInitialSyncValue = 0; ///< Used for initializing peer counter
static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFF; ///< 28-bit mask

/**
* Initialize a local message counter with random value between [0, 2^28-1]. This increases the difficulty of traffic analysis
* attacks by making it harder to determine how long a particular session has been open.
* Initialize a local message counter with random value between [1, 2^28]. This increases the difficulty of traffic analysis
* attacks by making it harder to determine how long a particular session has been open. The initial counter is always 1 or
* higher to guarantee first message is always greater than initial peer counter set to 0.
*/
LocalSessionMessageCounter() { value = Crypto::GetRandU32() & kMessageCounterRandomInitMask; }
LocalSessionMessageCounter() { mValue = (Crypto::GetRandU32() & kMessageCounterRandomInitMask) + 1; }

Type GetType() override { return Session; }
uint32_t Value() override { return value; }
uint32_t Value() override { return mValue; }
CHIP_ERROR Advance() override
{
++value;
++mValue;
return CHIP_NO_ERROR;
}

private:
uint32_t value;
uint32_t mValue;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/transport/PairingSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DLL_EXPORT PairingSession
virtual uint32_t GetPeerCounter()
{
// TODO(#6652): This is a stub implementation, should be replaced by the real one when CASE and PASE is completed
return LocalSessionMessageCounter::kInitialValue;
return LocalSessionMessageCounter::kInitialSyncValue;
}

/**
Expand Down