mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 15:38:11 +00:00
use mp_obj_new_str_of_type where it makes sense instead of mp_obj_new_str_from_vstr
This commit is contained in:
parent
2bba78bf87
commit
3e8b025cbd
@ -30,10 +30,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Bip39_generate(mp_obj_t self, mp_obj_t strength
|
||||
mp_raise_ValueError("Invalid bit strength (only 128, 160, 192, 224 and 256 values are allowed)");
|
||||
}
|
||||
const char *mnemo = mnemonic_generate(bits);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, strlen(mnemo));
|
||||
strcpy(vstr.buf, mnemo);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Bip39_generate_obj, mod_TrezorCrypto_Bip39_generate);
|
||||
|
||||
@ -48,10 +45,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Bip39_from_data(mp_obj_t self, mp_obj_t data) {
|
||||
mp_raise_ValueError("Invalid data length (only 16, 20, 24, 28 and 32 bytes are allowed)");
|
||||
}
|
||||
const char *mnemo = mnemonic_from_data(bin.buf, bin.len);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, strlen(mnemo));
|
||||
strcpy(vstr.buf, mnemo);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Bip39_from_data_obj, mod_TrezorCrypto_Bip39_from_data);
|
||||
|
||||
|
@ -39,6 +39,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorCrypto_Random_uniform_obj, mod_Trezor
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_TrezorCrypto_Random_bytes(mp_obj_t self, mp_obj_t len) {
|
||||
uint32_t l = mp_obj_get_int(len);
|
||||
if (l > 8192) {
|
||||
mp_raise_ValueError("Maximum requested size is 8192");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, l);
|
||||
random_buffer((uint8_t *)vstr.buf, l);
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "py/runtime.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/mphal.h"
|
||||
#include "py/objstr.h"
|
||||
|
||||
#if MICROPY_PY_TREZORMSG
|
||||
|
||||
@ -107,12 +108,9 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) {
|
||||
uint8_t recvbuf[64];
|
||||
ssize_t l = msg_recv(&iface, recvbuf, 64);
|
||||
if (l > 0) {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, l);
|
||||
memcpy(vstr.buf, recvbuf, l);
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
|
||||
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(iface);
|
||||
tuple->items[1] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
tuple->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, recvbuf, l);
|
||||
return MP_OBJ_FROM_PTR(tuple);
|
||||
}
|
||||
if (timeout <= 0) {
|
||||
|
@ -23,6 +23,10 @@ class TestCryptoRandom(unittest.TestCase):
|
||||
lst = random.bytes(l)
|
||||
self.assertEqual(len(lst), l)
|
||||
|
||||
def test_bytes_big_length(self):
|
||||
with self.assertRaises(ValueError):
|
||||
random.bytes(10000)
|
||||
|
||||
def test_bytes_uniform(self):
|
||||
for _ in range(100):
|
||||
b = random.bytes(8000)
|
||||
|
Loading…
Reference in New Issue
Block a user