1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 05:48:23 +00:00

feat(crypto): expose BIP39 wordlist

This commit is contained in:
matejcik 2022-05-30 11:13:24 +02:00 committed by matejcik
parent e98464d2c8
commit 61adb0a62b
3 changed files with 18 additions and 14 deletions

View File

@ -25,7 +25,6 @@
#include <string.h>
#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');

View File

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

View File

@ -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",
};