Skip to content

Commit 368f608

Browse files
committed
Merge branch 'rust-bip32-3'
2 parents 1b8cc7e + 8c961f9 commit 368f608

File tree

15 files changed

+306
-450
lines changed

15 files changed

+306
-450
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# limitations under the License.
1616

1717
set(DBB-FIRMWARE-SOURCES
18-
${CMAKE_SOURCE_DIR}/src/bip32.c
1918
${CMAKE_SOURCE_DIR}/src/firmware_main_loop.c
2019
${CMAKE_SOURCE_DIR}/src/keystore.c
2120
${CMAKE_SOURCE_DIR}/src/random.c

src/bip32.c

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/bip32.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/keystore.c

Lines changed: 22 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static bool _is_unlocked_bip39 = false;
4545
// Stores a random keyy after bip39-unlock which, after stretching, is used to encrypt the retained
4646
// bip39 seed.
4747
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().
4949
// Stores the encrypted BIP-39 seed after bip39-unlock.
5050
static uint8_t _retained_bip39_seed_encrypted[64 + 64] = {0};
5151
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)
115115
return true;
116116
}
117117

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)
126119
{
127120
if (!_is_unlocked_bip39) {
128121
return false;
@@ -529,103 +522,6 @@ bool keystore_bip39_mnemonic_to_seed(const char* mnemonic, uint8_t* seed_out, si
529522
return bip39_mnemonic_to_bytes(NULL, mnemonic, seed_out, 32, seed_len_out) == WALLY_OK;
530523
}
531524

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-
629525
bool keystore_get_bip39_word(uint16_t idx, char** word_out)
630526
{
631527
return bip39_get_word(NULL, idx, word_out) == WALLY_OK;
@@ -638,18 +534,17 @@ bool keystore_secp256k1_nonce_commit(
638534
const uint8_t* host_commitment,
639535
uint8_t* signer_commitment_out)
640536
{
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)))) {
643541
return false;
644542
}
543+
645544
const secp256k1_context* ctx = wally_get_secp_context();
646545
secp256k1_ecdsa_s2c_opening signer_commitment;
647546
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)) {
653548
return false;
654549
}
655550

@@ -670,19 +565,17 @@ bool keystore_secp256k1_sign(
670565
if (keystore_is_locked()) {
671566
return false;
672567
}
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)))) {
675572
return false;
676573
}
574+
677575
const secp256k1_context* ctx = wally_get_secp_context();
678576
secp256k1_ecdsa_signature secp256k1_sig = {0};
679577
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)) {
686579
return false;
687580
}
688581
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)
698591
}
699592
uint8_t bip39_seed[64] = {0};
700593
UTIL_CLEANUP_64(bip39_seed);
701-
if (!_copy_bip39_seed(bip39_seed)) {
594+
if (!keystore_copy_bip39_seed(bip39_seed)) {
702595
return false;
703596
}
704597
const uint8_t message[] = "u2f";
@@ -713,7 +606,7 @@ bool keystore_get_ed25519_seed(uint8_t* seed_out)
713606
{
714607
uint8_t bip39_seed[64] = {0};
715608
UTIL_CLEANUP_64(bip39_seed);
716-
if (!_copy_bip39_seed(bip39_seed)) {
609+
if (!keystore_copy_bip39_seed(bip39_seed)) {
717610
return false;
718611
}
719612

@@ -746,19 +639,6 @@ bool keystore_get_ed25519_seed(uint8_t* seed_out)
746639
return true;
747640
}
748641

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-
762642
static bool _schnorr_keypair(
763643
const uint32_t* keypath,
764644
size_t keypath_len,
@@ -769,13 +649,15 @@ static bool _schnorr_keypair(
769649
if (keystore_is_locked()) {
770650
return false;
771651
}
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)))) {
774656
return false;
775657
}
776-
const uint8_t* secret_key = xprv.priv_key + 1; // first byte is 0;
658+
777659
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)) {
779661
return false;
780662
}
781663
if (tweak != NULL) {
@@ -815,19 +697,6 @@ bool keystore_secp256k1_schnorr_sign(
815697
return secp256k1_schnorrsig_verify(ctx, sig64_out, msg32, 32, &pubkey) == 1;
816698
}
817699

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-
831700
#ifdef TESTING
832701
void keystore_mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t* bip39_seed)
833702
{

0 commit comments

Comments
 (0)