Skip to content

Commit ef3c68c

Browse files
committed
Randomly generate the TCLK if one is not present in the backup
1 parent 94fef2c commit ef3c68c

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

zigpy_znp/api.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,19 @@ async def write_network_info(
357357

358358
tclk_seed = None
359359

360-
if (
361-
self.version > 1.2
362-
and network_info.stack_specific is not None
363-
and "tclk_seed" in network_info.stack_specific.get("zstack", {})
364-
):
365-
tclk_seed, _ = t.KeyData.deserialize(
366-
bytes.fromhex(network_info.stack_specific["zstack"]["tclk_seed"])
367-
)
360+
if self.version > 1.2:
361+
if (
362+
network_info.stack_specific is not None
363+
and network_info.stack_specific.get("zstack", {}).get("tclk_seed")
364+
):
365+
tclk_seed, _ = t.KeyData.deserialize(
366+
bytes.fromhex(network_info.stack_specific["zstack"]["tclk_seed"])
367+
)
368+
else:
369+
tclk_seed = t.KeyData(os.urandom(16))
370+
368371
nvram[OsalNvIds.TCLK_SEED] = tclk_seed
369-
elif self.version == 1.2:
372+
else:
370373
nvram[OsalNvIds.TCLK_SEED] = const.DEFAULT_TC_LINK_KEY
371374

372375
for key, value in nvram.items():
@@ -407,17 +410,21 @@ async def write_network_info(
407410

408411
LOGGER.debug("Writing children and keys")
409412

410-
optimal_tclk_seed = security.find_optimal_tclk_seed(devices.values(), tclk_seed)
411-
412-
if tclk_seed != optimal_tclk_seed:
413-
LOGGER.warning(
414-
"Provided TCLK seed %s is not optimal, using %s instead.",
415-
tclk_seed,
416-
optimal_tclk_seed,
413+
# Recompute the TCLK if necessary
414+
if self.version > 1.2:
415+
optimal_tclk_seed = security.find_optimal_tclk_seed(
416+
devices.values(), tclk_seed
417417
)
418418

419-
await self.nvram.osal_write(OsalNvIds.TCLK_SEED, optimal_tclk_seed)
420-
tclk_seed = optimal_tclk_seed
419+
if tclk_seed != optimal_tclk_seed:
420+
LOGGER.warning(
421+
"Provided TCLK seed %s is not optimal, using %s instead.",
422+
tclk_seed,
423+
optimal_tclk_seed,
424+
)
425+
426+
await self.nvram.osal_write(OsalNvIds.TCLK_SEED, optimal_tclk_seed)
427+
tclk_seed = optimal_tclk_seed
421428

422429
await security.write_devices(
423430
znp=self,

0 commit comments

Comments
 (0)