@@ -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
205214void 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
380398void 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
0 commit comments