From 21eb3e1e7292a77df5d7aaf4711faa170e3c8ff6 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Fri, 10 Jun 2022 12:16:55 +0800 Subject: [PATCH 1/2] Fix GlobalUnencryptedMessageCounter initial value --- src/transport/BUILD.gn | 1 - src/transport/MessageCounter.cpp | 35 -------------------------------- src/transport/MessageCounter.h | 12 +++++++---- 3 files changed, 8 insertions(+), 40 deletions(-) delete mode 100644 src/transport/MessageCounter.cpp diff --git a/src/transport/BUILD.gn b/src/transport/BUILD.gn index defde06d387fdc..e70679a50cb61d 100644 --- a/src/transport/BUILD.gn +++ b/src/transport/BUILD.gn @@ -27,7 +27,6 @@ static_library("transport") { "GroupPeerMessageCounter.cpp", "GroupPeerMessageCounter.h", "GroupSession.h", - "MessageCounter.cpp", "MessageCounter.h", "MessageCounterManagerInterface.h", "PeerMessageCounter.h", diff --git a/src/transport/MessageCounter.cpp b/src/transport/MessageCounter.cpp deleted file mode 100644 index 99930cfd47c46f..00000000000000 --- a/src/transport/MessageCounter.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2021 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * This file defines the CHIP message counters. - * - */ - -#include - -#include -#include - -namespace chip { - -void GlobalUnencryptedMessageCounter::Init() -{ - mLastUsedValue = Crypto::GetRandU32(); -} - -} // namespace chip diff --git a/src/transport/MessageCounter.h b/src/transport/MessageCounter.h index 06934c0ef8ea5f..42089c97c2e0d2 100644 --- a/src/transport/MessageCounter.h +++ b/src/transport/MessageCounter.h @@ -39,6 +39,8 @@ namespace chip { class MessageCounter { public: + static constexpr uint32_t kMessageCounterRandomInitMask = 0x0FFFFFFF; ///< 28-bit mask + enum Type : uint8_t { GlobalUnencrypted, @@ -50,6 +52,9 @@ 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 call from global variable constructor. + static uint32_t GetDefaultInitialValuePredecessor() { return Crypto::GetRandU32() & kMessageCounterRandomInitMask; } }; class GlobalUnencryptedMessageCounter : public MessageCounter @@ -57,7 +62,7 @@ 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 @@ -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 @@ -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 From 87607692506e74d870436ab4a43dccde5f90afe2 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Fri, 10 Jun 2022 13:56:51 +0800 Subject: [PATCH 2/2] Update src/transport/MessageCounter.h Co-authored-by: Boris Zbarsky --- src/transport/MessageCounter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/MessageCounter.h b/src/transport/MessageCounter.h index 42089c97c2e0d2..5ca92af9e660bc 100644 --- a/src/transport/MessageCounter.h +++ b/src/transport/MessageCounter.h @@ -53,7 +53,7 @@ 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 call from global variable constructor. + // 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; } };