Skip to content

Commit

Permalink
Fix GetRand due to that InitEntropy (srand) is not called.
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Sep 2, 2021
1 parent f0ae9d8 commit e071bd6
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 548 deletions.
7 changes: 6 additions & 1 deletion src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack()
// Arrange for Device Layer errors to be translated to text.
RegisterDeviceLayerErrorFormatter();

// TODO Initialize the source used by CHIP to get secure random data.
err = InitEntropy();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Entropy initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);

err = ConfigurationMgr().Init();
if (err != CHIP_NO_ERROR)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ if (chip_device_platform != "none") {
"../include/platform/internal/GenericSoftwareUpdateManagerImpl_BDX.h",
"../include/platform/internal/testing/ConfigUnitTest.h",
"DeviceControlServer.cpp",
"Entropy.cpp",
"GeneralUtils.cpp",
"Globals.cpp",
"LockTracker.cpp",
Expand All @@ -270,6 +271,7 @@ if (chip_device_platform != "none") {

public_deps = [
":platform_base",
"${chip_root}/src/crypto",
"${chip_root}/src/lib/support",
]

Expand Down
124 changes: 0 additions & 124 deletions src/platform/EFR32/Entropy.cpp

This file was deleted.

90 changes: 0 additions & 90 deletions src/platform/ESP32/Entropy.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions src/platform/ESP32/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
}
}

ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16));

// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_InitChipStack());

ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16));

exit:
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
}
Expand Down
22 changes: 3 additions & 19 deletions src/platform/Linux/Entropy.cpp → src/platform/Entropy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,18 @@
* on the Linux platforms.
*/

#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <support/crypto/CHIPRNG.h>

using namespace ::chip;
#include <crypto/CHIPCryptoPAL.h>

namespace chip {
namespace DeviceLayer {
namespace Internal {

CHIP_ERROR InitEntropy()
{
CHIP_ERROR err;
unsigned int seed;

// Initialize the source used by CHIP to get secure random data.
err = Platform::Security::InitSecureRandomDataSource(getentropy, 64, NULL, 0);
SuccessOrExit(err);

// Seed the standard rand() pseudo-random generator with data from the secure random source.
err = Platform::Security::GetSecureRandomData((uint8_t *) &seed, sizeof(seed));
SuccessOrExit(err);
ReturnErrorOnFailure(chip::Crypto::DRBG_get_bytes((uint8_t *) &seed, sizeof(seed)));
srand(seed);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Crypto, "InitEntropy() failed: %d" err);
}
return err;
return CHIP_NO_ERROR;
}

} // namespace Internal
Expand Down
Loading

0 comments on commit e071bd6

Please sign in to comment.