From 0443a1e11ae88b2f45e9f4ea60449e136afd69fa Mon Sep 17 00:00:00 2001 From: Struan Clark Date: Wed, 1 Mar 2023 23:49:38 -0700 Subject: [PATCH] updating to add beginnings of key generation --- crypto/bip39.c | 3 ++- views/flipbip39_scene_1.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crypto/bip39.c b/crypto/bip39.c index adbdd210422..961be0d0437 100644 --- a/crypto/bip39.c +++ b/crypto/bip39.c @@ -189,7 +189,8 @@ void mnemonic_to_seed(const char *mnemonic, const char *passphrase, void (*progress_callback)(uint32_t current, uint32_t total)) { int mnemoniclen = strlen(mnemonic); - int passphraselen = strnlen(passphrase, 256); + int passphraselen = strlen(passphrase); + if (passphraselen > 256) passphraselen = 256; #if USE_BIP39_CACHE // check cache if (mnemoniclen < 256 && passphraselen < 64) { diff --git a/views/flipbip39_scene_1.c b/views/flipbip39_scene_1.c index b654cd57fbd..6206913ce28 100644 --- a/views/flipbip39_scene_1.c +++ b/views/flipbip39_scene_1.c @@ -24,6 +24,7 @@ struct FlipBip39Scene1 { typedef struct { int strength; + const char* seed; const char* mnemonic1; const char* mnemonic2; const char* mnemonic3; @@ -63,7 +64,7 @@ void flipbip39_scene_1_draw(Canvas* canvas, FlipBip39Scene1Model* model) { static void flipbip39_scene_1_model_init(FlipBip39Scene1Model* const model, const int strength) { // Generate a random mnemonic using trezor-crypto model->strength = strength; - const char* mnemonic = mnemonic_generate(strength); + const char* mnemonic = mnemonic_generate(model->strength); // Delineate 6 sections of the mnemonic char *str = malloc(strlen(mnemonic) + 1); @@ -78,7 +79,12 @@ static void flipbip39_scene_1_model_init(FlipBip39Scene1Model* const model, cons } } - // Split the mnemonic into 6 parts + // Generate a seed from the mnemonic + uint8_t seed[64]; + //mnemonic_to_seed(mnemonic, "", seed, 0); + model->seed = (char *)seed; + + // Split the mnemonic into parts char *ptr = strtok (str, ","); int partnum = 0; while(ptr != NULL) @@ -97,12 +103,11 @@ static void flipbip39_scene_1_model_init(FlipBip39Scene1Model* const model, cons ptr = strtok(NULL, ","); } + // WIP / TODO: Generate a BIP32 root key from the mnemonic // //bool root_set = false; // HDNode root; - // uint8_t seed[64]; - // mnemonic_to_seed(mnemonic, "", seed, 0); // hdnode_from_seed(seed, 64, SECP256K1_NAME, &root); // //root_set = true; @@ -192,6 +197,7 @@ void flipbip39_scene_1_exit(void* context) { { // Clear the mnemonic from memory model->strength = 0; + memzero((void*)model->seed, strlen(model->seed)); memzero((void*)model->mnemonic1, strlen(model->mnemonic1)); memzero((void*)model->mnemonic2, strlen(model->mnemonic2)); memzero((void*)model->mnemonic3, strlen(model->mnemonic3));