mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-08 05:32:39 +00:00
feat(core): make starting channel id random
[no changelog]
This commit is contained in:
parent
7e68d85a64
commit
335b348de2
@ -98,11 +98,16 @@ class SessionThpCache(ConnectionCache):
|
|||||||
|
|
||||||
_CHANNELS: list[ChannelCache] = []
|
_CHANNELS: list[ChannelCache] = []
|
||||||
_SESSIONS: list[SessionThpCache] = []
|
_SESSIONS: list[SessionThpCache] = []
|
||||||
|
cid_counter: int = 0
|
||||||
|
|
||||||
|
# Last-used counter
|
||||||
|
_usage_counter = 0
|
||||||
|
|
||||||
|
|
||||||
def initialize() -> None:
|
def initialize() -> None:
|
||||||
global _CHANNELS
|
global _CHANNELS
|
||||||
global _SESSIONS
|
global _SESSIONS
|
||||||
|
global cid_counter
|
||||||
|
|
||||||
for _ in range(_MAX_CHANNELS_COUNT):
|
for _ in range(_MAX_CHANNELS_COUNT):
|
||||||
_CHANNELS.append(ChannelCache())
|
_CHANNELS.append(ChannelCache())
|
||||||
@ -114,12 +119,9 @@ def initialize() -> None:
|
|||||||
for session in _SESSIONS:
|
for session in _SESSIONS:
|
||||||
session.clear()
|
session.clear()
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
# First unauthenticated channel will have index 0
|
cid_counter = random.randint(0, 0xFFFE)
|
||||||
_usage_counter = 0
|
|
||||||
|
|
||||||
# with this (arbitrary) value=4659, the first allocated channel will have cid=1234 (hex)
|
|
||||||
cid_counter: int = 4659 # TODO change to random value on start
|
|
||||||
|
|
||||||
|
|
||||||
def get_new_channel(iface: bytes) -> ChannelCache:
|
def get_new_channel(iface: bytes) -> ChannelCache:
|
||||||
@ -211,11 +213,6 @@ def get_new_session(channel: ChannelCache):
|
|||||||
return _SESSIONS[index]
|
return _SESSIONS[index]
|
||||||
|
|
||||||
|
|
||||||
def _get_usage_counter() -> int:
|
|
||||||
global _usage_counter
|
|
||||||
return _usage_counter
|
|
||||||
|
|
||||||
|
|
||||||
def _get_usage_counter_and_increment() -> int:
|
def _get_usage_counter_and_increment() -> int:
|
||||||
global _usage_counter
|
global _usage_counter
|
||||||
_usage_counter += 1
|
_usage_counter += 1
|
||||||
@ -303,7 +300,8 @@ def _get_cid(session: SessionThpCache) -> int:
|
|||||||
def get_least_recently_used_item(
|
def get_least_recently_used_item(
|
||||||
list: list[ChannelCache] | list[SessionThpCache], max_count: int
|
list: list[ChannelCache] | list[SessionThpCache], max_count: int
|
||||||
):
|
):
|
||||||
lru_counter = _get_usage_counter()
|
global _usage_counter
|
||||||
|
lru_counter = _usage_counter + 1
|
||||||
lru_item_index = 0
|
lru_item_index = 0
|
||||||
for i in range(max_count):
|
for i in range(max_count):
|
||||||
if list[i].last_usage < lru_counter:
|
if list[i].last_usage < lru_counter:
|
||||||
|
Loading…
Reference in New Issue
Block a user