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
38 changes: 38 additions & 0 deletions docs/anti-exfil.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Draft Taproot anti-exfiltration
==============================

This experimental firmware implements the single-signer BIP-EXFIL protocol via
``sign_psbt``. ``get_version_info`` reports ``JADE_TAPROOT_ANTI_EXFIL: true``.
Hosts must require that capability before sending an anti-exfil request; released
firmware does not recognize these fields and could sign normally.

The provisional input key is ``fc 02 61 65 <subtype> <32-byte output key>``.
Subtype 0 carries the host commitment, subtype 1 the full compressed signer
nonce, and subtype 2 the host entropy. See the BIP draft for the exact tagged
hashes and encoding. BIP assignment will change the domains and test vectors.

The first invocation carries only commitments and returns public nonces without
signatures. After retaining those nonces, the host adds entropy to a second
invocation. The signer recomputes and checks the original commitment and nonce,
adds the host scalar, and verifies the BIP340 signature before releasing it.
Nothing secret is retained across calls. Retry the identical session; never
restart automatically with fresh entropy after a failure.

Before using a signature, the host must independently select the output key and
BIP341 message, verify the BIP340 signature and ``sig.r == x(R0 + rho*G)``, and
reject changed transaction, UTXO, transcript, sighash, or extra-signature data.
Transport participation alone is not anti-exfil verification. HWI enforces these
host checks around the native PSBT adapter.

This first implementation requires every input to be a BIP86 key-path input
owned by this device and carrying a request for the same round. PSBT v0 and v2,
SIGHASH_DEFAULT and SIGHASH_ALL are supported. Script paths, merkle roots,
pre-existing signatures, mixed rounds, and fields outside input maps are rejected.
The actual tweaked signing public key must match the requested output key.
Each invocation retains the ordinary transaction and fee review.

``python3 test_anti_exfil.py`` compiles the actual bundled libsecp dependency
natively and checks the BIP's shared vectors and transcript rejection. HWI's
``jade_test_suite(..., anti_exfil=True)`` additionally exercises the real QEMU
firmware, reconnects between rounds, submits altered transcripts directly to
firmware, verifies both inputs, and broadcasts and mines the result.
5 changes: 5 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2304,3 +2304,8 @@ Indices and tables
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

.. toctree::
:maxdepth: 1

anti-exfil
3 changes: 2 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ endif()
if(CONFIG_AMALGAMATED_BUILD)
set(wallydir "${PROJECT_DIR}/components/libwally-core")
list(APPEND wallydirs "${wallydir}" "${wallydir}/upstream" "${wallydir}/upstream/src" "${wallydir}/upstream/src/ccan")
set(secpdir "${wallydir}/upstream/src/secp256k1/include")
endif()

if(CONFIG_LOG_WIFI)
list(APPEND extra_priv_requires "esp_wifi")
endif()

set(secpdir "${PROJECT_DIR}/components/libwally-core/upstream/src/secp256k1/include")

idf_component_register(SRC_DIRS "."
"attestation"
"fonts"
Expand Down
1 change: 1 addition & 0 deletions main/amalgamated.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void __wrap_abort(void);
#define HAVE_BUILTIN_POPCOUNT 1
#include "../components/libwally-core/upstream/src/amalgamation/combined.c"

#include "./anti_exfil.c"
#include "./aes.c"
#include "./assets.c"
#include "./attestation/attestation.c"
Expand Down
137 changes: 137 additions & 0 deletions main/anti_exfil.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#ifndef AMALGAMATED_BUILD
#include "anti_exfil.h"

#include <secp256k1_extrakeys.h>
#include <secp256k1_schnorrsig.h>
#include <string.h>

static void ae_clear(void* data, size_t len)
{
volatile unsigned char* p = data;
while (len--) {
*p++ = 0;
}
}

/* SHA256 results are below 2^256, so at most one subtraction is needed.
* Select the reduced scalar without branching on secret nonce material. */
static void ae_reduce(unsigned char scalar[32])
{
static const unsigned char order[32] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41 };
unsigned char reduced[32];
unsigned int borrow = 0;
for (size_t i = 32; i-- > 0;) {
const unsigned int difference = (unsigned int)scalar[i] - order[i] - borrow;
reduced[i] = difference & 0xff;
borrow = (difference >> 8) & 1;
}
const unsigned int mask = 0 - borrow;
for (size_t i = 0; i < 32; ++i) {
scalar[i] = (scalar[i] & mask) | (reduced[i] & ~mask);
}
ae_clear(reduced, sizeof(reduced));
}

typedef struct {
const secp256k1_context* ctx;
const unsigned char* commitment;
const unsigned char* entropy;
} ae_nonce_data;

/* libsecp passes the even-Y normalized secret/public key to this callback. */
static int ae_nonce(unsigned char* nonce32, const unsigned char* msg, size_t msglen,
const unsigned char* key32, const unsigned char* xonly32, const unsigned char* algo,
size_t algolen, void* data)
{
const ae_nonce_data* session = data;
unsigned char aux[32], preimage[96];
int ok = msglen == 32 && algo != NULL && algolen != 0;
if (ok) {
ok = secp256k1_tagged_sha256(session->ctx, aux, (const unsigned char*)"BIP0340/aux", 11,
session->commitment, 32);
}
if (ok) {
for (size_t i = 0; i < 32; ++i) {
preimage[i] = key32[i] ^ aux[i];
}
memcpy(preimage + 32, xonly32, 32);
memcpy(preimage + 64, msg, 32);
ok = secp256k1_tagged_sha256(session->ctx, nonce32, (const unsigned char*)"BIP-EXFIL/nonce", 15,
preimage, sizeof(preimage));
}
if (ok) {
ae_reduce(nonce32);
ok = secp256k1_ec_seckey_verify(session->ctx, nonce32);
}
if (ok && session->entropy) {
ok = secp256k1_ec_seckey_verify(session->ctx, session->entropy)
&& secp256k1_ec_seckey_tweak_add(session->ctx, nonce32, session->entropy);
}
ae_clear(aux, sizeof(aux));
ae_clear(preimage, sizeof(preimage));
if (!ok) {
ae_clear(nonce32, 32);
}
return ok;
}

int jade_anti_exfil_commit(const secp256k1_context* ctx, unsigned char nonce33[33],
const unsigned char secret32[32], const unsigned char msg32[32], const unsigned char commitment32[32])
{
secp256k1_keypair keypair;
secp256k1_xonly_pubkey pubkey;
secp256k1_pubkey nonce;
unsigned char normalized[32], xonly[32], scalar[32];
int parity = 0;
size_t len = 33;
ae_nonce_data data = { ctx, commitment32, NULL };
int ok = secp256k1_keypair_create(ctx, &keypair, secret32)
&& secp256k1_keypair_xonly_pub(ctx, &pubkey, &parity, &keypair)
&& secp256k1_xonly_pubkey_serialize(ctx, xonly, &pubkey);
memcpy(normalized, secret32, 32);
if (ok && parity) {
ok = secp256k1_ec_seckey_negate(ctx, normalized);
}
if (ok) {
ok = ae_nonce(scalar, msg32, 32, normalized, xonly, (const unsigned char*)"BIP0340/nonce", 13, &data)
&& secp256k1_ec_pubkey_create(ctx, &nonce, scalar)
&& secp256k1_ec_pubkey_serialize(ctx, nonce33, &len, &nonce, SECP256K1_EC_COMPRESSED);
}
ae_clear(normalized, sizeof(normalized));
ae_clear(scalar, sizeof(scalar));
ae_clear(&keypair, sizeof(keypair));
if (!ok) {
ae_clear(nonce33, 33);
}
return ok;
}

int jade_anti_exfil_sign(const secp256k1_context* ctx, unsigned char signature64[64],
const unsigned char secret32[32], const unsigned char msg32[32], const unsigned char commitment32[32],
const unsigned char nonce33[33], const unsigned char entropy32[32])
{
unsigned char commitment[32], nonce[33];
secp256k1_keypair keypair;
secp256k1_xonly_pubkey pubkey;
ae_nonce_data data = { ctx, commitment32, entropy32 };
secp256k1_schnorrsig_extraparams params = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
params.noncefp = ae_nonce;
params.ndata = &data;
int ok = secp256k1_ec_seckey_verify(ctx, entropy32)
&& secp256k1_tagged_sha256(ctx, commitment, (const unsigned char*)"BIP-EXFIL/host", 14, entropy32, 32)
&& memcmp(commitment, commitment32, 32) == 0
&& jade_anti_exfil_commit(ctx, nonce, secret32, msg32, commitment32)
&& memcmp(nonce, nonce33, 33) == 0
&& secp256k1_keypair_create(ctx, &keypair, secret32)
&& secp256k1_keypair_xonly_pub(ctx, &pubkey, NULL, &keypair)
&& secp256k1_schnorrsig_sign_custom(ctx, signature64, msg32, 32, &keypair, &params)
&& secp256k1_schnorrsig_verify(ctx, signature64, msg32, 32, &pubkey);
ae_clear(&keypair, sizeof(keypair));
if (!ok) {
ae_clear(signature64, 64);
}
return ok;
}
#endif // AMALGAMATED_BUILD
14 changes: 14 additions & 0 deletions main/anti_exfil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef JADE_ANTI_EXFIL_H
#define JADE_ANTI_EXFIL_H

#include <secp256k1.h>

/* Draft BIP-EXFIL. All keys are actual, already Taproot-tweaked signing keys.
* The full compressed nonce (including parity) must be retained between rounds. */
int jade_anti_exfil_commit(const secp256k1_context* ctx, unsigned char nonce33[33],
const unsigned char secret32[32], const unsigned char msg32[32], const unsigned char commitment32[32]);
int jade_anti_exfil_sign(const secp256k1_context* ctx, unsigned char signature64[64],
const unsigned char secret32[32], const unsigned char msg32[32], const unsigned char commitment32[32],
const unsigned char nonce33[33], const unsigned char entropy32[32]);

#endif
Loading
Loading