mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-08 21:52:42 +00:00
core: modify function signatures to match python-shamir-mnemonic
This commit is contained in:
parent
f491239c26
commit
f90ba10af3
@ -48,7 +48,7 @@ def get_seed(passphrase: str = "", progress_bar: bool = True) -> bytes:
|
|||||||
# Identifier or exponent expected but not found
|
# Identifier or exponent expected but not found
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
seed = slip39.decrypt(
|
seed = slip39.decrypt(
|
||||||
identifier, iteration_exponent, mnemonic_secret, passphrase.encode()
|
mnemonic_secret, passphrase.encode(), iteration_exponent, identifier
|
||||||
)
|
)
|
||||||
|
|
||||||
return seed
|
return seed
|
||||||
|
@ -24,15 +24,17 @@ async def load_device(ctx, msg):
|
|||||||
secret = msg.mnemonics[0].encode()
|
secret = msg.mnemonics[0].encode()
|
||||||
backup_type = BackupType.Bip39
|
backup_type = BackupType.Bip39
|
||||||
else:
|
else:
|
||||||
identifier, iteration_exponent, secret, group_count = slip39.combine_mnemonics(
|
identifier, iteration_exponent, secret = slip39.recover_ems(msg.mnemonics)
|
||||||
msg.mnemonics
|
|
||||||
)
|
# this must succeed if the recover_ems call succeeded
|
||||||
if group_count == 1:
|
share = slip39.decode_mnemonic(msg.mnemonics[0])
|
||||||
|
if share.group_count == 1:
|
||||||
backup_type = BackupType.Slip39_Basic
|
backup_type = BackupType.Slip39_Basic
|
||||||
elif group_count > 1:
|
elif share.group_count > 1:
|
||||||
backup_type = BackupType.Slip39_Advanced
|
backup_type = BackupType.Slip39_Advanced
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("Invalid group count")
|
raise RuntimeError("Invalid group count")
|
||||||
|
|
||||||
storage.device.set_slip39_identifier(identifier)
|
storage.device.set_slip39_identifier(identifier)
|
||||||
storage.device.set_slip39_iteration_exponent(iteration_exponent)
|
storage.device.set_slip39_iteration_exponent(iteration_exponent)
|
||||||
|
|
||||||
|
@ -46,9 +46,7 @@ def process_slip39(words: str) -> Tuple[Optional[bytes], slip39.Share]:
|
|||||||
# if share threshold and group threshold are 1
|
# if share threshold and group threshold are 1
|
||||||
# we can calculate the secret right away
|
# we can calculate the secret right away
|
||||||
if share.threshold == 1 and share.group_threshold == 1:
|
if share.threshold == 1 and share.group_threshold == 1:
|
||||||
identifier, iteration_exponent, secret, _ = slip39.combine_mnemonics(
|
identifier, iteration_exponent, secret = slip39.recover_ems([words])
|
||||||
[words]
|
|
||||||
)
|
|
||||||
return secret, share
|
return secret, share
|
||||||
else:
|
else:
|
||||||
# we need more shares
|
# 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
|
# in case of slip39 basic we only need the first and only group
|
||||||
mnemonics = storage.recovery_shares.fetch_group(0)
|
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
|
return secret, share
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,12 +101,12 @@ async def backup_slip39_basic(
|
|||||||
threshold = await layout.slip39_prompt_threshold(ctx, shares_count)
|
threshold = await layout.slip39_prompt_threshold(ctx, shares_count)
|
||||||
|
|
||||||
# generate the mnemonics
|
# generate the mnemonics
|
||||||
mnemonics = slip39.generate_mnemonics_from_data(
|
mnemonics = slip39.split_ems(
|
||||||
encrypted_master_secret,
|
|
||||||
storage.device.get_slip39_identifier(),
|
|
||||||
1, # Single Group threshold
|
1, # Single Group threshold
|
||||||
[(threshold, shares_count)], # Single Group threshold/count
|
[(threshold, shares_count)], # Single Group threshold/count
|
||||||
|
storage.device.get_slip39_identifier(),
|
||||||
storage.device.get_slip39_iteration_exponent(),
|
storage.device.get_slip39_iteration_exponent(),
|
||||||
|
encrypted_master_secret,
|
||||||
)[0]
|
)[0]
|
||||||
|
|
||||||
# show and confirm individual shares
|
# show and confirm individual shares
|
||||||
@ -136,12 +136,12 @@ async def backup_slip39_advanced(
|
|||||||
groups.append((share_threshold, share_count))
|
groups.append((share_threshold, share_count))
|
||||||
|
|
||||||
# generate the mnemonics
|
# generate the mnemonics
|
||||||
mnemonics = slip39.generate_mnemonics_from_data(
|
mnemonics = slip39.split_ems(
|
||||||
encrypted_master_secret=encrypted_master_secret,
|
|
||||||
identifier=storage.device.get_slip39_identifier(),
|
|
||||||
group_threshold=group_threshold,
|
group_threshold=group_threshold,
|
||||||
groups=groups,
|
groups=groups,
|
||||||
|
identifier=storage.device.get_slip39_identifier(),
|
||||||
iteration_exponent=storage.device.get_slip39_iteration_exponent(),
|
iteration_exponent=storage.device.get_slip39_iteration_exponent(),
|
||||||
|
encrypted_master_secret=encrypted_master_secret,
|
||||||
)
|
)
|
||||||
|
|
||||||
# show and confirm individual shares
|
# show and confirm individual shares
|
||||||
|
@ -165,10 +165,10 @@ class Share:
|
|||||||
|
|
||||||
|
|
||||||
def decrypt(
|
def decrypt(
|
||||||
identifier: int,
|
|
||||||
iteration_exponent: int,
|
|
||||||
encrypted_master_secret: bytes,
|
encrypted_master_secret: bytes,
|
||||||
passphrase: bytes,
|
passphrase: bytes,
|
||||||
|
iteration_exponent: int,
|
||||||
|
identifier: int,
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
"""
|
"""
|
||||||
Converts the Encrypted Master Secret to a Master Secret by applying the passphrase.
|
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)
|
return identifier & ((1 << _ID_LENGTH_BITS) - 1)
|
||||||
|
|
||||||
|
|
||||||
def generate_mnemonics_from_data(
|
def split_ems(
|
||||||
encrypted_master_secret: bytes, # The encrypted master secret to split.
|
|
||||||
identifier: int,
|
|
||||||
group_threshold: int, # The number of groups required to reconstruct the master secret.
|
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).
|
groups: List[Tuple[int, int]], # A list of (member_threshold, member_count).
|
||||||
|
identifier: int,
|
||||||
iteration_exponent: int,
|
iteration_exponent: int,
|
||||||
|
encrypted_master_secret: bytes, # The encrypted master secret to split.
|
||||||
) -> List[List[str]]:
|
) -> List[List[str]]:
|
||||||
"""
|
"""
|
||||||
Splits an encrypted master secret into mnemonic shares using Shamir's secret sharing scheme.
|
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
|
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
|
Combines mnemonic shares to obtain the encrypted master secret which was previously
|
||||||
split using Shamir's secret sharing scheme.
|
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)
|
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:
|
def decode_mnemonic(mnemonic: str) -> Share:
|
||||||
|
Loading…
Reference in New Issue
Block a user