1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 03:48:09 +00:00
trezor-firmware/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h

566 lines
23 KiB
C
Raw Normal View History

2016-10-06 12:00:57 +00:00
/*
2018-02-26 13:06:10 +00:00
* This file is part of the TREZOR project, https://trezor.io/
2016-10-06 12:00:57 +00:00
*
2018-02-26 13:06:10 +00:00
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-06 12:00:57 +00:00
*/
#include "py/objstr.h"
#include "embed/extmod/trezorobj.h"
#include "bip32.h"
2018-06-14 14:28:50 +00:00
#include "bip39.h"
#include "curves.h"
#include "memzero.h"
2018-03-15 13:52:34 +00:00
#include "nem.h"
2016-10-06 12:00:57 +00:00
xmr: master merging commit templates: build style: correct math in comment [260130c1] xmr: show address fix [abd27e6c] xmr: reorganize module structure [cd9e5a5d] xmr: simplify layout code [a5b56f17] xmr: monero.crypto test fix [60bc30ec] xmr: minor fixes [f82bd9c0] xmr: use trezor.utils.ensure [adf119ac] xmr: get rid of xmr.common module [3531a42f] pipenv: temporary fix attempt for travis - until pipenv bug is resolved [d172f86f] xmr: protob messages refactored [e83085ea] trezor-common version bump & messages regenerated xmr: mlsag_hasher simplified [feb5f1c6] xmr: simplify key_image_sync workflow [d4cb0084] xmr: tiny note in README and typo [62411cde] xmr: readme updates [ff15b46e] xmr: rename and order mlsag functions [3fb57dab] xmr: mlsag notes [c27ae90b] xmr: output index check added in step6 [973c457b] xmr: grouping is mandatory [684c7e19] xmr: range sig grouping check added [012ca766] xmr: small refactor in borromean range proof [1ba72b65] xmr: move range signatures to seperate file; rename mlsag2 to mlsag [a89f3ab0] xmr: fix wrong annotation in modtrezorcrypto [8303b42f] xmr: state's use_simple_rct and use_bulletproof modified to enums [276712a9] xmr: re-export most of functions in apps.monero.xmr.crypto [74165457] xmr: out_pk_masks changed to out_pk_commitments [1e186727] xmr: state comments [3c69a2e1] xmr: TrezorTxPrefixHashNotMatchingError note removed only concerns multisig [5af0feae] xmr: master merging commit xmr: step 10 review [d8e99375] xmr: step 09 review [a510150d] travis: workaround form 6a0ea22b6eb3bdc9bce62d7c88fb8841ef8fdb0c [03d27115] mocks: regenerate [bd24bb35] mocks: add support for entering the global scope [f75c1902] mocks: regenerate [bce85968] modtrezorcrypto: define mock package [7c077521] mocks: support package definition [b3f1017b] xmr: step 05 and 06 masks and range proofs review Masks are now always generated in step 5 and stored in state. Range proofs were reviewed only in a high-level manner and will be reviewed later. [67f391cd] xmr: step 08 review [673bf01f] xmr: steps 04, 05, 06 (almost) and 07 review _range_proof in step 06 is still to be reviewed [24c52517] xmr: simplify serialization, remove Archive [896cdebf] xmr: redundant exception removed [cb3813aa] xmr: serializer simplified [471213b4] xmr: serializer flake8 fix [9d4df17f] xmr: aescbc not needed [52dd8b3b] xmr: serializer - erefs kicked out [9e3be78f] xmr: sign cleanup, comments - state cleanup, comments added, unused code removed [164a7d6a] xmr: sign step 09 - fix in_memory artifact, dead branch [8fa4066d] xmr: sign step 03 - permutation length check added [55a593aa] xmr: multisig removed from protocol and functions [6470678d] xmr: black styling [02664401] xmr: step 03 review [daf7b7d6] xmr: step 02 review xmr: adding agent tests to travis [c752866f] xmr: unused imports removed [85115fd9] xmr: serializer test fixed - removed unsupported messages after serialization simplification [60874751] xmr: sign step 06 - comment on bulletproof hashing - hash_bp(bp) != hash(serialize(bp)) because hash does not contain array lengths [4abb5473] xmr: sign protocol - multisig logic removed [813cb3a0] xmr: lite protocol removed - backup left in xmr-total-full-with-lite branch [e5f5b5bd] xmr: serialization slimming - base types reduced, not needed for now - some int serialization methods not used now [fb515aaf] xmr: serialization - archive simplified - simple parameter passing, no kwargs - unused methods removed - reader/writer passing removed for archive methods [ec4c4adb] xmr: KeccakXmrArchive simplified, no archive used - getting rid of container_size. We dont use containers with fixed size so this special case can be abandoned. - KeccakXmrArchive is lighweight without need to touch main serialization [90065bd4] xmr: serialization - serialize_archive removed - custom serialization routine is not required at this moment [b98c2f89] xmr: extra serialization refactored, manual serialization - extra is serialized manually to reduce serialization overhead - extra contains simple structures now: - payment ID = already serialized manually - tx pub key = easy to serialize manually - tx additional pub keys = serialized manually with little effort, more efficient memory usage [8ce28a54] xmr: state 6 - provided tx keys removed - needed only in the multisig scenario which is pruned now [8d827f46] xmr: PreMlsagHasher pseudo out hashing fix [49e552da] xmr: redundant import removed [4199943d] xmr: KeccakXmrArchive simplified [69bbf5fa] xmr: PreMlsagHasher - KeyV import removed, comment added [9a194fa2] xmr: step7 - manual hashing of Extra [d8a09283] xmr: PreMlsagHasher state load/save removed - not needed, state not serialized anymore [d5f43fa4] xmr: serialize reimport removed - complex types are not surviving protocol boundary anymore, no need to fix hierarchy problems due to unimporting [3b045612] xmr: serialize - TupleType removed [b9a56981] xmr: serialization schemes simplified [a59dbb8d] xmr: HashWrapper removed [d0d1f05b] xmr: step 01 cleanup and comments [8f7a7782] xmr: isort, black, flake8 fixes [14265eb2] xmr: getting rid of CtKey from the state - only lightweight objects are kept in the state. CtKey is import heavy object. Each set_out call locally imports a new own version of the Ctkey which causes a memory leak. [c0cfc203] xmr: set_out minor function call fix [c11c4686] xmr: sign_tx unimport optimization to reduce fragmentation [df0a1dfc] xmr: range_sig allocation reordering, large chunks first [63cddd5f] xmr: remove misc.StdObj [8c8e3f33] xmr: proper memory usage in workflow [9be1e0ac] xmr: typos and renames [87f718b5] xmr: back to flat workflow [64751339] xmr: refactor builder to seperate steps - lot of work to be done, but the general idea will probably stay - the messages workflow works, but the signed tx was not accepted by daemon, so there is a bug somewhere - additional cleanup/refactoring is defintely needed [14b0a851] xmr: iface modified to layout [27d568e1] xmr: extmod refactoring - *_into removed, replaced by generic methods - point_add, point_sub added - code cleanup (+1 squashed commit) Squashed commits: [fbe39496] monero support added Squashed commits: xmr: hmac/enc keys removed from builder [41028dff] xmr: unused function removed [358573e3] xmr: PR comments fixes [4abf9dc9] xmr: test fixes after refactoring [192785ad] template rebuilt [57a1f25e] xmr: wrapper protocol messages removed [6f40ce17] xmr: trezor-common version bump & sync [03e71de2] xmr: check input permutation [6fc8b0ec] xmr: code cleanup, refactoring [85ecc151] xmr: crypto code cleanup [20b4113c] xmr: chunked bulletproof vectors - workaround for the heap fragmentation problems [66786f9d] tools: enable to reset class level indentation for mocks gen [dc6f84a9] xmr: extmod-monero comments added, for mocks [b1d4ab11] xmr: code cleanup [447a8625] xmr: tsx confirmation raises exception on cancellation [00dd8f6c] xmr: protocol optimizations removed, flow unified - in_memory optimization stored tx.vin parts in the memory which enabled to skip roundtrips with permutations and hash_vini. Optimizations was removed so the protocol flow is unified among inputs, independent of the tx specs - many_inputs: optimization stored spending keys for UTXO in memory, now it is offloaded in the encrypted form. [ea69c7ad] vendor: trezor-common version bump & pb sync [5d81c2a8] xmr: manual BP serialization - more memory effective as the memory is critical in the range proof section [d64bda7c] xmr: range_proof C-impl deprecated - using now partitioned implementation in Python, which is also quite fast and easier to maintain due to allocations and buffers. [18604e0f] xmr: borromean range sig generated by partitions - overcomes heap fragmentation problem [65a5116b] xmr: comments removed [ca2bd0cc] xmr: auto-generated intelliJ param comments removed [a75ef32e] xmr: code cleanup, heap fragmentations fixes Squashed commits: [d2ac2eb6] xmr: addr cleanup [7e4c1a9c] xmr: code cleanup, heap fragmentations fixes [93af8afb] xmr: refactoring, typing, comments [28df8662] xmr: comment fix [8b4f4d9c] xmr: serializer test fix (+34 squashed commits) Squashed commits: [823ee192] xmr: crypto comment cleanup [6debfb6b] xmr: ring_ct cleanup [759f52bf] xmr: tsx signer code style, hintins [0b175bc9] xmr: tsx builder external state removed [fee4a5ad] xmr: builder state fix [92736fa7] xmr: sign_tx unimport [a570ecb9] xmr: misc code cleanup [4a496bb7] xmr: hash wrapper not needed in writer [fefdb834] xmr: signer serialization improved [8fa6eec9] xmr: signer mem clean [66c53fe5] xmr: isort [6996bd90] xmr: black [59915a85] xmr: tsx input serialization refactored [326af131] xmr: msg dump with prefix [6e39801d] xmr: manual serialization of tx prefix [9e5e047a] xmr: manual serialization improvements [d07cee60] xmr: manual serialization of txout elements [8d56c80e] xmr: TxOut custom serialization optimized II [c19ba122] xmr: TxOut custom serialization optimized [ce0d9b0b] xmr: TxOut manual serialization [44e38342] xmr: sing_tx unimport [61ac61be] xmr: lite log trace rename [176b4275] xmr: de-async overhaul [89ae3bad] xmr: diag style [5ccb2fbb] xmr: wrappers cleanup [aa86fb19] xmr: py3 only inheritance [8031b1b0] xmr: builder, log_trace -> mem_trace for clarity [25bf70d7] xmr: debugging logging only in debug mode [c7c8d3c8] xmr: iface cleanup [b0373396] xmr: lite debug only [b1f6ce04] xmr: diag only in debug [de7d718b] xmr: tsx counter removed [76729beb] xmr: tsx_sign removed [c6e6ffa8] Merge commit 'ba500bf4ec1ef9cd953bdf5a47888c5226db8d0b' into xmr [ee97ef9f] xmr: minor code cleanup xmr: black xmr: minor code cleanup [bae3ecac] xmr: bp comments [5e812e6f] xmr: sign - mem_trace, pydoc [7216a8c6] xmr: pydoc removed [e87365f4] xmr: layout cleanup [8d21d82e] xmr: redundant constructors removed [9aa82bed] xmr: redundant comments removed [9b926d6c] xmr: preludes removed [bc9e77f1] xmr: readme update [cf62047a] xmr: aggregated bulletproofs + rsig offloading xmr: change idx fix xmr: iface refactoring, integrated address (+5 squashed commits) xmr: layout pagination refactoring xmr: addr - integrated address pb: sync vendor: trezor-common version bump xmr: style fixes xmr: handle sweep tsx correctly - handle dummy change address correctly xmr: integrated address generation build: fix after trezor-crypto version bump xmr: new protocol dispatch handlers xmr: slip0010 [43cf4c3c] xmr: comment fix xmr: extmod pointer aritm fix xmr: _into api unified, result is the first parameter xmr: bp cleanup xmr: scalar nullity test fix xmr: msg registration improved - lite protocol optional - diag protocol optional xmr: unused imports (+33 squashed commits) [b4d045ae] xmr: bp - noqa flake8 false positive [2c79d4be] xmr: isort [8b9d2835] xmr: code cleanup [eb7496e9] xmr: iface - shorter timeouts for faster tests [59520b63] xmr: ringct comment [6b16088e] xmr: signer - comment fixes [a08958e2] xmr: simple and bulletproof condition fix [4e0289a9] vendor: trezor-common version bump [de472e5a] xmr: black [234d2249] xmr: lightening, fixes, KeccakXmrArchive - builder keys - unload mods before memory intensive operation [abdec665] xmr: sign_tx logging [989d8687] xmr: serialize lightening [7d61f056] xmr: tsx sign refactoring, lightening - wake_up state restore - minimize import weight [3a0daa8b] xmr: serialize thinning [65ad1d2e] xmr: serialize thinning [501221d5] xmr: bp - thinning [3d980377] xmr: bp - generalization with proof_v8 [10d11d60] xmr: extended rsig - offloading protocol [a8f5caa2] xmr: crypto - rsig params fix [f5e130b8] xmr: crypto - inv8 [dbc3f9d8] xmr: rsig pb sync [5748a13e] xmr: bp - data for bp4 fix (+18 squashed commits) Squashed commits: [5bcd54e3] xmr: bp - black [e93e97dd] xmr: bp refactoring, large memory optimizations - memoryview in __getitem__ requires new memory allocation so the refactored version uses to(), read() methods that can operate directly on buffers without need to create memory views. [c30745ae] xmr: bp - black [f5c40699] xmr: bp - tests extended [8dae75de] xmr: bp - get_exponent optim [3e59ff80] xmr: bp - precomputations for 4 statements [d1d2e29c] xmr: bp - gc.collect [1bb6b5ba] xmr: bp - optimizations, streamlining [2a2b0cb7] xmr: bp - verification in log(MN) memory for 1 proof - not allocating MN vectors - sequential multiexec added for memory efficient verification - bulletproofs: maintain -z4, -z5, and -y0 to avoid subtractions [8276d25] - bulletproofs: merge multiexps as per sarang's new python code [acd64d2b] [75aa7dee] xmr: bp - memory optimization [a10d05a9] xmr: bp - deterministic mask generation init [5060d6a7] xmr: bp optimizations [dd69eb1b] xmr: bp - black [19f0f64b] xmr: bp - optimizations, power key vector [2ba63f8c] xmr: bp - minor cleanup, optimizations, scalarmultH [31c9ca2d] xmr: bp - mem clean [3fc2c793] xmr: bp - memory save [5b16c9cc] bp: black [f1040c97] xmr: crypto - memory leak fix [ff863510] xmr: iface - flake [6ebf69c2] xmr: lite - flake8, black [eee55d62] xmr: bp - memory diag [2767009b] xmr: bulletproofs upgrade, mainnet version, cleaning [be6ebbd5] xmr: lite protocol [d603e96d] xmr: pb sync [5da15da9] vendor: trezor-common fix [0373b97e] xmr: iface - output confirmation split, subaddr fix [2cf32176] xmr: monero - subaddress fixed for index (0, 0) [3bb8f08b] xmr: enc.aescbc added - for lite protocol (+1 squashed commit) Squashed commits: [011dbaab] TMP: trezor-common on master, crypto on ph4 - trezor-crypto on ph4r04 fork as it has all required stuff - Lite protocol not merged in master, thus does not work in the PR [795b34e1] xmr: get_address fix [2d39c90c] xmr: bp - import fix (squashed commit) Squashed commits: [2d5c6cee] extmod: monero - reduce32 and ge25519_norm removed (squashed commit) - not needed in trezor-core (+4 squashed commits) Squashed commits: [90e6b5c5] xmr: bp optimization [4fda0d22] xmr: redundant ge_ functions removed [68903767] xmr: crypto - sc_reduce32 not needed [c8a6c807] xmr: test for inversion added (+12 squashed commits) Squashed commits: [378928db] xmr: adapting to new trezor-crypto [8f4ff8c1] protob sync [82dff70a] vendor: trezor-common version bump [fabc67b3] extmod: monero - inversion mod curve order optimized a bit [4f29fe4c] xmr: import fix [f6f8e300] xmr: bp - code cleanup [d54b4f3b] xmr: bp - memory cleaning [1065abc3] xmr: tsx_signer - bulletproofs fixes [9f8a700f] xmr: bp key vector iterator fix [49c25977] xmr.serialize: bulletproof fix [1ee77378] xmr: monero - format [cf0a7104] xmr: bp last mask fix (+20 squashed commits) Squashed commits: [fa1c3623] xmr: black [3f3e31f3] xmr: bulletproofs added to signer [d23d9284] xmr: protocol.tsx_sign_builder - logger collects [a28eb55f] xmr: bp - memory optimizations [d2fcb23a] xmr: tests for bulletproofs added [82eef146] xmr: bp - gc (+14 squashed commits) Squashed commits: [4cf70d97] xmr: bp - gc [42877b05] xmr: bp - minor memory optimization [2c612e45] xmr: bp - use sc_inv_into [d7e9dab4] xmr: bp - KeyVEval fix [1523f400] xmr: bp - blacked [b264a65b] xmr: bp - KeyVEval - caching current element, avoid allocations [83ba7a65] xmr: bp - memory view optimized [b517906c] xmr: bp - gc() during inversion [92d37c88] xmr: bp - gc.collect() after expensive inversion [e7fad558] xmr: bp - hashing memory optimization [4c278152] xmr: bp - deterministic masks optimization, prove_s1 optim [cbf74a70] xmr: bp - detect which modular inversion is usable [8ea1ec43] xmr: better memory tracing for bulletproofs [2f4dd552] xmr: bulletproofs added [1928e2d3] xmr: crypto - sc_inv_into added (+2 squashed commits) Squashed commits: [f895fa6e] xmr: crypto - hash to existing buffer [b76c6b09] xmr: crypto - in-place crypto functions added - required for Bulletproof to minimize the heap fragmentation [cab4366e] extmod: monero - modular inversion mod curve order added (+2 squashed commits) Squashed commits: [52a6e487] extmod: monero - hash into buffer added [695a3827] extmod: monero module - muladd256_modm added - required for Bulletproof [3f4498d7] xmr: crypto tests added - basic unit tests for crypto, tests monero module and underlying trezor-crypto + basic address manipulation [820d012d] pb sync [49eeddd1] vendor: trezor-common version bump [30382440] xmr: crypto - point norm not needed [89701c41] tests: xmr - serializer tests added [bfee46db] tests: support async unit tests, assertListEqual added [55c14487] xmr: serialize - serialization logic cleaned, refactored [4b771638] xmr: simplification, do not ask to confirm change tx output - change address checked to match main address in the builder [f334d8ad] xmr: protocol: simplification - require change address to equal the main address [1a3416eb] xmr: unpack256_modm_noreduce added - 32B array to integer mod curve order, without modular reduction after conversion - required for bulletproofs [1c94b5d4] xmr: readme added [3cc9f9fa] extmod/monero: mul256_modm added, required for BP [5cf77a4c] xmr: monero support added [for review] depends on PRs: https://github.com/trezor/trezor-crypto/pull/162 https://github.com/trezor/trezor-core/pull/286
2018-08-06 12:07:14 +00:00
/// package: trezorcrypto.bip32
/// class HDNode:
/// '''
/// BIP0032 HD node structure.
/// '''
2016-10-06 12:00:57 +00:00
typedef struct _mp_obj_HDNode_t {
mp_obj_base_t base;
uint32_t fingerprint;
HDNode hdnode;
} mp_obj_HDNode_t;
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type;
2016-11-06 15:06:11 +00:00
#define XPUB_MAXLEN 128
#define ADDRESS_MAXLEN 36
/// def __init__(self,
/// depth: int,
/// fingerprint: int,
/// child_num: int,
/// chain_code: bytes,
/// private_key: bytes = None,
/// public_key: bytes = None,
/// curve_name: str = None) -> None:
/// '''
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC const mp_arg_t allowed_args[] = {
{ MP_QSTR_depth, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_fingerprint, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_child_num, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_chain_code, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_private_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_public_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
{ MP_QSTR_curve_name, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
};
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
mp_buffer_info_t chain_code;
mp_buffer_info_t private_key;
mp_buffer_info_t public_key;
mp_buffer_info_t curve_name;
const uint32_t depth = trezor_obj_get_uint(vals[0].u_obj);
const uint32_t fingerprint = trezor_obj_get_uint(vals[1].u_obj);
const uint32_t child_num = trezor_obj_get_uint(vals[2].u_obj);
mp_get_buffer_raise(vals[3].u_obj, &chain_code, MP_BUFFER_READ);
mp_get_buffer_raise(vals[4].u_obj, &private_key, MP_BUFFER_READ);
mp_get_buffer_raise(vals[5].u_obj, &public_key, MP_BUFFER_READ);
mp_get_buffer_raise(vals[6].u_obj, &curve_name, MP_BUFFER_READ);
2018-01-26 12:51:44 +00:00
if (32 != chain_code.len) {
mp_raise_ValueError("chain_code is invalid");
}
2018-01-26 12:51:44 +00:00
if (0 == public_key.len && 0 == private_key.len) {
mp_raise_ValueError("either public_key or private_key is required");
}
2018-01-26 12:51:44 +00:00
if (0 != private_key.len && 32 != private_key.len) {
mp_raise_ValueError("private_key is invalid");
}
2018-01-26 12:51:44 +00:00
if (0 != public_key.len && 33 != public_key.len) {
mp_raise_ValueError("public_key is invalid");
}
const curve_info *curve = NULL;
2018-01-26 12:51:44 +00:00
if (0 == curve_name.len) {
curve = get_curve_by_name(SECP256K1_NAME);
} else {
curve = get_curve_by_name(curve_name.buf);
}
if (NULL == curve) {
mp_raise_ValueError("curve_name is invalid");
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
o->base.type = type;
o->fingerprint = fingerprint;
o->hdnode.depth = depth;
o->hdnode.child_num = child_num;
2018-01-26 12:51:44 +00:00
if (32 == chain_code.len) {
memcpy(o->hdnode.chain_code, chain_code.buf, 32);
} else {
memzero(o->hdnode.chain_code, 32);
}
2018-01-26 12:51:44 +00:00
if (32 == private_key.len) {
memcpy(o->hdnode.private_key, private_key.buf, 32);
} else {
memzero(o->hdnode.private_key, 32);
}
2018-01-26 12:51:44 +00:00
if (33 == public_key.len) {
memcpy(o->hdnode.public_key, public_key.buf, 33);
} else {
memzero(o->hdnode.public_key, 33);
}
o->hdnode.curve = curve;
return MP_OBJ_FROM_PTR(o);
}
2018-01-26 12:53:32 +00:00
/// def derive(self, index: int, public: bool=False) -> None:
2016-10-06 12:00:57 +00:00
/// '''
/// Derive a BIP0032 child node in place.
/// '''
2018-01-26 12:53:32 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(size_t n_args, const mp_obj_t *args) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(args[0]);
uint32_t i = trezor_obj_get_uint(args[1]);
2016-10-06 12:00:57 +00:00
uint32_t fp = hdnode_fingerprint(&o->hdnode);
2018-01-26 12:53:32 +00:00
bool public = n_args > 2 && args[2] == mp_const_true;
2016-10-06 12:00:57 +00:00
2018-01-26 12:53:32 +00:00
int res;
if (public) {
res = hdnode_public_ckd(&o->hdnode, i);
} else {
if (0 == memcmp(o->hdnode.private_key, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) {
2018-01-26 12:53:32 +00:00
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive, private key not set");
}
res = hdnode_private_ckd(&o->hdnode, i);
}
if (!res) {
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive");
2016-10-06 12:00:57 +00:00
}
o->fingerprint = fp;
return mp_const_none;
}
2018-01-26 12:53:32 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_derive_obj, 2, 3, mod_trezorcrypto_HDNode_derive);
2016-10-06 12:00:57 +00:00
2018-06-14 14:28:50 +00:00
/// def derive_cardano(self, index: int) -> None:
/// '''
/// Derive a BIP0032 child node in place using Cardano algorithm.
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_cardano(mp_obj_t self, mp_obj_t index) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
uint32_t i = mp_obj_get_int_truncated(index);
uint32_t fp = hdnode_fingerprint(&o->hdnode);
2018-06-14 14:28:50 +00:00
int res;
// same as in derive
if (0 == memcmp(o->hdnode.private_key, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) {
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive, private key not set");
}
// special for cardano
res = hdnode_private_ckd_cardano(&o->hdnode, i);
if (!res) {
memzero(&o->hdnode, sizeof(o->hdnode));
mp_raise_ValueError("Failed to derive");
}
o->fingerprint = fp;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_cardano_obj, mod_trezorcrypto_HDNode_derive_cardano);
/// def derive_path(self, path: List[int]) -> None:
2016-10-06 12:00:57 +00:00
/// '''
/// Go through a list of indexes and iteratively derive a child node in place.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
// get path objects and length
size_t plen;
2016-10-06 12:00:57 +00:00
mp_obj_t *pitems;
mp_obj_get_array(path, &plen, &pitems);
if (plen > 32) {
mp_raise_ValueError("Path cannot be longer than 32 indexes");
}
2016-10-06 12:00:57 +00:00
// 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]);
}
2016-10-06 12:00:57 +00:00
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");
2016-10-06 12:00:57 +00:00
}
return mp_const_none;
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_path_obj, mod_trezorcrypto_HDNode_derive_path);
2016-10-06 12:00:57 +00:00
STATIC mp_obj_t serialize_public_private(mp_obj_t self, bool use_public, uint32_t version) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
char xpub[XPUB_MAXLEN] = {0};
int written;
if (use_public) {
hdnode_fill_public_key(&o->hdnode);
written = hdnode_serialize_public(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
} else {
written = hdnode_serialize_private(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
}
2016-10-06 12:00:57 +00:00
if (written <= 0) {
mp_raise_ValueError("Failed to serialize");
2016-10-06 12:00:57 +00:00
}
return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)xpub, written - 1); // written includes 0 at the end
2016-10-06 12:00:57 +00:00
}
/// def serialize_public(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Serialize the public info from HD node to base58 string.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_public(mp_obj_t self, mp_obj_t version) {
uint32_t ver = trezor_obj_get_uint(version);
return serialize_public_private(self, true, ver);
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_public_obj, mod_trezorcrypto_HDNode_serialize_public);
2016-10-06 12:00:57 +00:00
/// def serialize_private(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Serialize the private info HD node to base58 string.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_private(mp_obj_t self, mp_obj_t version) {
uint32_t ver = trezor_obj_get_uint(version);
return serialize_public_private(self, false, ver);
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_private_obj, mod_trezorcrypto_HDNode_serialize_private);
2016-10-06 12:00:57 +00:00
/// def clone(self) -> HDNode:
2016-11-06 13:17:05 +00:00
/// '''
/// Returns a copy of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_clone(mp_obj_t self) {
2016-11-06 13:17:05 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
mp_obj_HDNode_t *copy = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
copy->base.type = &mod_trezorcrypto_HDNode_type;
2016-11-06 13:17:05 +00:00
copy->hdnode = o->hdnode;
copy->fingerprint = o->fingerprint;
return MP_OBJ_FROM_PTR(copy);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_clone_obj, mod_trezorcrypto_HDNode_clone);
2016-11-06 13:17:05 +00:00
/// def depth(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a depth of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_depth(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->hdnode.depth);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_depth_obj, mod_trezorcrypto_HDNode_depth);
2016-10-06 12:00:57 +00:00
/// def fingerprint(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a fingerprint of the HD node (hash of the parent public key).
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_fingerprint(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->fingerprint);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_fingerprint_obj, mod_trezorcrypto_HDNode_fingerprint);
2016-10-06 12:00:57 +00:00
/// def child_num(self) -> int:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a child index of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_child_num(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_int_from_uint(o->hdnode.child_num);
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_child_num_obj, mod_trezorcrypto_HDNode_child_num);
2016-10-06 12:00:57 +00:00
/// def chain_code(self) -> bytes:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a chain code of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_chain_code(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_bytes(o->hdnode.chain_code, sizeof(o->hdnode.chain_code));
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_chain_code_obj, mod_trezorcrypto_HDNode_chain_code);
2016-10-06 12:00:57 +00:00
/// def private_key(self) -> bytes:
/// '''
/// Returns a private key of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_private_key(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_bytes(o->hdnode.private_key, sizeof(o->hdnode.private_key));
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_private_key_obj, mod_trezorcrypto_HDNode_private_key);
2018-06-14 14:28:50 +00:00
/// def private_key_ext(self) -> bytes:
/// '''
/// Returns a private key extension of the HD node.
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_private_key_ext(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
return mp_obj_new_bytes(o->hdnode.private_key_extension, sizeof(o->hdnode.private_key_extension));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_private_key_ext_obj, mod_trezorcrypto_HDNode_private_key_ext);
/// def public_key(self) -> bytes:
2016-10-06 12:00:57 +00:00
/// '''
/// Returns a public key of the HD node.
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_public_key(mp_obj_t self) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
hdnode_fill_public_key(&o->hdnode);
return mp_obj_new_bytes(o->hdnode.public_key, sizeof(o->hdnode.public_key));
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_public_key_obj, mod_trezorcrypto_HDNode_public_key);
2016-10-06 12:00:57 +00:00
/// def address(self, version: int) -> str:
2016-10-06 12:00:57 +00:00
/// '''
/// Compute a base58-encoded address string from the HD node.
2016-10-06 12:00:57 +00:00
/// '''
2017-06-14 16:47:38 +00:00
STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self, mp_obj_t version) {
2016-10-06 12:00:57 +00:00
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
uint32_t v = trezor_obj_get_uint(version);
2018-06-05 14:23:23 +00:00
char address[ADDRESS_MAXLEN] = {0};
hdnode_get_address(&o->hdnode, v, address, ADDRESS_MAXLEN);
return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)address, strlen(address));
2016-10-06 12:00:57 +00:00
}
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj, mod_trezorcrypto_HDNode_address);
2018-03-09 10:37:06 +00:00
/// def nem_address(self, network: int) -> str:
/// '''
/// Compute a NEM address string from the HD node.
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_nem_address(mp_obj_t self, mp_obj_t network) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
2018-06-05 14:23:23 +00:00
uint8_t n = trezor_obj_get_uint8(network);
char address[NEM_ADDRESS_SIZE + 1] = {0}; // + 1 for the 0 byte
if (!hdnode_get_nem_address(&o->hdnode, n, address)) {
mp_raise_ValueError("Failed to compute a NEM address");
}
return mp_obj_new_str_copy(&mp_type_str, (const uint8_t *)address, strlen(address));
2018-03-09 10:37:06 +00:00
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_nem_address_obj, mod_trezorcrypto_HDNode_nem_address);
2018-03-15 13:52:34 +00:00
/// def nem_encrypt(self, transfer_public_key: bytes, iv: bytes, salt: bytes, payload: bytes) -> bytes:
/// '''
/// Encrypts payload using the transfer's public key
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_nem_encrypt(size_t n_args, const mp_obj_t *args) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(args[0]);
mp_buffer_info_t transfer_pk;
mp_get_buffer_raise(args[1], &transfer_pk, MP_BUFFER_READ);
if (transfer_pk.len != 32) {
mp_raise_ValueError("transfer_public_key has invalid length");
}
mp_buffer_info_t iv;
mp_get_buffer_raise(args[2], &iv, MP_BUFFER_READ);
if (iv.len != 16) {
2018-06-05 14:23:23 +00:00
mp_raise_ValueError("iv has invalid length");
2018-03-15 13:52:34 +00:00
}
mp_buffer_info_t salt;
mp_get_buffer_raise(args[3], &salt, MP_BUFFER_READ);
if (salt.len != NEM_SALT_SIZE) {
2018-06-05 14:23:23 +00:00
mp_raise_ValueError("salt has invalid length");
2018-03-15 13:52:34 +00:00
}
mp_buffer_info_t payload;
mp_get_buffer_raise(args[4], &payload, MP_BUFFER_READ);
if (payload.len == 0) {
2018-06-05 14:23:23 +00:00
mp_raise_ValueError("payload is empty");
2018-03-15 13:52:34 +00:00
}
2018-06-05 14:23:23 +00:00
vstr_t vstr;
vstr_init_len(&vstr, NEM_ENCRYPTED_SIZE(payload.len));
if (!hdnode_nem_encrypt(&o->hdnode, *(const ed25519_public_key *)transfer_pk.buf, iv.buf, salt.buf, payload.buf, payload.len, (uint8_t *)vstr.buf)) {
mp_raise_ValueError("HDNode nem encrypt failed");
}
2018-06-05 14:23:23 +00:00
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
2018-03-15 13:52:34 +00:00
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_nem_encrypt_obj, 5, 5, mod_trezorcrypto_HDNode_nem_encrypt);
/// def ethereum_pubkeyhash(self) -> bytes:
/// '''
/// Compute an Ethereum pubkeyhash (aka address) from the HD node.
/// '''
STATIC mp_obj_t mod_trezorcrypto_HDNode_ethereum_pubkeyhash(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
uint8_t pkh[20];
hdnode_get_ethereum_pubkeyhash(&o->hdnode, pkh);
return mp_obj_new_bytes(pkh, sizeof(pkh));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj, mod_trezorcrypto_HDNode_ethereum_pubkeyhash);
STATIC mp_obj_t mod_trezorcrypto_HDNode___del__(mp_obj_t self) {
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
o->fingerprint = 0;
memzero(&o->hdnode, sizeof(o->hdnode));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode___del___obj, mod_trezorcrypto_HDNode___del__);
2017-06-14 16:47:38 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_HDNode_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_trezorcrypto_HDNode___del___obj) },
2017-06-14 16:47:38 +00:00
{ MP_ROM_QSTR(MP_QSTR_derive), MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_obj) },
2018-06-14 14:28:50 +00:00
{ MP_ROM_QSTR(MP_QSTR_derive_cardano), MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_cardano_obj) },
2017-06-14 16:47:38 +00:00
{ MP_ROM_QSTR(MP_QSTR_derive_path), MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_path_obj) },
{ MP_ROM_QSTR(MP_QSTR_serialize_private), MP_ROM_PTR(&mod_trezorcrypto_HDNode_serialize_private_obj) },
{ MP_ROM_QSTR(MP_QSTR_serialize_public), MP_ROM_PTR(&mod_trezorcrypto_HDNode_serialize_public_obj) },
{ MP_ROM_QSTR(MP_QSTR_clone), MP_ROM_PTR(&mod_trezorcrypto_HDNode_clone_obj) },
{ MP_ROM_QSTR(MP_QSTR_depth), MP_ROM_PTR(&mod_trezorcrypto_HDNode_depth_obj) },
{ MP_ROM_QSTR(MP_QSTR_fingerprint), MP_ROM_PTR(&mod_trezorcrypto_HDNode_fingerprint_obj) },
{ MP_ROM_QSTR(MP_QSTR_child_num), MP_ROM_PTR(&mod_trezorcrypto_HDNode_child_num_obj) },
{ MP_ROM_QSTR(MP_QSTR_chain_code), MP_ROM_PTR(&mod_trezorcrypto_HDNode_chain_code_obj) },
{ MP_ROM_QSTR(MP_QSTR_private_key), MP_ROM_PTR(&mod_trezorcrypto_HDNode_private_key_obj) },
2018-06-14 14:28:50 +00:00
{ MP_ROM_QSTR(MP_QSTR_private_key_ext), MP_ROM_PTR(&mod_trezorcrypto_HDNode_private_key_ext_obj) },
2017-06-14 16:47:38 +00:00
{ MP_ROM_QSTR(MP_QSTR_public_key), MP_ROM_PTR(&mod_trezorcrypto_HDNode_public_key_obj) },
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&mod_trezorcrypto_HDNode_address_obj) },
2018-03-09 10:37:06 +00:00
{ MP_ROM_QSTR(MP_QSTR_nem_address), MP_ROM_PTR(&mod_trezorcrypto_HDNode_nem_address_obj) },
2018-03-15 13:52:34 +00:00
{ MP_ROM_QSTR(MP_QSTR_nem_encrypt), MP_ROM_PTR(&mod_trezorcrypto_HDNode_nem_encrypt_obj) },
{ MP_ROM_QSTR(MP_QSTR_ethereum_pubkeyhash), MP_ROM_PTR(&mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj) },
2016-10-06 12:00:57 +00:00
};
2017-06-14 16:47:38 +00:00
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_HDNode_locals_dict, mod_trezorcrypto_HDNode_locals_dict_table);
2016-10-06 12:00:57 +00:00
2017-06-14 16:47:38 +00:00
STATIC const mp_obj_type_t mod_trezorcrypto_HDNode_type = {
2016-10-06 12:00:57 +00:00
{ &mp_type_type },
.name = MP_QSTR_HDNode,
.make_new = mod_trezorcrypto_HDNode_make_new,
2017-06-14 16:47:38 +00:00
.locals_dict = (void*)&mod_trezorcrypto_HDNode_locals_dict,
2016-10-06 12:00:57 +00:00
};
/// def deserialize(self, value: str, version_public: int, version_private: int) -> HDNode:
/// '''
/// Construct a BIP0032 HD node from a base58-serialized value.
/// '''
STATIC mp_obj_t mod_trezorcrypto_bip32_deserialize(mp_obj_t value, mp_obj_t version_public, mp_obj_t version_private) {
mp_buffer_info_t valueb;
mp_get_buffer_raise(value, &valueb, MP_BUFFER_READ);
if (valueb.len == 0) {
mp_raise_ValueError("Invalid value");
}
uint32_t vpub = trezor_obj_get_uint(version_public);
uint32_t vpriv = trezor_obj_get_uint(version_private);
HDNode hdnode;
uint32_t fingerprint;
if (hdnode_deserialize(valueb.buf, vpub, vpriv, SECP256K1_NAME, &hdnode, &fingerprint) < 0) {
mp_raise_ValueError("Failed to deserialize");
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
o->base.type = &mod_trezorcrypto_HDNode_type;
o->hdnode = hdnode;
o->fingerprint = fingerprint;
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_bip32_deserialize_obj, mod_trezorcrypto_bip32_deserialize);
/// def from_seed(seed: bytes, curve_name: str) -> HDNode:
2016-10-06 12:00:57 +00:00
/// '''
/// Construct a BIP0032 HD node from a BIP0039 seed value.
/// '''
STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed, mp_obj_t curve_name) {
2016-10-06 12:00:57 +00:00
mp_buffer_info_t seedb;
mp_get_buffer_raise(seed, &seedb, MP_BUFFER_READ);
if (seedb.len == 0) {
mp_raise_ValueError("Invalid seed");
2016-10-06 12:00:57 +00:00
}
mp_buffer_info_t curveb;
mp_get_buffer_raise(curve_name, &curveb, MP_BUFFER_READ);
if (curveb.len == 0) {
mp_raise_ValueError("Invalid curve name");
2016-10-06 12:00:57 +00:00
}
HDNode hdnode;
if (!hdnode_from_seed(seedb.buf, seedb.len, curveb.buf, &hdnode)) {
mp_raise_ValueError("Failed to derive the root node");
2016-10-06 12:00:57 +00:00
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
2017-06-14 16:47:38 +00:00
o->base.type = &mod_trezorcrypto_HDNode_type;
2016-10-06 12:00:57 +00:00
o->hdnode = hdnode;
o->fingerprint = 0;
2016-10-06 12:00:57 +00:00
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_seed_obj, mod_trezorcrypto_bip32_from_seed);
2016-10-06 12:00:57 +00:00
/// def from_mnemonic_cardano(mnemonic: str, passphrase: str) -> bytes:
2018-06-14 14:28:50 +00:00
/// '''
/// Convert mnemonic to hdnode
/// '''
STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(mp_obj_t mnemonic, mp_obj_t passphrase) {
mp_buffer_info_t mnemo, phrase;
2018-06-14 14:28:50 +00:00
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ);
mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ);
2018-06-14 14:28:50 +00:00
HDNode hdnode;
const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : "";
const char *ppassphrase = phrase.len > 0 ? phrase.buf : "";
uint8_t entropy[64];
int entropy_len = mnemonic_to_entropy(pmnemonic, entropy);
2018-06-14 14:28:50 +00:00
if (entropy_len == 0) {
mp_raise_ValueError("Invalid mnemonic");
}
const int res = hdnode_from_seed_cardano((const uint8_t *)ppassphrase, phrase.len, entropy, entropy_len / 8, &hdnode);
2018-06-14 14:28:50 +00:00
if (!res) {
mp_raise_ValueError("Secret key generation from mnemonic is looping forever");
} else
if (res == -1) {
2018-06-14 14:28:50 +00:00
mp_raise_ValueError("Invalid mnemonic");
}
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
o->base.type = &mod_trezorcrypto_HDNode_type;
o->hdnode = hdnode;
o->fingerprint = 0;
2018-06-14 14:28:50 +00:00
return MP_OBJ_FROM_PTR(o);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_mnemonic_cardano_obj, mod_trezorcrypto_bip32_from_mnemonic_cardano);
2018-06-14 14:28:50 +00:00
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip32_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip32) },
{ MP_ROM_QSTR(MP_QSTR_HDNode), MP_ROM_PTR(&mod_trezorcrypto_HDNode_type) },
{ MP_ROM_QSTR(MP_QSTR_deserialize), MP_ROM_PTR(&mod_trezorcrypto_bip32_deserialize_obj) },
{ MP_ROM_QSTR(MP_QSTR_from_seed), MP_ROM_PTR(&mod_trezorcrypto_bip32_from_seed_obj) },
2018-06-14 14:28:50 +00:00
{ MP_ROM_QSTR(MP_QSTR_from_mnemonic_cardano), MP_ROM_PTR(&mod_trezorcrypto_bip32_from_mnemonic_cardano_obj) },
2016-10-06 12:00:57 +00:00
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip32_globals, mod_trezorcrypto_bip32_globals_table);
2016-10-06 12:00:57 +00:00
STATIC const mp_obj_module_t mod_trezorcrypto_bip32_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_bip32_globals,
2016-10-06 12:00:57 +00:00
};