@@ -39,6 +39,8 @@ namespace chip {
39
39
class MessageCounter
40
40
{
41
41
public:
42
+ static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF ; // /< 28-bit mask
43
+
42
44
enum Type : uint8_t
43
45
{
44
46
GlobalUnencrypted,
@@ -50,14 +52,17 @@ class MessageCounter
50
52
51
53
virtual Type GetType () const = 0;
52
54
virtual CHIP_ERROR AdvanceAndConsume (uint32_t & fetch) = 0; /* * Advance the counter, and feed the new counter to fetch */
55
+
56
+ // Note: this function must be called after Crypto is initialized. It can not be called from global variable constructor.
57
+ static uint32_t GetDefaultInitialValuePredecessor () { return Crypto::GetRandU32 () & kMessageCounterRandomInitMask ; }
53
58
};
54
59
55
60
class GlobalUnencryptedMessageCounter : public MessageCounter
56
61
{
57
62
public:
58
63
GlobalUnencryptedMessageCounter () : mLastUsedValue (0 ) {}
59
64
60
- void Init ();
65
+ void Init () { mLastUsedValue = GetDefaultInitialValuePredecessor (); }
61
66
62
67
Type GetType () const override { return GlobalUnencrypted; }
63
68
CHIP_ERROR AdvanceAndConsume (uint32_t & fetch) override
@@ -73,8 +78,7 @@ class GlobalUnencryptedMessageCounter : public MessageCounter
73
78
class LocalSessionMessageCounter : public MessageCounter
74
79
{
75
80
public:
76
- static constexpr uint32_t kMessageCounterMax = 0xFFFFFFFF ;
77
- static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF ; // /< 28-bit mask
81
+ static constexpr uint32_t kMessageCounterMax = 0xFFFFFFFF ;
78
82
79
83
/* *
80
84
* Initialize a local message counter with random value between [1, 2^28]. This increases the difficulty of traffic analysis
@@ -83,7 +87,7 @@ class LocalSessionMessageCounter : public MessageCounter
83
87
*
84
88
* The mLastUsedValue is the predecessor of the initial value, it will be advanced before using, so don't need to add 1 here.
85
89
*/
86
- LocalSessionMessageCounter () { mLastUsedValue = ( Crypto::GetRandU32 () & kMessageCounterRandomInitMask ); }
90
+ LocalSessionMessageCounter () { mLastUsedValue = GetDefaultInitialValuePredecessor ( ); }
87
91
88
92
Type GetType () const override { return Session; }
89
93
CHIP_ERROR AdvanceAndConsume (uint32_t & fetch) override
0 commit comments