2016-05-13 18:33:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
2016-05-28 12:37:32 +00:00
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
2016-05-13 18:33:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
2017-12-12 19:31:03 +00:00
|
|
|
#include "bip39.h"
|
2016-05-13 18:33:14 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def find_word(prefix: str) -> Optional[str]:
|
2017-02-08 15:18:11 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Return the first word from the wordlist starting with prefix.
|
2017-02-08 15:18:11 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_find_word(mp_obj_t prefix)
|
2017-02-08 15:18:11 +00:00
|
|
|
{
|
|
|
|
mp_buffer_info_t pfx;
|
|
|
|
mp_get_buffer_raise(prefix, &pfx, MP_BUFFER_READ);
|
|
|
|
if (pfx.len == 0) {
|
|
|
|
mp_raise_ValueError("Invalid word prefix");
|
|
|
|
}
|
|
|
|
for (const char * const *w = mnemonic_wordlist(); *w != 0; w++) {
|
|
|
|
if (strncmp(*w, pfx.buf, pfx.len) == 0) {
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_str(*w, strlen(*w), false);
|
2017-02-08 15:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_find_word_obj, mod_trezorcrypto_bip39_find_word);
|
2017-02-08 15:18:11 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def complete_word(prefix: str) -> int:
|
2017-02-08 15:18:11 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Return possible 1-letter suffixes for given word prefix.
|
2017-02-08 15:18:11 +00:00
|
|
|
/// Result is a bitmask, with 'a' on the lowest bit, 'b' on the second lowest, etc.
|
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_complete_word(mp_obj_t prefix)
|
2017-02-08 15:18:11 +00:00
|
|
|
{
|
|
|
|
mp_buffer_info_t pfx;
|
|
|
|
mp_get_buffer_raise(prefix, &pfx, MP_BUFFER_READ);
|
|
|
|
if (pfx.len == 0) {
|
|
|
|
mp_raise_ValueError("Invalid word prefix");
|
|
|
|
}
|
|
|
|
uint32_t res = 0;
|
|
|
|
uint8_t bit;
|
|
|
|
const char *word;
|
|
|
|
const char *const *wlist;
|
|
|
|
for (wlist = mnemonic_wordlist(); *wlist != 0; wlist++) {
|
|
|
|
word = *wlist;
|
|
|
|
if (strncmp(word, pfx.buf, pfx.len) == 0 && strlen(word) > pfx.len) {
|
|
|
|
bit = word[pfx.len] - 'a';
|
|
|
|
res |= 1 << bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_obj_new_int(res);
|
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_complete_word_obj, mod_trezorcrypto_bip39_complete_word);
|
2017-02-08 15:18:11 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def generate(strength: int) -> str:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Generate a mnemonic of given strength (128, 160, 192, 224 and 256 bits).
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_generate(mp_obj_t strength) {
|
2016-05-13 18:33:14 +00:00
|
|
|
int bits = mp_obj_get_int(strength);
|
|
|
|
if (bits % 32 || bits < 128 || bits > 256) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid bit strength (only 128, 160, 192, 224 and 256 values are allowed)");
|
2016-05-13 18:33:14 +00:00
|
|
|
}
|
2016-05-15 20:05:44 +00:00
|
|
|
const char *mnemo = mnemonic_generate(bits);
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_str(mnemo, strlen(mnemo), false);
|
2016-05-13 18:33:14 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_generate_obj, mod_trezorcrypto_bip39_generate);
|
2016-05-13 18:33:14 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def from_data(data: bytes) -> str:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Generate a mnemonic from given data (of 16, 20, 24, 28 and 32 bytes).
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_from_data(mp_obj_t data) {
|
2016-05-13 18:33:14 +00:00
|
|
|
mp_buffer_info_t bin;
|
|
|
|
mp_get_buffer_raise(data, &bin, MP_BUFFER_READ);
|
|
|
|
if (bin.len % 4 || bin.len < 16 || bin.len > 32) {
|
2016-10-07 10:09:05 +00:00
|
|
|
mp_raise_ValueError("Invalid data length (only 16, 20, 24, 28 and 32 bytes are allowed)");
|
2016-05-13 18:33:14 +00:00
|
|
|
}
|
2016-05-15 20:05:44 +00:00
|
|
|
const char *mnemo = mnemonic_from_data(bin.buf, bin.len);
|
2018-01-03 20:53:58 +00:00
|
|
|
return mp_obj_new_str(mnemo, strlen(mnemo), false);
|
2016-05-13 18:33:14 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_from_data_obj, mod_trezorcrypto_bip39_from_data);
|
2016-05-13 18:33:14 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def check(mnemonic: str) -> bool:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Check whether given mnemonic is valid.
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_check(mp_obj_t mnemonic) {
|
2016-05-13 18:33:14 +00:00
|
|
|
mp_buffer_info_t text;
|
|
|
|
mp_get_buffer_raise(mnemonic, &text, MP_BUFFER_READ);
|
2016-10-04 16:53:32 +00:00
|
|
|
return (text.len > 0 && mnemonic_check(text.buf)) ? mp_const_true : mp_const_false;
|
2016-05-13 18:33:14 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_check_obj, mod_trezorcrypto_bip39_check);
|
2016-05-13 18:33:14 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
/// def seed(mnemonic: str, passphrase: str) -> bytes:
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-14 15:40:50 +00:00
|
|
|
/// Generate seed from mnemonic and passphrase.
|
2016-06-06 08:18:55 +00:00
|
|
|
/// '''
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC mp_obj_t mod_trezorcrypto_bip39_seed(mp_obj_t mnemonic, mp_obj_t passphrase) {
|
2016-05-15 20:05:44 +00:00
|
|
|
mp_buffer_info_t mnemo;
|
|
|
|
mp_buffer_info_t phrase;
|
|
|
|
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ);
|
|
|
|
mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ);
|
2018-01-03 20:53:58 +00:00
|
|
|
uint8_t seed[64];
|
2016-10-04 16:53:32 +00:00
|
|
|
const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : "";
|
|
|
|
const char *ppassphrase = phrase.len > 0 ? phrase.buf : "";
|
2018-01-03 20:53:58 +00:00
|
|
|
mnemonic_to_seed(pmnemonic, ppassphrase, seed, NULL); // no callback for now
|
|
|
|
return mp_obj_new_bytes(seed, sizeof(seed));
|
2016-05-15 20:05:44 +00:00
|
|
|
}
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip39_seed_obj, mod_trezorcrypto_bip39_seed);
|
2016-05-15 20:05:44 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mod_trezorcrypto_bip39_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bip39) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_find_word), MP_ROM_PTR(&mod_trezorcrypto_bip39_find_word_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_complete_word), MP_ROM_PTR(&mod_trezorcrypto_bip39_complete_word_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_generate), MP_ROM_PTR(&mod_trezorcrypto_bip39_generate_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_from_data), MP_ROM_PTR(&mod_trezorcrypto_bip39_from_data_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_check), MP_ROM_PTR(&mod_trezorcrypto_bip39_check_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_trezorcrypto_bip39_seed_obj) },
|
2016-05-13 18:33:14 +00:00
|
|
|
};
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_bip39_globals, mod_trezorcrypto_bip39_globals_table);
|
2016-05-13 18:33:14 +00:00
|
|
|
|
2017-06-20 15:45:27 +00:00
|
|
|
STATIC const mp_obj_module_t mod_trezorcrypto_bip39_module = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mod_trezorcrypto_bip39_globals,
|
2016-05-13 18:33:14 +00:00
|
|
|
};
|