From 090ed57141471e171ac8803946ccafc4b732e124 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 30 Oct 2019 17:03:10 -0700 Subject: [PATCH] [commissioner] change GeneratePskc to use otPskc/Pskc types (#4294) --- include/openthread/commissioner.h | 14 ++++++-------- src/core/api/commissioner_api.cpp | 11 ++++------- src/core/meshcop/commissioner.cpp | 4 ++-- src/core/meshcop/commissioner.hpp | 5 +++-- tests/unit/test_pskc.cpp | 8 ++++---- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/include/openthread/commissioner.h b/include/openthread/commissioner.h index 8ce874f19..169ac1ec4 100644 --- a/include/openthread/commissioner.h +++ b/include/openthread/commissioner.h @@ -403,25 +403,23 @@ uint16_t otCommissionerGetSessionId(otInstance *aInstance); otCommissionerState otCommissionerGetState(otInstance *aInstance); /** - * This method generates PSKc. + * This helper function generates PSKc from a given pass-phrase, network name, and extended PAN Id. * * PSKc is used to establish the Commissioner Session. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aPassPhrase The commissioning passphrase. + * @param[in] aPassPhrase The commissioning pass-phrase. * @param[in] aNetworkName The network name for PSKc computation. - * @param[in] aExtPanId The extended pan id for PSKc computation. - * @param[out] aPskc A pointer to the generated PSKc. + * @param[in] aExtPanId The extended PAN ID for PSKc computation. + * @param[out] aPskc A pointer to variable to output the generated PSKc. * * @retval OT_ERROR_NONE Successfully generate PSKc. * @retval OT_ERROR_INVALID_ARGS If any of the input arguments is invalid. * */ -otError otCommissionerGeneratePskc(otInstance * aInstance, - const char * aPassPhrase, +otError otCommissionerGeneratePskc(const char * aPassPhrase, const char * aNetworkName, const otExtendedPanId *aExtPanId, - uint8_t * aPskc); + otPskc * aPskc); /** * @} diff --git a/src/core/api/commissioner_api.cpp b/src/core/api/commissioner_api.cpp index 2524f07a6..9e9f38244 100644 --- a/src/core/api/commissioner_api.cpp +++ b/src/core/api/commissioner_api.cpp @@ -181,15 +181,12 @@ otCommissionerState otCommissionerGetState(otInstance *aInstance) return instance.Get().GetState(); } -otError otCommissionerGeneratePskc(otInstance * aInstance, - const char * aPassPhrase, +otError otCommissionerGeneratePskc(const char * aPassPhrase, const char * aNetworkName, const otExtendedPanId *aExtPanId, - uint8_t * aPskc) + otPskc * aPskc) { - OT_UNUSED_VARIABLE(aInstance); - - return MeshCoP::Commissioner::GeneratePskc(aPassPhrase, aNetworkName, - *static_cast(aExtPanId), aPskc); + return MeshCoP::Commissioner::GeneratePskc( + aPassPhrase, aNetworkName, *static_cast(aExtPanId), *static_cast(aPskc)); } #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 67d7cb6c9..e5ece645b 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -1100,7 +1100,7 @@ exit: otError Commissioner::GeneratePskc(const char * aPassPhrase, const char * aNetworkName, const Mac::ExtendedPanId &aExtPanId, - uint8_t * aPskc) + Pskc & aPskc) { otError error = OT_ERROR_NONE; const char *saltPrefix = "Thread"; @@ -1122,7 +1122,7 @@ otError Commissioner::GeneratePskc(const char * aPassPhrase, saltLen += static_cast(strlen(aNetworkName)); otPbkdf2Cmac(reinterpret_cast(aPassPhrase), static_cast(strlen(aPassPhrase)), - reinterpret_cast(salt), saltLen, 16384, OT_PSKC_MAX_SIZE, aPskc); + reinterpret_cast(salt), saltLen, 16384, OT_PSKC_MAX_SIZE, aPskc.m8); exit: return error; diff --git a/src/core/meshcop/commissioner.hpp b/src/core/meshcop/commissioner.hpp index eb42211ef..a59dc6e29 100644 --- a/src/core/meshcop/commissioner.hpp +++ b/src/core/meshcop/commissioner.hpp @@ -48,6 +48,7 @@ #include "meshcop/energy_scan_client.hpp" #include "meshcop/panid_query_client.hpp" #include "net/udp6.hpp" +#include "thread/key_manager.hpp" #include "thread/mle.hpp" namespace ot { @@ -218,7 +219,7 @@ public: * @param[in] aPassPhrase The commissioning passphrase. * @param[in] aNetworkName The network name for PSKc computation. * @param[in] aExtPanId The extended pan id for PSKc computation. - * @param[out] aPskc A pointer to where the generated PSKc will be placed. + * @param[out] aPskc A reference to a PSKc where the generated PSKc will be placed. * * @retval OT_ERROR_NONE Successfully generate PSKc. * @retval OT_ERROR_INVALID_ARGS If the length of passphrase is out of range. @@ -227,7 +228,7 @@ public: static otError GeneratePskc(const char * aPassPhrase, const char * aNetworkName, const Mac::ExtendedPanId &aExtPanId, - uint8_t * aPskc); + Pskc & aPskc); /** * This method returns a reference to the AnnounceBeginClient instance. diff --git a/tests/unit/test_pskc.cpp b/tests/unit/test_pskc.cpp index a95e86bd0..3f58e6da4 100644 --- a/tests/unit/test_pskc.cpp +++ b/tests/unit/test_pskc.cpp @@ -40,7 +40,7 @@ static const otExtendedPanId sXPanId = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0 void TestMinimumPassphrase(void) { - uint8_t pskc[OT_PSKC_MAX_SIZE]; + ot::Pskc pskc; const uint8_t expectedPskc[] = {0x44, 0x98, 0x8e, 0x22, 0xcf, 0x65, 0x2e, 0xee, 0xcc, 0xd1, 0xe4, 0xc0, 0x1d, 0x01, 0x54, 0xf8}; const char passphrase[] = "123456"; @@ -48,13 +48,13 @@ void TestMinimumPassphrase(void) SuccessOrQuit(ot::MeshCoP::Commissioner::GeneratePskc(passphrase, "OpenThread", static_cast(sXPanId), pskc), "TestMinimumPassphrase failed to generate PSKc"); - VerifyOrQuit(memcmp(pskc, expectedPskc, sizeof(pskc)) == 0, "TestMinimumPassphrase got wrong pskc"); + VerifyOrQuit(memcmp(pskc.m8, expectedPskc, sizeof(pskc)) == 0, "TestMinimumPassphrase got wrong pskc"); testFreeInstance(instance); } void TestMaximumPassphrase(void) { - uint8_t pskc[OT_PSKC_MAX_SIZE]; + ot::Pskc pskc; const uint8_t expectedPskc[] = {0x9e, 0x81, 0xbd, 0x35, 0xa2, 0x53, 0x76, 0x2f, 0x80, 0xee, 0x04, 0xff, 0x2f, 0xa2, 0x85, 0xe9}; const char passphrase[] = "1234567812345678" @@ -78,7 +78,7 @@ void TestMaximumPassphrase(void) SuccessOrQuit(ot::MeshCoP::Commissioner::GeneratePskc(passphrase, "OpenThread", static_cast(sXPanId), pskc), "TestMaximumPassphrase failed to generate PSKc"); - VerifyOrQuit(memcmp(pskc, expectedPskc, sizeof(pskc)) == 0, "TestMaximumPassphrase got wrong pskc"); + VerifyOrQuit(memcmp(pskc.m8, expectedPskc, sizeof(pskc)) == 0, "TestMaximumPassphrase got wrong pskc"); testFreeInstance(instance); }