From 045ef22d98e22a19971ad8f6787241a940c2463b Mon Sep 17 00:00:00 2001 From: Saleem Rashid Date: Mon, 18 Dec 2017 21:16:05 +0000 Subject: [PATCH] storage: Do not use Nanopb --- firmware/Makefile | 1 - firmware/fsm.c | 2 +- firmware/protob/Makefile | 2 +- firmware/protob/storage.options | 5 --- firmware/protob/storage.proto | 1 - firmware/storage.c | 55 +++++++++++++++++++++++++-------- firmware/storage.h | 55 +++++++++++++++++++++++++++++++-- 7 files changed, 97 insertions(+), 24 deletions(-) delete mode 100644 firmware/protob/storage.options delete mode 120000 firmware/protob/storage.proto diff --git a/firmware/Makefile b/firmware/Makefile index 577d20e34..714a823d0 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -79,7 +79,6 @@ OBJS += ../vendor/trezor-qrenc/qr_encode.o OBJS += protob/pb_decode.o OBJS += protob/pb_encode.o OBJS += protob/messages.pb.o -OBJS += protob/storage.pb.o OBJS += protob/types.pb.o include ../Makefile.include diff --git a/firmware/fsm.c b/firmware/fsm.c index f88e3710a..b57d4225f 100644 --- a/firmware/fsm.c +++ b/firmware/fsm.c @@ -1627,7 +1627,7 @@ void fsm_msgDebugLinkGetState(DebugLinkGetState *msg) if (storage_hasNode()) { resp.has_node = true; - memcpy(&(resp.node), storage_getNode(), sizeof(HDNode)); + storage_dumpNode(&(resp.node)); } resp.has_passphrase_protection = true; diff --git a/firmware/protob/Makefile b/firmware/protob/Makefile index 34a4739bb..8dc4c38fd 100644 --- a/firmware/protob/Makefile +++ b/firmware/protob/Makefile @@ -1,4 +1,4 @@ -all: messages.pb.c storage.pb.c types.pb.c messages_map.h +all: messages.pb.c types.pb.c messages_map.h %.pb.c: %.pb %.options ../../vendor/nanopb/generator/nanopb_generator.py $< -L '#include "%s"' -T diff --git a/firmware/protob/storage.options b/firmware/protob/storage.options deleted file mode 100644 index b7b6db2e9..000000000 --- a/firmware/protob/storage.options +++ /dev/null @@ -1,5 +0,0 @@ -Storage.mnemonic max_size:241 -Storage.pin max_size:10 -Storage.language max_size:17 -Storage.label max_size:33 -Storage.homescreen max_size:1024 diff --git a/firmware/protob/storage.proto b/firmware/protob/storage.proto deleted file mode 120000 index 7502e62f6..000000000 --- a/firmware/protob/storage.proto +++ /dev/null @@ -1 +0,0 @@ -../../vendor/trezor-common/protob/storage.proto \ No newline at end of file diff --git a/firmware/storage.c b/firmware/storage.c index 8b982ea1a..04e705d2e 100644 --- a/firmware/storage.c +++ b/firmware/storage.c @@ -23,7 +23,6 @@ #include #include "messages.pb.h" -#include "storage.pb.h" #include "trezor.h" #include "sha2.h" @@ -278,7 +277,7 @@ static void get_u2froot_callback(uint32_t iter, uint32_t total) layoutProgress(_("Updating"), 1000 * iter / total); } -static void storage_compute_u2froot(const char* mnemonic, HDNodeType *u2froot) { +static void storage_compute_u2froot(const char* mnemonic, StorageHDNode *u2froot) { static CONFIDENTIAL HDNode node; char oldTiny = usbTiny(1); mnemonic_to_seed(mnemonic, "", sessionSeed, get_u2froot_callback); // BIP-0039 @@ -312,11 +311,11 @@ static void storage_commit_locked(bool update) storageUpdate.version = STORAGE_VERSION; if (!storageUpdate.has_node && !storageUpdate.has_mnemonic) { storageUpdate.has_node = storageRom->has_node; - memcpy(&storageUpdate.node, &storageRom->node, sizeof(HDNodeType)); + memcpy(&storageUpdate.node, &storageRom->node, sizeof(StorageHDNode)); storageUpdate.has_mnemonic = storageRom->has_mnemonic; strlcpy(storageUpdate.mnemonic, storageRom->mnemonic, sizeof(storageUpdate.mnemonic)); storageUpdate.has_u2froot = storageRom->has_u2froot; - memcpy(&storageUpdate.u2froot, &storageRom->u2froot, sizeof(HDNodeType)); + memcpy(&storageUpdate.u2froot, &storageRom->u2froot, sizeof(StorageHDNode)); } else if (storageUpdate.has_mnemonic) { storageUpdate.has_u2froot = true; storage_compute_u2froot(storageUpdate.mnemonic, &storageUpdate.u2froot); @@ -406,6 +405,38 @@ void storage_update(void) storage_check_flash_errors(); } +static void storage_setNode(const HDNodeType *node) { + storageUpdate.node.depth = node->depth; + storageUpdate.node.fingerprint = node->fingerprint; + storageUpdate.node.child_num = node->child_num; + + storageUpdate.node.chain_code.size = 32; + memcpy(storageUpdate.node.chain_code.bytes, node->chain_code.bytes, 32); + + if (node->has_private_key) { + storageUpdate.node.has_private_key = true; + storageUpdate.node.private_key.size = 32; + memcpy(storageUpdate.node.private_key.bytes, node->private_key.bytes, 32); + } +} + +#if DEBUG_LINK +void storage_dumpNode(HDNodeType *node) { + node->depth = storageRom->node.depth; + node->fingerprint = storageRom->node.fingerprint; + node->child_num = storageRom->node.child_num; + + node->chain_code.size = 32; + memcpy(node->chain_code.bytes, storageRom->node.chain_code.bytes, 32); + + if (storageRom->node.has_private_key) { + node->has_private_key = true; + node->private_key.size = 32; + memcpy(node->private_key.bytes, storageRom->node.private_key.bytes, 32); + } +} +#endif + void storage_loadDevice(LoadDevice *msg) { session_clear(true); @@ -419,7 +450,7 @@ void storage_loadDevice(LoadDevice *msg) if (msg->has_node) { storageUpdate.has_node = true; storageUpdate.has_mnemonic = false; - memcpy(&storageUpdate.node, &(msg->node), sizeof(HDNodeType)); + storage_setNode(&(msg->node)); sessionSeedCached = false; memset(&sessionSeed, 0, sizeof(sessionSeed)); } else if (msg->has_mnemonic) { @@ -526,10 +557,13 @@ const uint8_t *storage_getSeed(bool usePassphrase) return NULL; } +static bool storage_loadNode(const StorageHDNode *node, const char *curve, HDNode *out) { + return hdnode_from_xprv(node->depth, node->child_num, node->chain_code.bytes, node->private_key.bytes, curve, out); +} + bool storage_getU2FRoot(HDNode *node) { - return storageRom->has_u2froot - && hdnode_from_xprv(storageRom->u2froot.depth, storageRom->u2froot.child_num, storageRom->u2froot.chain_code.bytes, storageRom->u2froot.private_key.bytes, NIST256P1_NAME, node); + return storageRom->has_u2froot && storage_loadNode(&storageRom->u2froot, NIST256P1_NAME, node); } bool storage_getRootNode(HDNode *node, const char *curve, bool usePassphrase) @@ -539,7 +573,7 @@ bool storage_getRootNode(HDNode *node, const char *curve, bool usePassphrase) if (!protectPassphrase()) { return false; } - if (hdnode_from_xprv(storageRom->node.depth, storageRom->node.child_num, storageRom->node.chain_code.bytes, storageRom->node.private_key.bytes, curve, node) == 0) { + if (!storage_loadNode(&storageRom->node, curve, node)) { return false; } if (storageRom->has_passphrase_protection && storageRom->passphrase_protection && sessionPassphraseCached && strlen(sessionPassphrase) > 0) { @@ -595,11 +629,6 @@ bool storage_hasNode(void) return storageRom->has_node; } -const HDNode *storage_getNode(void) -{ - return storageRom->has_node ? (const HDNode *)&storageRom->node : 0; -} - bool storage_hasMnemonic(void) { return storageRom->has_mnemonic; diff --git a/firmware/storage.h b/firmware/storage.h index 9d7935258..a8a883299 100644 --- a/firmware/storage.h +++ b/firmware/storage.h @@ -21,10 +21,59 @@ #define __STORAGE_H__ #include "types.pb.h" -#include "storage.pb.h" #include "messages.pb.h" #include "bip32.h" +#define STORAGE_FIELD(TYPE, NAME) \ + bool has_##NAME; \ + TYPE NAME; + +#define STORAGE_STRING(NAME, SIZE) \ + bool has_##NAME; \ + char NAME[SIZE]; + +#define STORAGE_BYTES(NAME, SIZE) \ + bool has_##NAME; \ + struct { \ + size_t size; \ + uint8_t bytes[SIZE]; \ + } NAME; + +#define STORAGE_BOOL(NAME) STORAGE_FIELD(bool, NAME) +#define STORAGE_NODE(NAME) STORAGE_FIELD(StorageHDNode, NAME) +#define STORAGE_UINT32(NAME) STORAGE_FIELD(uint32_t, NAME) + +typedef struct { + uint32_t depth; + uint32_t fingerprint; + uint32_t child_num; + struct { + size_t size; + uint8_t bytes[32]; + } chain_code; + + STORAGE_BYTES(private_key, 32); + STORAGE_BYTES(public_key, 33); +} StorageHDNode; + +typedef struct _Storage { + uint32_t version; + + STORAGE_NODE (node) + STORAGE_STRING (mnemonic, 241) + STORAGE_BOOL (passphrase_protection) + STORAGE_UINT32 (pin_failed_attempts) + STORAGE_STRING (pin, 10) + STORAGE_STRING (language, 17) + STORAGE_STRING (label, 33) + STORAGE_BOOL (imported) + STORAGE_BYTES (homescreen, 1024) + STORAGE_UINT32 (u2f_counter) + STORAGE_BOOL (needs_backup) + STORAGE_UINT32 (flags) + STORAGE_NODE (u2froot) +} Storage; + extern Storage storageUpdate; void storage_init(void); @@ -61,7 +110,9 @@ bool storage_hasMnemonic(void); const char *storage_getMnemonic(void); bool storage_hasNode(void); -const HDNode *storage_getNode(void); +#if DEBUG_LINK +void storage_dumpNode(HDNodeType *node); +#endif bool storage_containsPin(const char *pin); bool storage_hasPin(void);