Skip to content

Commit

Permalink
Fix GlobalUnencryptedMessageCounter initial value (#19429)
Browse files Browse the repository at this point in the history
* Fix GlobalUnencryptedMessageCounter initial value

* Update src/transport/MessageCounter.h

Co-authored-by: Boris Zbarsky <[email protected]>

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jul 31, 2023
1 parent 1a5e567 commit 9003333
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 40 deletions.
1 change: 0 additions & 1 deletion src/transport/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static_library("transport") {
"GroupPeerMessageCounter.cpp",
"GroupPeerMessageCounter.h",
"GroupSession.h",
"MessageCounter.cpp",
"MessageCounter.h",
"MessageCounterManagerInterface.h",
"PeerMessageCounter.h",
Expand Down
35 changes: 0 additions & 35 deletions src/transport/MessageCounter.cpp

This file was deleted.

12 changes: 8 additions & 4 deletions src/transport/MessageCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace chip {
class MessageCounter
{
public:
static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask

enum Type : uint8_t
{
GlobalUnencrypted,
Expand All @@ -50,14 +52,17 @@ class MessageCounter

virtual Type GetType() const = 0;
virtual CHIP_ERROR AdvanceAndConsume(uint32_t & fetch) = 0; /** Advance the counter, and feed the new counter to fetch */

// Note: this function must be called after Crypto is initialized. It can not be called from global variable constructor.
static uint32_t GetDefaultInitialValuePredecessor() { return Crypto::GetRandU32() & kMessageCounterRandomInitMask; }
};

class GlobalUnencryptedMessageCounter : public MessageCounter
{
public:
GlobalUnencryptedMessageCounter() : mLastUsedValue(0) {}

void Init();
void Init() { mLastUsedValue = GetDefaultInitialValuePredecessor(); }

Type GetType() const override { return GlobalUnencrypted; }
CHIP_ERROR AdvanceAndConsume(uint32_t & fetch) override
Expand All @@ -73,8 +78,7 @@ class GlobalUnencryptedMessageCounter : public MessageCounter
class LocalSessionMessageCounter : public MessageCounter
{
public:
static constexpr uint32_t kMessageCounterMax = 0xFFFFFFFF;
static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask
static constexpr uint32_t kMessageCounterMax = 0xFFFFFFFF;

/**
* Initialize a local message counter with random value between [1, 2^28]. This increases the difficulty of traffic analysis
Expand All @@ -83,7 +87,7 @@ class LocalSessionMessageCounter : public MessageCounter
*
* The mLastUsedValue is the predecessor of the initial value, it will be advanced before using, so don't need to add 1 here.
*/
LocalSessionMessageCounter() { mLastUsedValue = (Crypto::GetRandU32() & kMessageCounterRandomInitMask); }
LocalSessionMessageCounter() { mLastUsedValue = GetDefaultInitialValuePredecessor(); }

Type GetType() const override { return Session; }
CHIP_ERROR AdvanceAndConsume(uint32_t & fetch) override
Expand Down

0 comments on commit 9003333

Please sign in to comment.