diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 1f613ad7d..e5301be4e 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -131,7 +131,7 @@ otError otThreadSetMasterKey(otInstance *aInstance, const otMasterKey *aKey) VerifyOrExit(aKey != NULL, error = OT_ERROR_INVALID_ARGS); VerifyOrExit(instance.Get().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE); - error = instance.Get().SetMasterKey(*aKey); + error = instance.Get().SetMasterKey(*static_cast(aKey)); instance.Get().Clear(); instance.Get().Clear(); diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index c9ea286e2..af062d7bb 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -320,7 +320,7 @@ otError Dataset::Set(const otOperationalDataset &aDataset) { MeshCoP::NetworkMasterKeyTlv tlv; tlv.Init(); - tlv.SetNetworkMasterKey(aDataset.mMasterKey); + tlv.SetNetworkMasterKey(static_cast(aDataset.mMasterKey)); Set(tlv); } @@ -564,8 +564,7 @@ otError Dataset::ApplyConfiguration(Instance &aInstance, bool *aIsMasterKeyUpdat { const NetworkMasterKeyTlv *key = static_cast(cur); - if (aIsMasterKeyUpdated && - memcmp(&key->GetNetworkMasterKey(), &keyManager.GetMasterKey(), OT_MASTER_KEY_SIZE)) + if (aIsMasterKeyUpdated && (key->GetNetworkMasterKey() != keyManager.GetMasterKey())) { *aIsMasterKeyUpdated = true; } diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 3b57563eb..0ab3b7531 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -470,7 +470,7 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con { NetworkMasterKeyTlv masterkey; masterkey.Init(); - masterkey.SetNetworkMasterKey(aDataset.mMasterKey); + masterkey.SetNetworkMasterKey(static_cast(aDataset.mMasterKey)); SuccessOrExit(error = message->AppendTlv(masterkey)); } diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index e9c1ea253..1fc7259be 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -157,8 +157,7 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf // check network master key if (Tlv::GetTlv(aMessage, Tlv::kNetworkMasterKey, sizeof(masterKey), masterKey) == OT_ERROR_NONE && - masterKey.IsValid() && - memcmp(&masterKey.GetNetworkMasterKey(), &Get().GetMasterKey(), OT_MASTER_KEY_SIZE)) + masterKey.IsValid() && (masterKey.GetNetworkMasterKey() != Get().GetMasterKey())) { doesAffectConnectivity = true; doesAffectMasterKey = true; @@ -166,8 +165,7 @@ otError DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInf // check active timestamp rollback if (type == Tlv::kPendingTimestamp && - (masterKey.GetLength() == 0 || - memcmp(&masterKey.GetNetworkMasterKey(), &Get().GetMasterKey(), OT_MASTER_KEY_SIZE) == 0)) + ((masterKey.GetLength() == 0) || (masterKey.GetNetworkMasterKey() == Get().GetMasterKey()))) { // no change to master key, active timestamp must be ahead const Timestamp *localActiveTimestamp = Get().GetTimestamp(); diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index 6eeeb2e68..39579af4d 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -51,6 +51,7 @@ #include "meshcop/timestamp.hpp" #include "net/ip6_address.hpp" #include "radio/radio.hpp" +#include "thread/key_manager.hpp" namespace ot { namespace MeshCoP { @@ -519,18 +520,18 @@ public: * @returns The Network Master Key value. * */ - const otMasterKey &GetNetworkMasterKey(void) const { return mNetworkMasterKey; } + const MasterKey &GetNetworkMasterKey(void) const { return mNetworkMasterKey; } /** * This method sets the Network Master Key value. * - * @param[in] aNetworkMasterKey A pointer to the Network Master Key value. + * @param[in] aMasterKey The Network Master Key. * */ - void SetNetworkMasterKey(const otMasterKey &aNetworkMasterKey) { mNetworkMasterKey = aNetworkMasterKey; } + void SetNetworkMasterKey(const MasterKey &aMasterKey) { mNetworkMasterKey = aMasterKey; } private: - otMasterKey mNetworkMasterKey; + MasterKey mNetworkMasterKey; } OT_TOOL_PACKED_END; /** diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index 3367392bc..1474a886f 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -44,11 +44,11 @@ namespace ot { -static const uint8_t kThreadString[] = { +const uint8_t KeyManager::kThreadString[] = { 'T', 'h', 'r', 'e', 'a', 'd', }; -static const otMasterKey kDefaultMasterKey = {{ +const otMasterKey KeyManager::kDefaultMasterKey = {{ 0x00, 0x11, 0x22, @@ -69,7 +69,6 @@ static const otMasterKey kDefaultMasterKey = {{ KeyManager::KeyManager(Instance &aInstance) : InstanceLocator(aInstance) - , mMasterKey(kDefaultMasterKey) , mKeySequence(0) , mMacFrameCounter(0) , mMleFrameCounter(0) @@ -84,6 +83,7 @@ KeyManager::KeyManager(Instance &aInstance) , mSecurityPolicyFlags(0xff) , mIsPSKcSet(false) { + mMasterKey = static_cast(kDefaultMasterKey); memset(&mPSKc, 0, sizeof(mPSKc)); ComputeKey(mKeySequence, mKey); } @@ -111,18 +111,12 @@ exit: } #endif // OPENTHREAD_MTD || OPENTHREAD_FTD -const otMasterKey &KeyManager::GetMasterKey(void) const -{ - return mMasterKey; -} - -otError KeyManager::SetMasterKey(const otMasterKey &aKey) +otError KeyManager::SetMasterKey(const MasterKey &aKey) { otError error = OT_ERROR_NONE; Router *routers; - VerifyOrExit(memcmp(&mMasterKey, &aKey, sizeof(mMasterKey)) != 0, - Get().SignalIfFirst(OT_CHANGED_MASTER_KEY)); + VerifyOrExit(mMasterKey != aKey, Get().SignalIfFirst(OT_CHANGED_MASTER_KEY)); mMasterKey = aKey; mKeySequence = 0; diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index e6bc05e8b..a9028de57 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -56,6 +56,42 @@ namespace ot { * @{ */ +/** + * This class represents a Thread Master Key. + * + */ +OT_TOOL_PACKED_BEGIN +class MasterKey : public otMasterKey +{ +public: + /** + * This method evaluates whether or not the Thread Master Keys match. + * + * @param[in] aOther The Thread Master Key to compare. + * + * @retval TRUE If the Thread Master Keys match. + * @retval FALSE If the Thread Master Keys do not match. + * + */ + bool operator==(const MasterKey &aOther) const { return memcmp(m8, aOther.m8, sizeof(MasterKey)) == 0; } + + /** + * This method evaluates whether or not the Thread Master Keys match. + * + * @param[in] aOther The Thread Master Key to compare. + * + * @retval TRUE If the Thread Master Keys do not match. + * @retval FALSE If the Thread Master Keys match. + * + */ + bool operator!=(const MasterKey &aOther) const { return memcmp(m8, aOther.m8, sizeof(MasterKey)) != 0; } + +} OT_TOOL_PACKED_END; + +/** + * This class defines Thread Key Manager. + * + */ class KeyManager : public InstanceLocator { public: @@ -86,23 +122,23 @@ public: void Stop(void); /** - * This method returns a reference to the Thread Master Key + * This method returns the Thread Master Key. * - * @returns A reference to the Thread Master Key. + * @returns The Thread Master Key. * */ - const otMasterKey &GetMasterKey(void) const; + const MasterKey &GetMasterKey(void) const { return mMasterKey; } /** * This method sets the Thread Master Key. * - * @param[in] aKey A reference to the Thread Master Key. + * @param[in] aKey A Thread Master Key. * * @retval OT_ERROR_NONE Successfully set the Thread Master Key. * @retval OT_ERROR_INVALID_ARGS The @p aKeyLength value was invalid. * */ - otError SetMasterKey(const otMasterKey &aKey); + otError SetMasterKey(const MasterKey &aKey); #if OPENTHREAD_FTD || OPENTHREAD_MTD /** @@ -370,7 +406,10 @@ private: static void HandleKeyRotationTimer(Timer &aTimer); void HandleKeyRotationTimer(void); - otMasterKey mMasterKey; + static const uint8_t kThreadString[]; + static const otMasterKey kDefaultMasterKey; + + MasterKey mMasterKey; uint32_t mKeySequence; uint8_t mKey[Crypto::HmacSha256::kHashSize];