Skip to content

Commit

Permalink
[msg] Fix counter init. (#12948)
Browse files Browse the repository at this point in the history
  • Loading branch information
turon authored and pull[bot] committed Mar 25, 2022
1 parent ddfbda5 commit 1724178
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
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

0 comments on commit 1724178

Please sign in to comment.