Skip to content

Commit

Permalink
WIP - allocate blob in RAM for translations data
Browse files Browse the repository at this point in the history
  • Loading branch information
grdddj committed Nov 21, 2023
1 parent 83805e5 commit ed4a12b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions core/src/apps/management/change_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,39 @@ async def change_language(msg: ChangeLanguage) -> Success:
):
raise DataError("Invalid translations version")

# Confirm with user and wipe old data
# Confirm with user
await _require_confirm_change_language(header.language)
translations.wipe()

# TODO: add loader, so that the screen does not freeze?

# Loading all the data at once, so we can verify its fingerprint
# If we saved it gradually to the storage and only checked the fingerprint at the end
# (with the idea of deleting the data if the fingerprint does not match),
# attackers could still write some data into storage and then unplug the device.
blob = utils.empty_bytearray(translations.data_max_size())

# Write the header
translations.write(header_data, 0)
blob.extend(header_data)

# Requesting the data in chunks and saving them
# Requesting the data in chunks and storing them in the blob
# Also checking the hash of the data for consistency
data_left = data_length - len(header_data)
offset = len(header_data)
hash_writer = utils.HashWriter(sha256())
while data_left > 0:
data_chunk = await get_data_chunk(data_left, offset)
translations.write(data_chunk, offset)
blob.extend(data_chunk)
hash_writer.write(data_chunk)
data_left -= len(data_chunk)
offset += len(data_chunk)

# When the data do not match the hash, wipe all the written translations
# When the data do not match the hash, do not write anything
if hash_writer.get_digest() != header.data_hash:
translations.wipe()
raise DataError("Invalid data hash")

translations.wipe()
translations.write(blob, 0)

return Success(message="Language changed")


Expand Down
2 changes: 1 addition & 1 deletion core/src/trezor/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_language() -> str:
return DEFAULT_LANGUAGE


def write(data: bytes, offset: int) -> None:
def write(data: bytes | bytearray, offset: int) -> None:
from trezor import wire

if offset + len(data) > data_max_size():
Expand Down

0 comments on commit ed4a12b

Please sign in to comment.