Skip to content

Commit

Permalink
Implement the missing parts to link message layer to CHIP.
Browse files Browse the repository at this point in the history
* Implement a basic set of protocols to enable basic feature of message layer
* Add message layer to GN build system

Restyled by whitespace

Restyled by clang-format

Restyled by gn
  • Loading branch information
yufengwangca committed Sep 15, 2020
1 parent 7aa6f53 commit 5754565
Show file tree
Hide file tree
Showing 23 changed files with 2,451 additions and 32 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if (current_toolchain != "${dir_pw_toolchain}/dummy:dummy") {
"${chip_root}/src/inet",
"${chip_root}/src/lib",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/message",
"${chip_root}/src/lib/shell",
"${chip_root}/src/lib/support",
"${chip_root}/src/lwip:all",
Expand Down
6 changes: 6 additions & 0 deletions src/include/platform/CHIPDeviceLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <ble/BleLayer.h>
#include <core/CHIPCore.h>
#include <message/CHIPGlobals.h>
#include <platform/CHIPDeviceError.h>
#include <platform/ConfigurationManager.h>
#include <platform/ConnectivityManager.h>
Expand All @@ -40,6 +41,11 @@ namespace chip {
namespace DeviceLayer {

struct ChipDeviceEvent;

using chip::ExchangeMgr;
using chip::FabricState;
using chip::MessageLayer;

extern chip::System::Layer SystemLayer;
extern Inet::InetLayer InetLayer;

Expand Down
46 changes: 42 additions & 4 deletions src/include/platform/internal/GenericPlatformManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack(void)
}
SuccessOrExit(err);

// Initialize the CHIP system layer.
// Initialize the CHIP System Layer.
new (&SystemLayer) System::Layer();
err = SystemLayer.Init(NULL);
if (err != CHIP_SYSTEM_NO_ERROR)
Expand All @@ -70,7 +70,7 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack(void)
}
SuccessOrExit(err);

// Initialize the CHIP Inet layer.
// Initialize the CHIP Inet Layer.
new (&InetLayer) Inet::InetLayer();
err = InetLayer.Init(SystemLayer, NULL);
if (err != INET_NO_ERROR)
Expand All @@ -79,9 +79,47 @@ CHIP_ERROR GenericPlatformManagerImpl<ImplClass>::_InitChipStack(void)
}
SuccessOrExit(err);

// TODO Perform dynamic configuration of the core CHIP objects based on stored settings.
#if CHIP_ENABLE_MESSAGE_LAYER
// Initialize the CHIP Fabric State object.
new (&FabricState) ChipFabricState();
err = FabricState.Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "FabricState initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);

FabricState.DefaultSubnet = kChipSubnetId_PrimaryWiFi;

{
ChipMessageLayer::InitContext initContext;
initContext.systemLayer = &SystemLayer;
initContext.inet = &InetLayer;
initContext.listenTCP = true;
initContext.listenUDP = true;
initContext.fabricState = &FabricState;

// Initialize the CHIP Message Layer.
new (&MessageLayer) ChipMessageLayer();
err = MessageLayer.Init(&initContext);
if (err != CHIP_NO_ERROR) {
ChipLogError(DeviceLayer, "MessageLayer initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);
}

// Hook the MessageLayer activity changed callback.
MessageLayer.SetSignalMessageLayerActivityChanged(ImplClass::HandleMessageLayerActivityChanged);

// Initialize the CHIP Exchange Manager.
err = ExchangeMgr.Init(&MessageLayer);
if (err != CHIP_NO_ERROR) {
ChipLogError(DeviceLayer, "ExchangeMgr initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);
#endif

// Initialize the CHIP BLE manager.
// Initialize the CHIP BLE Manager.
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
err = BLEMgr().Init();
if (err != CHIP_NO_ERROR)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ static_library("lib") {
"${chip_root}/src/crypto",
"${chip_root}/src/inet",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/message",
"${chip_root}/src/lib/protocols",
"${chip_root}/src/lib/support",
"${chip_root}/src/platform",
"${chip_root}/src/setup_payload",
Expand Down
13 changes: 11 additions & 2 deletions src/lib/message/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ static_library("message") {
"CHIPServerBase.cpp",
"CHIPServerBase.h",
"ExchangeContext.cpp",
"support/crypto/AESBlockCipher.cpp",
"support/crypto/AESBlockCipher.h",
"support/crypto/CHIPCrypto.cpp",
"support/crypto/CHIPCrypto.h",
"support/crypto/CHIPRNG.cpp",
"support/crypto/CHIPRNG.h",
"support/crypto/CTRMode.cpp",
"support/crypto/CTRMode.h",
]

public_deps = [
Expand All @@ -51,13 +59,14 @@ static_library("message") {
"${chip_root}/src/crypto",
"${chip_root}/src/inet",
"${chip_root}/src/inet:inet_config_header",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/core:chip_config_header",
"${chip_root}/src/lib/protocols",
"${chip_root}/src/lib/support",
"${chip_root}/src/platform",
"${chip_root}/src/system",
"${nlio_root}:nlio",
]

allow_circular_includes_from = [ "${chip_root}/src/lib/protocols" ]

public_configs = [ ":includes" ]
}
112 changes: 112 additions & 0 deletions src/lib/message/support/crypto/AESBlockCipher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements AES block cipher functions for the Weave layer.
* The implementation is based on the OpenSSL library functions.
* This implementation is used when #CHIP_CONFIG_AES_IMPLEMENTATION_OPENSSL
* is enabled (1).
*
*/

#include <string.h>

#include "AESBlockCipher.h"
#include "CHIPCrypto.h"

namespace chip {
namespace Platform {
namespace Security {

using namespace chip::Crypto;

AES128BlockCipher::AES128BlockCipher()
{
memset(&mKey, 0, sizeof(mKey));
}

AES128BlockCipher::~AES128BlockCipher()
{
Reset();
}

void AES128BlockCipher::Reset()
{
ClearSecretData((uint8_t *) &mKey, sizeof(mKey));
}

void AES128BlockCipherEnc::SetKey(const uint8_t * key)
{
// AES_set_encrypt_key(key, kKeyLengthBits, &mKey);
}

void AES128BlockCipherEnc::EncryptBlock(const uint8_t * inBlock, uint8_t * outBlock)
{
// AES_encrypt(inBlock, outBlock, &mKey);
}

void AES128BlockCipherDec::SetKey(const uint8_t * key)
{
// AES_set_decrypt_key(key, kKeyLengthBits, &mKey);
}

void AES128BlockCipherDec::DecryptBlock(const uint8_t * inBlock, uint8_t * outBlock)
{
// AES_decrypt(inBlock, outBlock, &mKey);
}

AES256BlockCipher::AES256BlockCipher()
{
memset(&mKey, 0, sizeof(mKey));
}

AES256BlockCipher::~AES256BlockCipher()
{
Reset();
}

void AES256BlockCipher::Reset()
{
ClearSecretData((uint8_t *) &mKey, sizeof(mKey));
}

void AES256BlockCipherEnc::SetKey(const uint8_t * key)
{
// AES_set_encrypt_key(key, kKeyLengthBits, &mKey);
}

void AES256BlockCipherEnc::EncryptBlock(const uint8_t * inBlock, uint8_t * outBlock)
{
// AES_encrypt(inBlock, outBlock, &mKey);
}

void AES256BlockCipherDec::SetKey(const uint8_t * key)
{
// AES_set_decrypt_key(key, kKeyLengthBits, &mKey);
}

void AES256BlockCipherDec::DecryptBlock(const uint8_t * inBlock, uint8_t * outBlock)
{
// AES_decrypt(inBlock, outBlock, &mKey);
}

} // namespace Security
} // namespace Platform
} // namespace chip
24 changes: 0 additions & 24 deletions src/lib/message/support/crypto/AESBlockCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,7 @@ class AES128BlockCipher
AES128BlockCipher(void);
~AES128BlockCipher(void);

#if CHIP_CRYPTO_OPENSSL
AES_KEY mKey;
#elif CHIP_CONFIG_AES_IMPLEMENTATION_AESNI
__m128i mKey[kRoundCount + 1];
#elif CHIP_CRYPTO_MBEDTLS
mbedtls_aes_context mCtx;
#elif CHIP_CONFIG_AES_USE_EXPANDED_KEY
uint8_t mKey[kBlockLength * (kRoundCount + 1)];
#elif defined(CHIP_AES_128_CTX_PLATFORM)
CHIP_AES_128_CTX_PLATFORM mCtx;
#else
uint8_t mKey[kKeyLength];
#endif
};

class DLL_EXPORT AES128BlockCipherEnc : public AES128BlockCipher
Expand Down Expand Up @@ -125,19 +113,7 @@ class AES256BlockCipher
AES256BlockCipher(void);
~AES256BlockCipher(void);

#if CHIP_CONFIG_AES_IMPLEMENTATION_OPENSSL
AES_KEY mKey;
#elif CHIP_CONFIG_AES_IMPLEMENTATION_AESNI
__m128i mKey[kRoundCount + 1];
#elif CHIP_CRYPTO_MBEDTLS
mbedtls_aes_context mCtx;
#elif CHIP_CONFIG_AES_USE_EXPANDED_KEY
uint8_t mKey[kBlockLength * (kRoundCount + 1)];
#elif defined(CHIP_AES_256_CTX_PLATFORM)
CHIP_AES_256_CTX_PLATFORM mCtx;
#else
uint8_t mKey[kKeyLength];
#endif
};

class DLL_EXPORT AES256BlockCipherEnc : public AES256BlockCipher
Expand Down
57 changes: 57 additions & 0 deletions src/lib/message/support/crypto/CHIPCrypto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements general purpose cryptographic functions for the Message layer.
*
*/

#include <string.h>

#include "CHIPCrypto.h"

namespace chip {
namespace Crypto {

/**
* Compares the first `len` bytes of memory area `buf1` and memory area `buf2`.
*
* The time taken by this function is independent of the data in memory areas `buf1` and `buf2`.
*
* @param[in] buf1 Pointer to a memory block.
*
* @param[in] buf2 Pointer to a memory block.
*
* @param[in] len Size of memory area to compare in bytes.
*
* @retval true if first \c len bytes of memory area \c buf1 and \c buf2 are equal.
* @retval false otherwise.
*
*/
bool ConstantTimeCompare(const uint8_t * buf1, const uint8_t * buf2, uint16_t len)
{
uint8_t c = 0;
for (uint16_t i = 0; i < len; i++)
c |= buf1[i] ^ buf2[i];
return c == 0;
}

} // namespace Crypto
} // namespace chip
48 changes: 48 additions & 0 deletions src/lib/message/support/crypto/CHIPRNG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file implements secure random data initialization and generation functions
* for the Weave layer. The implementation is based on the OpenSSL Library functions.
* This implementation is used when #WEAVE_CONFIG_RNG_IMPLEMENTATION_OPENSSL
* is enabled (1).
*
*/

#include "CHIPRNG.h"

namespace chip {
namespace Platform {
namespace Security {

CHIP_ERROR InitSecureRandomDataSource(chip::Crypto::EntropyFunct entropyFunct, uint16_t entropyLen,
const uint8_t * personalizationData, uint16_t perDataLen)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

CHIP_ERROR GetSecureRandomData(uint8_t * buf, uint16_t len)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

} // namespace Security
} // namespace Platform
} // namespace chip
Loading

0 comments on commit 5754565

Please sign in to comment.