Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def reset(excluded: tuple[AnyBytes, AnyBytes] | None) -> None:
if utils.USE_THP:
device_secret = device.get_device_secret()
credential_counter = device.get_cred_auth_key_counter()
# keep the name cache since we're keeping BLE bonds as well as THP credentials
paired_names = device.get_thp_paired_names()
wipe(clear_cache=False)
wipe_cache(excluded)
common.set(common.APP_DEVICE, device.DEVICE_ID, device_id.encode(), public=True)
Expand All @@ -56,6 +58,8 @@ def reset(excluded: tuple[AnyBytes, AnyBytes] | None) -> None:
device.CRED_AUTH_KEY_COUNTER,
credential_counter,
)
if paired_names:
common.set(common.APP_DEVICE, device.THP_PAIRED_NAMES, paired_names)


def _migrate_from_version_01() -> None:
Expand Down
13 changes: 8 additions & 5 deletions core/src/storage/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
_DISABLE_HAPTIC_FEEDBACK = const(0x20) # bool (0x01 or empty)
_DISABLE_RGB_LED = const(0x21) # bool (0x01 or empty)
if utils.USE_THP:
_THP_PAIRED_CACHE = const(0x22) # bytes
THP_PAIRED_NAMES = const(0x22) # bytes
if utils.USE_POWER_MANAGER:
_AUTOLOCK_DELAY_BATT_MS = const(0x23) # int
_DISABLE_BLUETOOTH = const(0x24) # bool (0x01 or empty)
Expand Down Expand Up @@ -472,14 +472,17 @@ def get_rgb_led() -> bool:

if utils.USE_THP:

def set_thp_paired_cache(blob: AnyBytes) -> None:
def set_thp_paired_names(blob: AnyBytes) -> None:
"""
Set THP paired entries' cache (using protobuf serialization).
"""
common.set(_NAMESPACE, _THP_PAIRED_CACHE, blob)
common.set(_NAMESPACE, THP_PAIRED_NAMES, blob)

def get_thp_paired_cache() -> bytes | None:
Comment thread
mmilata marked this conversation as resolved.
def get_thp_paired_names() -> bytes | None:
"""
Get THP paired entries' cache (using protobuf serialization).

Please note that while THP calls this a cache, it is persisted
across reboots, unlike storage.cache.
"""
return common.get(_NAMESPACE, _THP_PAIRED_CACHE)
return common.get(_NAMESPACE, THP_PAIRED_NAMES)
8 changes: 4 additions & 4 deletions core/src/trezor/wire/thp/paired_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

def load() -> list[ThpPairedCacheEntry]:
"""Load THP paired entries from flash."""
from storage.device import get_thp_paired_cache
from storage.device import get_thp_paired_names
from trezor.protobuf import decode

if (blob := get_thp_paired_cache()) is None:
if (blob := get_thp_paired_names()) is None:
return [] # an empty cache

cache = decode(blob, ThpPairedCache, _ENABLE_EXPERIMENTAL)
Expand All @@ -29,7 +29,7 @@ def load() -> list[ThpPairedCacheEntry]:

def store(entries: list[ThpPairedCacheEntry], _bonds: set[bytes] | None = None) -> None:
"""Store THP paired entries to flash."""
from storage.device import set_thp_paired_cache
from storage.device import set_thp_paired_names
from trezor.protobuf import dump_message_buffer

if _bonds is None:
Expand All @@ -44,7 +44,7 @@ def store(entries: list[ThpPairedCacheEntry], _bonds: set[bytes] | None = None)
if __debug__:
log.debug(__name__, "storing THP cache:\n%s", utils.dump_protobuf(cache))

set_thp_paired_cache(dump_message_buffer(cache))
set_thp_paired_names(dump_message_buffer(cache))


def cache_host_info(mac_addr: AnyBytes | None, host_name: str, app_name: str) -> None:
Expand Down
6 changes: 3 additions & 3 deletions core/tests/test_apps.thp.paired_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from trezor import config, utils

if utils.USE_THP:
from storage.device import get_thp_paired_cache
from storage.device import get_thp_paired_names
from trezor.messages import ThpPairedCacheEntry
from trezor.wire.thp import paired_cache

Expand Down Expand Up @@ -88,7 +88,7 @@ def test_store_more_bonds(self):
self.assertListEqual(paired_cache.load(), [])

def test_max_size(self):
self.assertIsNone(get_thp_paired_cache())
self.assertIsNone(get_thp_paired_names())
# serialize longest `host_name` and `app_name` and maximal number of bonds
entries = [
ThpPairedCacheEntry(
Expand All @@ -100,7 +100,7 @@ def test_max_size(self):
paired_cache.store(entries=entries, _bonds=bonds)
self.assertListEqual(paired_cache.load(), entries)

cache_blob = get_thp_paired_cache()
cache_blob = get_thp_paired_names()
self.assertIsNotNone(cache_blob)
# Check that serialized size is not too large:
# 8 entries x (32 bytes [host name] + 32 bytes [app name] + 6 bytes [addr]) = 560 bytes
Expand Down