@@ -143,14 +143,18 @@ static cx_sha3_t *get_last_hash_ctx(void) {
143
143
* Finalize the last hashing context
144
144
*
145
145
* @param[out] hash pointer to buffer where the hash will be stored
146
+ * @return whether there was anything hashed at this depth
146
147
*/
147
- static void finalize_hash_depth (uint8_t * hash ) {
148
+ static bool finalize_hash_depth (uint8_t * hash ) {
148
149
const cx_sha3_t * hash_ctx ;
150
+ size_t hashed_bytes ;
149
151
150
152
hash_ctx = get_last_hash_ctx ();
153
+ hashed_bytes = hash_ctx -> blen ;
151
154
// finalize hash
152
155
cx_hash ((cx_hash_t * ) hash_ctx , CX_LAST , NULL , 0 , hash , KECCAK256_HASH_BYTESIZE );
153
156
mem_dealloc (sizeof (* hash_ctx )); // remove hash context
157
+ return hashed_bytes > 0 ;
154
158
}
155
159
156
160
/**
@@ -192,6 +196,7 @@ static bool push_new_hash_depth(bool init) {
192
196
*/
193
197
static bool path_depth_list_pop (void ) {
194
198
uint8_t hash [KECCAK256_HASH_BYTESIZE ];
199
+ bool to_feed ;
195
200
196
201
if (path_struct == NULL ) {
197
202
return false;
@@ -201,9 +206,11 @@ static bool path_depth_list_pop(void) {
201
206
}
202
207
path_struct -> depth_count -= 1 ;
203
208
204
- finalize_hash_depth (hash );
209
+ to_feed = finalize_hash_depth (hash );
205
210
if (path_struct -> depth_count > 0 ) {
206
- feed_last_hash_depth (hash );
211
+ if (to_feed ) {
212
+ feed_last_hash_depth (hash );
213
+ }
207
214
} else {
208
215
switch (path_struct -> root_type ) {
209
216
case ROOT_DOMAIN :
@@ -261,7 +268,7 @@ static bool array_depth_list_pop(void) {
261
268
return false;
262
269
}
263
270
264
- finalize_hash_depth (hash );
271
+ finalize_hash_depth (hash ); // return value not checked on purpose
265
272
feed_last_hash_depth (hash );
266
273
267
274
path_struct -> array_depth_count -= 1 ;
@@ -307,7 +314,10 @@ static bool path_update(void) {
307
314
}
308
315
feed_last_hash_depth (hash );
309
316
310
- ui_712_queue_struct_to_review ();
317
+ // TODO: Find a better way to show inner structs in verbose mode when it might be
318
+ // an empty array of structs in which case we don't want to show it but the
319
+ // size is only known later
320
+ // ui_712_queue_struct_to_review();
311
321
path_depth_list_push ();
312
322
}
313
323
return true;
@@ -421,6 +431,8 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
421
431
uint8_t total_count = 0 ;
422
432
uint8_t pidx ;
423
433
bool is_custom ;
434
+ uint8_t array_size ;
435
+ uint8_t array_depth_count_bak ;
424
436
425
437
if (path_struct == NULL ) {
426
438
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED ;
@@ -430,6 +442,8 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
430
442
return false;
431
443
}
432
444
445
+ array_size = * data ;
446
+ array_depth_count_bak = path_struct -> array_depth_count ;
433
447
for (pidx = 0 ; pidx < path_struct -> depth_count ; ++ pidx ) {
434
448
if ((field_ptr = get_nth_field (NULL , pidx + 1 )) == NULL ) {
435
449
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED ;
@@ -442,7 +456,7 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
442
456
}
443
457
total_count += depth_count ;
444
458
if (total_count > path_struct -> array_depth_count ) {
445
- if (!check_and_add_array_depth (depth , total_count , pidx , * data )) {
459
+ if (!check_and_add_array_depth (depth , total_count , pidx , array_size )) {
446
460
return false;
447
461
}
448
462
break ;
@@ -463,9 +477,18 @@ bool path_new_array_depth(const uint8_t *const data, uint8_t length) {
463
477
cx_sha3_t * hash_ctx = get_last_hash_ctx ();
464
478
cx_sha3_t * old_ctx = hash_ctx - 1 ;
465
479
466
- memcpy (hash_ctx , old_ctx , sizeof (* old_ctx ));
480
+ if (array_size > 0 ) {
481
+ memcpy (hash_ctx , old_ctx , sizeof (* old_ctx ));
482
+ } else {
483
+ cx_keccak_init (hash_ctx , 256 );
484
+ }
467
485
cx_keccak_init (old_ctx , 256 ); // init hash
468
486
}
487
+ if (array_size == 0 ) {
488
+ do {
489
+ path_advance ();
490
+ } while (path_struct -> array_depth_count != array_depth_count_bak );
491
+ }
469
492
470
493
return true;
471
494
}
@@ -515,7 +538,7 @@ static bool path_advance_in_array(void) {
515
538
516
539
if ((path_struct -> array_depth_count > 0 ) &&
517
540
(arr_depth -> path_index == (path_struct -> depth_count - 1 ))) {
518
- arr_depth -> size -= 1 ;
541
+ if ( arr_depth -> size > 0 ) arr_depth -> size -= 1 ;
519
542
if (arr_depth -> size == 0 ) {
520
543
array_depth_list_pop ();
521
544
end_reached = true;
0 commit comments