From 32e308833c957dc6a867662bb118874ba72885a5 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Thu, 16 Apr 2020 13:50:38 -0700 Subject: [PATCH] [random] use crypto RNG in places were non-trival random is required (#4840) --- src/core/coap/coap_message.cpp | 2 +- src/core/mac/mac_types.cpp | 4 +++- src/core/mac/mac_types.hpp | 4 ++++ src/core/net/dhcp6_client.cpp | 2 +- src/core/thread/mle.cpp | 8 ++++---- src/core/utils/slaac_address.cpp | 4 ++-- 6 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index 838a060ac..b5ea2a1bd 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -286,7 +286,7 @@ otError Message::SetToken(uint8_t aTokenLength) OT_ASSERT(aTokenLength <= sizeof(token)); - Random::NonCrypto::FillBuffer(token, aTokenLength); + Random::Crypto::FillBuffer(token, aTokenLength); return SetToken(token, aTokenLength); } diff --git a/src/core/mac/mac_types.cpp b/src/core/mac/mac_types.cpp index 65703fac5..c06c9c859 100644 --- a/src/core/mac/mac_types.cpp +++ b/src/core/mac/mac_types.cpp @@ -54,12 +54,14 @@ PanId GenerateRandomPanId(void) return panId; } +#if !OPENTHREAD_RADIO void ExtAddress::GenerateRandom(void) { - Random::NonCrypto::FillBuffer(m8, sizeof(ExtAddress)); + Random::Crypto::FillBuffer(m8, sizeof(ExtAddress)); SetGroup(false); SetLocal(true); } +#endif bool ExtAddress::operator==(const ExtAddress &aOther) const { diff --git a/src/core/mac/mac_types.hpp b/src/core/mac/mac_types.hpp index f38828ea0..2d3e6a0a5 100644 --- a/src/core/mac/mac_types.hpp +++ b/src/core/mac/mac_types.hpp @@ -73,6 +73,8 @@ typedef otPanId PanId; */ typedef otShortAddress ShortAddress; +#if !OPENTHREAD_RADIO + /** * This function generates a random IEEE 802.15.4 PAN ID. * @@ -81,6 +83,8 @@ typedef otShortAddress ShortAddress; */ PanId GenerateRandomPanId(void); +#endif + /** * This structure represents an IEEE 802.15.4 Extended Address. * diff --git a/src/core/net/dhcp6_client.cpp b/src/core/net/dhcp6_client.cpp index d4e7e8ed1..e84df5ead 100644 --- a/src/core/net/dhcp6_client.cpp +++ b/src/core/net/dhcp6_client.cpp @@ -202,7 +202,7 @@ bool Dhcp6Client::ProcessNextIdentityAssociation() } // new transaction id - Random::NonCrypto::FillBuffer(mTransactionId, kTransactionIdSize); + Random::Crypto::FillBuffer(mTransactionId, kTransactionIdSize); mIdentityAssociationCurrent = &mIdentityAssociations[i]; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index f8cbd7d03..fb29fab59 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -190,8 +190,8 @@ Mle::Mle(Instance &aInstance) // mesh-local 64 mMeshLocal64.Clear(); - Random::NonCrypto::FillBuffer(mMeshLocal64.GetAddress().mFields.m8 + OT_IP6_PREFIX_SIZE, - OT_IP6_ADDRESS_SIZE - OT_IP6_PREFIX_SIZE); + Random::Crypto::FillBuffer(mMeshLocal64.GetAddress().mFields.m8 + OT_IP6_PREFIX_SIZE, + OT_IP6_ADDRESS_SIZE - OT_IP6_PREFIX_SIZE); mMeshLocal64.mPrefixLength = MeshLocalPrefix::kLength; mMeshLocal64.mPreferred = true; @@ -1555,8 +1555,8 @@ void Mle::HandleStateChanged(otChangedFlags aFlags) if (!Get().IsUnicastAddress(mMeshLocal64.GetAddress())) { // Mesh Local EID was removed, choose a new one and add it back - Random::NonCrypto::FillBuffer(mMeshLocal64.GetAddress().mFields.m8 + OT_IP6_PREFIX_SIZE, - OT_IP6_ADDRESS_SIZE - OT_IP6_PREFIX_SIZE); + Random::Crypto::FillBuffer(mMeshLocal64.GetAddress().mFields.m8 + OT_IP6_PREFIX_SIZE, + OT_IP6_ADDRESS_SIZE - OT_IP6_PREFIX_SIZE); Get().AddUnicastAddress(mMeshLocal64); Get().Signal(OT_CHANGED_THREAD_ML_ADDR); diff --git a/src/core/utils/slaac_address.cpp b/src/core/utils/slaac_address.cpp index b31c4dfa4..b60b00447 100644 --- a/src/core/utils/slaac_address.cpp +++ b/src/core/utils/slaac_address.cpp @@ -298,7 +298,7 @@ void Slaac::GenerateIid(Ip6::NetifUnicastAddress &aAddress) const } otLogWarnUtil("SLAAC: Failed to generate a non-reserved IID after %d attempts", dadCounter); - Random::NonCrypto::FillBuffer(hash, Ip6::Address::kInterfaceIdentifierSize); + Random::Crypto::FillBuffer(hash, Ip6::Address::kInterfaceIdentifierSize); aAddress.GetAddress().SetIid(&hash[0]); exit: @@ -319,7 +319,7 @@ void Slaac::GetIidSecretKey(IidSecretKey &aKey) const if (error != OT_ERROR_NONE) { - Random::NonCrypto::FillBuffer(aKey.m8, sizeof(IidSecretKey)); + Random::Crypto::FillBuffer(aKey.m8, sizeof(IidSecretKey)); } Get().SaveSlaacIidSecretKey(aKey);