Skip to content

Commit 6189175

Browse files
yufengwangcakedars
authored andcommitted
[linux] Use new chip SHA256 class to re-implement CHIP provisioning HASH (project-chip#1564)
* SHA256 class for CHIP is merged. Replace legacy SHA256 with chip::Crypto::Hash_SHA256_stream
1 parent d306fe8 commit 6189175

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/crypto/CHIPCryptoPAL.h

+7
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ CHIP_ERROR add_entropy_source(entropy_source fn_source, void * p_source, size_t
215215
CHIP_ERROR pbkdf2_sha256(const unsigned char * password, size_t plen, const unsigned char * salt, size_t slen,
216216
unsigned int iteration_count, uint32_t key_length, unsigned char * output);
217217

218+
/** @brief Clears the first `len` bytes of memory area `buf`.
219+
* @param buf Pointer to a memory buffer holding secret data that should be cleared.
220+
* @param len Specifies secret data size in bytes.
221+
* @return void
222+
**/
223+
void ClearSecretData(uint8_t * buf, uint32_t len);
224+
218225
} // namespace Crypto
219226
} // namespace chip
220227

src/crypto/CHIPCryptoPALOpenSSL.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -776,5 +776,10 @@ CHIP_ERROR ECDH_derive_secret(const unsigned char * remote_public_key, const siz
776776
return error;
777777
}
778778

779+
void ClearSecretData(uint8_t * buf, uint32_t len)
780+
{
781+
memset(buf, 0, len);
782+
}
783+
779784
} // namespace Crypto
780785
} // namespace chip

src/crypto/CHIPCryptoPALmbedTLS.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -497,5 +497,10 @@ CHIP_ERROR ECDH_derive_secret(const unsigned char * remote_public_key, const siz
497497
return error;
498498
}
499499

500+
void ClearSecretData(uint8_t * buf, uint32_t len)
501+
{
502+
memset(buf, 0, len);
503+
}
504+
500505
} // namespace Crypto
501506
} // namespace chip

src/include/platform/internal/GenericConfigurationManagerImpl.ipp

+11-7
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@
3131
#include <platform/internal/GenericConfigurationManagerImpl.h>
3232
#include <support/Base64.h>
3333
#include <support/CodeUtils.h>
34+
#include <support/CHIPMem.h>
3435

3536
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
3637
#include <platform/ThreadStackManager.h>
3738
#endif
3839

40+
#if CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH
41+
#include <crypto/CHIPCryptoPAL.h>
42+
#endif
43+
3944
namespace chip {
4045
namespace DeviceLayer {
4146
namespace Internal {
@@ -94,7 +99,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_ConfigureChipStack()
9499

95100
#if CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH
96101
{
97-
uint8_t provHash[Platform::Security::SHA256::kHashLength];
102+
uint8_t provHash[chip::Crypto::kSHA256_Hash_Length];
98103
char provHashBase64[BASE64_ENCODED_LEN(sizeof(provHash)) + 1];
99104
err = Impl()->_ComputeProvisioningHash(provHash, sizeof(provHash));
100105
if (err == CHIP_NO_ERROR)
@@ -821,15 +826,14 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_ComputeProvisioningHash(
821826
CHIP_ERROR err = CHIP_NO_ERROR;
822827

823828
#if CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH
824-
using HashAlgo = Platform::Security::SHA256;
829+
using HashAlgo = chip::Crypto::Hash_SHA256_stream;
825830

826-
CHIP_ERROR err = CHIP_NO_ERROR;
827831
HashAlgo hash;
828832
uint8_t * dataBuf = NULL;
829833
size_t dataBufSize;
830834
constexpr uint16_t kLenFieldLen = 4; // 4 hex characters
831835

832-
VerifyOrExit(hashBufSize >= HashAlgo::kHashLength, err = CHIP_ERROR_BUFFER_TOO_SMALL);
836+
VerifyOrExit(hashBufSize >= chip::Crypto::kSHA256_Hash_Length, err = CHIP_ERROR_BUFFER_TOO_SMALL);
833837

834838
// Compute a hash of the device's provisioning data. The generated hash value confirms to the form
835839
// described in the CHIP Chip: Factory Provisioning Specification.
@@ -902,7 +906,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_ComputeProvisioningHash(
902906
chip::Platform::MemoryFree(dataBuf);
903907

904908
dataBufSize = certsLen;
905-
dataBuf = (uint8_t *) Platform::Security::MemoryAlloc(dataBufSize);
909+
dataBuf = (uint8_t *) chip::Platform::MemoryAlloc(dataBufSize);
906910
VerifyOrExit(dataBuf != NULL, err = CHIP_ERROR_NO_MEMORY);
907911
}
908912

@@ -951,8 +955,8 @@ CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::_ComputeProvisioningHash(
951955
exit:
952956
if (dataBuf != NULL)
953957
{
954-
Crypto::ClearSecretData(dataBuf, dataBufSize);
955-
Platform::Security::MemoryFree(dataBuf);
958+
chip::Crypto::ClearSecretData(dataBuf, dataBufSize);
959+
chip::Platform::MemoryFree(dataBuf);
956960
}
957961
#endif // CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH
958962

src/platform/tests/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ CHIP_LDADD = \
8787
$(top_builddir)/src/platform/libDeviceLayer.a \
8888
$(top_builddir)/src/inet/libInetLayer.a \
8989
$(top_builddir)/src/system/libSystemLayer.a \
90+
$(top_builddir)/src/crypto/libChipCrypto.a \
9091
$(top_builddir)/src/lib/support/libSupportLayer.a \
9192
$(NULL)
9293

0 commit comments

Comments
 (0)