diff --git a/examples/drivers/windows/otApi/otApi.cpp b/examples/drivers/windows/otApi/otApi.cpp index 7af9dbd04..e952d58af 100644 --- a/examples/drivers/windows/otApi/otApi.cpp +++ b/examples/drivers/windows/otApi/otApi.cpp @@ -1619,7 +1619,7 @@ otThreadSetMasterKey( } OTAPI -const uint8_t * +const otPSKc * OTCALL otThreadGetPSKc( _In_ otInstance *aInstance @@ -1627,7 +1627,7 @@ otThreadGetPSKc( { if (aInstance == nullptr) return nullptr; - uint8_t *Result = (uint8_t*)malloc(sizeof(otPSKc)); + otPSKc *Result = (otPSKc*)malloc(sizeof(otPSKc)); if (Result == nullptr) return nullptr; if (QueryIOCTL(aInstance, IOCTL_OTLWF_OT_PSKC, Result) != ERROR_SUCCESS) { @@ -1642,16 +1642,12 @@ otError OTCALL otThreadSetPSKc( _In_ otInstance *aInstance, - const uint8_t *aPSKc + const otPSKc *aPSKc ) { if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - BYTE Buffer[sizeof(GUID) + sizeof(otPSKc)]; - memcpy_s(Buffer, sizeof(Buffer), &aInstance->InterfaceGuid, sizeof(GUID)); - memcpy_s(Buffer + sizeof(GUID), sizeof(Buffer) - sizeof(GUID), aPSKc, sizeof(otPSKc)); - - return DwordToThreadError(SendIOCTL(aInstance->ApiHandle, IOCTL_OTLWF_OT_PSKC, Buffer, sizeof(Buffer), nullptr, 0)); + return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_PSKC, aPSKc)); } OTAPI diff --git a/examples/drivers/windows/otLwf/iocontrol.c b/examples/drivers/windows/otLwf/iocontrol.c index dca54f655..388c3c57f 100644 --- a/examples/drivers/windows/otLwf/iocontrol.c +++ b/examples/drivers/windows/otLwf/iocontrol.c @@ -1746,12 +1746,12 @@ otLwfIoCtl_otPSKc( if (InBufferLength >= sizeof(otPSKc)) { - status = ThreadErrorToNtstatus(otThreadSetPSKc(pFilter->otCtx, InBuffer)); + status = ThreadErrorToNtstatus(otThreadSetPSKc(pFilter->otCtx, (otPSKc*)InBuffer)); *OutBufferLength = 0; } else if (*OutBufferLength >= sizeof(otPSKc)) { - const uint8_t* aPSKc = otThreadGetPSKc(pFilter->otCtx); + const otPSKc* aPSKc = otThreadGetPSKc(pFilter->otCtx); memcpy(OutBuffer, aPSKc, sizeof(otPSKc)); *OutBufferLength = sizeof(otPSKc); status = STATUS_SUCCESS; diff --git a/examples/drivers/windows/otNodeApi/otNodeApi.cpp b/examples/drivers/windows/otNodeApi/otNodeApi.cpp index 84d1b1f27..97d61c4e0 100644 --- a/examples/drivers/windows/otNodeApi/otNodeApi.cpp +++ b/examples/drivers/windows/otNodeApi/otNodeApi.cpp @@ -1256,14 +1256,15 @@ OTNODEAPI int32_t OTCALL otNodeSetPSKc(otNode* aNode, const char *aPSKc) otLogFuncEntryMsg("[%d] %s", aNode->mId, aPSKc); printf("%d: pskc %s\r\n", aNode->mId, aPSKc); - uint8_t pskc[OT_PSKC_MAX_SIZE]; - if (Hex2Bin(aPSKc, pskc, sizeof(pskc)) != OT_PSKC_MAX_SIZE) + int pskcLength; + otPSKc pskc; + if ((pskcLength = Hex2Bin(aPSKc, pskc.m8, sizeof(aPSKc))) != OT_PSKC_MAX_SIZE) { - printf("invalid pskc %s\r\n", aPSKc); + printf("invalid length pskd %d\r\n", pskcLength); return OT_ERROR_PARSE; } - auto error = otThreadSetPSKc(aNode->mInstance, pskc); + auto error = otThreadSetPSKc(aNode->mInstance, &pskc); otLogFuncExit(); return error; } @@ -1278,7 +1279,7 @@ OTNODEAPI const char* OTCALL otNodeGetPSKc(otNode* aNode) { aNode->mMemoryToFree.push_back(str); for (int i = 0; i < OT_PSKC_MAX_SIZE; i++) - sprintf_s(str + i * 2, strLength - (2 * i), "%02x", aPSKc[i]); + sprintf_s(str + i * 2, strLength - (2 * i), "%02x", aPSKc->m8[i]); printf("%d: pskc\r\n%s\r\n", aNode->mId, str); } otFreeMemory(aPSKc); diff --git a/include/openthread/dataset.h b/include/openthread/dataset.h index ea804b9c2..f857d5ff8 100644 --- a/include/openthread/dataset.h +++ b/include/openthread/dataset.h @@ -112,7 +112,7 @@ struct otMeshLocalPrefix } OT_TOOL_PACKED_END; /** - * This structure represents a Mesh Local Prefix + * This structure represents a Mesh Local Prefix. * */ typedef struct otMeshLocalPrefix otMeshLocalPrefix; @@ -123,10 +123,17 @@ typedef struct otMeshLocalPrefix otMeshLocalPrefix; * This structure represents PSKc. * */ -typedef struct otPSKc +OT_TOOL_PACKED_BEGIN +struct otPSKc { uint8_t m8[OT_PSKC_MAX_SIZE]; ///< Byte values -} otPSKc; +} OT_TOOL_PACKED_END; + +/** + * This structure represents a PSKc. + * + */ +typedef struct otPSKc otPSKc; /** * This structure represent Security Policy. diff --git a/include/openthread/thread_ftd.h b/include/openthread/thread_ftd.h index 4dae3b1d2..6a25ebbb6 100644 --- a/include/openthread/thread_ftd.h +++ b/include/openthread/thread_ftd.h @@ -524,7 +524,7 @@ OTAPI otError OTCALL otThreadGetEidCacheEntry(otInstance *aInstance, uint8_t aIn * @sa otThreadSetPSKc * */ -OTAPI const uint8_t *OTCALL otThreadGetPSKc(otInstance *aInstance); +OTAPI const otPSKc *OTCALL otThreadGetPSKc(otInstance *aInstance); /** * Set the thrPSKc. @@ -542,7 +542,7 @@ OTAPI const uint8_t *OTCALL otThreadGetPSKc(otInstance *aInstance); * @sa otThreadGetPSKc * */ -OTAPI otError OTCALL otThreadSetPSKc(otInstance *aInstance, const uint8_t *aPSKc); +OTAPI otError OTCALL otThreadSetPSKc(otInstance *aInstance, const otPSKc *aPSKc); /** * Get the assigned parent priority. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 4eebdce42..87f99bf95 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -1515,21 +1515,21 @@ void Interpreter::ProcessPSKc(int argc, char *argv[]) if (argc == 0) { - const uint8_t *currentPSKc = otThreadGetPSKc(mInstance); + const otPSKc *pskc = otThreadGetPSKc(mInstance); for (int i = 0; i < OT_PSKC_MAX_SIZE; i++) { - mServer->OutputFormat("%02x", currentPSKc[i]); + mServer->OutputFormat("%02x", pskc->m8[i]); } mServer->OutputFormat("\r\n"); } else { - uint8_t newPSKc[OT_PSKC_MAX_SIZE]; + otPSKc pskc; - VerifyOrExit(Hex2Bin(argv[0], newPSKc, sizeof(newPSKc)) == OT_PSKC_MAX_SIZE, error = OT_ERROR_PARSE); - SuccessOrExit(error = otThreadSetPSKc(mInstance, newPSKc)); + VerifyOrExit(Hex2Bin(argv[0], pskc.m8, sizeof(pskc)) == OT_PSKC_MAX_SIZE, error = OT_ERROR_PARSE); + SuccessOrExit(error = otThreadSetPSKc(mInstance, &pskc)); } exit: diff --git a/src/core/api/thread_ftd_api.cpp b/src/core/api/thread_ftd_api.cpp index 7ba22e1a9..76f9943a4 100644 --- a/src/core/api/thread_ftd_api.cpp +++ b/src/core/api/thread_ftd_api.cpp @@ -344,21 +344,21 @@ otError otThreadSetSteeringData(otInstance *aInstance, const otExtAddress *aExtA return error; } -const uint8_t *otThreadGetPSKc(otInstance *aInstance) +const otPSKc *otThreadGetPSKc(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); - return instance.Get().GetPSKc(); + return &instance.Get().GetPSKc(); } -otError otThreadSetPSKc(otInstance *aInstance, const uint8_t *aPSKc) +otError otThreadSetPSKc(otInstance *aInstance, const otPSKc *aPSKc) { otError error = OT_ERROR_NONE; Instance &instance = *static_cast(aInstance); VerifyOrExit(instance.Get().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE); - instance.Get().SetPSKc(aPSKc); + instance.Get().SetPSKc(*aPSKc); instance.Get().Clear(); instance.Get().Clear(); diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 1927eb7f2..a4850a913 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -651,7 +651,7 @@ otError BorderAgent::Start(void) VerifyOrExit(mState == OT_BORDER_AGENT_STATE_STOPPED, error = OT_ERROR_ALREADY); SuccessOrExit(error = coaps.Start(kBorderAgentUdpPort)); - SuccessOrExit(error = coaps.SetPsk(Get().GetPSKc(), OT_PSKC_MAX_SIZE)); + SuccessOrExit(error = coaps.SetPsk(Get().GetPSKc().m8, OT_PSKC_MAX_SIZE)); coaps.SetConnectedCallback(HandleConnected, this); coaps.AddResource(mActiveGet); diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 09e3a1583..ac073476e 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -216,8 +216,8 @@ void Dataset::Get(otOperationalDataset &aDataset) const case Tlv::kPSKc: { - const PSKcTlv *tlv = static_cast(cur); - memcpy(aDataset.mPSKc.m8, tlv->GetPSKc(), tlv->GetLength()); + const PSKcTlv *tlv = static_cast(cur); + aDataset.mPSKc = tlv->GetPSKc(); aDataset.mComponents.mIsPSKcPresent = true; break; } @@ -347,7 +347,7 @@ otError Dataset::Set(const otOperationalDataset &aDataset) { MeshCoP::PSKcTlv tlv; tlv.Init(); - tlv.SetPSKc(aDataset.mPSKc.m8); + tlv.SetPSKc(aDataset.mPSKc); Set(tlv); } diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index e0664cdfe..63ac18b9e 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -460,7 +460,7 @@ public: * @returns The PSKc value. * */ - const uint8_t *GetPSKc(void) const { return mPSKc; } + const otPSKc &GetPSKc(void) const { return mPSKc; } /** * This method sets the PSKc value. @@ -468,10 +468,10 @@ public: * @param[in] aPSKc A pointer to the PSKc value. * */ - void SetPSKc(const uint8_t *aPSKc) { memcpy(mPSKc, aPSKc, sizeof(mPSKc)); } + void SetPSKc(const otPSKc &aPSKc) { mPSKc = aPSKc; } private: - uint8_t mPSKc[16]; + otPSKc mPSKc; } OT_TOOL_PACKED_END; /** diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index 76703d7da..681c20996 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -97,15 +97,10 @@ void KeyManager::Stop(void) } #if OPENTHREAD_MTD || OPENTHREAD_FTD -const uint8_t *KeyManager::GetPSKc(void) const +void KeyManager::SetPSKc(const otPSKc &aPSKc) { - return mPSKc; -} - -void KeyManager::SetPSKc(const uint8_t *aPSKc) -{ - VerifyOrExit(memcmp(mPSKc, aPSKc, sizeof(mPSKc)) != 0, Get().SignalIfFirst(OT_CHANGED_PSKC)); - memcpy(mPSKc, aPSKc, sizeof(mPSKc)); + VerifyOrExit(memcmp(&mPSKc, &aPSKc, sizeof(mPSKc)) != 0, Get().SignalIfFirst(OT_CHANGED_PSKC)); + mPSKc = aPSKc; Get().Signal(OT_CHANGED_PSKC); exit: diff --git a/src/core/thread/key_manager.hpp b/src/core/thread/key_manager.hpp index 27107615e..7452ba9b8 100644 --- a/src/core/thread/key_manager.hpp +++ b/src/core/thread/key_manager.hpp @@ -102,21 +102,23 @@ public: */ otError SetMasterKey(const otMasterKey &aKey); +#if OPENTHREAD_FTD || OPENTHREAD_MTD /** * This method returns a pointer to the PSKc. * - * @returns A pointer to the PSKc. + * @returns A reference to the PSKc. * */ - const uint8_t *GetPSKc(void) const; + const otPSKc &GetPSKc(void) const { return mPSKc; } /** * This method sets the PSKc. * - * @param[in] aPSKc A pointer to the PSKc. + * @param[in] aPSKc A reference to the PSKc. * */ - void SetPSKc(const uint8_t *aPSKc); + void SetPSKc(const otPSKc &aPSKc); +#endif /** * This method returns the current key sequence value. @@ -360,7 +362,7 @@ private: TimerMilli mKeyRotationTimer; #if OPENTHREAD_MTD || OPENTHREAD_FTD - uint8_t mPSKc[kMaxKeyLength]; + otPSKc mPSKc; #endif uint8_t mKek[kMaxKeyLength]; uint32_t mKekFrameCounter; diff --git a/src/ncp/ncp_base_ftd.cpp b/src/ncp/ncp_base_ftd.cpp index 2029d70d3..88a11631b 100644 --- a/src/ncp/ncp_base_ftd.cpp +++ b/src/ncp/ncp_base_ftd.cpp @@ -294,7 +294,7 @@ exit: template <> otError NcpBase::HandlePropertyGet(void) { - return mEncoder.WriteData(otThreadGetPSKc(mInstance), sizeof(spinel_net_pskc_t)); + return mEncoder.WriteData(otThreadGetPSKc(mInstance)->m8, sizeof(spinel_net_pskc_t)); } template <> otError NcpBase::HandlePropertySet(void) @@ -307,7 +307,7 @@ template <> otError NcpBase::HandlePropertySet(void) VerifyOrExit(len == sizeof(spinel_net_pskc_t), error = OT_ERROR_PARSE); - error = otThreadSetPSKc(mInstance, ptr); + error = otThreadSetPSKc(mInstance, reinterpret_cast(ptr)); exit: return error;