From 3e8b025cbdaf52d04da4d542136e3a7dc061b474 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 7 Oct 2016 13:57:21 +0200 Subject: [PATCH] use mp_obj_new_str_of_type where it makes sense instead of mp_obj_new_str_from_vstr --- extmod/modtrezorcrypto/modtrezorcrypto-bip39.h | 10 ++-------- extmod/modtrezorcrypto/modtrezorcrypto-random.h | 3 +++ extmod/modtrezormsg/modtrezormsg.c | 6 ++---- src/tests/test_crypto_random.py | 4 ++++ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-bip39.h b/extmod/modtrezorcrypto/modtrezorcrypto-bip39.h index 52fbda9028..66a7340ad5 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-bip39.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-bip39.h @@ -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); diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-random.h b/extmod/modtrezorcrypto/modtrezorcrypto-random.h index 6892c8eb44..bde50872fa 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-random.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-random.h @@ -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); diff --git a/extmod/modtrezormsg/modtrezormsg.c b/extmod/modtrezormsg/modtrezormsg.c index dc71a28f57..71a277336d 100644 --- a/extmod/modtrezormsg/modtrezormsg.c +++ b/extmod/modtrezormsg/modtrezormsg.c @@ -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) { diff --git a/src/tests/test_crypto_random.py b/src/tests/test_crypto_random.py index ecb3f95760..d4d2577b77 100644 --- a/src/tests/test_crypto_random.py +++ b/src/tests/test_crypto_random.py @@ -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)