From 5b188dccb000798abd704afcd85901343c7f1e44 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 11 Oct 2024 13:13:06 -0700 Subject: [PATCH] [mac-frame] update `CalculateKeySourceSize()` (#10815) This commit updates the helper method in `Mac::Frame` that determines the security header's "key source" field size based on the "Key ID Mode": - It is renamed to `CalculateKeySourceSize()` to align with other methods, such as `CalculateSecurityHeaderSize()` and `CalculateMicSize()`. - It now takes `uint8_t aSecurityControl` as input to harmonize with other `Calculate` helpers. --- src/core/mac/mac_frame.cpp | 56 +++++++++++++++----------------------- src/core/mac/mac_frame.hpp | 3 +- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/core/mac/mac_frame.cpp b/src/core/mac/mac_frame.cpp index b8dea18a466..6225420311f 100644 --- a/src/core/mac/mac_frame.cpp +++ b/src/core/mac/mac_frame.cpp @@ -746,55 +746,55 @@ const uint8_t *Frame::GetKeySource(void) const return &mPsdu[index + kSecurityControlSize + kFrameCounterSize]; } -uint8_t Frame::GetKeySourceLength(uint8_t aKeyIdMode) +uint8_t Frame::CalculateKeySourceSize(uint8_t aSecurityControl) { - uint8_t len = 0; + uint8_t size = 0; - switch (aKeyIdMode) + switch (aSecurityControl & kKeyIdModeMask) { case kKeyIdMode0: - len = kKeySourceSizeMode0; + size = kKeySourceSizeMode0; break; case kKeyIdMode1: - len = kKeySourceSizeMode1; + size = kKeySourceSizeMode1; break; case kKeyIdMode2: - len = kKeySourceSizeMode2; + size = kKeySourceSizeMode2; break; case kKeyIdMode3: - len = kKeySourceSizeMode3; + size = kKeySourceSizeMode3; break; } - return len; + return size; } void Frame::SetKeySource(const uint8_t *aKeySource) { - uint8_t keySourceLength; + uint8_t keySourceSize; uint8_t index = FindSecurityHeaderIndex(); OT_ASSERT(index != kInvalidIndex); - keySourceLength = GetKeySourceLength(mPsdu[index] & kKeyIdModeMask); + keySourceSize = CalculateKeySourceSize(mPsdu[index]); - memcpy(&mPsdu[index + kSecurityControlSize + kFrameCounterSize], aKeySource, keySourceLength); + memcpy(&mPsdu[index + kSecurityControlSize + kFrameCounterSize], aKeySource, keySourceSize); } Error Frame::GetKeyId(uint8_t &aKeyId) const { Error error = kErrorNone; - uint8_t keySourceLength; + uint8_t keySourceSize; uint8_t index = FindSecurityHeaderIndex(); VerifyOrExit(index != kInvalidIndex, error = kErrorParse); - keySourceLength = GetKeySourceLength(mPsdu[index] & kKeyIdModeMask); + keySourceSize = CalculateKeySourceSize(mPsdu[index]); - aKeyId = mPsdu[index + kSecurityControlSize + kFrameCounterSize + keySourceLength]; + aKeyId = mPsdu[index + kSecurityControlSize + kFrameCounterSize + keySourceSize]; exit: return error; @@ -802,14 +802,14 @@ Error Frame::GetKeyId(uint8_t &aKeyId) const void Frame::SetKeyId(uint8_t aKeyId) { - uint8_t keySourceLength; + uint8_t keySourceSize; uint8_t index = FindSecurityHeaderIndex(); OT_ASSERT(index != kInvalidIndex); - keySourceLength = GetKeySourceLength(mPsdu[index] & kKeyIdModeMask); + keySourceSize = CalculateKeySourceSize(mPsdu[index]); - mPsdu[index + kSecurityControlSize + kFrameCounterSize + keySourceLength] = aKeyId; + mPsdu[index + kSecurityControlSize + kFrameCounterSize + keySourceSize] = aKeyId; } Error Frame::GetCommandId(uint8_t &aCommandId) const @@ -943,27 +943,15 @@ uint16_t Frame::DetermineFcfAddrType(const Address &aAddress, uint16_t aBitShift uint8_t Frame::CalculateSecurityHeaderSize(uint8_t aSecurityControl) { - uint8_t size = kSecurityControlSize + kFrameCounterSize; + uint8_t size; VerifyOrExit((aSecurityControl & kSecLevelMask) != kSecurityNone, size = kInvalidSize); - switch (aSecurityControl & kKeyIdModeMask) - { - case kKeyIdMode0: - size += kKeySourceSizeMode0; - break; + size = kSecurityControlSize + kFrameCounterSize + CalculateKeySourceSize(aSecurityControl); - case kKeyIdMode1: - size += kKeySourceSizeMode1 + kKeyIndexSize; - break; - - case kKeyIdMode2: - size += kKeySourceSizeMode2 + kKeyIndexSize; - break; - - case kKeyIdMode3: - size += kKeySourceSizeMode3 + kKeyIndexSize; - break; + if ((aSecurityControl & kKeyIdModeMask) != kKeyIdMode0) + { + size += kKeyIndexSize; } exit: diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 245caa2898e..b5367663531 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -858,8 +858,6 @@ class Frame : public otRadioFrame uint8_t FindHeaderIeIndex(void) const; #endif - static uint8_t GetKeySourceLength(uint8_t aKeyIdMode); - #if OPENTHREAD_CONFIG_MAC_MULTIPURPOSE_FRAME static uint8_t GetFcfSize(uint16_t aFcf) { return IsShortFcf(aFcf) ? kShortFcfSize : kFcfSize; } #else @@ -915,6 +913,7 @@ class Frame : public otRadioFrame static uint8_t CalculateAddrFieldSize(uint16_t aFcf); static uint8_t CalculateSecurityHeaderSize(uint8_t aSecurityControl); + static uint8_t CalculateKeySourceSize(uint8_t aSecurityControl); static uint8_t CalculateMicSize(uint8_t aSecurityControl); };