@@ -540,8 +540,8 @@ __Z_INLINE zxerr_t copyKeys(keys_t *saplingKeys, key_kind_e requestedKeys, uint8
540
540
return zxerr_ok ;
541
541
}
542
542
543
- zxerr_t crypto_computeSaplingSeed (uint8_t * sk ) {
544
- if (sk == NULL ) {
543
+ zxerr_t crypto_computeSaplingSeed (uint8_t spendingKey [ static KEY_LENGTH ] ) {
544
+ if (spendingKey == NULL ) {
545
545
return zxerr_no_data ;
546
546
}
547
547
@@ -552,10 +552,11 @@ zxerr_t crypto_computeSaplingSeed(uint8_t *sk) {
552
552
HDPATH_LEN_DEFAULT ,
553
553
privateKeyData ,
554
554
NULL , NULL , 0 ));
555
- memcpy (sk , privateKeyData , KEY_LENGTH );
555
+ memcpy (spendingKey , privateKeyData , KEY_LENGTH );
556
556
557
557
catch_cx_error :
558
558
MEMZERO (privateKeyData , sizeof (privateKeyData ));
559
+ MEMZERO (spendingKey , KEY_LENGTH );
559
560
return zxerr_ok ;
560
561
}
561
562
@@ -570,16 +571,16 @@ zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen, key_kind
570
571
keys_t saplingKeys = {0 };
571
572
uint8_t sk [KEY_LENGTH ] = {0 };
572
573
CHECK_ZXERR (crypto_computeSaplingSeed (sk ))
573
- CHECK_PARSER_OK (computeMasterFromSeed (sk , saplingKeys .spendingKey ));
574
+ CHECK_PARSER_OK (computeMasterFromSeed (( const uint8_t * ) sk , saplingKeys .spendingKey ));
574
575
575
576
error = computeKeys (& saplingKeys );
576
577
577
578
// Copy keys
578
579
if (error == zxerr_ok ) {
579
580
error = copyKeys (& saplingKeys , requestedKey , output , outputLen );
580
581
} else {
581
- MEMZERO (sk , sizeof (sk ));
582
- MEMZERO (& saplingKeys , sizeof (saplingKeys ));
582
+ MEMZERO (sk , sizeof (sk ));
583
+ MEMZERO (& saplingKeys , sizeof (saplingKeys ));
583
584
}
584
585
585
586
return error ;
@@ -638,7 +639,7 @@ static parser_error_t h_star(uint8_t *a, uint16_t a_len, uint8_t *b, uint16_t b_
638
639
639
640
return parser_ok ;
640
641
}
641
- static zxerr_t sign_sapling_spend (keys_t * keys , uint8_t * alpha , uint8_t * sign_hash , uint8_t * signature ) {
642
+ static zxerr_t sign_sapling_spend (keys_t * keys , uint8_t alpha [ static KEY_LENGTH ] , uint8_t sign_hash [ static KEY_LENGTH ], uint8_t * signature ) {
642
643
if (alpha == NULL || sign_hash == NULL || signature == NULL ) {
643
644
return zxerr_no_data ;
644
645
}
@@ -679,7 +680,7 @@ static zxerr_t sign_sapling_spend(keys_t *keys, uint8_t *alpha, uint8_t *sign_ha
679
680
return zxerr_ok ;
680
681
}
681
682
682
- zxerr_t crypto_sign_spends_sapling (const parser_tx_t * txObj , uint8_t * output , uint16_t outputLen , uint16_t * responseLen ) {
683
+ zxerr_t crypto_sign_spends_sapling (const parser_tx_t * txObj , keys_t * keys , uint8_t * output , uint16_t outputLen , uint16_t * responseLen ) {
683
684
zemu_log_stack ("crypto_signspends_sapling" );
684
685
if (txObj -> transaction .sections .maspTx .data .sapling_bundle .n_shielded_spends == 0 ) {
685
686
return zxerr_ok ;
@@ -690,13 +691,6 @@ zxerr_t crypto_sign_spends_sapling(const parser_tx_t *txObj, uint8_t *output, ui
690
691
uint8_t sign_hash [HASH_LEN ] = {0 };
691
692
signature_hash (txObj , sign_hash );
692
693
693
- // Get keys to use ask
694
- uint8_t sapling_seed [KEY_LENGTH ] = {0 };
695
- keys_t keys = {0 };
696
- CHECK_ZXERR (crypto_computeSaplingSeed (sapling_seed ));
697
- CHECK_PARSER_OK (computeMasterFromSeed (sapling_seed , keys .spendingKey ));
698
- CHECK_ZXERR (computeKeys (& keys ));
699
-
700
694
uint8_t signature [2 * HASH_LEN ] = {0 };
701
695
uint8_t alpha [KEY_LENGTH ] = {0 };
702
696
const uint8_t * spend = txObj -> transaction .sections .maspBuilder .builder .sapling_builder .spends .ptr ;
@@ -707,7 +701,7 @@ zxerr_t crypto_sign_spends_sapling(const parser_tx_t *txObj, uint8_t *output, ui
707
701
spend += spendLen ;
708
702
MEMCPY (alpha , spend + ALPHA_OFFSET , KEY_LENGTH );
709
703
710
- CHECK_ZXERR (sign_sapling_spend (& keys , alpha , sign_hash , signature ));
704
+ CHECK_ZXERR (sign_sapling_spend (keys , alpha , sign_hash , signature ));
711
705
712
706
// Copy signature to output
713
707
MEMCPY (output + i * MASP_SIG_LEN , signature , MASP_SIG_LEN );
@@ -807,21 +801,15 @@ zxerr_t checkConverts(const parser_tx_t *txObj) {
807
801
return zxerr_ok ;
808
802
}
809
803
810
- zxerr_t crypto_check_masp (const parser_tx_t * txObj ) {
811
- if (txObj == NULL ) {
804
+ zxerr_t crypto_check_masp (const parser_tx_t * txObj , keys_t * keys ) {
805
+ if (txObj == NULL || keys == NULL ) {
812
806
return zxerr_unknown ;
813
807
}
814
- // Get keys to use ask
815
- uint8_t sapling_seed [KEY_LENGTH ] = {0 };
816
- keys_t keys = {0 };
817
- CHECK_ZXERR (crypto_computeSaplingSeed (sapling_seed ));
818
- CHECK_PARSER_OK (computeMasterFromSeed (sapling_seed , keys .spendingKey ));
819
- CHECK_ZXERR (computeKeys (& keys ));
820
808
821
809
#if !defined(LEDGER_SPECIFIC )
822
810
// For now verify cv and rk https://github.com/anoma/masp/blob/main/masp_proofs/src/sapling/prover.rs#L278
823
811
// Check Spends
824
- CHECK_ZXERR (checkSpends (txObj , & keys ));
812
+ CHECK_ZXERR (checkSpends (txObj , keys ));
825
813
826
814
// Check outputs
827
815
CHECK_ZXERR (checkOutputs (txObj ));
@@ -837,11 +825,23 @@ zxerr_t crypto_sign_masp(const parser_tx_t *txObj, uint8_t *output, uint16_t out
837
825
return zxerr_unknown ;
838
826
}
839
827
840
- CHECK_ZXERR (crypto_check_masp (txObj ));
828
+ // Get keys
829
+ uint8_t sapling_seed [KEY_LENGTH ] = {0 };
830
+ keys_t keys = {0 };
831
+ CHECK_ZXERR (crypto_computeSaplingSeed (sapling_seed ));
832
+ CHECK_PARSER_OK (computeMasterFromSeed (sapling_seed , keys .spendingKey ));
833
+
834
+ if (computeKeys (& keys ) != zxerr_ok || crypto_check_masp (txObj , & keys ) != zxerr_ok ) {
835
+ MEMZERO (sapling_seed , sizeof (sapling_seed ));
836
+ MEMZERO (& keys , sizeof (keys ));
837
+ return zxerr_invalid_crypto_settings ;
838
+ }
841
839
842
840
// Sign Sapling spends
843
- CHECK_ZXERR (crypto_sign_spends_sapling (txObj , output , outputLen , responseLen ));
841
+ CHECK_ZXERR (crypto_sign_spends_sapling (txObj , & keys , output , outputLen , responseLen ));
844
842
843
+ MEMZERO (sapling_seed , sizeof (sapling_seed ));
844
+ MEMZERO (& keys , sizeof (keys ));
845
845
return zxerr_ok ;
846
846
}
847
847
@@ -863,10 +863,10 @@ zxerr_t crypto_computeRandomness(const uint8_t *buffer, uint16_t bufferLen, uint
863
863
}
864
864
MEMZERO (out , outLen );
865
865
zemu_log_stack ("crypto_computeRandomness" );
866
- uint8_t spend_len = buffer [0 ];
867
- uint8_t output_len = buffer [1 ];
868
- uint8_t convert_len = buffer [2 ];
869
- uint8_t tmp_rnd [32 ] = {0 };
866
+ const uint8_t spend_len = buffer [0 ];
867
+ const uint8_t output_len = buffer [1 ];
868
+ const uint8_t convert_len = buffer [2 ];
869
+ uint8_t tmp_rnd [KEY_LENGTH ] = {0 };
870
870
871
871
zemu_log_stack ("crypto_computeRandomness" );
872
872
transaction_add_sizes (spend_len , output_len , convert_len );
0 commit comments