Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
url = https://github.com/coinkite/mpy-qr.git
[submodule "external/libngu"]
path = external/libngu
url = https://github.com/switck/libngu.git
url = https://github.com/Sjors/libngu.git
[submodule "stm32/mk4-bootloader/hal"]
path = stm32/mk4-bootloader/hal
url = https://github.com/STMicroelectronics/STM32CubeL4.git
Expand Down
50 changes: 50 additions & 0 deletions docs/anti-exfil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Experimental interactive Taproot anti-exfil

This branch implements the draft BIP-EXFIL protocol for BIP86 key-path inputs.
It supports PSBT versions 0 and 2 and SIGHASH_DEFAULT or SIGHASH_ALL. It does
not implement this protocol for MuSig2, script paths, registered Miniscript
wallets, or Taproot outputs with a script-tree commitment.

The host first adds a proprietary per-input key
`fc02616500 || output_key32` with a 32-byte host commitment. On approval,
the firmware adds `fc02616501 || output_key32` containing its full compressed
33-byte nonce point, and returns a PSBT without signatures. A retry of this
same request derives the same point, including its parity.

The host records that point before revealing its 32-byte nonzero scalar in
`fc02616502 || output_key32`. In the second invocation the firmware rederives
and checks both previous fields, adds the host contribution to its nonce,
verifies the resulting BIP340 signature, and returns the normal Taproot key
signature. The current UI requests transaction approval in both invocations.
No secret nonce state needs to survive between invocations.

The host must verify the original nonce relation and the signature before
using either. A bridge that only preserves unknown PSBT fields does not provide
this protection. Transcript fields remain in the response until the host
verifies it; the host must remove them before exporting the completed PSBT.
They must not be included in routine diagnostic logs. Automatic retries after
a nonce commitment must reuse the original transcript, rather than obtain new
host randomness.

Protected requests reject incomplete or noncanonical fields, more than one
signing key per input, mixed rounds, fields outside input maps, existing target
signatures/finalization, other sighashes, and unsupported spending paths. Only
targeted inputs receive new signatures. Input ownership, change checking,
transaction approval, and normal Taproot key-tweak checks still apply.

The native primitive comes from the linked experimental `Sjors/libngu` branch
`2026/10/taproot-anti-exfil`. Its nested libsecp256k1 version is unchanged.
`BIP-EXFIL/host` and `BIP-EXFIL/nonce` remain provisional domains; assigning a
BIP number requires coordinated vector regeneration and implementation changes.
This implementation has not received cryptographic review.

Tests for a native simulator build:

```
unix/coldcard-mpy external/libngu/ngu/ngu_tests/test_anti_exfil.py
env MICROPYPATH=shared unix/coldcard-mpy testing/devtest/anti_exfil_fields.py
```

HWI's corresponding branch includes device tests for both rounds, reconnecting,
firmware rejection of a bad reveal or nonce, both PSBT versions and supported
sighashes, and finalization, mempool acceptance, broadcast and confirmation.
2 changes: 1 addition & 1 deletion external/libngu
40 changes: 40 additions & 0 deletions shared/anti_exfil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# (c) Copyright 2026 by Sjors Provoost. This file is covered by license found in COPYING-CC.
# Experimental BIP-EXFIL proprietary PSBT input fields.

from uio import BytesIO
from serializations import deser_compact_size

PREFIX = b'\xfc\x02ae'

def read_fields(proxy, allowed=True):
# Unknown PSBT fields are coordinates into the original uploaded PSBT.
# Return one signing key and its complete host request, without changing
# the original fields that must survive through host verification.
fields = {}
pubkey = None
finalized = False
for key_coord, value_coord in proxy.unknown or ():
key = proxy.get(key_coord)
if key[0] in (7, 8):
finalized = True
if key[0] != 0xfc:
continue
stream = BytesIO(key[1:])
size = deser_compact_size(stream)
if stream.read(size) != b'ae':
continue
assert allowed, 'Anti-exfil fields outside input'
assert len(key) == 37 and key[:4] == PREFIX, 'Anti-exfil key encoding'
subtype = key[4]
assert subtype in (0, 1, 2), 'Anti-exfil subtype'
assert pubkey is None or pubkey == key[5:], 'Multiple anti-exfil keys'
assert subtype not in fields, 'Duplicate anti-exfil field'
value = proxy.get(value_coord)
assert len(value) == (33 if subtype == 1 else 32), 'Anti-exfil value length'
pubkey = key[5:]
fields[subtype] = value
if fields:
assert not finalized, 'Finalized anti-exfil target'
assert set(fields) in ({0}, {0, 1, 2}), 'Incomplete anti-exfil request'
return pubkey, fields
return None
1 change: 1 addition & 0 deletions shared/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'pincodes.py',
'precomp_tag_hash.py',
'psbt.py',
'anti_exfil.py',
'psram.py',
'pwsave.py',
'qrs.py',
Expand Down
59 changes: 54 additions & 5 deletions shared/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ class psbtInputProxy(psbtProxy):
'taproot_merkle_root', 'taproot_script_sigs', 'taproot_scripts',
'taproot_subpaths', 'taproot_internal_key', 'taproot_key_sig', 'tr_added_sigs',
'ik_idx', 'musig_pubkeys', 'musig_pubnonces', 'musig_part_sigs', 'musig_agg_idx',
'musig_added_pubnonces', 'musig_added_sigs', 'wif_key', 'wif_redeem_script'
'musig_added_pubnonces', 'musig_added_sigs', 'wif_key', 'wif_redeem_script',
'ae_request', 'ae_added_nonce'
)

def __init__(self, fd, idx):
Expand Down Expand Up @@ -1308,6 +1309,10 @@ def serialize(self, out_fd, is_v2):
if self.taproot_key_sig:
wr(PSBT_IN_TAP_KEY_SIG, self.taproot_key_sig)

if self.ae_added_nonce:
pubkey, nonce = self.ae_added_nonce
wr(PSBT_PROPRIETARY, nonce, b'\x02ae\x01' + pubkey)

if self.sighash is not None:
wr(PSBT_IN_SIGHASH_TYPE, pack('<I', self.sighash))

Expand Down Expand Up @@ -2860,6 +2865,30 @@ def sign_it(self, alternate_secret=None, my_xfp=None):
if my_xfp is None:
my_xfp = self.my_xfp

# Validate the complete protected request before deriving a nonce or
# signing any input. The host independently freezes the transaction
# across rounds; deterministic nonce derivation binds our key/message/h.
from anti_exfil import read_fields
read_fields(self, allowed=False)
for output in self.outputs:
read_fields(output, allowed=False)
ae_phase = None
for inp in self.inputs:
request = read_fields(inp)
if request is None:
continue
pubkey, fields = request
assert inp.af == AF_P2TR and inp.sp_idxs, 'Anti-exfil needs owned Taproot input'
assert inp.utxo_spk == b'\x51\x20' + pubkey, 'Anti-exfil output key'
assert inp.sighash in (None, SIGHASH_DEFAULT, SIGHASH_ALL), 'Anti-exfil sighash'
assert not inp.taproot_key_sig and not inp.taproot_script_sigs, 'Signed anti-exfil target'
assert not inp.is_musig and not self.session, 'Anti-exfil is single signer only'
assert not inp.is_miniscript and not inp.taproot_merkle_root, 'Anti-exfil currently requires BIP86'
phase = 2 if 2 in fields else 1
assert ae_phase is None or ae_phase == phase, 'Mixed anti-exfil rounds'
ae_phase = phase
inp.ae_request = (pubkey, fields, inp.sighash)

musig_session = None
session_id = None
session_rand = None
Expand All @@ -2880,7 +2909,7 @@ def sign_it(self, alternate_secret=None, my_xfp=None):

store_session = False
try:
self._sign_it(alternate_secret, my_xfp, musig_session)
self._sign_it(alternate_secret, my_xfp, musig_session, ae_phase)
store_session = self.allow_cache_store is True
finally:
if musig_session:
Expand All @@ -2889,7 +2918,7 @@ def sign_it(self, alternate_secret=None, my_xfp=None):
else:
stash.blank_object(session_rand)

def _sign_it(self, alternate_secret, my_xfp, musig_session):
def _sign_it(self, alternate_secret, my_xfp, musig_session, ae_phase):
# txn is approved. sign all inputs we can sign. add signatures
# - hash the txn first
# - sign all inputs we have the key for
Expand Down Expand Up @@ -2955,6 +2984,10 @@ def _sign_it(self, alternate_secret, my_xfp, musig_session):

inp = self.inputs[in_idx]

if ae_phase is not None and inp.ae_request is None:
# A protected request must not add unprotected signatures.
continue

if not inp.has_utxo():
# maybe they didn't provide the UTXO
continue
Expand Down Expand Up @@ -3165,7 +3198,20 @@ def _sign_it(self, alternate_secret, my_xfp, musig_session):
if sv.deltamode:
digest = ngu.hash.sha256d(digest)

sig = ngu.secp256k1.sign_schnorr(kpt, digest, ngu.random.bytes(32))
if inp.ae_request:
pubkey, fields, declared_sighash = inp.ae_request
assert kpt.xonly_pubkey().to_bytes() == pubkey, 'Anti-exfil signing key'
if ae_phase == 1:
nonce = ngu.secp256k1.anti_exfil_commit(kpt, digest, fields[0])
inp.ae_added_nonce = (pubkey, nonce)
del kpt, kp
stash.blank_object(sk)
stash.blank_object(node)
del sk, node
continue
sig = ngu.secp256k1.anti_exfil_sign(kpt, digest, fields[0], fields[1], fields[2])
else:
sig = ngu.secp256k1.sign_schnorr(kpt, digest, ngu.random.bytes(32))
if inp.sighash != SIGHASH_DEFAULT:
sig += bytes([inp.sighash])

Expand Down Expand Up @@ -3266,7 +3312,10 @@ def _sign_it(self, alternate_secret, my_xfp, musig_session):
if self.is_v2:
self.set_modifiable_flag(inp)

if drop_sighash:
if inp.ae_request:
# Preserve both explicit DEFAULT and an absent declaration.
inp.sighash = inp.ae_request[2]
elif drop_sighash:
# only drop after modifiable is set, in case of PSBTv2
# SIGHASH_DEFAULT if taproot
# SIGHASH_ALL if non-taproot
Expand Down
45 changes: 45 additions & 0 deletions testing/devtest/anti_exfil_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# (c) Copyright 2026 by Sjors Provoost. This file is covered by license found in COPYING-CC.
# Run with the firmware's native MicroPython, with shared/ in MICROPYPATH.
from anti_exfil import read_fields, PREFIX

class Proxy:
def __init__(self, pairs):
self.unknown = pairs

def get(self, value):
return value

pubkey = b'q' * 32
other = b'p' * 32
h = (PREFIX + b'\x00' + pubkey, b'h' * 32)
r = (PREFIX + b'\x01' + pubkey, b'\x02' + b'r' * 32)
rho = (PREFIX + b'\x02' + pubkey, b'r' * 32)
assert read_fields(Proxy([h])) == (pubkey, {0: h[1]})
assert read_fields(Proxy([rho, r, h])) == (pubkey, {0: h[1], 1: r[1], 2: rho[1]})
assert read_fields(Proxy([(b'\xfc\x03xyz\x00', b'unknown')])) is None

bad = [
[r], [rho], [h, r], [h, rho], [h, h],
[(h[0], b'h' * 31)], [(r[0], b'r' * 32), h, rho],
[(PREFIX + b'\x03' + pubkey, b'h' * 32)],
[(b'\xfc\xfd\x02\x00ae\x00' + pubkey, h[1])],
[(PREFIX + b'\xfd\x00\x00' + pubkey, h[1])],
[(PREFIX + b'\x00' + pubkey[:-1], h[1])],
[h, (PREFIX + b'\x01' + other, r[1]), rho],
[h, (b'\x07', b'final')], [h, (b'\x08', b'final')],
]
for pairs in bad:
try:
read_fields(Proxy(pairs))
except AssertionError:
pass
else:
raise AssertionError('Invalid anti-exfil fields accepted')
for pairs in ([h], [h, r, rho]):
try:
read_fields(Proxy(pairs), allowed=False)
except AssertionError:
pass
else:
raise AssertionError('Anti-exfil fields accepted outside input')
print('anti-exfil: parser states, encodings, locations, duplicates passed')