2019-10-02 14:07:43 +00:00
from common import *
2019-10-31 15:34:16 +00:00
from mock_storage import mock_storage
2019-10-02 14:07:43 +00:00
2019-10-31 15:34:16 +00:00
import storage
import storage . recovery
2019-10-02 14:07:43 +00:00
from apps . management . recovery_device . recover import process_slip39
2021-03-23 12:35:27 +00:00
from trezor . enums import BackupType
2019-12-27 19:02:30 +00:00
from apps . management . recovery_device . word_validity import check , IdentifierMismatch , AlreadyAdded , ThresholdReached
2019-10-02 14:07:43 +00:00
MNEMONIC_SLIP39_BASIC_20_3of6 = [
" extra extend academic bishop cricket bundle tofu goat apart victim enlarge program behavior permit course armed jerky faint language modern " ,
" extra extend academic acne away best indicate impact square oasis prospect painting voting guest either argue username racism enemy eclipse " ,
" extra extend academic arcade born dive legal hush gross briefing talent drug much home firefly toxic analysis idea umbrella slice " ,
]
# Shamir shares (128 bits, 2 groups from 1 of 1, 1 of 1, 3 of 5, 2 of 6)
MNEMONIC_SLIP39_ADVANCED_20 = [
" eraser senior beard romp adorn nuclear spill corner cradle style ancient family general leader ambition exchange unusual garlic promise voice " ,
" eraser senior ceramic snake clay various huge numb argue hesitate auction category timber browser greatest hanger petition script leaf pickup " ,
" eraser senior ceramic shaft dynamic become junior wrist silver peasant force math alto coal amazing segment yelp velvet image paces " ,
" eraser senior ceramic round column hawk trust auction smug shame alive greatest sheriff living perfect corner chest sled fumes adequate " ,
]
# Shamir shares (256 bits, 2 groups from 1 of 1, 1 of 1, 3 of 5, 2 of 6):
MNEMONIC_SLIP39_ADVANCED_33 = [
" wildlife deal beard romp alcohol space mild usual clothes union nuclear testify course research heat listen task location thank hospital slice smell failure fawn helpful priest ambition average recover lecture process dough stadium " ,
" wildlife deal acrobat romp anxiety axis starting require metric flexible geology game drove editor edge screw helpful have huge holy making pitch unknown carve holiday numb glasses survive already tenant adapt goat fangs " ,
]
class TestSlip39 ( unittest . TestCase ) :
2019-10-03 08:51:26 +00:00
2019-10-02 14:07:43 +00:00
@mock_storage
2019-10-03 08:51:26 +00:00
def test_process_slip39_basic ( self ) :
2019-10-02 14:07:43 +00:00
storage . recovery . set_in_progress ( True )
2019-10-03 08:51:26 +00:00
# first share (member index 5)
first = MNEMONIC_SLIP39_BASIC_20_3of6 [ 0 ]
secret , share = process_slip39 ( first )
self . assertIsNone ( secret )
self . assertEqual ( share . group_count , storage . recovery . get_slip39_group_count ( ) )
self . assertEqual ( share . iteration_exponent , storage . recovery . get_slip39_iteration_exponent ( ) )
self . assertEqual ( share . identifier , storage . recovery . get_slip39_identifier ( ) )
self . assertEqual ( storage . recovery . get_slip39_remaining_shares ( 0 ) , 2 )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , first )
# second share (member index 0)
second = MNEMONIC_SLIP39_BASIC_20_3of6 [ 1 ]
secret , share = process_slip39 ( second )
self . assertIsNone ( secret )
self . assertEqual ( storage . recovery . get_slip39_remaining_shares ( 0 ) , 1 )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , second )
self . assertEqual ( storage . recovery_shares . fetch_group ( share . group_index ) , [ second , first ] ) # ordered by index
# third share (member index 3)
third = MNEMONIC_SLIP39_BASIC_20_3of6 [ 2 ]
secret , share = process_slip39 ( third )
2019-10-04 13:47:52 +00:00
self . assertEqual ( secret , b ' I \x1b y[ \x80 \xfc ! \xcc \xdf Fl \x0f \xbc \x98 \xc8 \xfc ' )
2019-10-03 08:51:26 +00:00
self . assertEqual ( storage . recovery . get_slip39_remaining_shares ( 0 ) , 0 )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , third )
self . assertEqual ( storage . recovery_shares . fetch_group ( share . group_index ) , [ second , third , first ] ) # ordered by index
@mock_storage
def test_process_slip39_advanced ( self ) :
storage . recovery . set_in_progress ( True )
# complete group 1 (1of1)
words = MNEMONIC_SLIP39_ADVANCED_20 [ 0 ]
2019-10-02 14:07:43 +00:00
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
self . assertEqual ( share . group_count , storage . recovery . get_slip39_group_count ( ) )
2019-10-03 08:51:26 +00:00
self . assertEqual ( share . iteration_exponent , storage . recovery . get_slip39_iteration_exponent ( ) )
self . assertEqual ( share . identifier , storage . recovery . get_slip39_identifier ( ) )
self . assertEqual ( storage . recovery . fetch_slip39_remaining_shares ( ) , [ 16 , 0 , 16 , 16 ] )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , words )
# member index 4 from group 2 (3of5)
words = MNEMONIC_SLIP39_ADVANCED_20 [ 1 ]
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
self . assertEqual ( share . group_count , storage . recovery . get_slip39_group_count ( ) )
self . assertEqual ( share . iteration_exponent , storage . recovery . get_slip39_iteration_exponent ( ) )
self . assertEqual ( share . identifier , storage . recovery . get_slip39_identifier ( ) )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , words )
self . assertEqual ( storage . recovery_shares . fetch_group ( 1 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 0 ] ] )
self . assertEqual ( storage . recovery_shares . fetch_group ( 2 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 1 ] ] )
self . assertEqual ( storage . recovery . fetch_slip39_remaining_shares ( ) , [ 16 , 0 , 2 , 16 ] )
# member index 2 from group 2
words = MNEMONIC_SLIP39_ADVANCED_20 [ 2 ]
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
self . assertEqual ( share . group_count , storage . recovery . get_slip39_group_count ( ) )
self . assertEqual ( share . iteration_exponent , storage . recovery . get_slip39_iteration_exponent ( ) )
self . assertEqual ( share . identifier , storage . recovery . get_slip39_identifier ( ) )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , words )
self . assertEqual ( storage . recovery_shares . fetch_group ( 1 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 0 ] ] )
self . assertEqual ( storage . recovery_shares . fetch_group ( 2 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 2 ] , MNEMONIC_SLIP39_ADVANCED_20 [ 1 ] ] )
self . assertEqual ( storage . recovery . fetch_slip39_remaining_shares ( ) , [ 16 , 0 , 1 , 16 ] )
# last member index 0 from group 2
# now group 2 is complete => the whole Shamir recovery is completed
words = MNEMONIC_SLIP39_ADVANCED_20 [ 3 ]
secret , share = process_slip39 ( words )
2019-10-04 13:47:52 +00:00
self . assertEqual ( secret , b ' \xc2 \xd2 \xe2 j \xd0 `# \xc6 \x01 E \xf1 P \xab \xe2 \xdd + ' )
2019-10-03 08:51:26 +00:00
self . assertEqual ( share . group_count , storage . recovery . get_slip39_group_count ( ) )
self . assertEqual ( share . iteration_exponent , storage . recovery . get_slip39_iteration_exponent ( ) )
self . assertEqual ( share . identifier , storage . recovery . get_slip39_identifier ( ) )
self . assertEqual ( storage . recovery_shares . get ( share . index , share . group_index ) , words )
self . assertEqual ( storage . recovery_shares . fetch_group ( 1 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 0 ] ] )
self . assertEqual ( storage . recovery_shares . fetch_group ( 2 ) , [ MNEMONIC_SLIP39_ADVANCED_20 [ 3 ] , MNEMONIC_SLIP39_ADVANCED_20 [ 2 ] , MNEMONIC_SLIP39_ADVANCED_20 [ 1 ] ] )
self . assertEqual ( storage . recovery . fetch_slip39_remaining_shares ( ) , [ 16 , 0 , 0 , 16 ] )
@mock_storage
def test_exceptions ( self ) :
storage . recovery . set_in_progress ( True )
words = MNEMONIC_SLIP39_BASIC_20_3of6 [ 0 ]
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
# same mnemonic
words = MNEMONIC_SLIP39_BASIC_20_3of6 [ 0 ]
with self . assertRaises ( RuntimeError ) :
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
# identifier mismatch
words = MNEMONIC_SLIP39_ADVANCED_20 [ 0 ]
with self . assertRaises ( RuntimeError ) :
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
# same identifier but different group settings
words = MNEMONIC_SLIP39_BASIC_20_3of6 [ 0 ]
w = words . split ( )
w [ 2 ] = " check " # change the group settings
w [ 3 ] = " mortgage "
w [ 17 ] = " garden " # modify checksum accordingly
w [ 18 ] = " merchant "
w [ 19 ] = " merchant "
words = " " . join ( w )
with self . assertRaises ( RuntimeError ) :
secret , share = process_slip39 ( words )
self . assertIsNone ( secret )
2019-10-02 14:07:43 +00:00
2019-12-20 20:36:59 +00:00
@mock_storage
def test_check_word_validity ( self ) :
storage . recovery . set_in_progress ( True )
2019-12-27 18:46:22 +00:00
# We claim to know the backup type, but nothing is stored. That is an invalid state.
2019-12-20 20:36:59 +00:00
with self . assertRaises ( RuntimeError ) :
2019-12-27 18:46:22 +00:00
check ( BackupType . Slip39_Advanced , [ " ocean " ] )
2019-12-20 20:36:59 +00:00
# if backup type is not set we can not do any checks
2019-12-30 11:35:56 +00:00
check ( None , [ " ocean " ] )
2019-12-20 20:36:59 +00:00
# BIP-39 has no "on-the-fly" checks
2019-12-30 11:35:56 +00:00
check ( BackupType . Bip39 , [ " ocean " ] )
2019-12-20 20:36:59 +00:00
# let's store two shares in the storage
secret , share = process_slip39 ( " trash smug adjust ambition criminal prisoner security math cover pecan response pharmacy center criminal salary elbow bracelet lunar briefing dragon " )
self . assertIsNone ( secret )
secret , share = process_slip39 ( " trash smug adjust aide benefit temple round clogs devote prevent type cards clogs plastic aspect paper behavior lunar custody intimate " )
self . assertIsNone ( secret )
# different identifier
2019-12-27 19:02:30 +00:00
with self . assertRaises ( IdentifierMismatch ) :
check ( BackupType . Slip39_Advanced , [ " slush " ] )
2019-12-20 20:36:59 +00:00
# same first word but still a different identifier
2019-12-27 19:02:30 +00:00
with self . assertRaises ( IdentifierMismatch ) :
check ( BackupType . Slip39_Advanced , [ " trash " , " slush " ] )
2019-12-20 20:36:59 +00:00
2019-12-30 11:35:56 +00:00
# same identifier but different group settings for Slip 39 Basic
with self . assertRaises ( IdentifierMismatch ) :
check ( BackupType . Slip39_Basic , [ " trash " , " smug " , " slush " ] )
2019-12-20 20:36:59 +00:00
# same mnemonic found out using the index
2019-12-27 19:02:30 +00:00
with self . assertRaises ( AlreadyAdded ) :
check ( BackupType . Slip39_Advanced , [ " trash " , " smug " , " adjust " , " ambition " ] )
2019-12-20 20:36:59 +00:00
# Let's store two more. The group is 4/6 so this group is now complete.
secret , share = process_slip39 ( " trash smug adjust arena beard quick language program true hush amount round geology should training practice language diet order ruin " )
self . assertIsNone ( secret )
secret , share = process_slip39 ( " trash smug adjust beam brave sack magazine radar toxic emission domestic cradle vocal petition mule toxic acid hobo welcome downtown " )
self . assertIsNone ( secret )
# If trying to add another one from this group we get a warning.
2019-12-27 19:02:30 +00:00
with self . assertRaises ( ThresholdReached ) :
check ( BackupType . Slip39_Advanced , [ " trash " , " smug " , " adjust " ] )
2019-12-20 20:36:59 +00:00
2019-10-02 14:07:43 +00:00
if __name__ == " __main__ " :
unittest . main ( )