From 6bfe59dabf1cf4c2b02d950418a218a6a030d6c2 Mon Sep 17 00:00:00 2001 From: hemanth-silabs <50145824+hemanth-silabs@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:39:25 -0400 Subject: [PATCH] [spinel] use local variables to export mac key before sending over SPINEL (#9536) In RadioSpinel::SetMacKey we try to reuse the otMacKey passed to export the literal key from PSA to be sent over SPINEL to RCP. But as the passed param is const, we cant really use this. Better option is to extract it into a local buffer and use that to pass the keys to RCP. --- src/lib/spinel/radio_spinel.cpp | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/lib/spinel/radio_spinel.cpp b/src/lib/spinel/radio_spinel.cpp index 9328184d9..a13759fe9 100644 --- a/src/lib/spinel/radio_spinel.cpp +++ b/src/lib/spinel/radio_spinel.cpp @@ -884,35 +884,43 @@ otError RadioSpinel::SetMacKey(uint8_t aKeyIdMode, const otMacKeyMaterial *aCurrKey, const otMacKeyMaterial *aNextKey) { - otError error; - size_t aKeySize; + otError error; + size_t aKeySize; + otMacKey prevKey; + otMacKey currKey; + otMacKey nextKey; VerifyOrExit((aPrevKey != nullptr) && (aCurrKey != nullptr) && (aNextKey != nullptr), error = kErrorInvalidArgs); #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE - SuccessOrExit(error = otPlatCryptoExportKey(aPrevKey->mKeyMaterial.mKeyRef, aPrevKey->mKeyMaterial.mKey.m8, - sizeof(aPrevKey->mKeyMaterial.mKey.m8), &aKeySize)); - SuccessOrExit(error = otPlatCryptoExportKey(aCurrKey->mKeyMaterial.mKeyRef, aCurrKey->mKeyMaterial.mKey.m8, - sizeof(aCurrKey->mKeyMaterial.mKey.m8), &aKeySize)); - SuccessOrExit(error = otPlatCryptoExportKey(aNextKey->mKeyMaterial.mKeyRef, aNextKey->mKeyMaterial.mKey.m8, - sizeof(aNextKey->mKeyMaterial.mKey.m8), &aKeySize)); + SuccessOrExit(error = + otPlatCryptoExportKey(aPrevKey->mKeyMaterial.mKeyRef, prevKey.m8, OT_MAC_KEY_SIZE, &aKeySize)); + SuccessOrExit(error = + otPlatCryptoExportKey(aCurrKey->mKeyMaterial.mKeyRef, currKey.m8, OT_MAC_KEY_SIZE, &aKeySize)); + SuccessOrExit(error = + otPlatCryptoExportKey(aNextKey->mKeyMaterial.mKeyRef, nextKey.m8, OT_MAC_KEY_SIZE, &aKeySize)); #else OT_UNUSED_VARIABLE(aKeySize); + + prevKey = aPrevKey->mKeyMaterial.mKey; + currKey = aCurrKey->mKeyMaterial.mKey; + nextKey = aNextKey->mKeyMaterial.mKey; #endif SuccessOrExit(error = Set(SPINEL_PROP_RCP_MAC_KEY, SPINEL_DATATYPE_UINT8_S SPINEL_DATATYPE_UINT8_S SPINEL_DATATYPE_DATA_WLEN_S SPINEL_DATATYPE_DATA_WLEN_S SPINEL_DATATYPE_DATA_WLEN_S, - aKeyIdMode, aKeyId, aPrevKey->mKeyMaterial.mKey.m8, sizeof(otMacKey), - aCurrKey->mKeyMaterial.mKey.m8, sizeof(otMacKey), aNextKey->mKeyMaterial.mKey.m8, - sizeof(otMacKey))); + aKeyIdMode, aKeyId, prevKey.m8, OT_MAC_KEY_SIZE, currKey.m8, OT_MAC_KEY_SIZE, nextKey.m8, + OT_MAC_KEY_SIZE)); #if OPENTHREAD_SPINEL_CONFIG_RCP_RESTORATION_MAX_COUNT > 0 mKeyIdMode = aKeyIdMode; mKeyId = aKeyId; - memcpy(mPrevKey.m8, aPrevKey->mKeyMaterial.mKey.m8, OT_MAC_KEY_SIZE); - memcpy(mCurrKey.m8, aCurrKey->mKeyMaterial.mKey.m8, OT_MAC_KEY_SIZE); - memcpy(mNextKey.m8, aNextKey->mKeyMaterial.mKey.m8, OT_MAC_KEY_SIZE); + + mPrevKey = prevKey; + mCurrKey = currKey; + mNextKey = nextKey; + mMacKeySet = true; #endif