diff --git a/etc/visual-studio/mbedtls.vcxproj b/etc/visual-studio/mbedtls.vcxproj index 997d506ca..ac080dba6 100644 --- a/etc/visual-studio/mbedtls.vcxproj +++ b/etc/visual-studio/mbedtls.vcxproj @@ -54,7 +54,6 @@ - diff --git a/etc/visual-studio/mbedtls.vcxproj.filters b/etc/visual-studio/mbedtls.vcxproj.filters index ea9a869d6..a944bd019 100644 --- a/etc/visual-studio/mbedtls.vcxproj.filters +++ b/etc/visual-studio/mbedtls.vcxproj.filters @@ -13,9 +13,6 @@ - - Source Files - Source Files\repo.patched\library diff --git a/etc/visual-studio/mbedtls_k.vcxproj b/etc/visual-studio/mbedtls_k.vcxproj index c4d2ad39a..e488e88c5 100644 --- a/etc/visual-studio/mbedtls_k.vcxproj +++ b/etc/visual-studio/mbedtls_k.vcxproj @@ -57,7 +57,6 @@ - diff --git a/etc/visual-studio/mbedtls_k.vcxproj.filters b/etc/visual-studio/mbedtls_k.vcxproj.filters index 9468d50ea..8d80caeb2 100644 --- a/etc/visual-studio/mbedtls_k.vcxproj.filters +++ b/etc/visual-studio/mbedtls_k.vcxproj.filters @@ -13,9 +13,6 @@ - - Source Files - Source Files\repo.patched\library diff --git a/examples/platforms/cc2650/crypto/cc2650-mbedtls-config.h b/examples/platforms/cc2650/crypto/cc2650-mbedtls-config.h index 510f3268f..5574d4686 100644 --- a/examples/platforms/cc2650/crypto/cc2650-mbedtls-config.h +++ b/examples/platforms/cc2650/crypto/cc2650-mbedtls-config.h @@ -295,7 +295,7 @@ * * Uncomment to use your own hardware entropy collector. */ -#define MBEDTLS_ENTROPY_HARDWARE_ALT +//#define MBEDTLS_ENTROPY_HARDWARE_ALT /** * \def MBEDTLS_AES_ROM_TABLES @@ -776,7 +776,7 @@ * * Uncomment this macro to prevent loading of default entropy functions. */ -//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES +#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES /** * \def MBEDTLS_NO_PLATFORM_ENTROPY @@ -2461,7 +2461,7 @@ #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */ /* Entropy options */ -//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ +#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */ //#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ /* Memory buffer allocator options */ diff --git a/examples/platforms/cc2652/crypto/cc2652-mbedtls-config.h b/examples/platforms/cc2652/crypto/cc2652-mbedtls-config.h index 6317ea1cc..ced68f81c 100644 --- a/examples/platforms/cc2652/crypto/cc2652-mbedtls-config.h +++ b/examples/platforms/cc2652/crypto/cc2652-mbedtls-config.h @@ -295,7 +295,7 @@ * * Uncomment to use your own hardware entropy collector. */ -#define MBEDTLS_ENTROPY_HARDWARE_ALT +//#define MBEDTLS_ENTROPY_HARDWARE_ALT /** * \def MBEDTLS_AES_ROM_TABLES @@ -776,7 +776,7 @@ * * Uncomment this macro to prevent loading of default entropy functions. */ -//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES +#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES /** * \def MBEDTLS_NO_PLATFORM_ENTROPY @@ -2461,7 +2461,7 @@ #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */ /* Entropy options */ -//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ +#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */ //#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ /* Memory buffer allocator options */ diff --git a/examples/platforms/da15000/crypto/da15000-mbedtls-config.h b/examples/platforms/da15000/crypto/da15000-mbedtls-config.h index cc9cb2e7d..b5691f181 100644 --- a/examples/platforms/da15000/crypto/da15000-mbedtls-config.h +++ b/examples/platforms/da15000/crypto/da15000-mbedtls-config.h @@ -342,7 +342,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. * * Uncomment to use your own hardware entropy collector. */ -#define MBEDTLS_ENTROPY_HARDWARE_ALT +//#define MBEDTLS_ENTROPY_HARDWARE_ALT /** * \def MBEDTLS_AES_ROM_TABLES @@ -823,7 +823,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. * * Uncomment this macro to prevent loading of default entropy functions. */ -//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES +#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES /** * \def MBEDTLS_NO_PLATFORM_ENTROPY @@ -2539,7 +2539,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */ /* Entropy options */ -//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ +#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */ //#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ //#define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Default minimum number of bytes required for the hardware entropy source mbedtls_hardware_poll() before entropy is released */ diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index d217f8ec5..cbd2140f4 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -80,6 +80,31 @@ Dtls::Dtls(Instance &aInstance): mProvisioningUrl.Init(); } +int Dtls::HandleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen) +{ + otError error; + int rval = 0; + + OT_UNUSED_VARIABLE(aData); + + error = otPlatRandomGetTrue((uint8_t *)aOutput, (uint16_t)aInLen); + SuccessOrExit(error); + + if (aOutLen != NULL) + { + *aOutLen = aInLen; + } + +exit: + + if (error != OT_ERROR_NONE) + { + rval = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + } + + return rval; +} + otError Dtls::Start(bool aClient, ConnectedHandler aConnectedHandler, ReceiveHandler aReceiveHandler, SendHandler aSendHandler, void *aContext) { @@ -99,6 +124,9 @@ otError Dtls::Start(bool aClient, ConnectedHandler aConnectedHandler, ReceiveHan mbedtls_ssl_config_init(&mConf); mbedtls_ctr_drbg_init(&mCtrDrbg); mbedtls_entropy_init(&mEntropy); + rval = mbedtls_entropy_add_source(&mEntropy, &Dtls::HandleMbedtlsEntropyPoll, NULL, + MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_SOURCE_STRONG); + VerifyOrExit(rval == 0); // mbedTLS's debug level is almost the same as OpenThread's mbedtls_debug_set_threshold(OPENTHREAD_CONFIG_LOG_LEVEL); diff --git a/src/core/meshcop/dtls.hpp b/src/core/meshcop/dtls.hpp index 4f12eb675..0afb7fed5 100644 --- a/src/core/meshcop/dtls.hpp +++ b/src/core/meshcop/dtls.hpp @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -231,6 +232,8 @@ private: static void HandleTimer(Timer &aTimer); void HandleTimer(void); + static int HandleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen); + void Close(void); void Process(void); diff --git a/third_party/mbedtls/Makefile.am b/third_party/mbedtls/Makefile.am index 8adfc7a69..71f85017b 100644 --- a/third_party/mbedtls/Makefile.am +++ b/third_party/mbedtls/Makefile.am @@ -41,7 +41,6 @@ libmbedcrypto_a_CPPFLAGS = \ $(NULL) libmbedcrypto_a_SOURCES = \ - hardware_entropy.c \ repo.patched/library/md.c \ repo.patched/library/md_wrap.c \ repo.patched/library/memory_buffer_alloc.c \ diff --git a/third_party/mbedtls/hardware_entropy.c b/third_party/mbedtls/hardware_entropy.c deleted file mode 100644 index d43f2cf15..000000000 --- a/third_party/mbedtls/hardware_entropy.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2016 The OpenThread Authors. 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 to use hardware entropy source. - * uncomment MBEDTLS_ENTROPY_HARDWARE_ALT in mbedtls/mbedtls_config.h to use this. - */ - -#include - -#include "openthread/types.h" -#include "openthread/platform/random.h" - -#include "mbedtls/entropy.h" - -int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) -{ - otError error; - - (void)data; - - error = otPlatRandomGetTrue((uint8_t *)output, (uint16_t)len); - - if (error != OT_ERROR_NONE) - { - return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; - } - - if (olen != NULL) - { - *olen = len; - } - - return 0; -} diff --git a/third_party/mbedtls/mbedtls-config.h b/third_party/mbedtls/mbedtls-config.h index 0b9693bd5..a65b15331 100644 --- a/third_party/mbedtls/mbedtls-config.h +++ b/third_party/mbedtls/mbedtls-config.h @@ -341,7 +341,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. * * Uncomment to use your own hardware entropy collector. */ -#define MBEDTLS_ENTROPY_HARDWARE_ALT +//#define MBEDTLS_ENTROPY_HARDWARE_ALT /** * \def MBEDTLS_AES_ROM_TABLES @@ -822,7 +822,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. * * Uncomment this macro to prevent loading of default entropy functions. */ -//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES +#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES /** * \def MBEDTLS_NO_PLATFORM_ENTROPY @@ -2538,7 +2538,7 @@ __inline int windows_kernel_snprintf(char * s, size_t n, const char * format, .. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */ /* Entropy options */ -//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ +#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */ //#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ //#define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Default minimum number of bytes required for the hardware entropy source mbedtls_hardware_poll() before entropy is released */