From c8ad3550328ece002bcd56dd54f85529841e293b Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 26 Oct 2018 12:09:10 +0200 Subject: [PATCH] cardano: add low-level support for passhrase --- embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h | 12 +++++++----- src/apps/cardano/get_address.py | 2 +- src/apps/cardano/get_public_key.py | 2 +- src/apps/cardano/sign_tx.py | 2 +- tests/test_apps.cardano.address.py | 12 ++++++++---- tests/test_apps.cardano.get_public_key.py | 3 ++- 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index 76f696b1d..e65b3a900 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -504,15 +504,17 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed, mp_obj_t curve_n } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_seed_obj, mod_trezorcrypto_bip32_from_seed); -/// def from_mnemonic_cardano(mnemonic: str) -> bytes: +/// def from_mnemonic_cardano(mnemonic: str, passphrase: str) -> bytes: /// ''' /// Convert mnemonic to hdnode /// ''' -STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(mp_obj_t mnemonic) { - mp_buffer_info_t mnemo; +STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(mp_obj_t mnemonic, mp_obj_t passphrase) { + mp_buffer_info_t mnemo, phrase; mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ); + mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ); 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); @@ -521,7 +523,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(mp_obj_t mnemonic) mp_raise_ValueError("Invalid mnemonic"); } - const int res = hdnode_from_seed_cardano((const uint8_t *)"", 0, entropy, entropy_len / 8, &hdnode); + const int res = hdnode_from_seed_cardano((const uint8_t *)ppassphrase, phrase.len, entropy, entropy_len / 8, &hdnode); if (!res) { mp_raise_ValueError("Secret key generation from mnemonic is looping forever"); @@ -536,7 +538,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(mp_obj_t mnemonic) return MP_OBJ_FROM_PTR(o); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip32_from_mnemonic_cardano_obj, +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_mnemonic_cardano_obj, mod_trezorcrypto_bip32_from_mnemonic_cardano); diff --git a/src/apps/cardano/get_address.py b/src/apps/cardano/get_address.py index 2e81f0073..db22e6207 100644 --- a/src/apps/cardano/get_address.py +++ b/src/apps/cardano/get_address.py @@ -10,7 +10,7 @@ from apps.common import storage async def get_address(ctx, msg): mnemonic = storage.get_mnemonic() - root_node = bip32.from_mnemonic_cardano(mnemonic) + root_node = bip32.from_mnemonic_cardano(mnemonic, "") try: address, _ = derive_address_and_node(root_node, msg.address_n) diff --git a/src/apps/cardano/get_public_key.py b/src/apps/cardano/get_public_key.py index 813e0ac85..9c80b2602 100644 --- a/src/apps/cardano/get_public_key.py +++ b/src/apps/cardano/get_public_key.py @@ -12,7 +12,7 @@ from apps.common import layout, seed, storage async def get_public_key(ctx, msg): mnemonic = storage.get_mnemonic() - root_node = bip32.from_mnemonic_cardano(mnemonic) + root_node = bip32.from_mnemonic_cardano(mnemonic, "") try: key = _get_public_key(root_node, msg.address_n) diff --git a/src/apps/cardano/sign_tx.py b/src/apps/cardano/sign_tx.py index be5e976f0..547bdeb12 100644 --- a/src/apps/cardano/sign_tx.py +++ b/src/apps/cardano/sign_tx.py @@ -80,7 +80,7 @@ async def request_transaction(ctx, tx_req: CardanoTxRequest, index: int): async def sign_tx(ctx, msg): mnemonic = storage.get_mnemonic() - root_node = bip32.from_mnemonic_cardano(mnemonic) + root_node = bip32.from_mnemonic_cardano(mnemonic, "") progress.init(msg.transactions_count, "Loading data") diff --git a/tests/test_apps.cardano.address.py b/tests/test_apps.cardano.address.py index dbe82f3d3..12454d3db 100644 --- a/tests/test_apps.cardano.address.py +++ b/tests/test_apps.cardano.address.py @@ -14,7 +14,8 @@ from trezor.crypto import bip32 class TestCardanoAddress(unittest.TestCase): def test_hardened_address_derivation_scheme(self): mnemonic = "all all all all all all all all all all all all" - node = bip32.from_mnemonic_cardano(mnemonic) + passphrase = "" + node = bip32.from_mnemonic_cardano(mnemonic, passphrase) addresses = [ "Ae2tdPwUPEZ98eHFwxSsPBDz73amioKpr58Vw85mP1tMkzq8siaftiejJ3j", @@ -57,7 +58,8 @@ class TestCardanoAddress(unittest.TestCase): def test_non_hardened_address_derivation_scheme(self): mnemonic = "all all all all all all all all all all all all" - node = bip32.from_mnemonic_cardano(mnemonic) + passphrase = "" + node = bip32.from_mnemonic_cardano(mnemonic, passphrase) addresses = [ "Ae2tdPwUPEZ5YUb8sM3eS8JqKgrRLzhiu71crfuH2MFtqaYr5ACNRdsswsZ", @@ -101,7 +103,8 @@ class TestCardanoAddress(unittest.TestCase): def test_root_address_derivation_scheme(self): mnemonic = "all all all all all all all all all all all all" - node = bip32.from_mnemonic_cardano(mnemonic) + passphrase = "" + node = bip32.from_mnemonic_cardano(mnemonic, passphrase) # 44'/1815' address, _ = derive_address_and_node(node, [0x80000000 | 44, 0x80000000 | 1815]) @@ -152,7 +155,8 @@ class TestCardanoAddress(unittest.TestCase): def test_get_address_root_scheme(self): mnemonic = "all all all all all all all all all all all all" - root_node = bip32.from_mnemonic_cardano(mnemonic) + passphrase = "" + root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase) address_root = _get_address_root(root_node, {1: b'X\x1cr,zu\x81?\xaf\xde\x9f\xf9\xe4\xd4\x90\xadH$\xe9\xf3\x88\x16\xcb\xd2)\x02M\x0c#\xde'}) self.assertEqual(address_root, b'\xb3\xbbS\xa8;uN:E=\xe8\xe5\x9c\x18\xbcn\xcf\xd0c\xba\x0e\xba\xaelL}\xba\xbb') diff --git a/tests/test_apps.cardano.get_public_key.py b/tests/test_apps.cardano.get_public_key.py index 4021688d1..e9b7fdfa4 100644 --- a/tests/test_apps.cardano.get_public_key.py +++ b/tests/test_apps.cardano.get_public_key.py @@ -8,7 +8,8 @@ from ubinascii import hexlify class TestCardanoGetPublicKey(unittest.TestCase): def test_get_public_key_scheme(self): mnemonic = "all all all all all all all all all all all all" - node = bip32.from_mnemonic_cardano(mnemonic) + passphrase = "" + node = bip32.from_mnemonic_cardano(mnemonic, passphrase) derivation_paths = [ [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],