1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

core: introduce BITCOIN_ONLY flag

This commit is contained in:
Pavol Rusnak 2019-08-22 20:15:16 +02:00
parent 9ce2fceabe
commit 07ef01f3e4
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
13 changed files with 1727 additions and 1657 deletions

View File

@ -18,6 +18,7 @@ CROSS_PORT_OPTS ?=
PRODUCTION ?= 0
PYOPT ?= 1
BITCOIN_ONLY ?= 0
STLINK_VER ?= v2
OPENOCD = openocd -f interface/stlink-$(STLINK_VER).cfg -c "transport select hla_swd" -f target/stm32f4x.cfg
@ -105,13 +106,13 @@ build_reflash: ## build reflash firmware + reflash image
dd if=build/bootloader/bootloader.bin of=$(REFLASH_BUILD_DIR)/sdimage.bin bs=1 seek=49152
build_firmware: res build_cross ## build firmware with frozen modules
$(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" $(FIRMWARE_BUILD_DIR)/firmware.bin
$(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" BITCOIN_ONLY="$(BITCOIN_ONLY)" $(FIRMWARE_BUILD_DIR)/firmware.bin
build_unix: res ## build unix port
$(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/micropython $(UNIX_PORT_OPTS)
$(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/micropython $(UNIX_PORT_OPTS) BITCOIN_ONLY="$(BITCOIN_ONLY)"
build_unix_frozen: res build_cross ## build unix port with frozen modules
$(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/micropython $(UNIX_PORT_OPTS) PYOPT="$(PYOPT)" TREZOR_EMULATOR_FROZEN=1
$(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/micropython $(UNIX_PORT_OPTS) PYOPT="$(PYOPT)" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_FROZEN=1
build_cross: ## build mpy-cross port
$(MAKE) -C vendor/micropython/mpy-cross $(CROSS_PORT_OPTS)

View File

@ -2,7 +2,8 @@
import os
EVERYTHING = True
BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0')
EVERYTHING = BITCOIN_ONLY != '1'
CCFLAGS_MOD = ''
CPPPATH_MOD = []
@ -326,7 +327,7 @@ SOURCE_TREZORHAL = [
SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_MICROPYTHON_SPEED
env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s -DPYOPT=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'), PYOPT))
env = Environment(ENV=os.environ, CFLAGS='%s -DPRODUCTION=%s -DPYOPT=%s -DBITCOIN_ONLY=%s' % (ARGUMENTS.get('CFLAGS', ''), ARGUMENTS.get('PRODUCTION', '0'), PYOPT, BITCOIN_ONLY))
env.Tool('micropython')
@ -365,7 +366,7 @@ env.Replace(
'-fstack-protector-all '
+ CPU_CCFLAGS + CCFLAGS_MOD,
CCFLAGS_QSTR='-DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB',
LINKFLAGS='-T embed/firmware/memory_${TREZOR_MODEL}.ld -Wl,--gc-sections -Wl,-Map=build/firmware/firmware.map -Wl,--warn-common',
LINKFLAGS='-T embed/firmware/memory_${TREZOR_MODEL}%s.ld -Wl,--gc-sections -Wl,-Map=build/firmware/firmware.map -Wl,--warn-common' % ('' if EVERYTHING else '_min'),
CPPPATH=[
'.',
'embed/firmware',
@ -504,7 +505,7 @@ if FROZEN:
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/webauthn/*.py'))
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR)
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY)
source_mpyc = env.FrozenCFile(
target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed)

View File

@ -2,7 +2,8 @@
import os
EVERYTHING = True
BITCOIN_ONLY = ARGUMENTS.get('BITCOIN_ONLY', '0')
EVERYTHING = BITCOIN_ONLY != '1'
CCFLAGS_MOD = ''
CPPPATH_MOD = []
@ -299,7 +300,7 @@ SOURCE_UNIX = [
SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_UNIX
env = Environment(ENV=os.environ, CFLAGS='%s -DPYOPT=%s' % (ARGUMENTS.get('CFLAGS', ''), PYOPT))
env = Environment(ENV=os.environ, CFLAGS='%s -DPYOPT=%s -DBITCOIN_ONLY=%s' % (ARGUMENTS.get('CFLAGS', ''), PYOPT, BITCOIN_ONLY))
env.Tool('micropython')
@ -467,7 +468,7 @@ if FROZEN:
SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/webauthn/*.py'))
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR)
source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY)
source_mpyc = env.FrozenCFile(
target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed)

View File

@ -25,7 +25,9 @@
#include "bip39.h"
#include "curves.h"
#include "memzero.h"
#if !BITCOIN_ONLY
#include "nem.h"
#endif
/// package: trezorcrypto.bip32
@ -184,6 +186,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_HDNode_derive_obj,
2, 3,
mod_trezorcrypto_HDNode_derive);
#if !BITCOIN_ONLY
/// def derive_cardano(self, index: int) -> None:
/// """
/// Derive a BIP0032 child node in place using Cardano algorithm.
@ -217,6 +221,8 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_cardano(mp_obj_t self,
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_cardano_obj,
mod_trezorcrypto_HDNode_derive_cardano);
#endif
/// def derive_path(self, path: List[int]) -> None:
/// """
/// Go through a list of indexes and iteratively derive a child node in
@ -409,6 +415,8 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self,
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj,
mod_trezorcrypto_HDNode_address);
#if !BITCOIN_ONLY
/// def nem_address(self, network: int) -> str:
/// """
/// Compute a NEM address string from the HD node.
@ -489,6 +497,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(
mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj,
mod_trezorcrypto_HDNode_ethereum_pubkeyhash);
#endif
/// def __del__(self) -> None:
/// """
/// Cleans up sensitive memory.
@ -507,15 +517,16 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_HDNode_locals_dict_table[] = {
MP_ROM_PTR(&mod_trezorcrypto_HDNode___del___obj)},
{MP_ROM_QSTR(MP_QSTR_derive),
MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_obj)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_derive_cardano),
MP_ROM_PTR(&mod_trezorcrypto_HDNode_derive_cardano_obj)},
#endif
{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),
@ -534,12 +545,14 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_HDNode_locals_dict_table[] = {
MP_ROM_PTR(&mod_trezorcrypto_HDNode_public_key_obj)},
{MP_ROM_QSTR(MP_QSTR_address),
MP_ROM_PTR(&mod_trezorcrypto_HDNode_address_obj)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_nem_address),
MP_ROM_PTR(&mod_trezorcrypto_HDNode_nem_address_obj)},
{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)},
#endif
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_HDNode_locals_dict,
mod_trezorcrypto_HDNode_locals_dict_table);
@ -606,8 +619,10 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed,
int res = 0;
if (strcmp(curveb.buf, ED25519_CARDANO_NAME) != 0) {
res = hdnode_from_seed(seedb.buf, seedb.len, curveb.buf, &hdnode);
#if !BITCOIN_ONLY
} else {
res = hdnode_from_seed_cardano(seedb.buf, seedb.len, &hdnode);
#endif
}
if (!res) {
@ -623,6 +638,8 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed,
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip32_from_seed_obj,
mod_trezorcrypto_bip32_from_seed);
#if !BITCOIN_ONLY
/// def from_mnemonic_cardano(mnemonic: str, passphrase: str) -> bytes:
/// """
/// Construct a HD node from a BIP-0039 mnemonic using the Icarus derivation
@ -661,11 +678,12 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(
o->fingerprint = 0;
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);
#endif
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)},
@ -673,8 +691,10 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip32_globals_table[] = {
MP_ROM_PTR(&mod_trezorcrypto_bip32_deserialize_obj)},
{MP_ROM_QSTR(MP_QSTR_from_seed),
MP_ROM_PTR(&mod_trezorcrypto_bip32_from_seed_obj)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_from_mnemonic_cardano),
MP_ROM_PTR(&mod_trezorcrypto_bip32_from_mnemonic_cardano_obj)},
#endif
};
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip32_globals,
mod_trezorcrypto_bip32_globals_table);

View File

@ -100,6 +100,8 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_sign(size_t n_args,
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_ed25519_sign_obj, 2,
3, mod_trezorcrypto_ed25519_sign);
#if !BITCOIN_ONLY
/// def sign_ext(
/// secret_key: bytes, secret_extension: bytes, message: bytes
/// ) -> bytes:
@ -135,6 +137,8 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_sign_ext(mp_obj_t secret_key,
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorcrypto_ed25519_sign_ext_obj,
mod_trezorcrypto_ed25519_sign_ext);
#endif
/// def verify(public_key: bytes, signature: bytes, message: bytes) -> bool:
/// """
/// Uses public key to verify the signature of the message.
@ -282,8 +286,10 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_ed25519_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_publickey),
MP_ROM_PTR(&mod_trezorcrypto_ed25519_publickey_obj)},
{MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&mod_trezorcrypto_ed25519_sign_obj)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_sign_ext),
MP_ROM_PTR(&mod_trezorcrypto_ed25519_sign_ext_obj)},
#endif
{MP_ROM_QSTR(MP_QSTR_verify),
MP_ROM_PTR(&mod_trezorcrypto_ed25519_verify_obj)},
{MP_ROM_QSTR(MP_QSTR_cosi_combine_publickeys),

View File

@ -36,15 +36,12 @@
#include "modtrezorcrypto-curve25519.h"
#include "modtrezorcrypto-ed25519.h"
#include "modtrezorcrypto-groestl.h"
#include "modtrezorcrypto-monero.h"
#include "modtrezorcrypto-nem.h"
#include "modtrezorcrypto-nist256p1.h"
#include "modtrezorcrypto-pbkdf2.h"
#include "modtrezorcrypto-random.h"
#include "modtrezorcrypto-rfc6979.h"
#include "modtrezorcrypto-ripemd160.h"
#include "modtrezorcrypto-secp256k1.h"
#include "modtrezorcrypto-secp256k1_zkp.h"
#include "modtrezorcrypto-sha1.h"
#include "modtrezorcrypto-sha256.h"
#include "modtrezorcrypto-sha3-256.h"
@ -52,6 +49,11 @@
#include "modtrezorcrypto-sha512.h"
#include "modtrezorcrypto-shamir.h"
#include "modtrezorcrypto-slip39.h"
#if !BITCOIN_ONLY
#include "modtrezorcrypto-monero.h"
#include "modtrezorcrypto-nem.h"
#include "modtrezorcrypto-secp256k1_zkp.h"
#endif
STATIC const mp_rom_map_elem_t mp_module_trezorcrypto_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorcrypto)},
@ -69,12 +71,16 @@ STATIC const mp_rom_map_elem_t mp_module_trezorcrypto_globals_table[] = {
MP_ROM_PTR(&mod_trezorcrypto_curve25519_module)},
{MP_ROM_QSTR(MP_QSTR_ed25519),
MP_ROM_PTR(&mod_trezorcrypto_ed25519_module)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_monero), MP_ROM_PTR(&mod_trezorcrypto_monero_module)},
#endif
{MP_ROM_QSTR(MP_QSTR_nist256p1),
MP_ROM_PTR(&mod_trezorcrypto_nist256p1_module)},
{MP_ROM_QSTR(MP_QSTR_groestl512),
MP_ROM_PTR(&mod_trezorcrypto_Groestl512_type)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_nem), MP_ROM_PTR(&mod_trezorcrypto_nem_module)},
#endif
{MP_ROM_QSTR(MP_QSTR_pbkdf2), MP_ROM_PTR(&mod_trezorcrypto_Pbkdf2_type)},
{MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_trezorcrypto_random_module)},
{MP_ROM_QSTR(MP_QSTR_rfc6979), MP_ROM_PTR(&mod_trezorcrypto_Rfc6979_type)},
@ -82,8 +88,10 @@ STATIC const mp_rom_map_elem_t mp_module_trezorcrypto_globals_table[] = {
MP_ROM_PTR(&mod_trezorcrypto_Ripemd160_type)},
{MP_ROM_QSTR(MP_QSTR_secp256k1),
MP_ROM_PTR(&mod_trezorcrypto_secp256k1_module)},
#if !BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_secp256k1_zkp),
MP_ROM_PTR(&mod_trezorcrypto_secp256k1_zkp_module)},
#endif
{MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&mod_trezorcrypto_Sha1_type)},
{MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&mod_trezorcrypto_Sha256_type)},
{MP_ROM_QSTR(MP_QSTR_sha512), MP_ROM_PTR(&mod_trezorcrypto_Sha512_type)},

View File

@ -147,6 +147,11 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
#else
{MP_ROM_QSTR(MP_QSTR_EMULATOR), mp_const_false},
#endif
#if BITCOIN_ONLY
{MP_ROM_QSTR(MP_QSTR_BITCOIN_ONLY), mp_const_true},
#else
{MP_ROM_QSTR(MP_QSTR_BITCOIN_ONLY), mp_const_false},
#endif
};
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorutils_globals,

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
# generated from coininfo.py.mako
# do not edit manually!
from trezor import utils
from trezor.crypto.base58 import blake256d_32, groestl512d_32, keccak_32, sha256d_32
from trezor.crypto.scripts import blake256_ripemd160_digest, sha256_ripemd160_digest
@ -108,16 +109,33 @@ ATTRIBUTES = (
("curve_name", lambda r: repr(r.replace("_", "-"))),
("confidential_assets", optional_dict),
)
btc_names = ["Bitcoin", "Testnet", "Regtest"]
coins_btc = [c for c in supported_on("trezor2", bitcoin) if c.name in btc_names]
coins_alt = [c for c in supported_on("trezor2", bitcoin) if c.name not in btc_names]
%>\
def by_name(name: str) -> CoinInfo:
if False:
pass
% for coin in supported_on("trezor2", bitcoin):
% for coin in coins_btc:
elif name == ${black_repr(coin["coin_name"])}:
return CoinInfo(
% for attr, func in ATTRIBUTES:
${attr}=${func(coin[attr])},
% endfor
)
% endfor
if not utils.BITCOIN_ONLY:
if False:
pass
% for coin in coins_alt:
elif name == ${black_repr(coin["coin_name"])}:
return CoinInfo(
% for attr, func in ATTRIBUTES:
${attr}=${func(coin[attr])},
% endfor
)
% endfor
raise ValueError('Unknown coin name "%s"' % name)

View File

@ -32,40 +32,45 @@ def _boot_default() -> None:
import apps.homescreen
import apps.management
import apps.wallet
import apps.ethereum
import apps.lisk
import apps.monero
import apps.nem
import apps.stellar
import apps.ripple
import apps.cardano
import apps.tezos
import apps.eos
import apps.binance
if not utils.BITCOIN_ONLY:
import apps.ethereum
import apps.lisk
import apps.monero
import apps.nem
import apps.stellar
import apps.ripple
import apps.cardano
import apps.tezos
import apps.eos
import apps.binance
if __debug__:
import apps.debug
else:
import apps.webauthn
if not utils.BITCOIN_ONLY:
import apps.webauthn
# boot applications
apps.homescreen.boot()
apps.management.boot()
apps.wallet.boot()
apps.ethereum.boot()
apps.lisk.boot()
apps.monero.boot()
apps.nem.boot()
apps.stellar.boot()
apps.ripple.boot()
apps.cardano.boot()
apps.tezos.boot()
apps.eos.boot()
apps.binance.boot()
if not utils.BITCOIN_ONLY:
apps.ethereum.boot()
apps.lisk.boot()
apps.monero.boot()
apps.nem.boot()
apps.stellar.boot()
apps.ripple.boot()
apps.cardano.boot()
apps.tezos.boot()
apps.eos.boot()
apps.binance.boot()
if __debug__:
apps.debug.boot()
else:
apps.webauthn.boot(usb.iface_webauthn)
if not utils.BITCOIN_ONLY:
apps.webauthn.boot(usb.iface_webauthn)
# run main event loop and specify which screen is the default
from apps.homescreen.homescreen import homescreen

View File

@ -1,12 +1,14 @@
from trezor import utils
from trezorcrypto import ( # noqa: F401
aes,
bip32,
bip39,
chacha20poly1305,
crc,
monero,
nem,
pbkdf2,
random,
rfc6979,
)
if not utils.BITCOIN_ONLY:
from trezorcrypto import monero, nem # noqa: F401

View File

@ -1,7 +1,5 @@
from trezorcrypto import ( # noqa: F401
curve25519,
ed25519,
nist256p1,
secp256k1,
secp256k1_zkp,
)
from trezor import utils
from trezorcrypto import curve25519, ed25519, nist256p1, secp256k1 # noqa: F401
if not utils.BITCOIN_ONLY:
from trezorcrypto import secp256k1_zkp # noqa: F401

View File

@ -1,6 +1,7 @@
import gc
import sys
from trezorutils import ( # noqa: F401
BITCOIN_ONLY,
EMULATOR,
GITREV,
MODEL,