@@ -45,7 +45,7 @@ static bool _is_unlocked_bip39 = false;
45
45
// Stores a random keyy after bip39-unlock which, after stretching, is used to encrypt the retained
46
46
// bip39 seed.
47
47
static uint8_t _unstretched_retained_bip39_seed_encryption_key [32 ] = {0 };
48
- // Must be defined if _is_unlocked is true. ONLY ACCESS THIS WITH _copy_bip39_seed ().
48
+ // Must be defined if _is_unlocked is true. ONLY ACCESS THIS WITH keystore_copy_bip39_seed ().
49
49
// Stores the encrypted BIP-39 seed after bip39-unlock.
50
50
static uint8_t _retained_bip39_seed_encrypted [64 + 64 ] = {0 };
51
51
static size_t _retained_bip39_seed_encrypted_len = 0 ;
@@ -115,14 +115,7 @@ bool keystore_copy_seed(uint8_t* seed_out, size_t* length_out)
115
115
return true;
116
116
}
117
117
118
- /**
119
- * Copies the retained bip39 seed into the given buffer. The caller must
120
- * zero the seed with util_zero once it is no longer needed.
121
- * @param[out] bip39_seed_out The seed bytes copied from the retained bip39 seed.
122
- * The buffer must be 64 bytes long.
123
- * @return true if the bip39 seed is available.
124
- */
125
- static bool _copy_bip39_seed (uint8_t * bip39_seed_out )
118
+ bool keystore_copy_bip39_seed (uint8_t * bip39_seed_out )
126
119
{
127
120
if (!_is_unlocked_bip39 ) {
128
121
return false;
@@ -529,103 +522,6 @@ bool keystore_bip39_mnemonic_to_seed(const char* mnemonic, uint8_t* seed_out, si
529
522
return bip39_mnemonic_to_bytes (NULL , mnemonic , seed_out , 32 , seed_len_out ) == WALLY_OK ;
530
523
}
531
524
532
- static bool _get_xprv (const uint32_t * keypath , const size_t keypath_len , struct ext_key * xprv_out )
533
- {
534
- if (keystore_is_locked ()) {
535
- return false;
536
- }
537
-
538
- uint8_t bip39_seed [64 ] = {0 };
539
- UTIL_CLEANUP_64 (bip39_seed );
540
- if (!_copy_bip39_seed (bip39_seed )) {
541
- return false;
542
- }
543
- struct ext_key xprv_master __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
544
-
545
- if (bip32_key_from_seed (
546
- bip39_seed , BIP32_ENTROPY_LEN_512 , BIP32_VER_MAIN_PRIVATE , 0 , & xprv_master ) !=
547
- WALLY_OK ) {
548
- return false;
549
- }
550
- util_zero (bip39_seed , sizeof (bip39_seed ));
551
- if (keypath_len == 0 ) {
552
- * xprv_out = xprv_master ;
553
- } else if (
554
- bip32_key_from_parent_path (
555
- & xprv_master , keypath , keypath_len , BIP32_FLAG_KEY_PRIVATE , xprv_out ) != WALLY_OK ) {
556
- keystore_zero_xkey (xprv_out );
557
- return false;
558
- }
559
- return true;
560
- }
561
-
562
- static bool _ext_key_equal (struct ext_key * one , struct ext_key * two )
563
- {
564
- if (!MEMEQ (one -> chain_code , two -> chain_code , sizeof (one -> chain_code ))) {
565
- return false;
566
- }
567
- if (!MEMEQ (one -> parent160 , two -> parent160 , sizeof (one -> parent160 ))) {
568
- return false;
569
- }
570
- if (one -> depth != two -> depth ) {
571
- return false;
572
- }
573
- if (!MEMEQ (one -> priv_key , two -> priv_key , sizeof (one -> priv_key ))) {
574
- return false;
575
- }
576
- if (one -> child_num != two -> child_num ) {
577
- return false;
578
- }
579
- if (!MEMEQ (one -> hash160 , two -> hash160 , sizeof (one -> hash160 ))) {
580
- return false;
581
- }
582
- if (one -> version != two -> version ) {
583
- return false;
584
- }
585
- if (!MEMEQ (one -> pub_key , two -> pub_key , sizeof (one -> pub_key ))) {
586
- return false;
587
- }
588
- return true;
589
- }
590
-
591
- static bool _get_xprv_twice (
592
- const uint32_t * keypath ,
593
- const size_t keypath_len ,
594
- struct ext_key * xprv_out )
595
- {
596
- struct ext_key one __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
597
- if (!_get_xprv (keypath , keypath_len , & one )) {
598
- return false;
599
- }
600
- if (!_get_xprv (keypath , keypath_len , xprv_out )) {
601
- return false;
602
- }
603
- if (!_ext_key_equal (& one , xprv_out )) {
604
- keystore_zero_xkey (xprv_out );
605
- return false;
606
- }
607
- return true;
608
- }
609
-
610
- bool keystore_get_xpub (
611
- const uint32_t * keypath ,
612
- const size_t keypath_len ,
613
- struct ext_key * hdkey_neutered_out )
614
- {
615
- struct ext_key xprv __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
616
- if (!_get_xprv_twice (keypath , keypath_len , & xprv )) {
617
- return false;
618
- }
619
- bip32_key_strip_private_key (& xprv ); // neuter
620
- * hdkey_neutered_out = xprv ;
621
- return true;
622
- }
623
-
624
- void keystore_zero_xkey (struct ext_key * xkey )
625
- {
626
- util_zero (xkey , sizeof (struct ext_key ));
627
- }
628
-
629
525
bool keystore_get_bip39_word (uint16_t idx , char * * word_out )
630
526
{
631
527
return bip39_get_word (NULL , idx , word_out ) == WALLY_OK ;
@@ -638,18 +534,17 @@ bool keystore_secp256k1_nonce_commit(
638
534
const uint8_t * host_commitment ,
639
535
uint8_t * signer_commitment_out )
640
536
{
641
- struct ext_key xprv __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
642
- if (!_get_xprv (keypath , keypath_len , & xprv )) {
537
+ uint8_t private_key [32 ] = {0 };
538
+ UTIL_CLEANUP_32 (private_key );
539
+ if (!rust_secp256k1_get_private_key (
540
+ keypath , keypath_len , rust_util_bytes_mut (private_key , sizeof (private_key )))) {
643
541
return false;
644
542
}
543
+
645
544
const secp256k1_context * ctx = wally_get_secp_context ();
646
545
secp256k1_ecdsa_s2c_opening signer_commitment ;
647
546
if (!secp256k1_ecdsa_anti_exfil_signer_commit (
648
- ctx ,
649
- & signer_commitment ,
650
- msg32 ,
651
- xprv .priv_key + 1 , // first byte is 0,
652
- host_commitment )) {
547
+ ctx , & signer_commitment , msg32 , private_key , host_commitment )) {
653
548
return false;
654
549
}
655
550
@@ -670,19 +565,17 @@ bool keystore_secp256k1_sign(
670
565
if (keystore_is_locked ()) {
671
566
return false;
672
567
}
673
- struct ext_key xprv __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
674
- if (!_get_xprv (keypath , keypath_len , & xprv )) {
568
+ uint8_t private_key [32 ] = {0 };
569
+ UTIL_CLEANUP_32 (private_key );
570
+ if (!rust_secp256k1_get_private_key (
571
+ keypath , keypath_len , rust_util_bytes_mut (private_key , sizeof (private_key )))) {
675
572
return false;
676
573
}
574
+
677
575
const secp256k1_context * ctx = wally_get_secp_context ();
678
576
secp256k1_ecdsa_signature secp256k1_sig = {0 };
679
577
if (!secp256k1_anti_exfil_sign (
680
- ctx ,
681
- & secp256k1_sig ,
682
- msg32 ,
683
- xprv .priv_key + 1 , // first byte is 0
684
- host_nonce32 ,
685
- recid_out )) {
578
+ ctx , & secp256k1_sig , msg32 , private_key , host_nonce32 , recid_out )) {
686
579
return false;
687
580
}
688
581
if (!secp256k1_ecdsa_signature_serialize_compact (ctx , sig_compact_out , & secp256k1_sig )) {
@@ -698,7 +591,7 @@ bool keystore_get_u2f_seed(uint8_t* seed_out)
698
591
}
699
592
uint8_t bip39_seed [64 ] = {0 };
700
593
UTIL_CLEANUP_64 (bip39_seed );
701
- if (!_copy_bip39_seed (bip39_seed )) {
594
+ if (!keystore_copy_bip39_seed (bip39_seed )) {
702
595
return false;
703
596
}
704
597
const uint8_t message [] = "u2f" ;
@@ -713,7 +606,7 @@ bool keystore_get_ed25519_seed(uint8_t* seed_out)
713
606
{
714
607
uint8_t bip39_seed [64 ] = {0 };
715
608
UTIL_CLEANUP_64 (bip39_seed );
716
- if (!_copy_bip39_seed (bip39_seed )) {
609
+ if (!keystore_copy_bip39_seed (bip39_seed )) {
717
610
return false;
718
611
}
719
612
@@ -746,19 +639,6 @@ bool keystore_get_ed25519_seed(uint8_t* seed_out)
746
639
return true;
747
640
}
748
641
749
- USE_RESULT bool keystore_encode_xpub_at_keypath (
750
- const uint32_t * keypath ,
751
- size_t keypath_len ,
752
- uint8_t * out )
753
- {
754
- struct ext_key derived_xpub __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
755
- if (!keystore_get_xpub (keypath , keypath_len , & derived_xpub )) {
756
- return false;
757
- }
758
- return bip32_key_serialize (& derived_xpub , BIP32_FLAG_KEY_PUBLIC , out , BIP32_SERIALIZED_LEN ) ==
759
- WALLY_OK ;
760
- }
761
-
762
642
static bool _schnorr_keypair (
763
643
const uint32_t * keypath ,
764
644
size_t keypath_len ,
@@ -769,13 +649,15 @@ static bool _schnorr_keypair(
769
649
if (keystore_is_locked ()) {
770
650
return false;
771
651
}
772
- struct ext_key xprv __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
773
- if (!_get_xprv (keypath , keypath_len , & xprv )) {
652
+ uint8_t private_key [32 ] = {0 };
653
+ UTIL_CLEANUP_32 (private_key );
654
+ if (!rust_secp256k1_get_private_key (
655
+ keypath , keypath_len , rust_util_bytes_mut (private_key , sizeof (private_key )))) {
774
656
return false;
775
657
}
776
- const uint8_t * secret_key = xprv . priv_key + 1 ; // first byte is 0;
658
+
777
659
const secp256k1_context * ctx = wally_get_secp_context ();
778
- if (!secp256k1_keypair_create (ctx , keypair_out , secret_key )) {
660
+ if (!secp256k1_keypair_create (ctx , keypair_out , private_key )) {
779
661
return false;
780
662
}
781
663
if (tweak != NULL ) {
@@ -815,19 +697,6 @@ bool keystore_secp256k1_schnorr_sign(
815
697
return secp256k1_schnorrsig_verify (ctx , sig64_out , msg32 , 32 , & pubkey ) == 1 ;
816
698
}
817
699
818
- bool keystore_secp256k1_get_private_key (
819
- const uint32_t * keypath ,
820
- const size_t keypath_len ,
821
- uint8_t * key_out )
822
- {
823
- struct ext_key xprv __attribute__((__cleanup__ (keystore_zero_xkey ))) = {0 };
824
- if (!_get_xprv_twice (keypath , keypath_len , & xprv )) {
825
- return false;
826
- }
827
- memcpy (key_out , xprv .priv_key + 1 , 32 );
828
- return true;
829
- }
830
-
831
700
#ifdef TESTING
832
701
void keystore_mock_unlocked (const uint8_t * seed , size_t seed_len , const uint8_t * bip39_seed )
833
702
{
0 commit comments