1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

modtrezorcrypto: use cached bip32 path derivation

This commit is contained in:
Jan Pochyla 2016-12-12 15:18:17 +01:00
parent 1de4cf4a18
commit 47994e35f4

View File

@ -45,30 +45,30 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_HDNode_derive_obj, mod_TrezorC
/// ''' /// '''
STATIC mp_obj_t mod_TrezorCrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path) { STATIC mp_obj_t mod_TrezorCrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
// get path objects and length
mp_uint_t plen; mp_uint_t plen;
mp_obj_t *pitems; mp_obj_t *pitems;
mp_obj_get_array(path, &plen, &pitems); mp_obj_get_array(path, &plen, &pitems);
if (plen > 32) {
mp_raise_ValueError("Path cannot be longer than 32 indexes");
}
for (uint32_t pi = 0; pi < plen; pi++) { // convert path to int array
mp_obj_t iobj = pitems[pi]; uint32_t pi;
if (!MP_OBJ_IS_INT(iobj)) { uint32_t pints[plen];
// some value from the path is not integer, reset the state and raise for (pi = 0; pi < plen; pi++) {
o->fingerprint = 0; if (!MP_OBJ_IS_INT(pitems[pi])) {
memset(&o->hdnode, 0, sizeof(o->hdnode));
mp_raise_TypeError("Index has to be int"); mp_raise_TypeError("Index has to be int");
} }
uint32_t i = mp_obj_get_int_truncated(iobj); pints[pi] = mp_obj_get_int_truncated(pitems[pi]);
}
if (pi == plen - 1) { if (!hdnode_private_ckd_cached(&o->hdnode, pints, plen, &o->fingerprint)) {
// compute fingerprint before overwriting the node, but only for the nth-1 node // derivation failed, reset the state and raise
o->fingerprint = hdnode_fingerprint(&o->hdnode); o->fingerprint = 0;
} memset(&o->hdnode, 0, sizeof(o->hdnode));
if (!hdnode_private_ckd(&o->hdnode, i)) { mp_raise_ValueError("Failed to derive path");
// derivation failed, reset the state and raise
o->fingerprint = 0;
memset(&o->hdnode, 0, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive path");
}
} }
return mp_const_none; return mp_const_none;