Skip to content

Commit 522fa0b

Browse files
committed
[WIP] Support functionality of toranj/wpanctl to leave before join
Without PSA API supported, the default random Network Key is not reset after factory reset. This PR mimics the same functionality for the PSA crypto backend.
1 parent 574da8f commit 522fa0b

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/core/thread/key_manager.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,10 @@ KeyManager::KeyManager(Instance &aInstance)
179179
otPlatCryptoInit();
180180

181181
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
182-
{
183-
NetworkKey networkKey;
184-
185-
mNetworkKeyRef = Crypto::Storage::kInvalidKeyRef;
186-
mPskcRef = Crypto::Storage::kInvalidKeyRef;
187-
188-
IgnoreError(networkKey.GenerateRandom());
189-
StoreNetworkKey(networkKey, /* aOverWriteExisting */ false);
190-
}
182+
mNetworkKeyRef = Crypto::Storage::kInvalidKeyRef;
183+
mPskcRef = Crypto::Storage::kInvalidKeyRef;
191184
#else
192-
IgnoreError(mNetworkKey.GenerateRandom());
185+
mNetworkKey.Clear();
193186
mPskc.Clear();
194187
#endif
195188

@@ -200,6 +193,22 @@ void KeyManager::Start(void)
200193
{
201194
mKeySwitchGuardTimer = 0;
202195
ResetKeyRotationTimer();
196+
197+
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
198+
NetworkKey networkKey;
199+
200+
// Generate random Network Key, if there is none currently.
201+
if (mNetworkKeyRef == Crypto::Storage::kInvalidKeyRef)
202+
{
203+
IgnoreError(networkKey.GenerateRandom());
204+
SetNetworkKey(networkKey);
205+
}
206+
#else
207+
if (mNetworkKey.IsEmpty())
208+
{
209+
mNetworkKey.GenerateRandom();
210+
}
211+
#endif
203212
}
204213

205214
void KeyManager::Stop(void) { mKeyRotationTimer.Stop(); }
@@ -345,6 +354,12 @@ void KeyManager::UpdateKeyMaterial(void)
345354
{
346355
HashKeys hashKeys;
347356

357+
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
358+
VerifyOrExit(Crypto::Storage::IsKeyRefValid(mNetworkKeyRef));
359+
#else
360+
VerifyOrExit(!mNetworkKey.IsEmpty());
361+
#endif
362+
348363
ComputeKeys(mKeySequence, hashKeys);
349364

350365
mMleKey.SetFrom(hashKeys.GetMleKey());
@@ -375,6 +390,9 @@ void KeyManager::UpdateKeyMaterial(void)
375390
mTrelKey.SetFrom(key);
376391
}
377392
#endif
393+
394+
exit:
395+
return;
378396
}
379397

380398
void KeyManager::SetCurrentKeySequence(uint32_t aKeySequence, KeySeqUpdateFlags aFlags)
@@ -711,7 +729,13 @@ void KeyManager::DestroyTemporaryKeys(void)
711729
Get<Mac::Mac>().ClearMode2Key();
712730
}
713731

714-
void KeyManager::DestroyPersistentKeys(void) { Get<Crypto::Storage::KeyRefManager>().DestroyPersistentKeys(); }
732+
void KeyManager::DestroyPersistentKeys(void)
733+
{
734+
Get<Crypto::Storage::KeyRefManager>().DestroyPersistentKeys();
735+
736+
mNetworkKeyRef = Crypto::Storage::kInvalidKeyRef;
737+
mPskcRef = Crypto::Storage::kInvalidKeyRef;
738+
}
715739

716740
#endif // OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
717741

src/core/thread/key_manager.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ class NetworkKey : public otNetworkKey, public Equatable<NetworkKey>, public Cle
148148
* @retval kErrorFailed Failed to generate random sequence.
149149
*/
150150
Error GenerateRandom(void) { return Random::Crypto::Fill(*this); }
151+
152+
/**
153+
* Checks if the Network Key is empty (all bytes are zero).
154+
*
155+
* @retval true The key is empty.
156+
* @retval false The key is not empty.
157+
*/
158+
bool IsEmpty(void)
159+
{
160+
for (uint8_t i = 0; i < kSize; i++)
161+
{
162+
if (m8[i] != 0)
163+
{
164+
return false;
165+
}
166+
}
167+
168+
return true;
169+
}
151170
#endif
152171

153172
} OT_TOOL_PACKED_END;

tests/toranj/ncp/test-002-form.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@
9090

9191
node.set(wpan.WPAN_PANID, '0x1977')
9292
node.set(wpan.WPAN_XPANID, '1020031510006016', binary_data=True)
93-
node.set(wpan.WPAN_KEY, '0123456789abcdeffecdba9876543210', binary_data=True)
9493

9594
node.form('mazda', channel=12)
9695
verify(node.get(wpan.WPAN_STATE) == wpan.STATE_ASSOCIATED)
9796
verify(node.get(wpan.WPAN_NAME) == '"mazda"')
9897
verify(node.get(wpan.WPAN_CHANNEL) == '12')
99-
verify(node.get(wpan.WPAN_KEY) == '[0123456789ABCDEFFECDBA9876543210]')
10098
verify(node.get(wpan.WPAN_PANID) == '0x1977')
10199
verify(node.get(wpan.WPAN_XPANID) == '0x1020031510006016')
102100

0 commit comments

Comments
 (0)