1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 20:38:10 +00:00

refactor(legacy): Factor out multisig fingerprint processing.

This commit is contained in:
Andrew Kozlik 2022-03-04 20:02:48 +01:00 committed by Andrew Kozlik
parent 43f1d67289
commit 0dfbfefd2a

View File

@ -622,6 +622,39 @@ void phase2_request_next_input(void) {
} }
} }
static bool extract_input_multisig_fp(TxInfo *tx_info,
const TxInputType *txinput) {
if (txinput->has_multisig && !tx_info->multisig_fp_mismatch) {
uint8_t h[32] = {0};
if (cryptoMultisigFingerprint(&txinput->multisig, h) == 0) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Error computing multisig fingerprint"));
signing_abort();
return false;
}
if (tx_info->multisig_fp_set) {
if (memcmp(tx_info->multisig_fp, h, 32) != 0) {
tx_info->multisig_fp_mismatch = true;
}
} else {
memcpy(tx_info->multisig_fp, h, 32);
tx_info->multisig_fp_set = true;
}
} else { // single signature
tx_info->multisig_fp_mismatch = true;
}
return true;
}
bool check_change_multisig_fp(const TxInfo *tx_info,
const TxOutputType *txoutput) {
uint8_t h[32] = {0};
return tx_info->multisig_fp_set && !tx_info->multisig_fp_mismatch &&
cryptoMultisigFingerprint(&(txoutput->multisig), h) &&
memcmp(tx_info->multisig_fp, h, 32) == 0;
}
void extract_input_bip32_path(TxInfo *tx_info, const TxInputType *tinput) { void extract_input_bip32_path(TxInfo *tx_info, const TxInputType *tinput) {
if (tx_info->in_address_n_count == BIP32_NOCHANGEALLOWED) { if (tx_info->in_address_n_count == BIP32_NOCHANGEALLOWED) {
return; return;
@ -1174,25 +1207,9 @@ static bool tx_info_add_input(TxInfo *tx_info, const TxInputType *txinput) {
// Compute multisig fingerprint for change-output detection. In order for an // Compute multisig fingerprint for change-output detection. In order for an
// output to be considered a change-output, it must have the same fingerprint // output to be considered a change-output, it must have the same fingerprint
// as all inputs. // as all inputs.
if (txinput->has_multisig && !tx_info->multisig_fp_mismatch) { if (!extract_input_multisig_fp(tx_info, txinput)) {
uint8_t h[32] = {0};
if (cryptoMultisigFingerprint(&txinput->multisig, h) == 0) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Error computing multisig fingerprint"));
signing_abort();
return false; return false;
} }
if (tx_info->multisig_fp_set) {
if (memcmp(tx_info->multisig_fp, h, 32) != 0) {
tx_info->multisig_fp_mismatch = true;
}
} else {
memcpy(tx_info->multisig_fp, h, 32);
tx_info->multisig_fp_set = true;
}
} else { // single signature
tx_info->multisig_fp_mismatch = true;
}
// Remember the input's BIP-32 path. Change-outputs must use the same path // Remember the input's BIP-32 path. Change-outputs must use the same path
// as all inputs. // as all inputs.
@ -1358,14 +1375,9 @@ static bool is_change_output(const TxInfo *tx_info,
/* /*
* For multisig check that all inputs are multisig * For multisig check that all inputs are multisig
*/ */
if (txoutput->has_multisig) { if (txoutput->has_multisig && !check_change_multisig_fp(tx_info, txoutput)) {
uint8_t h[32] = {0};
if (!tx_info->multisig_fp_set || tx_info->multisig_fp_mismatch ||
!cryptoMultisigFingerprint(&(txoutput->multisig), h) ||
memcmp(tx_info->multisig_fp, h, 32) != 0) {
return false; return false;
} }
}
return check_change_bip32_path(tx_info, txoutput); return check_change_bip32_path(tx_info, txoutput);
} }