Skip to content

Commit f6e67fe

Browse files
authored
Add more heart beats (#117)
* Add more heartbeats in sapling path and some other intensive crypto operations * Bump version and update snapshots
1 parent 9299a42 commit f6e67fe

File tree

11 files changed

+27
-1
lines changed

11 files changed

+27
-1
lines changed

app/Makefile.version

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ APPVERSION_M=4
33
# This is the minor version
44
APPVERSION_N=1
55
# This is the patch version
6-
APPVERSION_P=1
6+
APPVERSION_P=2

app/rust/src/zip32_extern.rs

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub extern "C" fn get_pkd_from_seed(
150150
div_ptr: *mut Diversifier,
151151
pkd_ptr: *mut [u8; 32],
152152
) {
153+
crate::bolos::heartbeat();
153154
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, account];
154155
let start = unsafe { &mut *start_diversifier };
155156
let div_out = unsafe { &mut *div_ptr };
@@ -158,8 +159,10 @@ pub extern "C" fn get_pkd_from_seed(
158159
let dk = key_bundle.dk();
159160

160161
div_out.copy_from_slice(&zip32::diversifier_find_valid(&dk, start));
162+
crate::bolos::heartbeat();
161163

162164
let ivk = sapling_asknsk_to_ivk(&key_bundle.ask(), &key_bundle.nsk());
165+
crate::bolos::heartbeat();
163166
let tmp_pkd = zip32::pkd_default(&ivk, div_out);
164167

165168
let pkd_out = unsafe { &mut *pkd_ptr };

app/src/crypto.c

+23
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ typedef struct {
113113

114114
// NOTE: Uses global hdPath / HDPATH_LEN_DEFAULT
115115
static zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
116+
io_seproxyhal_io_heartbeat();
116117
if (pubKey == NULL || pubKeyLen < PK_LEN_SECP256K1) {
117118
return zxerr_invalid_crypto_settings;
118119
}
@@ -128,6 +129,7 @@ static zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
128129
zxerr_t error = zxerr_unknown;
129130
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, hdPath.secp256k1_path, HDPATH_LEN_BIP44, privateKeyData, NULL));
130131
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1, privateKeyData, SK_SECP256K1_SIZE, &cx_privateKey));
132+
io_seproxyhal_io_heartbeat();
131133
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_256K1, NULL, 0, &cx_publicKey));
132134
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1));
133135

@@ -149,6 +151,7 @@ static zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
149151
// handleGetAddrSecp256K1
150152
// NOTE: Uses global hdPath / HDPATH_LEN_DEFAULT (indirectly)
151153
zxerr_t crypto_fillAddress_secp256k1(uint8_t *buffer, uint16_t buffer_len, uint16_t *replyLen) {
154+
io_seproxyhal_io_heartbeat();
152155
if (buffer_len < sizeof(answer_t)) {
153156
return zxerr_unknown;
154157
}
@@ -160,6 +163,7 @@ zxerr_t crypto_fillAddress_secp256k1(uint8_t *buffer, uint16_t buffer_len, uint1
160163
answer_t *const answer = (answer_t *)buffer;
161164

162165
CHECK_ZXERR(crypto_extractPublicKey(answer->publicKey, sizeof_field(answer_t, publicKey)));
166+
io_seproxyhal_io_heartbeat();
163167

164168
address_temp_t address_temp;
165169

@@ -179,6 +183,7 @@ zxerr_t crypto_fillAddress_secp256k1(uint8_t *buffer, uint16_t buffer_len, uint1
179183
// 7. 25 bytes BTC address = [extended ripemd-160][checksum]
180184
// Encode as base58
181185
size_t outLen = sizeof_field(answer_t, address);
186+
io_seproxyhal_io_heartbeat();
182187
int err =
183188
encode_base58(address_temp.address, VERSION_SIZE + CX_RIPEMD160_SIZE + CHECKSUM_SIZE, answer->address, &outLen);
184189
if (err != 0) {
@@ -224,6 +229,7 @@ zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed) {
224229
// handleInitTX step 1/2
225230
zxerr_t crypto_extracttx_sapling(uint8_t *buffer, uint16_t bufferLen, const uint8_t *txdata, const uint16_t txdatalen) {
226231
ZEMU_LOGF(100, "crypto_extracttx_sapling\n");
232+
io_seproxyhal_io_heartbeat();
227233

228234
MEMZERO(buffer, bufferLen);
229235
uint8_t t_in_len = *txdata;
@@ -275,6 +281,7 @@ zxerr_t crypto_extracttx_sapling(uint8_t *buffer, uint16_t bufferLen, const uint
275281
}
276282
start += T_IN_INPUT_LEN;
277283
}
284+
io_seproxyhal_io_heartbeat();
278285

279286
for (int i = 0; i < t_out_len; i++) {
280287
uint8_t *addr = (uint8_t *)(start + INDEX_INPUT_TOUT_ADDR);
@@ -292,6 +299,7 @@ zxerr_t crypto_extracttx_sapling(uint8_t *buffer, uint16_t bufferLen, const uint
292299
}
293300
start += T_OUT_INPUT_LEN;
294301
}
302+
io_seproxyhal_io_heartbeat();
295303

296304
for (int i = 0; i < spend_len; i++) {
297305
pars_ctx.offset = 0;
@@ -325,6 +333,7 @@ zxerr_t crypto_extracttx_sapling(uint8_t *buffer, uint16_t bufferLen, const uint
325333
}
326334
start += SPEND_INPUT_LEN;
327335
}
336+
io_seproxyhal_io_heartbeat();
328337

329338
for (int i = 0; i < output_len; i++) {
330339
uint8_t *div = start + INDEX_INPUT_OUTPUTDIV;
@@ -362,6 +371,7 @@ zxerr_t crypto_extracttx_sapling(uint8_t *buffer, uint16_t bufferLen, const uint
362371
}
363372
start += OUTPUT_INPUT_LEN;
364373
}
374+
io_seproxyhal_io_heartbeat();
365375

366376
uint64_t tx_value__flash = get_totalvalue();
367377
#ifdef HAVE_ZIP0317
@@ -430,6 +440,7 @@ zxerr_t crypto_extract_spend_proofkeyandrnd(uint8_t *buffer, uint16_t bufferLen)
430440

431441
// handleExtractOutputData
432442
zxerr_t crypto_extract_output_rnd(uint8_t *buffer, uint16_t bufferLen, uint16_t *replyLen) {
443+
io_seproxyhal_io_heartbeat();
433444
if (!outputlist_more_extract()) {
434445
return zxerr_unknown;
435446
}
@@ -469,6 +480,7 @@ zxerr_t crypto_check_prevouts(uint8_t *buffer, uint16_t bufferLen, const uint8_t
469480
if (get_state() != STATE_CHECKING_ALL_TXDATA) {
470481
return zxerr_unknown;
471482
}
483+
io_seproxyhal_io_heartbeat();
472484

473485
uint8_t hash[HASH_SIZE] = {0};
474486
size_t prevouts_hash_offset = 0;
@@ -499,6 +511,7 @@ zxerr_t crypto_check_sequence(uint8_t *buffer, uint16_t bufferLen, const uint8_t
499511

500512
uint8_t hash[HASH_SIZE] = {0};
501513
size_t sequence_hash_offset = 0;
514+
io_seproxyhal_io_heartbeat();
502515

503516
if (tx_version == TX_VERSION_SAPLING) {
504517
sapling_transparent_sequence_hash(txdata, hash);
@@ -708,6 +721,7 @@ zxerr_t crypto_checkspend_sapling(
708721
}
709722

710723
// NOTE: This use is probably correct
724+
io_seproxyhal_io_heartbeat();
711725
compute_nullifier(tmp_buf->ncm_full, notepos, tmp.step4.nsk, tmp_buf->nf);
712726
if (MEMCMP(tmp_buf->nf, start_spenddata + INDEX_SPEND_NF + i * SPEND_TX_LEN, NULLIFIER_SIZE) != 0) {
713727
CHECK_ZXERROR_AND_CLEAN(zxerr_unknown)
@@ -903,6 +917,7 @@ typedef struct {
903917
// handleCheckandSign step 8/11
904918
zxerr_t crypto_checkencryptions_sapling(uint8_t *buffer, uint16_t bufferLen, const uint8_t *txdata) {
905919
zemu_log_stack("crypto_checkencryptions_sapling");
920+
io_seproxyhal_io_heartbeat();
906921
MEMZERO(buffer, bufferLen);
907922
tmp_enc *tmp = (tmp_enc *)buffer;
908923

@@ -927,6 +942,7 @@ zxerr_t crypto_checkencryptions_sapling(uint8_t *buffer, uint16_t bufferLen, con
927942
// get shared key (used as encryption key) from esk, epk and pkd
928943
ka_to_key(tmp->step1.esk, (uint8_t *)item->pkd, tmp->step1.epk, tmp->step2.sharedkey);
929944
CHECK_APP_CANARY()
945+
io_seproxyhal_io_heartbeat();
930946
// encode (div, value rseed and memotype) into step2.compactout ready to be
931947
// encrypted
932948
prepare_compact_note((uint8_t *)item->div, item->value, (uint8_t *)item->rseed, item->memotype,
@@ -938,6 +954,7 @@ zxerr_t crypto_checkencryptions_sapling(uint8_t *buffer, uint16_t bufferLen, con
938954
// step2.compactoutput (reusing the same memory for input and output)
939955
chacha(tmp->step2.compactout, tmp->step2.compactout, COMPACT_OUT_SIZE, tmp->step2.sharedkey, tmp->step2.chachanonce,
940956
1);
957+
io_seproxyhal_io_heartbeat();
941958
CHECK_APP_CANARY()
942959
// check that the computed encryption is the same as that provided in the
943960
// transaction data
@@ -997,6 +1014,7 @@ zxerr_t crypto_checkencryptions_sapling(uint8_t *buffer, uint16_t bufferLen, con
9971014
// that provided in the transaction data
9981015
chacha(tmp->step4b.encciph, tmp->step4b.encciph, ENC_CIPHER_SIZE, tmp->step4b.outkey, tmp->step4b.chachanonce,
9991016
1);
1017+
io_seproxyhal_io_heartbeat();
10001018
if (MEMCMP(tmp->step4b.encciph, start_outputdata + INDEX_OUTPUT_OUT + i * OUTPUT_TX_LEN, ENC_CIPHER_SIZE) != 0) {
10011019
return zxerr_unknown;
10021020
}
@@ -1135,6 +1153,7 @@ zxerr_t crypto_sign_and_check_transparent(
11351153
size_t signatureLen = DER_MAX_SIZE;
11361154
CATCH_CXERROR(cx_ecdsa_sign_no_throw(&cx_privateKey, CX_RND_RFC6979 | CX_LAST, CX_SHA256, message_digest,
11371155
CX_SHA256_SIZE, signature->step1.der_signature, &signatureLen, &info));
1156+
io_seproxyhal_io_heartbeat();
11381157

11391158
if (convertDERtoRSV(signature->step1.der_signature, info, signature->step1.r, signature->step1.s,
11401159
&signature->step1.v) != no_error ||
@@ -1262,6 +1281,7 @@ zxerr_t crypto_ivk_sapling(uint8_t *buffer, uint16_t bufferLen, uint32_t zip32_a
12621281

12631282
tmp_sapling_ivk_and_default_div *out = (tmp_sapling_ivk_and_default_div *)buffer;
12641283
MEMZERO(buffer, bufferLen);
1284+
io_seproxyhal_io_heartbeat();
12651285

12661286
CHECK_APP_CANARY()
12671287
// get incomming viewing key
@@ -1270,6 +1290,7 @@ zxerr_t crypto_ivk_sapling(uint8_t *buffer, uint16_t bufferLen, uint32_t zip32_a
12701290
CHECK_APP_CANARY()
12711291
// get default diversifier for start index 0
12721292
diversifier_find_valid(zip32_account, out->default_div);
1293+
io_seproxyhal_io_heartbeat();
12731294

12741295
CHECK_APP_CANARY()
12751296
*replyLen = IVK_SIZE + DIV_SIZE;
@@ -1421,6 +1442,7 @@ zxerr_t crypto_fillAddress_sapling(uint8_t *buffer, uint16_t bufferLen, uint32_t
14211442
if (bufferLen < sizeof(tmp_buf_addr_s)) {
14221443
return zxerr_unknown;
14231444
}
1445+
io_seproxyhal_io_heartbeat();
14241446

14251447
zemu_log_stack("crypto_fillAddress_sapling");
14261448
tmp_buf_addr_s *const out = (tmp_buf_addr_s *)buffer;
@@ -1429,6 +1451,7 @@ zxerr_t crypto_fillAddress_sapling(uint8_t *buffer, uint16_t bufferLen, uint32_t
14291451
get_pkd_from_seed(p, out->startindex, out->diversifier, out->pkd);
14301452
MEMZERO(out + DIV_SIZE, MAX_SIZE_BUF_ADDR - DIV_SIZE);
14311453
CHECK_APP_CANARY()
1454+
io_seproxyhal_io_heartbeat();
14321455

14331456
if (bech32EncodeFromBytes(out->address_bech32, sizeof_field(tmp_buf_addr_s, address_bech32), BECH32_HRP,
14341457
out->address_raw, sizeof_field(tmp_buf_addr_s, address_raw), 1,
231 Bytes
Loading
10 Bytes
Loading
10 Bytes
Loading
20 Bytes
Loading
20 Bytes
Loading
216 Bytes
Loading
20 Bytes
Loading
20 Bytes
Loading

0 commit comments

Comments
 (0)