[crypto] allow platform to define its own CSPRNG (#7355)

This commit provides the following changes:

 * Introduce a new `otPlatCryptoRandom*` API
 * Move usage of `mbedtls_entropy_*` and `mbedtls_ctr_drbg_*` out of
   ARM PSA enabled platforms
 * Stop exposing internal mbedtls contextes for entropy and CTR DRBG
   in OpenThread API

Signed-off-by: Lukasz Duda <[email protected]>
This commit is contained in:
Łukasz Duda
2022-01-28 09:06:21 -08:00
committed by GitHub
parent 9f70bafd67
commit 925d9b657c
21 changed files with 134 additions and 311 deletions
-1
View File
@@ -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 \
-1
View File
@@ -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
-1
View File
@@ -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 \
-1
View File
@@ -76,7 +76,6 @@ source_set("openthread") {
"dns.h",
"dns_client.h",
"dnssd_server.h",
"entropy.h",
"error.h",
"heap.h",
"history_tracker.h",
-70
View File
@@ -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 <mbedtls/entropy.h>
#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_
+25 -4
View File
@@ -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);
/**
* @}
*
-7
View File
@@ -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);
/**
* @}
*
-1
View File
@@ -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",
-1
View File
@@ -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
-1
View File
@@ -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 \
-43
View File
@@ -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 <openthread/entropy.h>
#include "common/random_manager.hpp"
using namespace ot;
mbedtls_entropy_context *otEntropyMbedTlsContextGet(void)
{
return RandomManager::GetMbedTlsEntropyContext();
}
-5
View File
@@ -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();
}
-11
View File
@@ -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
+2 -85
View File
@@ -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<uint8_t *>(&seed), sizeof(seed)));
#else
SuccessOrAssert(otPlatEntropyGet(reinterpret_cast<uint8_t *>(&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<uint8_t *>(aOutput), static_cast<uint16_t>(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<unsigned char *>(aBuffer), static_cast<size_t>(aSize)));
}
#endif // #if !OPENTHREAD_RADIO
} // namespace ot
+2 -62
View File
@@ -38,19 +38,11 @@
#include <stdint.h>
#if !OPENTHREAD_RADIO
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#endif
#include <openthread/platform/crypto.h>
#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
+67 -2
View File
@@ -33,11 +33,14 @@
#include "openthread-core-config.h"
#include <mbedtls/aes.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/md.h>
#include <mbedtls/sha256.h>
#include <openthread/instance.h>
#include <openthread/platform/crypto.h>
#include <openthread/platform/entropy.h>
#include <openthread/platform/time.h>
#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<uint8_t *>(aOutput), static_cast<uint16_t>(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<unsigned char *>(aBuffer), static_cast<size_t>(aSize)));
}
#endif // #if !OPENTHREAD_RADIO
#endif // #if OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_MBEDTLS
+6 -10
View File
@@ -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);
+13
View File
@@ -43,8 +43,10 @@
#include <mbedtls/pem.h>
#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<uint16_t>(aSize)));
return 0;
}
#endif // !OPENTHREAD_RADIO
} // namespace Crypto
} // namespace ot
+14
View File
@@ -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
};
/**
+4 -4
View File
@@ -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<const unsigned char *>(mPrivateKeySrc),
static_cast<size_t>(mPrivateKeyLength), nullptr, 0, mbedtls_ctr_drbg_random,
Random::Crypto::MbedTlsContextGet());
static_cast<size_t>(mPrivateKeyLength), nullptr, 0,
Crypto::MbedTls::CryptoSecurePrng, nullptr);
#else
rval = mbedtls_pk_parse_key(&mPrivateKey, static_cast<const unsigned char *>(mPrivateKeySrc),
static_cast<size_t>(mPrivateKeyLength), nullptr, 0);
+1 -1
View File
@@ -181,7 +181,7 @@ KeyManager::KeyManager(Instance &aInstance)
, mKekFrameCounter(0)
, mIsPskcSet(false)
{
IgnoreError(otPlatCryptoInit());
otPlatCryptoInit();
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
{