From 61adb0a62b5cb0566b5bb5733091d86947731896 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 30 May 2022 11:13:24 +0200 Subject: [PATCH] feat(crypto): expose BIP39 wordlist --- crypto/bip39.c | 24 ++++++++++----------- crypto/bip39.h | 2 ++ crypto/{bip39_english.h => bip39_english.c} | 6 ++++-- 3 files changed, 18 insertions(+), 14 deletions(-) rename crypto/{bip39_english.h => bip39_english.c} (99%) diff --git a/crypto/bip39.c b/crypto/bip39.c index 95ebed3db1..2dae55c7b2 100644 --- a/crypto/bip39.c +++ b/crypto/bip39.c @@ -25,7 +25,6 @@ #include #include "bip39.h" -#include "bip39_english.h" #include "hmac.h" #include "memzero.h" #include "options.h" @@ -87,8 +86,8 @@ const char *mnemonic_from_data(const uint8_t *data, int len) { idx <<= 1; idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0; } - strcpy(p, wordlist[idx]); - p += strlen(wordlist[idx]); + strcpy(p, BIP39_WORDLIST_ENGLISH[idx]); + p += strlen(BIP39_WORDLIST_ENGLISH[idx]); *p = (i < mlen - 1) ? ' ' : 0; p++; } @@ -144,10 +143,11 @@ int mnemonic_to_bits(const char *mnemonic, uint8_t *bits) { } k = 0; for (;;) { - if (!wordlist[k]) { // word not found + if (!BIP39_WORDLIST_ENGLISH[k]) { // word not found return 0; } - if (strcmp(current_word, wordlist[k]) == 0) { // word found on index k + if (strcmp(current_word, BIP39_WORDLIST_ENGLISH[k]) == + 0) { // word found on index k for (ki = 0; ki < 11; ki++) { if (k & (1 << (10 - ki))) { result[bi / 8] |= 1 << (7 - (bi % 8)); @@ -245,7 +245,7 @@ int mnemonic_find_word(const char *word) { int lo = 0, hi = BIP39_WORDS - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; - int cmp = strcmp(word, wordlist[mid]); + int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]); if (cmp == 0) { return mid; } @@ -261,9 +261,9 @@ int mnemonic_find_word(const char *word) { const char *mnemonic_complete_word(const char *prefix, int len) { // we need to perform linear search, // because we want to return the first match - for (const char *const *w = wordlist; *w != 0; w++) { - if (strncmp(*w, prefix, len) == 0) { - return *w; + for (int i = 0; i < BIP39_WORDS; i++) { + if (strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) { + return BIP39_WORDLIST_ENGLISH[i]; } } return NULL; @@ -271,7 +271,7 @@ const char *mnemonic_complete_word(const char *prefix, int len) { const char *mnemonic_get_word(int index) { if (index >= 0 && index < BIP39_WORDS) { - return wordlist[index]; + return BIP39_WORDLIST_ENGLISH[index]; } else { return NULL; } @@ -282,8 +282,8 @@ uint32_t mnemonic_word_completion_mask(const char *prefix, int len) { return 0x3ffffff; // all letters (bits 1-26 set) } uint32_t res = 0; - for (const char *const *w = wordlist; *w != 0; w++) { - const char *word = *w; + for (int i = 0; i < BIP39_WORDS; i++) { + const char *word = BIP39_WORDLIST_ENGLISH[i]; if (strncmp(word, prefix, len) == 0 && word[len] >= 'a' && word[len] <= 'z') { res |= 1 << (word[len] - 'a'); diff --git a/crypto/bip39.h b/crypto/bip39.h index 3db8efc352..682ee577eb 100644 --- a/crypto/bip39.h +++ b/crypto/bip39.h @@ -36,6 +36,8 @@ void bip39_cache_clear(void); #endif +extern const char *const BIP39_WORDLIST_ENGLISH[BIP39_WORDS]; + const char *mnemonic_generate(int strength); // strength in bits const char *mnemonic_from_data(const uint8_t *data, int len); void mnemonic_clear(void); diff --git a/crypto/bip39_english.h b/crypto/bip39_english.c similarity index 99% rename from crypto/bip39_english.h rename to crypto/bip39_english.c index c57fca365f..31a36f35d3 100644 --- a/crypto/bip39_english.h +++ b/crypto/bip39_english.c @@ -21,7 +21,9 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -static const char* const wordlist[] = { +#include "bip39.h" + +const char* const BIP39_WORDLIST_ENGLISH[BIP39_WORDS] = { "abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract", "absurd", "abuse", "access", "accident", "account", "accuse", "achieve", "acid", "acoustic", "acquire", @@ -363,5 +365,5 @@ static const char* const wordlist[] = { "work", "world", "worry", "worth", "wrap", "wreck", "wrestle", "wrist", "write", "wrong", "yard", "year", "yellow", "you", "young", "youth", "zebra", "zero", - "zone", "zoo", 0, + "zone", "zoo", };