Skip to content

Commit

Permalink
Move audit to InitEntropy()
Browse files Browse the repository at this point in the history
- InitEntropy is in `GenericPlatformManagerImpl` and should always
  be called. Lack of the logs will help catch bad init.
  • Loading branch information
tcarmelveilleux committed Oct 13, 2021
1 parent e160290 commit 33a1302
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
55 changes: 0 additions & 55 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@
#include <inttypes.h>
#include <stddef.h>

// Temporary includes for TemporaryAuditRandomPerformance()
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
#if 1
#include <crypto/CHIPCryptoPAL.h>
#include <lib/support/BytesToHex.h>
#endif

#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPEncoding.h>
#include <lib/support/CHIPFaultInjection.h>
Expand All @@ -57,49 +50,6 @@ using namespace chip::System;
namespace chip {
namespace Messaging {

namespace {

// Audit random number generator proper initialization with prints.
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
void TemporaryAuditRandomNumberGenerator()
{
uint8_t buf1[16];
uint8_t buf2[16];

memset(&buf1[0], 0, sizeof(buf1));
memset(&buf2[0], 0, sizeof(buf2));

VerifyOrDie(chip::Crypto::DRBG_get_bytes(&buf1[0], sizeof(buf1)) == CHIP_NO_ERROR);
VerifyOrDie(chip::Crypto::DRBG_get_bytes(&buf2[0], sizeof(buf2)) == CHIP_NO_ERROR);

char hex_buf[sizeof(buf1) * 2 + 1];

ChipLogProgress(ExchangeManager, "AUDIT: ===== RANDOM NUMBER GENERATOR AUDIT START ====");
ChipLogProgress(ExchangeManager, "AUDIT: * Validate buf1 and buf2 are <<<different every run/boot!>>>");
ChipLogProgress(ExchangeManager, "AUDIT: * Validate r1 and r2 are <<<different every run/boot!>>>");

memset(&hex_buf[0], 0, sizeof(hex_buf));
VerifyOrDie(Encoding::BytesToUppercaseHexString(&buf1[0], sizeof(buf1), &hex_buf[0], sizeof(hex_buf)) == CHIP_NO_ERROR);
ChipLogProgress(ExchangeManager, "AUDIT: * buf1: %s", &hex_buf[0]);

memset(&hex_buf[0], 0, sizeof(hex_buf));
VerifyOrDie(Encoding::BytesToUppercaseHexString(&buf2[0], sizeof(buf2), &hex_buf[0], sizeof(hex_buf)) == CHIP_NO_ERROR);
ChipLogProgress(ExchangeManager, "AUDIT: * buf2: %s", &hex_buf[0]);

VerifyOrDieWithMsg(memcmp(&buf1[0], &buf2[0], sizeof(buf1)) != 0, ExchangeManager,
"AUDIT: FAILED: buf1, buf2 are equal: DRBG_get_bytes() does not function!");

uint32_t r1 = GetRandU32();
uint32_t r2 = GetRandU32();

ChipLogProgress(ExchangeManager, "AUDIT: * r1: 0x%08" PRIX32 " r2: 0x%08" PRIX32, r1, r2);
VerifyOrDieWithMsg(r1 != r2, ExchangeManager,
"AUDIT: FAILED: buf1, buf2 are equal: random number generator does not function!");
ChipLogProgress(ExchangeManager, "AUDIT: ===== RANDOM NUMBER GENERATOR AUDIT END ====");
}

} // namespace

/**
* Constructor for the ExchangeManager class.
* It sets the state to kState_NotInitialized.
Expand All @@ -122,11 +72,6 @@ CHIP_ERROR ExchangeManager::Init(SessionManager * sessionManager)

mSessionManager = sessionManager;

{
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
TemporaryAuditRandomNumberGenerator();
}

mNextExchangeId = GetRandU16();
mNextKeyId = 0;

Expand Down
57 changes: 57 additions & 0 deletions src/platform/Entropy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,60 @@
*/

#include <crypto/CHIPCryptoPAL.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/RandUtils.h>

// Temporary includes for TemporaryAuditRandomPerformance()
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
#if 1
#include <lib/support/BytesToHex.h>
#endif

namespace chip {

namespace {

// Audit random number generator proper initialization with prints.
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
void TemporaryAuditRandomNumberGenerator()
{
uint8_t buf1[16];
uint8_t buf2[16];

memset(&buf1[0], 0, sizeof(buf1));
memset(&buf2[0], 0, sizeof(buf2));

VerifyOrDie(chip::Crypto::DRBG_get_bytes(&buf1[0], sizeof(buf1)) == CHIP_NO_ERROR);
VerifyOrDie(chip::Crypto::DRBG_get_bytes(&buf2[0], sizeof(buf2)) == CHIP_NO_ERROR);

char hex_buf[sizeof(buf1) * 2 + 1];

ChipLogProgress(DeviceLayer, "AUDIT: ===== RANDOM NUMBER GENERATOR AUDIT START ====");
ChipLogProgress(DeviceLayer, "AUDIT: * Validate buf1 and buf2 are <<<different every run/boot!>>>");
ChipLogProgress(DeviceLayer, "AUDIT: * Validate r1 and r2 are <<<different every run/boot!>>>");

memset(&hex_buf[0], 0, sizeof(hex_buf));
VerifyOrDie(Encoding::BytesToUppercaseHexString(&buf1[0], sizeof(buf1), &hex_buf[0], sizeof(hex_buf)) == CHIP_NO_ERROR);
ChipLogProgress(DeviceLayer, "AUDIT: * buf1: %s", &hex_buf[0]);

memset(&hex_buf[0], 0, sizeof(hex_buf));
VerifyOrDie(Encoding::BytesToUppercaseHexString(&buf2[0], sizeof(buf2), &hex_buf[0], sizeof(hex_buf)) == CHIP_NO_ERROR);
ChipLogProgress(DeviceLayer, "AUDIT: * buf2: %s", &hex_buf[0]);

VerifyOrDieWithMsg(memcmp(&buf1[0], &buf2[0], sizeof(buf1)) != 0, DeviceLayer,
"AUDIT: FAILED: buf1, buf2 are equal: DRBG_get_bytes() does not function!");

uint32_t r1 = GetRandU32();
uint32_t r2 = GetRandU32();

ChipLogProgress(DeviceLayer, "AUDIT: * r1: 0x%08" PRIX32 " r2: 0x%08" PRIX32, r1, r2);
VerifyOrDieWithMsg(r1 != r2, DeviceLayer,
"AUDIT: FAILED: buf1, buf2 are equal: random number generator does not function!");
ChipLogProgress(DeviceLayer, "AUDIT: ===== RANDOM NUMBER GENERATOR AUDIT END ====");
}

} // namespace

namespace DeviceLayer {
namespace Internal {

Expand All @@ -33,6 +85,11 @@ CHIP_ERROR InitEntropy()
unsigned int seed;
ReturnErrorOnFailure(chip::Crypto::DRBG_get_bytes((uint8_t *) &seed, sizeof(seed)));
srand(seed);

{
// TODO: remove once https://github.com/project-chip/connectedhomeip/issues/10454 is done.
TemporaryAuditRandomNumberGenerator();
}
return CHIP_NO_ERROR;
}

Expand Down

0 comments on commit 33a1302

Please sign in to comment.