Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions main/core/bip322.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,21 @@ static bool get_message_field(const struct wally_psbt *psbt,
return true;
}

/* The BIP322 to_sign shape: version 0, locktime 0, one input spending
* to_spend:0 with sequence 0, one 0-value OP_RETURN output. */
static bool is_to_sign_tx(const struct wally_tx *tx) {
return tx && tx->version == 0 && tx->locktime == 0 && tx->num_inputs == 1 &&
tx->num_outputs == 1 && tx->inputs[0].index == 0 &&
tx->inputs[0].sequence == 0 && tx->outputs[0].satoshi == 0 &&
tx->outputs[0].script_len == 1 &&
tx->outputs[0].script[0] == OP_RETURN;
}

bool bip322_detect(const struct wally_psbt *psbt) {
const unsigned char *msg;
size_t msg_len;
return psbt && get_message_field(psbt, &msg, &msg_len);
return psbt && get_message_field(psbt, &msg, &msg_len) &&
is_to_sign_tx(psbt->tx);
}

/* BIP322 tagged hash: SHA256(SHA256(tag) || SHA256(tag) || message). libwally
Expand Down Expand Up @@ -92,22 +103,11 @@ bool bip322_parse(const struct wally_psbt *psbt, bool is_testnet,
return false;
}

/* to_sign structure per BIP322 */
const struct wally_tx *tx = psbt->tx;
if (!tx || tx->version != 0 || tx->locktime != 0 || tx->num_inputs != 1 ||
tx->num_outputs != 1) {
if (!is_to_sign_tx(tx)) {
ESP_LOGW(TAG, "to_sign transaction structure mismatch");
return false;
}
if (tx->inputs[0].index != 0 || tx->inputs[0].sequence != 0) {
ESP_LOGW(TAG, "to_sign input must spend to_spend:0 with sequence 0");
return false;
}
if (tx->outputs[0].satoshi != 0 || tx->outputs[0].script_len != 1 ||
tx->outputs[0].script[0] != OP_RETURN) {
ESP_LOGW(TAG, "to_sign output must be 0-value OP_RETURN");
return false;
}

const struct wally_tx_output *utxo = psbt->inputs[0].witness_utxo;
if (!utxo || utxo->satoshi != 0 || !utxo->script || utxo->script_len == 0 ||
Expand Down
4 changes: 3 additions & 1 deletion main/core/bip322.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ typedef struct {
} bip322_request_t;

// True if the PSBT carries the PSBT_GLOBAL_GENERIC_SIGNED_MESSAGE (0x09)
// global field, marking it as a BIP322 message-signing request.
// global field over a BIP322 to_sign transaction, marking it as a BIP322
// message-signing request. A nonce cache session uses the same key on a
// regular transaction.
KERN_WARN_UNUSED_RESULT bool bip322_detect(const struct wally_psbt *psbt);

// Validates a BIP322 to_sign PSBT: structure (version-0 tx, single input at
Expand Down
45 changes: 45 additions & 0 deletions main/core/test/test_bip322.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <wally_crypto.h>
#include <wally_map.h>
#include <wally_psbt.h>
#include <wally_script.h>
#include <wally_transaction.h>

#include "core/bip322.h"
Expand Down Expand Up @@ -182,6 +183,49 @@ static void test_message_characters(void) {
}
}

/* A nonce cache session carries global key 0x09 too, on a regular
* transaction: only the to_sign shape makes a request BIP322. Each case breaks
* one field of it. */
static void test_detect_requires_to_sign(const uint8_t *raw, size_t raw_len) {
static const struct {
const char *name;
uint32_t version, locktime, index, sequence;
uint64_t satoshi;
unsigned char script;
} cases[] = {
{"version 2", 2, 0, 0, 0, 0, OP_RETURN},
{"locktime 1", 0, 1, 0, 0, 0, OP_RETURN},
{"input index 1", 0, 0, 1, 0, 0, OP_RETURN},
{"sequence 0xffffffff", 0, 0, 0, 0xffffffff, 0, OP_RETURN},
{"output value 1", 0, 0, 0, 0, 1, OP_RETURN},
{"output OP_TRUE", 0, 0, 0, 0, 0, OP_TRUE},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
printf("bip322_detect ignores field 0x09 with %s: ", cases[i].name);
struct wally_psbt *psbt = NULL;
if (wally_psbt_from_bytes(raw, raw_len, 0, &psbt) != WALLY_OK) {
FAIL("PSBT failed to parse");
continue;
}
struct wally_tx *tx = psbt->tx;
tx->version = cases[i].version;
tx->locktime = cases[i].locktime;
tx->inputs[0].index = cases[i].index;
tx->inputs[0].sequence = cases[i].sequence;
tx->outputs[0].satoshi = cases[i].satoshi;
tx->outputs[0].script[0] = cases[i].script;
bip322_request_t req = {0};
if (bip322_detect(psbt))
FAIL("detected on a regular transaction");
else if (bip322_parse(psbt, true, &req))
FAIL("parsed a regular transaction");
else
PASS();
bip322_request_free(&req);
wally_psbt_free(psbt);
}
}

int main(void) {
/* The host libwally hashes through PSA, as the device does, which needs
* wally_init. */
Expand Down Expand Up @@ -275,6 +319,7 @@ int main(void) {
PASS(); /* rejected at parse time (current upstream behavior) */
}

test_detect_requires_to_sign(raw, raw_len);
test_message_characters();

printf("\nResults: %d passed, %d failed\n", tests_passed, tests_failed);
Expand Down
Loading