From 93ad60452486474ed8da25cfb6f2a07dd21583f2 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Tue, 28 Nov 2023 11:22:21 +0100 Subject: [PATCH] [crypto] add error handling to PBDF2 generate key function (#9655) Add error handling to PBKDF2 generate key function. The PBKDF2 functions may fail and the error should be handled by the caller of the function. --- include/openthread/instance.h | 2 +- include/openthread/platform/crypto.h | 18 ++++++++----- src/core/crypto/crypto_platform.cpp | 39 +++++++++++++++++----------- src/core/meshcop/meshcop.cpp | 4 +-- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index e32193881..ebc3fe122 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (381) +#define OPENTHREAD_API_VERSION (382) /** * @addtogroup api-instance diff --git a/include/openthread/platform/crypto.h b/include/openthread/platform/crypto.h index 8762fc2ce..fe3c0ead5 100644 --- a/include/openthread/platform/crypto.h +++ b/include/openthread/platform/crypto.h @@ -743,14 +743,18 @@ otError otPlatCryptoEcdsaVerifyUsingKeyRef(otCryptoKeyRef aKe * @param[in] aKeyLen Length of generated key in bytes. * @param[out] aKey A pointer to the generated key. * + * @retval OT_ERROR_NONE A new key-pair was generated successfully. + * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for key generation. + * @retval OT_ERROR_NOT_CAPABLE Feature not supported. + * @retval OT_ERROR_FAILED Failed to generate key. */ -void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, - uint16_t aPasswordLen, - const uint8_t *aSalt, - uint16_t aSaltLen, - uint32_t aIterationCounter, - uint16_t aKeyLen, - uint8_t *aKey); +otError otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, + uint16_t aPasswordLen, + const uint8_t *aSalt, + uint16_t aSaltLen, + uint32_t aIterationCounter, + uint16_t aKeyLen, + uint8_t *aKey); /** * @} diff --git a/src/core/crypto/crypto_platform.cpp b/src/core/crypto/crypto_platform.cpp index 6c7e54cee..0e8126e5b 100644 --- a/src/core/crypto/crypto_platform.cpp +++ b/src/core/crypto/crypto_platform.cpp @@ -716,13 +716,13 @@ OT_TOOL_WEAK otError otPlatCryptoEcdsaVerify(const otPlatCryptoEcdsaPublicKey *a #if OPENTHREAD_FTD -OT_TOOL_WEAK void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, - uint16_t aPasswordLen, - const uint8_t *aSalt, - uint16_t aSaltLen, - uint32_t aIterationCounter, - uint16_t aKeyLen, - uint8_t *aKey) +OT_TOOL_WEAK otError otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, + uint16_t aPasswordLen, + const uint8_t *aSalt, + uint16_t aSaltLen, + uint32_t aIterationCounter, + uint16_t aKeyLen, + uint8_t *aKey) { #if (MBEDTLS_VERSION_NUMBER >= 0x03050000) const size_t kBlockSize = MBEDTLS_CMAC_MAX_BLOCK_SIZE; @@ -737,6 +737,8 @@ OT_TOOL_WEAK void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, uint8_t *key = aKey; uint16_t keyLen = aKeyLen; uint16_t useLen = 0; + Error error = kErrorNone; + int ret; OT_ASSERT(aSaltLen <= sizeof(prfInput)); memcpy(prfInput, aSalt, aSaltLen); @@ -757,12 +759,14 @@ OT_TOOL_WEAK void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, prfInput[aSaltLen + 3] = static_cast(blockCounter); // Calculate U_1 - mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, prfInput, aSaltLen + 4, - reinterpret_cast(keyBlock)); + ret = mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, prfInput, aSaltLen + 4, + reinterpret_cast(keyBlock)); + VerifyOrExit(ret == 0, error = MbedTls::MapError(ret)); // Calculate U_2 - mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(keyBlock), kBlockSize, - reinterpret_cast(prfOne)); + ret = mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(keyBlock), kBlockSize, + reinterpret_cast(prfOne)); + VerifyOrExit(ret == 0, error = MbedTls::MapError(ret)); for (uint32_t j = 0; j < kBlockSize / sizeof(long); ++j) { @@ -772,11 +776,13 @@ OT_TOOL_WEAK void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, for (uint32_t i = 1; i < aIterationCounter; ++i) { // Calculate U_{2 * i - 1} - mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(prfOne), kBlockSize, - reinterpret_cast(prfTwo)); + ret = mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(prfOne), + kBlockSize, reinterpret_cast(prfTwo)); + VerifyOrExit(ret == 0, error = MbedTls::MapError(ret)); // Calculate U_{2 * i} - mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(prfTwo), kBlockSize, - reinterpret_cast(prfOne)); + ret = mbedtls_aes_cmac_prf_128(aPassword, aPasswordLen, reinterpret_cast(prfTwo), + kBlockSize, reinterpret_cast(prfOne)); + VerifyOrExit(ret == 0, error = MbedTls::MapError(ret)); for (uint32_t j = 0; j < kBlockSize / sizeof(long); ++j) { @@ -789,6 +795,9 @@ OT_TOOL_WEAK void otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, key += useLen; keyLen -= useLen; } + +exit: + return error; } #endif // #if OPENTHREAD_FTD diff --git a/src/core/meshcop/meshcop.cpp b/src/core/meshcop/meshcop.cpp index 35ad539b5..d2d14c321 100644 --- a/src/core/meshcop/meshcop.cpp +++ b/src/core/meshcop/meshcop.cpp @@ -334,8 +334,8 @@ Error GeneratePskc(const char *aPassPhrase, memcpy(salt + saltLen, aNetworkName.GetAsCString(), networkNameLen); saltLen += networkNameLen; - otPlatCryptoPbkdf2GenerateKey(reinterpret_cast(aPassPhrase), passphraseLen, salt, saltLen, 16384, - OT_PSKC_MAX_SIZE, aPskc.m8); + error = otPlatCryptoPbkdf2GenerateKey(reinterpret_cast(aPassPhrase), passphraseLen, salt, saltLen, + 16384, OT_PSKC_MAX_SIZE, aPskc.m8); exit: return error;