[key-manager] add a common GenerateNonce used by both MAC and MLE (#4086)

This commit is contained in:
Abtin Keshavarzian
2019-08-12 09:06:16 -07:00
committed by Jonathan Hui
parent c5be94a7dc
commit 7d42350693
6 changed files with 39 additions and 61 deletions
+4 -25
View File
@@ -768,27 +768,6 @@ void Mac::FinishOperation(void)
mOperation = kOperationIdle;
}
void Mac::GenerateNonce(const ExtAddress &aAddress, uint32_t aFrameCounter, uint8_t aSecurityLevel, uint8_t *aNonce)
{
// source address
for (int i = 0; i < 8; i++)
{
aNonce[i] = aAddress.m8[i];
}
aNonce += 8;
// frame counter
aNonce[0] = (aFrameCounter >> 24) & 0xff;
aNonce[1] = (aFrameCounter >> 16) & 0xff;
aNonce[2] = (aFrameCounter >> 8) & 0xff;
aNonce[3] = (aFrameCounter >> 0) & 0xff;
aNonce += 4;
// security level
aNonce[0] = aSecurityLevel;
}
otError Mac::PrepareDataRequest(TxFrame &aFrame)
{
otError error = OT_ERROR_NONE;
@@ -913,7 +892,7 @@ void Mac::ProcessTransmitAesCcm(TxFrame &aFrame, const ExtAddress *aExtAddress)
{
uint32_t frameCounter = 0;
uint8_t securityLevel;
uint8_t nonce[kNonceSize];
uint8_t nonce[KeyManager::kNonceSize];
uint8_t tagLength;
Crypto::AesCcm aesCcm;
otError error;
@@ -921,7 +900,7 @@ void Mac::ProcessTransmitAesCcm(TxFrame &aFrame, const ExtAddress *aExtAddress)
aFrame.GetSecurityLevel(securityLevel);
aFrame.GetFrameCounter(frameCounter);
GenerateNonce(*aExtAddress, frameCounter, securityLevel, nonce);
KeyManager::GenerateNonce(*aExtAddress, frameCounter, securityLevel, nonce);
aesCcm.SetKey(aFrame.GetAesKey(), 16);
tagLength = aFrame.GetFooterLength() - Frame::kFcsSize;
@@ -1380,7 +1359,7 @@ otError Mac::ProcessReceiveSecurity(RxFrame &aFrame, const Address &aSrcAddr, Ne
uint8_t securityLevel;
uint8_t keyIdMode;
uint32_t frameCounter;
uint8_t nonce[kNonceSize];
uint8_t nonce[KeyManager::kNonceSize];
uint8_t tag[Frame::kMaxMicSize];
uint8_t tagLength;
uint8_t keyid;
@@ -1472,7 +1451,7 @@ otError Mac::ProcessReceiveSecurity(RxFrame &aFrame, const Address &aSrcAddr, Ne
break;
}
GenerateNonce(*extAddress, frameCounter, securityLevel, nonce);
KeyManager::GenerateNonce(*extAddress, frameCounter, securityLevel, nonce);
tagLength = aFrame.GetFooterLength() - Frame::kFcsSize;
aesCcm.SetKey(macKey, 16);
-6
View File
@@ -73,7 +73,6 @@ enum
{
kDataPollTimeout = 100, ///< Timeout for receiving Data Frame (milliseconds).
kSleepDelay = 300, ///< Max sleep delay when frame is pending (milliseconds).
kNonceSize = 13, ///< Size of IEEE 802.15.4 Nonce (bytes).
kScanDurationDefault = 300, ///< Default interval between channels (milliseconds).
@@ -645,11 +644,6 @@ private:
*/
void ProcessTransmitSecurity(TxFrame &aFrame, bool aProcessAesCcm);
static void GenerateNonce(const ExtAddress &aAddress,
uint32_t aFrameCounter,
uint8_t aSecurityLevel,
uint8_t * aNonce);
otError ProcessReceiveSecurity(RxFrame &aFrame, const Address &aSrcAddr, Neighbor *aNeighbor);
void UpdateIdleMode(void);
void StartOperation(Operation aOperation);
+14
View File
@@ -292,4 +292,18 @@ void KeyManager::HandleKeyRotationTimer(void)
}
}
void KeyManager::GenerateNonce(const Mac::ExtAddress &aAddress,
uint32_t aFrameCounter,
uint8_t aSecurityLevel,
uint8_t * aNonce)
{
memcpy(aNonce, aAddress.m8, sizeof(Mac::ExtAddress));
aNonce += sizeof(Mac::ExtAddress);
Encoding::BigEndian::WriteUint32(aFrameCounter, aNonce);
aNonce += sizeof(uint32_t);
aNonce[0] = aSecurityLevel;
}
} // namespace ot
+16
View File
@@ -43,6 +43,7 @@
#include "common/locator.hpp"
#include "common/timer.hpp"
#include "crypto/hmac_sha256.hpp"
#include "mac/mac_frame.hpp"
namespace ot {
@@ -61,6 +62,7 @@ public:
enum
{
kMaxKeyLength = 16,
kNonceSize = 13, ///< Size of IEEE 802.15.4 Nonce (bytes).
};
/**
@@ -338,6 +340,20 @@ public:
*/
void SetSecurityPolicyFlags(uint8_t aSecurityPolicyFlags);
/**
* This static method generates IEEE 802.15.4 nonce byte sequence.
*
* @param[in] aAddress An extended address.
* @param[in] aFrameCounter A frame counter.
* @param[in] aSecurityLevel A security level.
* @param[out] aNonce A buffer (with `kNonceSize` bytes) to place the generated nonce.
*
*/
static void GenerateNonce(const Mac::ExtAddress &aAddress,
uint32_t aFrameCounter,
uint8_t aSecurityLevel,
uint8_t * aNonce);
private:
enum
{
+5 -25
View File
@@ -1083,26 +1083,6 @@ exit:
return error;
}
void Mle::GenerateNonce(const Mac::ExtAddress &aMacAddr,
uint32_t aFrameCounter,
uint8_t aSecurityLevel,
uint8_t * aNonce)
{
// source address
memcpy(aNonce, aMacAddr.m8, sizeof(aMacAddr));
aNonce += sizeof(aMacAddr);
// frame counter
aNonce[0] = (aFrameCounter >> 24) & 0xff;
aNonce[1] = (aFrameCounter >> 16) & 0xff;
aNonce[2] = (aFrameCounter >> 8) & 0xff;
aNonce[3] = aFrameCounter & 0xff;
aNonce += 4;
// security level
aNonce[0] = aSecurityLevel;
}
Message *Mle::NewMleMessage(void)
{
Message * message;
@@ -2513,7 +2493,7 @@ otError Mle::SendMessage(Message &aMessage, const Ip6::Address &aDestination)
otError error = OT_ERROR_NONE;
Header header;
uint32_t keySequence;
uint8_t nonce[13];
uint8_t nonce[KeyManager::kNonceSize];
uint8_t tag[4];
uint8_t tagLength;
Crypto::AesCcm aesCcm;
@@ -2532,8 +2512,8 @@ otError Mle::SendMessage(Message &aMessage, const Ip6::Address &aDestination)
aMessage.Write(0, header.GetLength(), &header);
GenerateNonce(Get<Mac::Mac>().GetExtAddress(), Get<KeyManager>().GetMleFrameCounter(), Mac::Frame::kSecEncMic32,
nonce);
KeyManager::GenerateNonce(Get<Mac::Mac>().GetExtAddress(), Get<KeyManager>().GetMleFrameCounter(),
Mac::Frame::kSecEncMic32, nonce);
aesCcm.SetKey(Get<KeyManager>().GetCurrentMleKey(), 16);
error = aesCcm.Init(16 + 16 + header.GetHeaderLength(), aMessage.GetLength() - (header.GetLength() - 1),
@@ -2616,7 +2596,7 @@ void Mle::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageIn
const uint8_t * mleKey;
uint32_t frameCounter;
uint8_t messageTag[4];
uint8_t nonce[13];
uint8_t nonce[KeyManager::kNonceSize];
Mac::ExtAddress macAddr;
Crypto::AesCcm aesCcm;
uint16_t mleOffset;
@@ -2675,7 +2655,7 @@ void Mle::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageIn
aMessageInfo.GetPeerAddr().ToExtAddress(macAddr);
frameCounter = header.GetFrameCounter();
GenerateNonce(macAddr, frameCounter, Mac::Frame::kSecEncMic32, nonce);
KeyManager::GenerateNonce(macAddr, frameCounter, Mac::Frame::kSecEncMic32, nonce);
aesCcm.SetKey(mleKey, 16);
SuccessOrExit(
-5
View File
@@ -1688,11 +1688,6 @@ private:
kDataRequestActive, ///< Data Request has been sent, Data Response is expected.
};
void GenerateNonce(const Mac::ExtAddress &aMacAddr,
uint32_t aFrameCounter,
uint8_t aSecurityLevel,
uint8_t * aNonce);
static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags);
void HandleStateChanged(otChangedFlags aFlags);
static void HandleAttachTimer(Timer &aTimer);