mirror of
https://github.com/espressif/openthread.git
synced 2026-08-05 18:37:45 +00:00
[key-manager] add MasterKey class (wrapper over otMasterKey) (#4150)
The `MasterKey` class mainly provides overloads of operators `==` and `!=` which help simplfy the code comparing keys.
This commit is contained in:
committed by
Jonathan Hui
parent
472a03af88
commit
0b220613ab
@@ -131,7 +131,7 @@ otError otThreadSetMasterKey(otInstance *aInstance, const otMasterKey *aKey)
|
||||
VerifyOrExit(aKey != NULL, error = OT_ERROR_INVALID_ARGS);
|
||||
VerifyOrExit(instance.Get<Mle::MleRouter>().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
error = instance.Get<KeyManager>().SetMasterKey(*aKey);
|
||||
error = instance.Get<KeyManager>().SetMasterKey(*static_cast<const MasterKey *>(aKey));
|
||||
instance.Get<MeshCoP::ActiveDataset>().Clear();
|
||||
instance.Get<MeshCoP::PendingDataset>().Clear();
|
||||
|
||||
|
||||
@@ -320,7 +320,7 @@ otError Dataset::Set(const otOperationalDataset &aDataset)
|
||||
{
|
||||
MeshCoP::NetworkMasterKeyTlv tlv;
|
||||
tlv.Init();
|
||||
tlv.SetNetworkMasterKey(aDataset.mMasterKey);
|
||||
tlv.SetNetworkMasterKey(static_cast<const MasterKey &>(aDataset.mMasterKey));
|
||||
Set(tlv);
|
||||
}
|
||||
|
||||
@@ -564,8 +564,7 @@ otError Dataset::ApplyConfiguration(Instance &aInstance, bool *aIsMasterKeyUpdat
|
||||
{
|
||||
const NetworkMasterKeyTlv *key = static_cast<const NetworkMasterKeyTlv *>(cur);
|
||||
|
||||
if (aIsMasterKeyUpdated &&
|
||||
memcmp(&key->GetNetworkMasterKey(), &keyManager.GetMasterKey(), OT_MASTER_KEY_SIZE))
|
||||
if (aIsMasterKeyUpdated && (key->GetNetworkMasterKey() != keyManager.GetMasterKey()))
|
||||
{
|
||||
*aIsMasterKeyUpdated = true;
|
||||
}
|
||||
|
||||
@@ -470,7 +470,7 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con
|
||||
{
|
||||
NetworkMasterKeyTlv masterkey;
|
||||
masterkey.Init();
|
||||
masterkey.SetNetworkMasterKey(aDataset.mMasterKey);
|
||||
masterkey.SetNetworkMasterKey(static_cast<const MasterKey &>(aDataset.mMasterKey));
|
||||
SuccessOrExit(error = message->AppendTlv(masterkey));
|
||||
}
|
||||
|
||||
|
||||
@@ -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<KeyManager>().GetMasterKey(), OT_MASTER_KEY_SIZE))
|
||||
masterKey.IsValid() && (masterKey.GetNetworkMasterKey() != Get<KeyManager>().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<KeyManager>().GetMasterKey(), OT_MASTER_KEY_SIZE) == 0))
|
||||
((masterKey.GetLength() == 0) || (masterKey.GetNetworkMasterKey() == Get<KeyManager>().GetMasterKey())))
|
||||
{
|
||||
// no change to master key, active timestamp must be ahead
|
||||
const Timestamp *localActiveTimestamp = Get<ActiveDataset>().GetTimestamp();
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<const MasterKey &>(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<Notifier>().SignalIfFirst(OT_CHANGED_MASTER_KEY));
|
||||
VerifyOrExit(mMasterKey != aKey, Get<Notifier>().SignalIfFirst(OT_CHANGED_MASTER_KEY));
|
||||
|
||||
mMasterKey = aKey;
|
||||
mKeySequence = 0;
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user