1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +00:00

storage: Do not use Nanopb

This commit is contained in:
Saleem Rashid 2017-12-18 21:16:05 +00:00 committed by Pavol Rusnak
parent bb1c42b16c
commit 045ef22d98
7 changed files with 97 additions and 24 deletions

View File

@ -79,7 +79,6 @@ OBJS += ../vendor/trezor-qrenc/qr_encode.o
OBJS += protob/pb_decode.o OBJS += protob/pb_decode.o
OBJS += protob/pb_encode.o OBJS += protob/pb_encode.o
OBJS += protob/messages.pb.o OBJS += protob/messages.pb.o
OBJS += protob/storage.pb.o
OBJS += protob/types.pb.o OBJS += protob/types.pb.o
include ../Makefile.include include ../Makefile.include

View File

@ -1627,7 +1627,7 @@ void fsm_msgDebugLinkGetState(DebugLinkGetState *msg)
if (storage_hasNode()) { if (storage_hasNode()) {
resp.has_node = true; resp.has_node = true;
memcpy(&(resp.node), storage_getNode(), sizeof(HDNode)); storage_dumpNode(&(resp.node));
} }
resp.has_passphrase_protection = true; resp.has_passphrase_protection = true;

View File

@ -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 %.pb.c: %.pb %.options
../../vendor/nanopb/generator/nanopb_generator.py $< -L '#include "%s"' -T ../../vendor/nanopb/generator/nanopb_generator.py $< -L '#include "%s"' -T

View File

@ -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

View File

@ -1 +0,0 @@
../../vendor/trezor-common/protob/storage.proto

View File

@ -23,7 +23,6 @@
#include <libopencm3/stm32/flash.h> #include <libopencm3/stm32/flash.h>
#include "messages.pb.h" #include "messages.pb.h"
#include "storage.pb.h"
#include "trezor.h" #include "trezor.h"
#include "sha2.h" #include "sha2.h"
@ -278,7 +277,7 @@ static void get_u2froot_callback(uint32_t iter, uint32_t total)
layoutProgress(_("Updating"), 1000 * iter / 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; static CONFIDENTIAL HDNode node;
char oldTiny = usbTiny(1); char oldTiny = usbTiny(1);
mnemonic_to_seed(mnemonic, "", sessionSeed, get_u2froot_callback); // BIP-0039 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; storageUpdate.version = STORAGE_VERSION;
if (!storageUpdate.has_node && !storageUpdate.has_mnemonic) { if (!storageUpdate.has_node && !storageUpdate.has_mnemonic) {
storageUpdate.has_node = storageRom->has_node; 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; storageUpdate.has_mnemonic = storageRom->has_mnemonic;
strlcpy(storageUpdate.mnemonic, storageRom->mnemonic, sizeof(storageUpdate.mnemonic)); strlcpy(storageUpdate.mnemonic, storageRom->mnemonic, sizeof(storageUpdate.mnemonic));
storageUpdate.has_u2froot = storageRom->has_u2froot; 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) { } else if (storageUpdate.has_mnemonic) {
storageUpdate.has_u2froot = true; storageUpdate.has_u2froot = true;
storage_compute_u2froot(storageUpdate.mnemonic, &storageUpdate.u2froot); storage_compute_u2froot(storageUpdate.mnemonic, &storageUpdate.u2froot);
@ -406,6 +405,38 @@ void storage_update(void)
storage_check_flash_errors(); 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) void storage_loadDevice(LoadDevice *msg)
{ {
session_clear(true); session_clear(true);
@ -419,7 +450,7 @@ void storage_loadDevice(LoadDevice *msg)
if (msg->has_node) { if (msg->has_node) {
storageUpdate.has_node = true; storageUpdate.has_node = true;
storageUpdate.has_mnemonic = false; storageUpdate.has_mnemonic = false;
memcpy(&storageUpdate.node, &(msg->node), sizeof(HDNodeType)); storage_setNode(&(msg->node));
sessionSeedCached = false; sessionSeedCached = false;
memset(&sessionSeed, 0, sizeof(sessionSeed)); memset(&sessionSeed, 0, sizeof(sessionSeed));
} else if (msg->has_mnemonic) { } else if (msg->has_mnemonic) {
@ -526,10 +557,13 @@ const uint8_t *storage_getSeed(bool usePassphrase)
return NULL; 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) bool storage_getU2FRoot(HDNode *node)
{ {
return storageRom->has_u2froot return storageRom->has_u2froot && storage_loadNode(&storageRom->u2froot, NIST256P1_NAME, node);
&& hdnode_from_xprv(storageRom->u2froot.depth, storageRom->u2froot.child_num, storageRom->u2froot.chain_code.bytes, storageRom->u2froot.private_key.bytes, NIST256P1_NAME, node);
} }
bool storage_getRootNode(HDNode *node, const char *curve, bool usePassphrase) 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()) { if (!protectPassphrase()) {
return false; 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; return false;
} }
if (storageRom->has_passphrase_protection && storageRom->passphrase_protection && sessionPassphraseCached && strlen(sessionPassphrase) > 0) { if (storageRom->has_passphrase_protection && storageRom->passphrase_protection && sessionPassphraseCached && strlen(sessionPassphrase) > 0) {
@ -595,11 +629,6 @@ bool storage_hasNode(void)
return storageRom->has_node; return storageRom->has_node;
} }
const HDNode *storage_getNode(void)
{
return storageRom->has_node ? (const HDNode *)&storageRom->node : 0;
}
bool storage_hasMnemonic(void) bool storage_hasMnemonic(void)
{ {
return storageRom->has_mnemonic; return storageRom->has_mnemonic;

View File

@ -21,10 +21,59 @@
#define __STORAGE_H__ #define __STORAGE_H__
#include "types.pb.h" #include "types.pb.h"
#include "storage.pb.h"
#include "messages.pb.h" #include "messages.pb.h"
#include "bip32.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; extern Storage storageUpdate;
void storage_init(void); void storage_init(void);
@ -61,7 +110,9 @@ bool storage_hasMnemonic(void);
const char *storage_getMnemonic(void); const char *storage_getMnemonic(void);
bool storage_hasNode(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_containsPin(const char *pin);
bool storage_hasPin(void); bool storage_hasPin(void);