From f90ba10af3d6b9c6ffc12a3b4e900613beabe114 Mon Sep 17 00:00:00 2001 From: matejcik Date: Thu, 12 Dec 2019 14:43:17 +0100 Subject: [PATCH] core: modify function signatures to match python-shamir-mnemonic --- core/src/apps/common/mnemonic.py | 2 +- core/src/apps/debug/load_device.py | 12 +++++++----- .../src/apps/management/recovery_device/recover.py | 6 ++---- core/src/apps/management/reset_device/__init__.py | 12 ++++++------ core/src/trezor/crypto/slip39.py | 14 +++++++------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/src/apps/common/mnemonic.py b/core/src/apps/common/mnemonic.py index 2ee6f764b9..cae74f2cf6 100644 --- a/core/src/apps/common/mnemonic.py +++ b/core/src/apps/common/mnemonic.py @@ -48,7 +48,7 @@ def get_seed(passphrase: str = "", progress_bar: bool = True) -> bytes: # Identifier or exponent expected but not found raise RuntimeError seed = slip39.decrypt( - identifier, iteration_exponent, mnemonic_secret, passphrase.encode() + mnemonic_secret, passphrase.encode(), iteration_exponent, identifier ) return seed diff --git a/core/src/apps/debug/load_device.py b/core/src/apps/debug/load_device.py index 7227bc6683..7a0b011553 100644 --- a/core/src/apps/debug/load_device.py +++ b/core/src/apps/debug/load_device.py @@ -24,15 +24,17 @@ async def load_device(ctx, msg): secret = msg.mnemonics[0].encode() backup_type = BackupType.Bip39 else: - identifier, iteration_exponent, secret, group_count = slip39.combine_mnemonics( - msg.mnemonics - ) - if group_count == 1: + identifier, iteration_exponent, secret = slip39.recover_ems(msg.mnemonics) + + # this must succeed if the recover_ems call succeeded + share = slip39.decode_mnemonic(msg.mnemonics[0]) + if share.group_count == 1: backup_type = BackupType.Slip39_Basic - elif group_count > 1: + elif share.group_count > 1: backup_type = BackupType.Slip39_Advanced else: raise RuntimeError("Invalid group count") + storage.device.set_slip39_identifier(identifier) storage.device.set_slip39_iteration_exponent(iteration_exponent) diff --git a/core/src/apps/management/recovery_device/recover.py b/core/src/apps/management/recovery_device/recover.py index 26627d240d..98d09230ac 100644 --- a/core/src/apps/management/recovery_device/recover.py +++ b/core/src/apps/management/recovery_device/recover.py @@ -46,9 +46,7 @@ def process_slip39(words: str) -> Tuple[Optional[bytes], slip39.Share]: # if share threshold and group threshold are 1 # we can calculate the secret right away if share.threshold == 1 and share.group_threshold == 1: - identifier, iteration_exponent, secret, _ = slip39.combine_mnemonics( - [words] - ) + identifier, iteration_exponent, secret = slip39.recover_ems([words]) return secret, share else: # we need more shares @@ -89,7 +87,7 @@ def process_slip39(words: str) -> Tuple[Optional[bytes], slip39.Share]: # in case of slip39 basic we only need the first and only group mnemonics = storage.recovery_shares.fetch_group(0) - identifier, iteration_exponent, secret, _ = slip39.combine_mnemonics(mnemonics) + identifier, iteration_exponent, secret = slip39.recover_ems(mnemonics) return secret, share diff --git a/core/src/apps/management/reset_device/__init__.py b/core/src/apps/management/reset_device/__init__.py index 41bd6c8f96..e15c848d69 100644 --- a/core/src/apps/management/reset_device/__init__.py +++ b/core/src/apps/management/reset_device/__init__.py @@ -101,12 +101,12 @@ async def backup_slip39_basic( threshold = await layout.slip39_prompt_threshold(ctx, shares_count) # generate the mnemonics - mnemonics = slip39.generate_mnemonics_from_data( - encrypted_master_secret, - storage.device.get_slip39_identifier(), + mnemonics = slip39.split_ems( 1, # Single Group threshold [(threshold, shares_count)], # Single Group threshold/count + storage.device.get_slip39_identifier(), storage.device.get_slip39_iteration_exponent(), + encrypted_master_secret, )[0] # show and confirm individual shares @@ -136,12 +136,12 @@ async def backup_slip39_advanced( groups.append((share_threshold, share_count)) # generate the mnemonics - mnemonics = slip39.generate_mnemonics_from_data( - encrypted_master_secret=encrypted_master_secret, - identifier=storage.device.get_slip39_identifier(), + mnemonics = slip39.split_ems( group_threshold=group_threshold, groups=groups, + identifier=storage.device.get_slip39_identifier(), iteration_exponent=storage.device.get_slip39_iteration_exponent(), + encrypted_master_secret=encrypted_master_secret, ) # show and confirm individual shares diff --git a/core/src/trezor/crypto/slip39.py b/core/src/trezor/crypto/slip39.py index 2b869670aa..06749181ff 100644 --- a/core/src/trezor/crypto/slip39.py +++ b/core/src/trezor/crypto/slip39.py @@ -165,10 +165,10 @@ class Share: def decrypt( - identifier: int, - iteration_exponent: int, encrypted_master_secret: bytes, passphrase: bytes, + iteration_exponent: int, + identifier: int, ) -> bytes: """ Converts the Encrypted Master Secret to a Master Secret by applying the passphrase. @@ -194,12 +194,12 @@ def generate_random_identifier() -> int: return identifier & ((1 << _ID_LENGTH_BITS) - 1) -def generate_mnemonics_from_data( - encrypted_master_secret: bytes, # The encrypted master secret to split. - identifier: int, +def split_ems( group_threshold: int, # The number of groups required to reconstruct the master secret. groups: List[Tuple[int, int]], # A list of (member_threshold, member_count). + identifier: int, iteration_exponent: int, + encrypted_master_secret: bytes, # The encrypted master secret to split. ) -> List[List[str]]: """ Splits an encrypted master secret into mnemonic shares using Shamir's secret sharing scheme. @@ -253,7 +253,7 @@ def generate_mnemonics_from_data( return mnemonics -def combine_mnemonics(mnemonics: List[str]) -> Tuple[int, int, bytes, int]: +def recover_ems(mnemonics: List[str]) -> Tuple[int, int, bytes]: """ Combines mnemonic shares to obtain the encrypted master secret which was previously split using Shamir's secret sharing scheme. @@ -292,7 +292,7 @@ def combine_mnemonics(mnemonics: List[str]) -> Tuple[int, int, bytes, int]: ] encrypted_master_secret = _recover_secret(group_threshold, group_shares) - return identifier, iteration_exponent, encrypted_master_secret, group_count + return identifier, iteration_exponent, encrypted_master_secret def decode_mnemonic(mnemonic: str) -> Share: