-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapdu-signature.c
1286 lines (1042 loc) · 33 KB
/
apdu-signature.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* apdu-signature.c - transaction parsing & signing for hns
* Copyright (c) 2018, Boyma Fahnbulleh (MIT License).
* https://github.com/handshake-org/ledger-app-hns
*/
#include <stdbool.h>
#include <string.h>
#include "apdu.h"
#include "ledger.h"
#include "utils.h"
/**
* These constants are used to determine the contents of P1.
*/
#define P1_INIT_MASK 0x01 /* xx1 */
#define P1_NETWORK_MASK 0x06 /* 11x */
#define NO 0x00
#define YES 0x01
/**
* These constants are used to determine which operation
* mode is indicated by P2.
*/
#define PARSE 0x00
#define SIGN 0x01
/**
* These constants are used to determine which transaction
* detail is currently being parsed.
*/
#define PREVOUT 0x00
#define SEQUENCE 0x01
#define INPUT_VALUE 0x02
#define OUTPUT_VALUE 0x03
#define ADDR_VERSION 0x04
#define ADDR_HASH_LEN 0x05
#define ADDR_HASH 0x06
#define COVENANT_TYPE 0x07
#define COVENANT_ITEMS_LEN 0x08
#define COVENANT_ITEMS 0x09
/**
* These constants are used to determine sighash types for
* the input signatures.
*/
#define ZERO 0x00
#define SIGHASH_ALL 0x01
#define SIGHASH_NONE 0x02
#define SIGHASH_SINGLE 0x03
#define SIGHASH_SINGLEREVERSE 0x04
#define SIGHASH_NOINPUT 0x40
#define SIGHASH_ANYONECANPAY 0x80
/**
* These constants are used to determine the change address flag.
*/
#define NO_CHANGE_ADDR 0x00
#define P2PKH_CHANGE_ADDR 0x01
#define P2SH_CHANGE_ADDR 0x02
/**
* These constants are used across all covenants.
*/
#define NAME_HASH 0x00
#define HEIGHT 0x01
/**
* These constants are used to determine the OPEN covenant items.
*/
#define OPEN_NAME 0x02
/**
* These constants are used to determine the BID covenant items.
*/
#define BID_NAME 0x02
#define BID_HASH 0x03
/**
* These constants are used to determine the REVEAL covenant items.
*/
#define REVEAL_NONCE 0x02
#define REVEAL_NAME 0x03
/**
* These constants are used to determine the REDEEM covenant items.
*/
#define REDEEM_NAME 0x02
/**
* These constants are used to determine the REGISTER covenant items.
*/
#define REGISTER_RESOURCE_LEN 0x02
#define REGISTER_RESOURCE 0x03
#define REGISTER_HASH 0x04
#define REGISTER_NAME 0x05
/**
* These constants are used to determine the UPDATE covenant items.
*/
#define UPDATE_RESOURCE_LEN 0x02
#define UPDATE_RESOURCE 0x03
#define UPDATE_NAME 0x04
/**
* These constants are used to determine the RENEW covenant items.
*/
#define RENEW_HASH 0x02
#define RENEW_NAME 0x03
/**
* These constants are used to determine the TRANSFER covenant items.
*/
#define ADDRESS_VER 0x02
#define ADDRESS_HASH 0x03
#define TRANSFER_NAME 0x04
/**
* These constants are used to determine the FINALIZE covenant items.
*/
#define FINALIZE_NAME 0x02
#define FLAGS 0x03
#define CLAIM_HEIGHT 0x04
#define RENEWAL_COUNT 0x05
#define FINALIZE_HASH 0x06
/**
* These constants are used to determine the REVOKE covenant items.
*/
#define REVOKE_NAME 0x02
/* Context used to handle the device's UI. */
static ledger_ui_ctx_t *ui = NULL;
/* Context used to handle parsing and signing state. */
static hns_tx_t ctx;
/* General purpose hashing context. */
static ledger_blake2b_ctx blake1;
/* General purpose hashing context. */
static ledger_blake2b_ctx blake2;
/**
* Parses an item from the covenant items list
* and adds it to the provided hash context.
* Upon completion, the item counter is increased.
*
* In:
* @param item_sz is the expected size of the item (bytes).
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param item is the parsed item.
* @param hash is the blake2b hash context.
* @returns a boolean indicating success or failure.
*/
static inline bool
parse_item(
volatile uint8_t **buf,
uint16_t *len,
uint8_t *item,
size_t item_sz,
ledger_blake2b_ctx *hash
) {
uint8_t item_len;
if (!read_varbytes(buf, len, item, item_sz, (size_t *)&item_len))
return false;
if (item_len != item_sz)
THROW(HNS_INCORRECT_PARSER_STATE);
ledger_blake2b_update(hash, &item_len, 1);
ledger_blake2b_update(hash, item, item_len);
ctx.next_item++;
return true;
}
/**
* Parses an address from the covenant items list
* and adds it to the provided hash context. Upon
* completion, the item counter is increased.
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param addr_hash is the parsed address hash.
* @param addr_len is the parsed address length.
* @param hash is the blake2b hash context.
* @returns a boolean indicating success or failure.
*/
static inline bool
parse_addr(
volatile uint8_t **buf,
uint16_t *len,
uint8_t *addr_hash,
uint8_t *addr_len,
ledger_blake2b_ctx *hash
){
uint8_t a[32];
uint8_t alen;
if (!read_varbytes(buf, len, a, 32, (size_t *)&alen))
return false;
ledger_blake2b_update(hash, &alen, 1);
ledger_blake2b_update(hash, a, alen);
memmove(addr_hash, a, alen);
*addr_len = alen;
ctx.next_item++;
return true;
}
/**
* Parses a name from the covenant items list
* and adds it to the provided hash context. Upon
* completion, the item counter is increased.
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param name is the parsed name.
* @param name_len is the parsed name length.
* @param hash is the blake2b hash context.
* @returns a boolean indicating success or failure.
*/
static inline bool
parse_name(
volatile uint8_t **buf,
uint16_t *len,
char *name,
uint8_t *name_len,
ledger_blake2b_ctx *hash
) {
uint8_t n[64];
uint8_t nlen;
if (!read_varbytes(buf, len, n, 63, (size_t *)&nlen))
return false;
if (nlen < 1 || nlen > 63)
THROW(HNS_INCORRECT_NAME_LEN);
n[nlen] = '\0';
ledger_blake2b_update(hash, &nlen, 1);
ledger_blake2b_update(hash, n, nlen);
strcpy(name, (char *)n);
*name_len = nlen;
ctx.next_item++;
return true;
}
/**
* Parses a name from the serialized tx and
* compares it against the name hash in the
* covenant items list.
*
* In:
* @param name_hash is the sha3 hash of the name.
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param name is the parsed name.
* @param name_len is the parsed name length.
* @returns a boolean indicating success or failure.
*/
static inline bool
cmp_name(
volatile uint8_t **buf,
uint16_t *len,
uint8_t *name_hash,
char *name,
uint8_t *name_len
) {
uint8_t n[64];
size_t nlen;
uint8_t digest[32];
if (!read_varbytes(buf, len, n, 63, &nlen))
return false;
if (nlen < 1 || nlen > 63)
THROW(HNS_INCORRECT_NAME_LEN);
if (!ledger_sha3(n, nlen, digest))
THROW(HNS_CANNOT_CREATE_COVENANT_NAME_HASH);
if (memcmp(name_hash, digest, 32) != 0)
THROW(HNS_COVENANT_NAME_HASH_MISMATCH);
n[nlen] = '\0';
strcpy(name, (char *)n);
*name_len = nlen;
ctx.next_item++;
return true;
}
/**
* Parses the length of the resource bytes from
* the covenant items list and adds it to the
* hash context. Upon completion, the item counter
* is increased.
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param ctr is the length of the resource.
* @param hash is the blake2b hash context.
* @returns a boolean indicating success or failure.
*/
static inline bool
parse_resource_len(
volatile uint8_t **buf,
uint16_t *len,
hns_varint_t *ctr,
ledger_blake2b_ctx *hash
) {
if (!peek_varint(buf, len, ctr))
return false;
uint8_t res_len[5] = {0};
uint8_t res_len_size = size_varint(*ctr);
if (!read_bytes(buf, len, res_len, res_len_size))
THROW(HNS_CANNOT_READ_RESOURCE_LEN);
ledger_blake2b_update(hash, res_len, res_len_size);
ctx.next_item++;
return true;
}
/**
* Parses resource bytes from the covenant
* items list and adds them to the hash context.
* Upon completion, the item counter is increased.
*
* Out:
* @param buf is the input buffer.
* @param len is the length of the input buffer.
* @param ctr is the length of the resource.
* @param hash is the blake2b hash context.
* @returns a boolean indicating success or failure.
*/
static inline bool
parse_resource(
volatile uint8_t **buf,
uint16_t *len,
hns_varint_t *ctr,
ledger_blake2b_ctx *hash
) {
hns_varint_t length = *ctr;
if (*ctr > 0) {
if (*ctr > *len)
length = *len;
ledger_blake2b_update(hash, *buf, length);
*buf += length;
*len -= length;
*ctr -= length;
if (*ctr > 0) {
if (*len != 0)
THROW(HNS_INCORRECT_PARSER_STATE);
return false;
}
}
ctx.next_item++;
return true;
}
/**
* Parses transactions details & begins sighash. Will require
* more than one message for serialized transactions longer
* than 255 bytes.
*
* In:
* @param p1 is the first apdu command parameter.
* @param buf is the input buffer.
* @param len is length of input buffer.
*
* Out:
* @param res is the APDU response.
* @param flags holds the apdu exchange buffer flags.
* @return the length of the APDU response.
*/
static inline uint8_t
parse(
uint8_t p1,
uint16_t *len,
volatile uint8_t *buf,
volatile uint8_t *res,
volatile uint8_t *flags
) {
hns_input_t in;
hns_output_t *out = &ctx.curr_output;
ledger_blake2b_ctx *prevs = &blake1;
ledger_blake2b_ctx *seqs = &blake2;
ledger_blake2b_ctx *outs = &blake2; /* Re-initialized before use. */
/**
* If this is an initial APDU message, clear
* the apdu cache and reset the parser's state.
*/
if (p1 & P1_INIT_MASK) {
ledger_apdu_cache_clear();
memset(&ctx, 0, sizeof(hns_tx_t));
if (!read_bytes(&buf, len, ctx.ver, sizeof(ctx.ver)))
THROW(HNS_CANNOT_READ_TX_VERSION);
if (!read_bytes(&buf, len, ctx.locktime, sizeof(ctx.locktime)))
THROW(HNS_CANNOT_READ_TX_LOCKTIME);
if (!read_u8(&buf, len, &ctx.ins_len))
THROW(HNS_CANNOT_READ_INPUTS_LEN);
if (!read_u8(&buf, len, &ctx.outs_len))
THROW(HNS_CANNOT_READ_OUTPUTS_LEN);
/**
* Read change address info. If the change flag is 0x01, we must parse the
* change output's index, and the corresponding address's version and
* derivation path. Otherwise, we move on to the input data.
*
* Due to a max derivation depth of 5, all change address information
* should fit within the first parsing message. Unknown flag values will
* throw an error.
*/
if (!read_u8(&buf, len, &ctx.change_flag))
THROW(HNS_CANNOT_READ_CHANGE_ADDR_FLAG);
switch(ctx.change_flag) {
case P2PKH_CHANGE_ADDR: {
ledger_ecdsa_xpub_t xpub;
uint8_t path_info = 0;
if (!read_u8(&buf, len, &ctx.change_index))
THROW(HNS_CANNOT_READ_CHANGE_OUTPUT_INDEX);
if (!read_u8(&buf, len, &ctx.change.ver))
THROW(HNS_CANNOT_READ_ADDR_VERSION);
if (!read_bip44_path(&buf, len, &xpub.depth, xpub.path, &path_info))
THROW(HNS_CANNOT_READ_BIP44_PATH);
if (path_info & HNS_BIP44_NON_ADDR)
THROW(HNS_INCORRECT_ADDR_PATH);
ledger_ecdsa_derive_xpub(&xpub);
if (ledger_blake2b(xpub.key, 33, ctx.change.hash, 20))
THROW(HNS_CANNOT_INIT_BLAKE2B_CTX);
ctx.change.hash_len = 20;
break;
}
case NO_CHANGE_ADDR:
case P2SH_CHANGE_ADDR:
break;
default:
THROW(HNS_INCORRECT_CHANGE_ADDR_FLAG);
}
ledger_blake2b_init(prevs, 32);
ledger_blake2b_init(seqs, 32);
}
/**
* Assert the parser is in a valid state and update
* the apdu buffer with any data left in the cache.
*/
if (ctx.ins_ctr == ctx.ins_len)
if (ctx.next_field < OUTPUT_VALUE)
THROW(HNS_INCORRECT_PARSER_STATE);
if (ctx.ins_ctr > ctx.ins_len)
THROW(HNS_INCORRECT_PARSER_STATE);
if (ctx.outs_ctr == ctx.outs_len)
if (ctx.next_field <= COVENANT_ITEMS)
THROW(HNS_INCORRECT_PARSER_STATE);
if (ctx.outs_ctr > ctx.outs_len)
THROW(HNS_INCORRECT_PARSER_STATE);
ledger_apdu_cache_flush(len);
/**
* Parse the transaction details.
*/
for (;;) {
bool should_continue = false;
switch(ctx.next_field) {
case PREVOUT: {
if (!read_bytes(&buf, len, in.prev, sizeof(in.prev)))
break;
ledger_blake2b_update(prevs, in.prev, sizeof(in.prev));
ctx.next_field++;
}
case SEQUENCE: {
if (!read_bytes(&buf, len, in.seq, sizeof(in.seq)))
break;
ledger_blake2b_update(seqs, in.seq, sizeof(in.seq));
ctx.next_field++;
}
case INPUT_VALUE: {
uint8_t val[8];
if (!read_bytes(&buf, len, val, 8))
break;
add_u64(ctx.fees, ctx.fees, val);
ctx.next_field++;
if (++ctx.ins_ctr < ctx.ins_len) {
memset(&in, 0, sizeof(hns_input_t));
ctx.next_field = PREVOUT;
should_continue = true;
break;
}
ledger_blake2b_final(prevs, ctx.prevs);
ledger_blake2b_final(seqs, ctx.seqs);
ledger_blake2b_init(outs, 32);
}
/**
* Outputs are variable length and can exceed 512 bytes.
* We hash them immediately to save RAM.
*/
case OUTPUT_VALUE: {
uint8_t *val = out->val;
if (!read_bytes(&buf, len, val, 8))
break;
sub_u64(ctx.fees, ctx.fees, val);
ledger_blake2b_update(outs, val, 8);
ctx.next_field++;
}
case ADDR_VERSION: {
uint8_t *ver = &out->addr.ver;
if (!read_u8(&buf, len, ver))
break;
ledger_blake2b_update(outs, ver, 1);
ctx.next_field++;
}
case ADDR_HASH_LEN: {
uint8_t *hash_len = &out->addr.hash_len;
if (!read_u8(&buf, len, hash_len))
break;
ledger_blake2b_update(outs, hash_len, 1);
ctx.next_field++;
}
case ADDR_HASH: {
hns_addr_t *addr = &out->addr;
if (!read_bytes(&buf, len, addr->hash, addr->hash_len))
break;
ledger_blake2b_update(outs, addr->hash, addr->hash_len);
ctx.next_field++;
}
case COVENANT_TYPE: {
uint8_t *type = &out->cov.type;
if (!read_u8(&buf, len, type))
break;
ledger_blake2b_update(outs, type, 1);
ctx.next_field++;
}
case COVENANT_ITEMS_LEN: {
hns_varint_t *items_len = &out->cov.items_len;
if (!peek_varint(&buf, len, items_len))
break;
uint8_t items_len_buf[5] = {0};
uint8_t items_len_size = size_varint(*items_len);
if (!read_bytes(&buf, len, items_len_buf, items_len_size))
THROW(HNS_CANNOT_READ_COVENANT_ITEMS_LEN);
ledger_blake2b_update(outs, items_len_buf, items_len_size);
ctx.next_field++;
}
/**
* If the output has a covenant type that includes the name in its items
* field, we do not need to verify the name hash. Otherwise, we need to
* verify the name hash before confirming the name on-screen. In this
* case, the client is required to send the name with the other
* covenant details.
*
* Note: the name is not included in the output commitment unless it is
* a part of the covenant's items list.
*/
case COVENANT_ITEMS: {
switch(out->cov.type) {
case HNS_NONE:
break;
/**
* For each subsequent case, the internal
* switch fall-throughs are intentional.
*/
case HNS_OPEN: {
hns_cov_t *c = &out->cov;
hns_open_t *o = &c->items.open;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, o->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, o->height, 4, outs))
goto inner_break;
case OPEN_NAME:
if (!parse_name(&buf, len, c->name, &c->name_len, outs))
goto inner_break;
}
break;
}
case HNS_BID: {
hns_cov_t *c = &out->cov;
hns_bid_t *b = &c->items.bid;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, b->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, b->height, 4, outs))
goto inner_break;
case BID_NAME:
if (!parse_name(&buf, len, c->name, &c->name_len, outs))
goto inner_break;
case BID_HASH:
if (!parse_item(&buf, len, b->hash, 32, outs))
goto inner_break;
}
break;
}
case HNS_REVEAL: {
hns_cov_t *c = &out->cov;
hns_reveal_t *r = &c->items.reveal;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, r->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, r->height, 4, outs))
goto inner_break;
case REVEAL_NONCE:
if (!parse_item(&buf, len, r->nonce, 32, outs))
goto inner_break;
case REVEAL_NAME:
if (!cmp_name(&buf, len, r->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_REDEEM: {
hns_cov_t *c = &out->cov;
hns_redeem_t *r = &c->items.redeem;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, r->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, r->height, 4, outs))
goto inner_break;
case REDEEM_NAME:
if (!cmp_name(&buf, len, r->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_REGISTER: {
hns_cov_t *c = &out->cov;
hns_register_t *r = &c->items.register_cov;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, r->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, r->height, 4, outs))
goto inner_break;
case REGISTER_RESOURCE_LEN:
if (!parse_resource_len(&buf, len, &r->resource_ctr, outs))
goto inner_break;
case REGISTER_RESOURCE:
if (!parse_resource(&buf, len, &r->resource_ctr, outs))
goto inner_break;
case REGISTER_HASH:
if (!parse_item(&buf, len, r->hash, 32, outs))
goto inner_break;
case REGISTER_NAME:
if (!cmp_name(&buf, len, r->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_UPDATE: {
hns_cov_t *c = &out->cov;
hns_update_t *u = &c->items.update;
switch(ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, u->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, u->height, 4, outs))
goto inner_break;
case UPDATE_RESOURCE_LEN:
if (!parse_resource_len(&buf, len, &u->resource_ctr, outs))
goto inner_break;
case UPDATE_RESOURCE:
if (!parse_resource(&buf, len, &u->resource_ctr, outs))
goto inner_break;
case UPDATE_NAME:
if (!cmp_name(&buf, len, u->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_RENEW: {
hns_cov_t *c = &out->cov;
hns_renew_t *r = &c->items.renew;
switch(ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, r->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, r->height, 4, outs))
goto inner_break;
case RENEW_HASH:
if (!parse_item(&buf, len, r->hash, 32, outs))
goto inner_break;
case RENEW_NAME:
if (!cmp_name(&buf, len, r->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_TRANSFER: {
hns_cov_t *c = &out->cov;
hns_transfer_t *t = &c->items.transfer;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, t->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, t->height, 4, outs))
goto inner_break;
case ADDRESS_VER:
if (!parse_item(&buf, len, &t->addr_ver, 1, outs))
goto inner_break;
case ADDRESS_HASH:
if (!parse_addr(&buf, len, t->addr_hash, &t->addr_len, outs))
goto inner_break;
case TRANSFER_NAME:
if (!cmp_name(&buf, len, t->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
case HNS_FINALIZE: {
hns_cov_t *c = &out->cov;
hns_finalize_t *f = &c->items.finalize;
switch(ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, f->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, f->height, 4, outs))
goto inner_break;
case FINALIZE_NAME:
if (!parse_name(&buf, len, c->name, &c->name_len, outs))
goto inner_break;
case FLAGS:
if (!parse_item(&buf, len, &f->flags, 1, outs))
goto inner_break;
case CLAIM_HEIGHT:
if (!parse_item(&buf, len, f->claim_height, 4, outs))
goto inner_break;
case RENEWAL_COUNT:
if (!parse_item(&buf, len, f->renewal_count, 4, outs))
goto inner_break;
case FINALIZE_HASH:
if (!parse_item(&buf, len, f->hash, 32, outs))
goto inner_break;
}
break;
}
case HNS_REVOKE: {
hns_cov_t *c = &out->cov;
hns_revoke_t *r = &c->items.revoke;
switch (ctx.next_item) {
case NAME_HASH:
if (!parse_item(&buf, len, r->name_hash, 32, outs))
goto inner_break;
case HEIGHT:
if (!parse_item(&buf, len, r->height, 4, outs))
goto inner_break;
case REVOKE_NAME:
if (!cmp_name(&buf, len, r->name_hash, c->name, &c->name_len))
goto inner_break;
}
break;
}
default:
THROW(HNS_UNSUPPORTED_COVENANT_TYPE);
}
if (ctx.change_flag == P2PKH_CHANGE_ADDR &&
ctx.change_index == ctx.outs_ctr) {
/**
* We need to verify that the change address details,
* sent by the client, match a key on this device.
*/
if (out->addr.ver != ctx.change.ver)
THROW(HNS_CHANGE_ADDRESS_MISMATCH);
if (out->addr.hash_len != ctx.change.hash_len)
THROW(HNS_CHANGE_ADDRESS_MISMATCH);
if (memcmp(out->addr.hash, ctx.change.hash, out->addr.hash_len) != 0)
THROW(HNS_CHANGE_ADDRESS_MISMATCH);
if (++ctx.outs_ctr < ctx.outs_len) {
ctx.next_field = OUTPUT_VALUE;
ctx.next_item = NAME_HASH;
should_continue = true;
break;
}
} else {
/**
* We need to handle the case in which there is still data left in
* the apdu buffer. Any remaining bytes are sent back to the client.
* We also store the signature ctx on the ui ctx so we can iterate
* through the output items during on-screen confirmation.
*/
ui->ctx = (void *)&ctx;
ui->flags = flags;
ui->buflen = *len;
ui->network = p1 & P1_NETWORK_MASK;
if (ui->buflen != 0) {
ui->buflen = write_u8(&res, *len);
ui->buflen += write_bytes(&res, buf, *len);
}
char *hdr = "Verify";
char *msg = ui->message;
snprintf(msg, 11, "Output #%d", ++(ui->ctr));
if (!ledger_ui_update(LEDGER_UI_OUTPUT, hdr, msg, flags))
THROW(HNS_CANNOT_UPDATE_UI);
if (++ctx.outs_ctr < ctx.outs_len) {
ctx.next_field = OUTPUT_VALUE;
ctx.next_item = NAME_HASH;
return ui->buflen;
}
}
ledger_blake2b_final(outs, ctx.outs);
ctx.tx_parsed = true;
ctx.next_field++;
break;
}
default:
THROW(HNS_INCORRECT_PARSER_STATE);
break;
}
inner_break:
if (should_continue)
continue;
if (*len < 0)
THROW(HNS_INCORRECT_PARSER_STATE);
if (*len > 0)
if(!ledger_apdu_cache_write(buf, *len))
THROW(HNS_INCORRECT_PARSER_STATE);
break;
}
return 0;
};
/**
* Parses the signing key's HD path, the sighash type, and the input details.
* Also parses output data for single output sighash types, then returns a
* signature for the specified input. Will require more than one message for
* scripts longer than 182 bytes (including varint length prefix).
*
* In:
* @param p1 is the first apdu command parameter.
* @param len is length of input buffer.
* @param buf is the input buffer.
*
* Out:
* @param sig is the output buffer.
* @param flags holds the apdu exchange buffer flags.
* @return the length of the APDU response.
*/
static inline uint8_t
sign(
uint8_t p1,
uint16_t *len,
volatile uint8_t *buf,
volatile uint8_t *sig,
volatile uint8_t *flags
) {
if (!ctx.tx_parsed)
THROW(HNS_INCORRECT_PARSER_STATE);
const uint8_t zero_hash[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,