Skip to content

Commit

Permalink
[core] replace #if !OPENTHREAD_RADIO with explicit FTD or MTD check (
Browse files Browse the repository at this point in the history
…openthread#11190)

This commit replaces the `#if !OPENTHREAD_RADIO` checks with explicit
`#if OPENTHREAD_FTD || OPENTHREAD_MTD` checks.

The existing check relied on the assumption that exactly one of the
three options `FTD`, `MTD`, or `RADIO` will be set, and therefore
`#if !RADIO`  was used indirectly as a check for `FTD` or `MTD`. This
change improves clarity and allows for future addition of new
configurations (where this assumption may no longer hold true).
  • Loading branch information
abtink authored Jan 23, 2025
1 parent e363fae commit 648f1a9
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/core/common/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Manager::Manager(void)

VerifyOrExit(sInitCount == 0);

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
otPlatCryptoRandomInit();
SuccessOrAssert(Random::Crypto::Fill(seed));
#else
Expand All @@ -72,7 +72,7 @@ Manager::~Manager(void)
sInitCount--;
VerifyOrExit(sInitCount == 0);

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
otPlatCryptoRandomDeinit();
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/core/common/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Manager : private NonCopyable
*/
static uint32_t NonCryptoGetUint32(void);

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Fills a given buffer with cryptographically secure random bytes.
*
Expand Down Expand Up @@ -189,7 +189,7 @@ uint32_t AddJitter(uint32_t aValue, uint16_t aJitter);

} // namespace NonCrypto

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD

namespace Crypto {

Expand Down Expand Up @@ -222,7 +222,7 @@ template <typename ObjectType> Error Fill(ObjectType &aObject)

} // namespace Crypto

#endif // !OPENTHREAD_RADIO
#endif // OPENTHREAD_FTD || OPENTHREAD_MTD

} // namespace Random
} // namespace ot
Expand Down
2 changes: 1 addition & 1 deletion src/core/crypto/aes_ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void AesCcm::Payload(void *aPlainText, void *aCipherText, uint32_t aLength, Mode
}
}

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
void AesCcm::Payload(Message &aMessage, uint16_t aOffset, uint16_t aLength, Mode aMode)
{
Message::MutableChunk chunk;
Expand Down
2 changes: 1 addition & 1 deletion src/core/crypto/aes_ccm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class AesCcm
*/
void Payload(void *aPlainText, void *aCipherText, uint32_t aLength, Mode aMode);

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Processes the payload within a given message.
*
Expand Down
10 changes: 5 additions & 5 deletions src/core/crypto/crypto_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ using namespace Crypto;
#define OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT
#endif

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
static mbedtls_ctr_drbg_context sCtrDrbgContext;
static mbedtls_entropy_context sEntropyContext;
#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT
Expand Down Expand Up @@ -147,7 +147,7 @@ OT_TOOL_WEAK otError otPlatCryptoAesFree(otCryptoContext *aContext)
return error;
}

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD

// HMAC implementations
OT_TOOL_WEAK otError otPlatCryptoHmacSha256Init(otCryptoContext *aContext)
Expand Down Expand Up @@ -660,7 +660,7 @@ OT_TOOL_WEAK otError otPlatCryptoEcdsaVerify(const otPlatCryptoEcdsaPublicKey *a

#endif // #if OPENTHREAD_CONFIG_ECDSA_ENABLE

#endif // #if !OPENTHREAD_RADIO
#endif // #if OPENTHREAD_FTD || OPENTHREAD_MTD

#if OPENTHREAD_FTD

Expand Down Expand Up @@ -752,7 +752,7 @@ OT_TOOL_WEAK otError otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword,

#elif OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
#if OPENTHREAD_CONFIG_ECDSA_ENABLE

OT_TOOL_WEAK otError otPlatCryptoEcdsaGenerateKey(otPlatCryptoEcdsaKeyPair *aKeyPair)
Expand Down Expand Up @@ -795,7 +795,7 @@ OT_TOOL_WEAK otError otPlatCryptoEcdsaVerify(const otPlatCryptoEcdsaPublicKey *a
}
#endif // #if OPENTHREAD_CONFIG_ECDSA_ENABLE

#endif // #if !OPENTHREAD_RADIO
#endif // #if OPENTHREAD_FTD || OPENTHREAD_MTD

#if OPENTHREAD_FTD

Expand Down
4 changes: 2 additions & 2 deletions src/core/crypto/mbedtls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Error MbedTls::MapError(int aMbedTlsError)
return error;
}

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD

int MbedTls::CryptoSecurePrng(void *, unsigned char *aBuffer, size_t aSize)
{
Expand All @@ -179,7 +179,7 @@ int MbedTls::CryptoSecurePrng(void *, unsigned char *aBuffer, size_t aSize)
return 0;
}

#endif // !OPENTHREAD_RADIO
#endif // OPENTHREAD_FTD || OPENTHREAD_MTD

} // namespace Crypto
} // namespace ot
2 changes: 1 addition & 1 deletion src/core/crypto/mbedtls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MbedTls : private NonCopyable
*/
static Error MapError(int aMbedTlsError);

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Fills a given buffer with cryptographically secure random bytes.
*
Expand Down
2 changes: 1 addition & 1 deletion src/core/mac/mac_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "common/log.hpp"
#include "common/num_utils.hpp"
#include "radio/trel_link.hpp"
#if !OPENTHREAD_RADIO || OPENTHREAD_CONFIG_MAC_SOFTWARE_TX_SECURITY_ENABLE
#if OPENTHREAD_FTD || OPENTHREAD_MTD || OPENTHREAD_CONFIG_MAC_SOFTWARE_TX_SECURITY_ENABLE
#include "crypto/aes_ccm.hpp"
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/core/mac/mac_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PanId GenerateRandomPanId(void)
return panId;
}

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
void ExtAddress::GenerateRandom(void)
{
IgnoreError(Random::Crypto::Fill(*this));
Expand Down
2 changes: 2 additions & 0 deletions src/core/mac/mac_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ class ExtAddress : public otExtAddress, public Equatable<ExtAddress>, public Cle
*/
void Fill(uint8_t aByte) { memset(this, aByte, sizeof(*this)); }

#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Generates a random IEEE 802.15.4 Extended Address.
*/
void GenerateRandom(void);
#endif

/**
* Sets the Extended Address from a given byte array.
Expand Down
4 changes: 2 additions & 2 deletions src/core/thread/key_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class NetworkKey : public otNetworkKey, public Equatable<NetworkKey>, public Cle
public:
static constexpr uint8_t kSize = OT_NETWORK_KEY_SIZE; ///< Size of the Thread Network Key (in bytes).

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Generates a cryptographically secure random sequence to populate the Thread Network Key.
*
Expand Down Expand Up @@ -168,7 +168,7 @@ class Pskc : public otPskc, public Equatable<Pskc>, public Clearable<Pskc>
public:
static constexpr uint8_t kSize = OT_PSKC_MAX_SIZE; ///< Size (number of bytes) of the PSKc.

#if !OPENTHREAD_RADIO
#if OPENTHREAD_FTD || OPENTHREAD_MTD
/**
* Generates a cryptographically secure random sequence to populate the Thread PSKc.
*
Expand Down

0 comments on commit 648f1a9

Please sign in to comment.