diff --git a/Android.mk b/Android.mk index 40bc58eb2..c484408ff 100644 --- a/Android.mk +++ b/Android.mk @@ -175,7 +175,6 @@ LOCAL_SRC_FILES := \ src/core/api/diags_api.cpp \ src/core/api/dns_api.cpp \ src/core/api/dns_server_api.cpp \ - src/core/api/entropy_api.cpp \ src/core/api/error_api.cpp \ src/core/api/heap_api.cpp \ src/core/api/history_tracker_api.cpp \ diff --git a/doc/ot_api_doc.h b/doc/ot_api_doc.h index 8843171e6..c826e68e2 100644 --- a/doc/ot_api_doc.h +++ b/doc/ot_api_doc.h @@ -121,7 +121,6 @@ * * @defgroup api-cli Command Line Interface * @defgroup api-crypto Crypto - Thread Stack - * @defgroup api-entropy Entropy Source * @defgroup api-factory-diagnostics Factory Diagnostics - Thread Stack * @defgroup api-heap Heap * @defgroup api-history-tracker History Tracker diff --git a/include/Makefile.am b/include/Makefile.am index 6b4f16635..426e17c2a 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -55,7 +55,6 @@ openthread_headers = \ openthread/dns.h \ openthread/dns_client.h \ openthread/dnssd_server.h \ - openthread/entropy.h \ openthread/error.h \ openthread/heap.h \ openthread/history_tracker.h \ diff --git a/include/openthread/BUILD.gn b/include/openthread/BUILD.gn index 6307afbcc..7a16e83e4 100644 --- a/include/openthread/BUILD.gn +++ b/include/openthread/BUILD.gn @@ -76,7 +76,6 @@ source_set("openthread") { "dns.h", "dns_client.h", "dnssd_server.h", - "entropy.h", "error.h", "heap.h", "history_tracker.h", diff --git a/include/openthread/entropy.h b/include/openthread/entropy.h deleted file mode 100644 index bc07c300b..000000000 --- a/include/openthread/entropy.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2019, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * @brief - * This file defines the OpenThread entropy source API. - */ - -#ifndef OPENTHREAD_ENTROPY_H_ -#define OPENTHREAD_ENTROPY_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @addtogroup api-entropy - * - * @brief - * This module includes functions that manages entropy source. - * - * @{ - * - */ - -/** - * This function returns initialized mbedtls_entropy_context. - * - * @returns A pointer to initialized mbedtls_entropy_context. - */ -mbedtls_entropy_context *otEntropyMbedTlsContextGet(void); - -/** - * @} - * - */ - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // OPENTHREAD_ENTROPY_H_ diff --git a/include/openthread/platform/crypto.h b/include/openthread/platform/crypto.h index efe40ab70..0d9bf5ca3 100644 --- a/include/openthread/platform/crypto.h +++ b/include/openthread/platform/crypto.h @@ -133,11 +133,8 @@ typedef struct otCryptoContext /** * Initialize the Crypto module. * - * @retval OT_ERROR_NONE Successfully initialized Crypto module. - * @retval OT_ERROR_FAILED Failed to initialize Crypto module. - * */ -otError otPlatCryptoInit(void); +void otPlatCryptoInit(void); /** * Import a key into PSA ITS. @@ -469,6 +466,30 @@ otError otPlatCryptoSha256Update(otCryptoContext *aContext, const void *aBuf, ui */ otError otPlatCryptoSha256Finish(otCryptoContext *aContext, uint8_t *aHash, uint16_t aHashSize); +/** + * Initialize cryptographically-secure pseudorandom number generator (CSPRNG). + * + */ +void otPlatCryptoRandomInit(void); + +/** + * Deinitialize cryptographically-secure pseudorandom number generator (CSPRNG). + * + */ +void otPlatCryptoRandomDeinit(void); + +/** + * Fills a given buffer with cryptographically secure random bytes. + * + * @param[out] aBuffer A pointer to a buffer to fill with the random bytes. + * @param[in] aSize Size of buffer (number of bytes to fill). + * + * @retval OT_ERROR_NONE Successfully filled buffer with random values. + * @retval OT_ERROR_FAILED Operation failed. + * + */ +otError otPlatCryptoRandomGet(uint8_t *aBuffer, uint16_t aSize); + /** * @} * diff --git a/include/openthread/random_crypto.h b/include/openthread/random_crypto.h index da4ba7b01..937136a78 100644 --- a/include/openthread/random_crypto.h +++ b/include/openthread/random_crypto.h @@ -63,13 +63,6 @@ extern "C" { */ otError otRandomCryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize); -/** - * This function returns initialized mbedtls_ctr_drbg_context. - * - * @returns A pointer to initialized mbedtls_ctr_drbg_context. - */ -mbedtls_ctr_drbg_context *otRandomCryptoMbedTlsContextGet(void); - /** * @} * diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index d04ec4b9e..f2a3894b0 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -316,7 +316,6 @@ openthread_core_files = [ "api/diags_api.cpp", "api/dns_api.cpp", "api/dns_server_api.cpp", - "api/entropy_api.cpp", "api/error_api.cpp", "api/heap_api.cpp", "api/history_tracker_api.cpp", diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2e1d2d6ba..013f47225 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -48,7 +48,6 @@ set(COMMON_SOURCES api/diags_api.cpp api/dns_api.cpp api/dns_server_api.cpp - api/entropy_api.cpp api/error_api.cpp api/heap_api.cpp api/history_tracker_api.cpp diff --git a/src/core/Makefile.am b/src/core/Makefile.am index e60cc29ed..f40f8510a 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -138,7 +138,6 @@ SOURCES_COMMON = \ api/diags_api.cpp \ api/dns_api.cpp \ api/dns_server_api.cpp \ - api/entropy_api.cpp \ api/error_api.cpp \ api/heap_api.cpp \ api/history_tracker_api.cpp \ diff --git a/src/core/api/entropy_api.cpp b/src/core/api/entropy_api.cpp deleted file mode 100644 index 469e55d31..000000000 --- a/src/core/api/entropy_api.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2019, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * This file implements the OpenThread entropy source management API. - */ - -#include - -#include "common/random_manager.hpp" - -using namespace ot; - -mbedtls_entropy_context *otEntropyMbedTlsContextGet(void) -{ - return RandomManager::GetMbedTlsEntropyContext(); -} diff --git a/src/core/api/random_crypto_api.cpp b/src/core/api/random_crypto_api.cpp index 08797e893..906a4a027 100644 --- a/src/core/api/random_crypto_api.cpp +++ b/src/core/api/random_crypto_api.cpp @@ -43,8 +43,3 @@ otError otRandomCryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return Random::Crypto::FillBuffer(aBuffer, aSize); } - -mbedtls_ctr_drbg_context *otRandomCryptoMbedTlsContextGet(void) -{ - return Random::Crypto::MbedTlsContextGet(); -} diff --git a/src/core/common/random.hpp b/src/core/common/random.hpp index da0028132..a7e269453 100644 --- a/src/core/common/random.hpp +++ b/src/core/common/random.hpp @@ -177,17 +177,6 @@ inline Error FillBuffer(uint8_t *aBuffer, uint16_t aSize) return RandomManager::CryptoFillBuffer(aBuffer, aSize); } -/** - * This function returns initialized mbedtls_ctr_drbg_context. - * - * @returns A pointer to initialized mbedtls_ctr_drbg_context. - * - */ -inline mbedtls_ctr_drbg_context *MbedTlsContextGet(void) -{ - return RandomManager::GetMbedTlsCtrDrbgContext(); -} - } // namespace Crypto #endif // !OPENTHREAD_RADIO diff --git a/src/core/common/random_manager.cpp b/src/core/common/random_manager.cpp index 4caf2269d..3ff7ffee3 100644 --- a/src/core/common/random_manager.cpp +++ b/src/core/common/random_manager.cpp @@ -46,11 +46,6 @@ namespace ot { uint16_t RandomManager::sInitCount = 0; RandomManager::NonCryptoPrng RandomManager::sPrng; -#if !OPENTHREAD_RADIO -RandomManager::Entropy RandomManager::sEntropy; -RandomManager::CryptoCtrDrbg RandomManager::sCtrDrbg; -#endif - RandomManager::RandomManager(void) { uint32_t seed; @@ -60,9 +55,7 @@ RandomManager::RandomManager(void) VerifyOrExit(sInitCount == 0); #if !OPENTHREAD_RADIO - sEntropy.Init(); - sCtrDrbg.Init(); - + otPlatCryptoRandomInit(); SuccessOrAssert(Random::Crypto::FillBuffer(reinterpret_cast(&seed), sizeof(seed))); #else SuccessOrAssert(otPlatEntropyGet(reinterpret_cast(&seed), sizeof(seed))); @@ -82,8 +75,7 @@ RandomManager::~RandomManager(void) VerifyOrExit(sInitCount == 0); #if !OPENTHREAD_RADIO - sCtrDrbg.Deinit(); - sEntropy.Deinit(); + otPlatCryptoRandomDeinit(); #endif exit: @@ -135,79 +127,4 @@ uint32_t RandomManager::NonCryptoPrng::GetNext(void) return mlcg; } -#if !OPENTHREAD_RADIO - -//------------------------------------------------------------------- -// Entropy - -void RandomManager::Entropy::Init(void) -{ - mbedtls_entropy_init(&mEntropyContext); - -#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT - mbedtls_entropy_add_source(&mEntropyContext, &RandomManager::Entropy::HandleMbedtlsEntropyPoll, nullptr, - kEntropyMinThreshold, MBEDTLS_ENTROPY_SOURCE_STRONG); -#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT -} - -void RandomManager::Entropy::Deinit(void) -{ - mbedtls_entropy_free(&mEntropyContext); -} - -#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT - -int RandomManager::Entropy::HandleMbedtlsEntropyPoll(void * aData, - unsigned char *aOutput, - size_t aInLen, - size_t * aOutLen) -{ - int rval = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; - - SuccessOrExit(otPlatEntropyGet(reinterpret_cast(aOutput), static_cast(aInLen))); - rval = 0; - - VerifyOrExit(aOutLen != nullptr); - *aOutLen = aInLen; - -exit: - OT_UNUSED_VARIABLE(aData); - return rval; -} - -#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT - -//------------------------------------------------------------------- -// CryptoCtrDrbg - -void RandomManager::CryptoCtrDrbg::Init(void) -{ - int rval; - - mbedtls_ctr_drbg_init(&mCtrDrbg); - - rval = - mbedtls_ctr_drbg_seed(&mCtrDrbg, mbedtls_entropy_func, RandomManager::GetMbedTlsEntropyContext(), nullptr, 0); - - if (rval != 0) - { - otLogCritMbedTls("Failed to seed the CTR DRBG"); - } - - OT_ASSERT(rval == 0); -} - -void RandomManager::CryptoCtrDrbg::Deinit(void) -{ - mbedtls_ctr_drbg_free(&mCtrDrbg); -} - -Error RandomManager::CryptoCtrDrbg::FillBuffer(uint8_t *aBuffer, uint16_t aSize) -{ - return ot::Crypto::MbedTls::MapError( - mbedtls_ctr_drbg_random(&mCtrDrbg, static_cast(aBuffer), static_cast(aSize))); -} - -#endif // #if !OPENTHREAD_RADIO - } // namespace ot diff --git a/src/core/common/random_manager.hpp b/src/core/common/random_manager.hpp index 42e7767ee..adc4d09ef 100644 --- a/src/core/common/random_manager.hpp +++ b/src/core/common/random_manager.hpp @@ -38,19 +38,11 @@ #include -#if !OPENTHREAD_RADIO -#include -#include -#endif +#include #include "common/error.hpp" #include "common/non_copyable.hpp" -#if (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ - (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || defined(MBEDTLS_HAVEGE_C) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT))) -#define OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT -#endif - namespace ot { /** @@ -81,13 +73,6 @@ public: static uint32_t NonCryptoGetUint32(void); #if !OPENTHREAD_RADIO - /** - * This static method returns the initialized mbedtls_entropy_context. - * - * @returns A pointer to initialized mbedtls_entropy_context. - */ - static mbedtls_entropy_context *GetMbedTlsEntropyContext(void) { return sEntropy.GetContext(); } - /** * This static method fills a given buffer with cryptographically secure random bytes. * @@ -97,15 +82,7 @@ public: * @retval kErrorNone Successfully filled buffer with random values. * */ - static Error CryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return sCtrDrbg.FillBuffer(aBuffer, aSize); } - - /** - * This static method returns the initialized mbedtls_ctr_drbg_context. - * - * @returns A pointer to the initialized mbedtls_ctr_drbg_context. - * - */ - static mbedtls_ctr_drbg_context *GetMbedTlsCtrDrbgContext(void) { return sCtrDrbg.GetContext(); } + static Error CryptoFillBuffer(uint8_t *aBuffer, uint16_t aSize) { return otPlatCryptoRandomGet(aBuffer, aSize); } #endif private: @@ -119,45 +96,8 @@ private: uint32_t mState; }; -#if !OPENTHREAD_RADIO - class Entropy - { - public: - void Init(void); - void Deinit(void); - - mbedtls_entropy_context *GetContext(void) { return &mEntropyContext; } - - private: -#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT - static int HandleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen); -#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT - - static constexpr size_t kEntropyMinThreshold = 32; - - mbedtls_entropy_context mEntropyContext; - }; - - class CryptoCtrDrbg - { - public: - void Init(void); - void Deinit(void); - Error FillBuffer(uint8_t *aBuffer, uint16_t aSize); - - mbedtls_ctr_drbg_context *GetContext(void) { return &mCtrDrbg; } - - private: - mbedtls_ctr_drbg_context mCtrDrbg; - }; -#endif - static uint16_t sInitCount; static NonCryptoPrng sPrng; -#if !OPENTHREAD_RADIO - static Entropy sEntropy; - static CryptoCtrDrbg sCtrDrbg; -#endif }; } // namespace ot diff --git a/src/core/crypto/crypto_platform.cpp b/src/core/crypto/crypto_platform.cpp index d49f1a635..7146151ba 100644 --- a/src/core/crypto/crypto_platform.cpp +++ b/src/core/crypto/crypto_platform.cpp @@ -33,11 +33,14 @@ #include "openthread-core-config.h" #include +#include +#include #include #include #include #include +#include #include #include "common/code_utils.hpp" @@ -51,12 +54,25 @@ using namespace ot; using namespace Crypto; +#if OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_MBEDTLS + //--------------------------------------------------------------------------------------------------------------------- // Default/weak implementation of crypto platform APIs -OT_TOOL_WEAK otError otPlatCryptoInit(void) +#if (!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \ + (!defined(MBEDTLS_NO_PLATFORM_ENTROPY) || defined(MBEDTLS_HAVEGE_C) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT))) +#define OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT +#endif + +#if !OPENTHREAD_RADIO +static mbedtls_ctr_drbg_context sCtrDrbgContext; +static mbedtls_entropy_context sEntropyContext; +static constexpr uint16_t kEntropyMinThreshold = 16; +#endif + +OT_TOOL_WEAK void otPlatCryptoInit(void) { - return kErrorNone; + // Intentionally empty. } // AES Implementation @@ -419,4 +435,53 @@ exit: return error; } +#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + +static int handleMbedtlsEntropyPoll(void *aData, unsigned char *aOutput, size_t aInLen, size_t *aOutLen) +{ + int rval = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + + SuccessOrExit(otPlatEntropyGet(reinterpret_cast(aOutput), static_cast(aInLen))); + rval = 0; + + VerifyOrExit(aOutLen != nullptr); + *aOutLen = aInLen; + +exit: + OT_UNUSED_VARIABLE(aData); + return rval; +} + +#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + +OT_TOOL_WEAK void otPlatCryptoRandomInit(void) +{ + mbedtls_entropy_init(&sEntropyContext); + +#ifndef OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + mbedtls_entropy_add_source(&sEntropyContext, handleMbedtlsEntropyPoll, nullptr, kEntropyMinThreshold, + MBEDTLS_ENTROPY_SOURCE_STRONG); +#endif // OT_MBEDTLS_STRONG_DEFAULT_ENTROPY_PRESENT + + mbedtls_ctr_drbg_init(&sCtrDrbgContext); + + int rval = mbedtls_ctr_drbg_seed(&sCtrDrbgContext, mbedtls_entropy_func, &sEntropyContext, nullptr, 0); + OT_ASSERT(rval == 0); + OT_UNUSED_VARIABLE(rval); +} + +OT_TOOL_WEAK void otPlatCryptoRandomDeinit(void) +{ + mbedtls_entropy_free(&sEntropyContext); + mbedtls_ctr_drbg_free(&sCtrDrbgContext); +} + +OT_TOOL_WEAK otError otPlatCryptoRandomGet(uint8_t *aBuffer, uint16_t aSize) +{ + return ot::Crypto::MbedTls::MapError( + mbedtls_ctr_drbg_random(&sCtrDrbgContext, static_cast(aBuffer), static_cast(aSize))); +} + #endif // #if !OPENTHREAD_RADIO + +#endif // #if OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_MBEDTLS diff --git a/src/core/crypto/ecdsa.cpp b/src/core/crypto/ecdsa.cpp index 373169bc0..f3ea08100 100644 --- a/src/core/crypto/ecdsa.cpp +++ b/src/core/crypto/ecdsa.cpp @@ -61,8 +61,7 @@ Error P256::KeyPair::Generate(void) ret = mbedtls_pk_setup(&pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); VerifyOrExit(ret == 0); - ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(pk), mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()); + ret = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(pk), MbedTls::CryptoSecurePrng, nullptr); VerifyOrExit(ret == 0); ret = mbedtls_pk_write_key_der(&pk, mDerBytes, sizeof(mDerBytes)); @@ -87,8 +86,7 @@ Error P256::KeyPair::Parse(void *aContext) const VerifyOrExit(mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)) == 0, error = kErrorFailed); #if (MBEDTLS_VERSION_NUMBER >= 0x03000000) - VerifyOrExit(mbedtls_pk_parse_key(pk, mDerBytes, mDerLength, nullptr, 0, mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()) == 0, + VerifyOrExit(mbedtls_pk_parse_key(pk, mDerBytes, mDerLength, nullptr, 0, MbedTls::CryptoSecurePrng, nullptr) == 0, error = kErrorParse); #else VerifyOrExit(mbedtls_pk_parse_key(pk, mDerBytes, mDerLength, nullptr, 0) == 0, error = kErrorParse); @@ -144,8 +142,7 @@ Error P256::KeyPair::Sign(const Sha256::Hash &aHash, Signature &aSignature) cons #if (MBEDTLS_VERSION_NUMBER >= 0x02130000) ret = mbedtls_ecdsa_sign_det_ext(&ecdsa.MBEDTLS_PRIVATE(grp), &r, &s, &ecdsa.MBEDTLS_PRIVATE(d), aHash.GetBytes(), - Sha256::Hash::kSize, MBEDTLS_MD_SHA256, mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()); + Sha256::Hash::kSize, MBEDTLS_MD_SHA256, MbedTls::CryptoSecurePrng, nullptr); #else ret = mbedtls_ecdsa_sign_det(&ecdsa.MBEDTLS_PRIVATE(grp), &r, &s, &ecdsa.MBEDTLS_PRIVATE(d), aHash.GetBytes(), Sha256::Hash::kSize, MBEDTLS_MD_SHA256); @@ -230,8 +227,8 @@ Error Sign(uint8_t * aOutput, // Parse a private key in PEM format. #if (MBEDTLS_VERSION_NUMBER >= 0x03000000) - VerifyOrExit(mbedtls_pk_parse_key(&pkCtx, aPrivateKey, aPrivateKeyLength, nullptr, 0, mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()) == 0, + VerifyOrExit(mbedtls_pk_parse_key(&pkCtx, aPrivateKey, aPrivateKeyLength, nullptr, 0, MbedTls::CryptoSecurePrng, + nullptr) == 0, error = kErrorInvalidArgs); #else VerifyOrExit(mbedtls_pk_parse_key(&pkCtx, aPrivateKey, aPrivateKeyLength, nullptr, 0) == 0, @@ -246,8 +243,7 @@ Error Sign(uint8_t * aOutput, // Sign using ECDSA. VerifyOrExit(mbedtls_ecdsa_sign(&ctx.MBEDTLS_PRIVATE(grp), &rMpi, &sMpi, &ctx.MBEDTLS_PRIVATE(d), aInputHash, - aInputHashLength, mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()) == 0, + aInputHashLength, MbedTls::CryptoSecurePrng, nullptr) == 0, error = kErrorFailed); VerifyOrExit(mbedtls_mpi_size(&rMpi) + mbedtls_mpi_size(&sMpi) <= aOutputLength, error = kErrorNoBufs); diff --git a/src/core/crypto/mbedtls.cpp b/src/core/crypto/mbedtls.cpp index 84c535cb2..f8c1c5319 100644 --- a/src/core/crypto/mbedtls.cpp +++ b/src/core/crypto/mbedtls.cpp @@ -43,8 +43,10 @@ #include #endif +#include "common/code_utils.hpp" #include "common/error.hpp" #include "common/heap.hpp" +#include "common/random.hpp" namespace ot { namespace Crypto { @@ -168,5 +170,16 @@ Error MbedTls::MapError(int aMbedTlsError) return error; } +#if !OPENTHREAD_RADIO + +int MbedTls::CryptoSecurePrng(void *, unsigned char *aBuffer, size_t aSize) +{ + IgnoreError(ot::Random::Crypto::FillBuffer(aBuffer, static_cast(aSize))); + + return 0; +} + +#endif // !OPENTHREAD_RADIO + } // namespace Crypto } // namespace ot diff --git a/src/core/crypto/mbedtls.hpp b/src/core/crypto/mbedtls.hpp index 2be5cfca8..a5fd869b6 100644 --- a/src/core/crypto/mbedtls.hpp +++ b/src/core/crypto/mbedtls.hpp @@ -89,6 +89,20 @@ public: * */ static Error MapError(int aMbedTlsError); + +#if !OPENTHREAD_RADIO + /** + * This function fills a given buffer with cryptographically secure random bytes. + * + * @param[in] aContext A pointer to arbitrary context. + * @param[out] aBuffer A pointer to a buffer to fill with the random bytes. + * @param[in] aSize Size of buffer (number of bytes to fill). + * + * @retval kErrorNone Successfully filled buffer with random values. + * + */ + static int CryptoSecurePrng(void *aContext, unsigned char *aBuffer, size_t aSize); +#endif }; /** diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index e90bbfcdc..12a43032c 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -281,7 +281,7 @@ Error Dtls::Setup(bool aClient) OT_UNUSED_VARIABLE(mVerifyPeerCertificate); #endif - mbedtls_ssl_conf_rng(&mConf, mbedtls_ctr_drbg_random, Random::Crypto::MbedTlsContextGet()); + mbedtls_ssl_conf_rng(&mConf, Crypto::MbedTls::CryptoSecurePrng, nullptr); mbedtls_ssl_conf_min_version(&mConf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); mbedtls_ssl_conf_max_version(&mConf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); @@ -307,7 +307,7 @@ Error Dtls::Setup(bool aClient) #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_COOKIE_C) if (!aClient) { - rval = mbedtls_ssl_cookie_setup(&mCookieCtx, mbedtls_ctr_drbg_random, Random::Crypto::MbedTlsContextGet()); + rval = mbedtls_ssl_cookie_setup(&mCookieCtx, Crypto::MbedTls::CryptoSecurePrng, nullptr); VerifyOrExit(rval == 0); mbedtls_ssl_conf_dtls_cookies(&mConf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &mCookieCtx); @@ -386,8 +386,8 @@ int Dtls::SetApplicationCoapSecureKeys(void) #if (MBEDTLS_VERSION_NUMBER >= 0x03000000) rval = mbedtls_pk_parse_key(&mPrivateKey, static_cast(mPrivateKeySrc), - static_cast(mPrivateKeyLength), nullptr, 0, mbedtls_ctr_drbg_random, - Random::Crypto::MbedTlsContextGet()); + static_cast(mPrivateKeyLength), nullptr, 0, + Crypto::MbedTls::CryptoSecurePrng, nullptr); #else rval = mbedtls_pk_parse_key(&mPrivateKey, static_cast(mPrivateKeySrc), static_cast(mPrivateKeyLength), nullptr, 0); diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index 6757fb49e..08c8b3e2b 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -181,7 +181,7 @@ KeyManager::KeyManager(Instance &aInstance) , mKekFrameCounter(0) , mIsPskcSet(false) { - IgnoreError(otPlatCryptoInit()); + otPlatCryptoInit(); #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE {