From 2cedc687e63f09fe3f372c8bdfaba7afca981c86 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 29 Apr 2020 10:59:10 +0200 Subject: [PATCH] core: disable trezor-crypto BIP32 cache --- core/SConscript.firmware | 1 + core/SConscript.unix | 1 + .../modtrezorcrypto/modtrezorcrypto-bip32.h | 23 +++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/SConscript.firmware b/core/SConscript.firmware index a4f60c7c7..96c07d8c2 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -36,6 +36,7 @@ CPPDEFINES_MOD += [ 'AES_128', 'AES_192', 'RAND_PLATFORM_INDEPENDENT', + ('USE_BIP32_CACHE', '0'), ('USE_KECCAK', '1'), ('USE_ETHEREUM', '1' if EVERYTHING else '0'), ('USE_MONERO', '1' if EVERYTHING else '0'), diff --git a/core/SConscript.unix b/core/SConscript.unix index 03e34776f..f1bff0901 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -34,6 +34,7 @@ CPPPATH_MOD += [ CPPDEFINES_MOD += [ 'AES_128', 'AES_192', + ('USE_BIP32_CACHE', '0'), ('USE_KECCAK', '1'), ('USE_ETHEREUM', '1' if EVERYTHING else '0'), ('USE_MONERO', '1' if EVERYTHING else '0'), diff --git a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index 04648e930..96f8d6bfe 100644 --- a/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -240,18 +240,17 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_path(mp_obj_t self, mp_raise_ValueError("Path cannot be longer than 32 indexes"); } - // convert path to int array - uint32_t pi; - uint32_t pints[plen]; - for (pi = 0; pi < plen; pi++) { - pints[pi] = trezor_obj_get_uint(pitems[pi]); - } - - if (!hdnode_private_ckd_cached(&o->hdnode, pints, plen, &o->fingerprint)) { - // derivation failed, reset the state and raise - o->fingerprint = 0; - memzero(&o->hdnode, sizeof(o->hdnode)); - mp_raise_ValueError("Failed to derive path"); + for (uint32_t pi = 0; pi < plen; pi++) { + if (pi == plen - 1) { + // fingerprint is calculated from the parent of the final derivation + o->fingerprint = hdnode_fingerprint(&o->hdnode); + } + uint32_t pitem = trezor_obj_get_uint(pitems[pi]); + if (!hdnode_private_ckd(&o->hdnode, pitem)) { + o->fingerprint = 0; + memzero(&o->hdnode, sizeof(o->hdnode)); + mp_raise_ValueError("Failed to derive path"); + } } return mp_const_none;