2023-12-27 22:29:22 +00:00
import pytest
2019-04-25 14:38:56 +00:00
from c0 . storage import Storage as StorageC0
2024-02-12 11:25:02 +00:00
from c3 . storage import Storage as StorageC3
2019-04-25 14:38:56 +00:00
from c . storage import Storage as StorageC
2023-08-15 15:57:02 +00:00
2024-02-05 11:52:08 +00:00
from python . src . norcow import NorcowBitwise
2019-04-25 14:38:56 +00:00
from python . src . storage import Storage as StoragePy
from . import common
# Strings for testing ChaCha20 encryption.
chacha_strings = [
b " Short string. " ,
b " " ,
b " Although ChaCha20 is a stream cipher, it operates on blocks of 64 bytes. This string is over 152 bytes in length so that we test multi-block encryption. " ,
b " This string is exactly 64 bytes long, that is exactly one block. " ,
]
def set_values ( s ) :
s . set ( 0xBEEF , b " Hello " )
s . set ( 0xCAFE , b " world! " )
s . set ( 0xDEAD , b " How \n " )
2020-08-14 16:54:44 +00:00
s . change_pin ( " " , " 222 " )
2019-04-25 14:38:56 +00:00
s . set ( 0xAAAA , b " are " )
s . set ( 0x0901 , b " you? " )
s . set ( 0x0902 , b " Lorem " )
s . set ( 0x0903 , b " ipsum " )
2020-08-14 16:54:44 +00:00
s . change_pin ( " 222 " , " 99 " )
2019-04-25 14:38:56 +00:00
s . set ( 0xDEAD , b " A \n " )
s . set ( 0xDEAD , b " AAAAAAAAAAA " )
s . set ( 0x2200 , b " BBBB " )
for i , string in enumerate ( chacha_strings ) :
s . set ( 0x0301 + i , string )
def check_values ( s ) :
2020-08-14 16:54:44 +00:00
assert s . unlock ( " 99 " )
2019-04-25 14:38:56 +00:00
assert s . get ( 0xAAAA ) == b " are "
assert s . get ( 0x0901 ) == b " you? "
assert s . get ( 0x0902 ) == b " Lorem "
assert s . get ( 0x0903 ) == b " ipsum "
assert s . get ( 0xDEAD ) == b " AAAAAAAAAAA "
assert s . get ( 0x2200 ) == b " BBBB "
for i , string in enumerate ( chacha_strings ) :
assert s . get ( 0x0301 + i ) == string
def test_upgrade ( ) :
sc0 = StorageC0 ( )
sc0 . init ( )
2020-08-14 16:54:44 +00:00
assert sc0 . unlock ( " " )
2019-04-25 14:38:56 +00:00
set_values ( sc0 )
for _ in range ( 10 ) :
2020-08-14 16:54:44 +00:00
assert not sc0 . unlock ( " 3 " )
2019-04-25 14:38:56 +00:00
2023-12-27 22:29:22 +00:00
sc1 = StorageC ( " libtrezor-storage.so " )
2019-04-25 14:38:56 +00:00
sc1 . _set_flash_buffer ( sc0 . _get_flash_buffer ( ) )
sc1 . init ( common . test_uid )
assert sc1 . get_pin_rem ( ) == 6
check_values ( sc1 )
2024-02-12 11:25:02 +00:00
def test_upgrade_from_3 ( ) :
sc3 = StorageC3 ( " libtrezor-storage3.so " )
sc3 . init ( common . test_uid )
assert sc3 . unlock ( " " )
set_values ( sc3 )
for _ in range ( 10 ) :
assert not sc3 . unlock ( " 3 " )
sc = StorageC ( " libtrezor-storage.so " )
sc . _set_flash_buffer ( sc3 . _get_flash_buffer ( ) )
sc . init ( common . test_uid )
assert sc . get_pin_rem ( ) == 6
check_values ( sc )
2024-02-05 11:52:08 +00:00
@pytest.mark.parametrize ( " nc_class " , [ NorcowBitwise ] )
2023-12-27 22:29:22 +00:00
def test_python_set_sectors ( nc_class ) :
sp0 = StoragePy ( nc_class )
sp0 . init ( common . test_uid )
assert sp0 . unlock ( " " )
set_values ( sp0 )
for _ in range ( 10 ) :
assert not sp0 . unlock ( " 3 " )
assert sp0 . get_pin_rem ( ) == 6
2019-04-25 14:38:56 +00:00
2023-12-27 22:29:22 +00:00
sp1 = StoragePy ( nc_class )
sp1 . nc . _set_sectors ( sp0 . _dump ( ) )
sp1 . init ( common . test_uid )
common . memory_equals ( sp0 , sp1 )
2019-04-25 14:38:56 +00:00
2023-12-27 22:29:22 +00:00
assert sp1 . get_pin_rem ( ) == 6
check_values ( sp1 )