1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

add mnemonic generation methods with indexes option

This commit is contained in:
Pavol Rusnak 2016-05-13 19:45:43 +02:00
parent 51c0bb09d8
commit aae96e8285
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 42 additions and 0 deletions

40
bip39.c
View File

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

View File

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