Skip to content

Commit

Permalink
Add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Komzpa authored Mar 31, 2024
1 parent 5d93a1d commit 901bdbb
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/bthome_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,15 @@ def _decrypt_bthome(
if (
new_encryption_counter < last_encryption_counter
and self.bindkey_verified is True
):
):
_LOGGER.debug(
"%s: Encryption counter decrement detected. got counter: %i, had counter: %i, reset counter: %i, message counter: %i",
self.title,
new_encryption_counter,
last_encryption_counter,
self.reset_counter,
self.message_since_last_reset
)
# Replay attack protection is two-edged sword: if you don't implement it, you allow attacker to replay a whole bunch of measurements.
# If you don't give some wiggle room for allowing resets changing batteries becomes a challenge
# Beware: If attacker manages to record a message with high encryption counter number they can
Expand Down Expand Up @@ -685,15 +693,15 @@ def _decrypt_bthome(
raise ValueError
else:
self.encryption_counter = new_encryption_counter
# Reset the reset_counter if a hundred messages have been received since the last reset
if self.message_since_last_reset >= 100 and self.reset_counter >= 1:
if self.reset_counter <= 1:
# There has been only one reset. No big deal. Clear it out.
self.reset_counter = 0
if self.reset_counter > 1:
# We're under seige, there has been multiple resets - carry this knowledge over into next hundred block.
self.reset_counter = 1
self.message_since_last_reset = 0
# Reset the reset_counter if a hundred messages have been received since the last reset
if self.message_since_last_reset >= 100 and self.reset_counter >= 1:
if self.reset_counter <= 1:
# There has been only one reset. No big deal. Clear it out.
self.reset_counter = 0
if self.reset_counter > 1:
# We're under seige, there has been multiple resets - carry this knowledge over into next hundred block.
self.reset_counter = 1
self.message_since_last_reset = 0

# decrypt the data
try:
Expand Down

0 comments on commit 901bdbb

Please sign in to comment.