mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 14:28:07 +00:00
add mnemonic generation methods with indexes option
This commit is contained in:
parent
51c0bb09d8
commit
aae96e8285
40
bip39.c
40
bip39.c
@ -55,6 +55,16 @@ const char *mnemonic_generate(int strength)
|
||||
return mnemonic_from_data(data, strength / 8);
|
||||
}
|
||||
|
||||
const uint16_t *mnemonic_generate_indexes(int strength)
|
||||
{
|
||||
if (strength % 32 || strength < 128 || strength > 256) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t data[32];
|
||||
random_buffer(data, 32);
|
||||
return mnemonic_from_data_indexes(data, strength / 8);
|
||||
}
|
||||
|
||||
const char *mnemonic_from_data(const uint8_t *data, int len)
|
||||
{
|
||||
if (len % 4 || len < 16 || len > 32) {
|
||||
@ -89,6 +99,36 @@ const char *mnemonic_from_data(const uint8_t *data, int len)
|
||||
return mnemo;
|
||||
}
|
||||
|
||||
const uint16_t *mnemonic_from_data_indexes(const uint8_t *data, int len)
|
||||
{
|
||||
if (len % 4 || len < 16 || len > 32) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t bits[32 + 1];
|
||||
|
||||
sha256_Raw(data, len, bits);
|
||||
// checksum
|
||||
bits[len] = bits[0];
|
||||
// data
|
||||
memcpy(bits, data, len);
|
||||
|
||||
int mlen = len * 3 / 4;
|
||||
static uint16_t mnemo[24];
|
||||
|
||||
int i, j, idx;
|
||||
for (i = 0; i < mlen; i++) {
|
||||
idx = 0;
|
||||
for (j = 0; j < 11; j++) {
|
||||
idx <<= 1;
|
||||
idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0;
|
||||
}
|
||||
mnemo[i] = idx;
|
||||
}
|
||||
|
||||
return mnemo;
|
||||
}
|
||||
|
||||
int mnemonic_check(const char *mnemonic)
|
||||
{
|
||||
if (!mnemonic) {
|
||||
|
2
bip39.h
2
bip39.h
@ -29,8 +29,10 @@
|
||||
#define BIP39_PBKDF2_ROUNDS 2048
|
||||
|
||||
const char *mnemonic_generate(int strength); // strength in bits
|
||||
const uint16_t *mnemonic_generate_indexes(int strength); // strength in bits
|
||||
|
||||
const char *mnemonic_from_data(const uint8_t *data, int len);
|
||||
const uint16_t *mnemonic_from_data_indexes(const uint8_t *data, int len);
|
||||
|
||||
int mnemonic_check(const char *mnemonic);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user