From 71b4e9978ced80f2779331621b0798f06a09476a Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 14 May 2018 16:43:29 +0200 Subject: [PATCH 1/4] embed/extmod: add trezor_obj helpers for int and uint --- embed/extmod/trezorobj.h | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 embed/extmod/trezorobj.h diff --git a/embed/extmod/trezorobj.h b/embed/extmod/trezorobj.h new file mode 100644 index 0000000000..2bd3a2fe97 --- /dev/null +++ b/embed/extmod/trezorobj.h @@ -0,0 +1,72 @@ +/* + * This file is part of the TREZOR project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "py/runtime.h" +#include "py/objint.h" + +#ifndef __TREZOROBJ_H__ +#define __TREZOROBJ_H__ + +#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_MPZ +#error Use MPZ for MicroPython long int implementation. +#endif + +// Casts int object into mp_int_t, without any conversions. Raises if object is +// not int or if it does not fit into mp_int_t representation. +static inline mp_int_t trezor_obj_get_int(mp_obj_t obj) { + if (MP_OBJ_IS_SMALL_INT(obj)) { + mp_int_t i = MP_OBJ_SMALL_INT_VALUE(obj); + return i; + } + else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) { + mp_int_t i = 0; + mp_obj_int_t *self = MP_OBJ_TO_PTR(obj); + if (!mpz_as_int_checked(&self->mpz, &i)) { + mp_raise_msg(&mp_type_OverflowError, "value does not fit into signed int type"); + } + return i; + } + else { + mp_raise_TypeError("value is not int"); + } +} + +// Casts int object into mp_uint_t, without any conversions. Raises if object is +// not int or if it does not fit into mp_uint_t representation (or is less than +// 0). +static inline mp_uint_t trezor_obj_get_uint(mp_obj_t obj) { + if (MP_OBJ_IS_SMALL_INT(obj)) { + mp_int_t i = MP_OBJ_SMALL_INT_VALUE(obj); + mp_uint_t u = i; + return u; + } + else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) { + mp_uint_t u = 0; + mp_obj_int_t *self = MP_OBJ_TO_PTR(obj); + if (!mpz_as_uint_checked(&self->mpz, &u)) { + mp_raise_msg(&mp_type_OverflowError, "value does not fit into unsigned int type"); + } + return u; + } + else { + mp_raise_TypeError("value is not int"); + } +} + +#endif From 39c1c85abbd6b66add926729e1386029a063e890 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 14 May 2018 16:44:08 +0200 Subject: [PATCH 2/4] embed/extmod: use trezor_obj_get_uint for uint32_t --- .../modtrezorcrypto/modtrezorcrypto-bip32.h | 22 ++++++++++--------- .../modtrezorcrypto/modtrezorcrypto-crc.h | 5 ++++- .../modtrezorcrypto/modtrezorcrypto-random.h | 4 +++- embed/extmod/modtrezorio/modtrezorio-poll.h | 4 +++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h index dc74f361af..6fc81aa1ca 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip32.h @@ -19,6 +19,8 @@ #include "py/objstr.h" +#include "embed/extmod/trezorobj.h" + #include "bip32.h" #include "curves.h" #include "memzero.h" @@ -66,9 +68,9 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size mp_buffer_info_t private_key; mp_buffer_info_t public_key; mp_buffer_info_t curve_name; - const uint32_t depth = mp_obj_get_int_truncated(vals[0].u_obj); - const uint32_t fingerprint = mp_obj_get_int_truncated(vals[1].u_obj); - const uint32_t child_num = mp_obj_get_int_truncated(vals[2].u_obj); + const uint32_t depth = trezor_obj_get_uint(vals[0].u_obj); + const uint32_t fingerprint = trezor_obj_get_uint(vals[1].u_obj); + const uint32_t child_num = trezor_obj_get_uint(vals[2].u_obj); mp_get_buffer_raise(vals[3].u_obj, &chain_code, MP_BUFFER_READ); mp_get_buffer_raise(vals[4].u_obj, &private_key, MP_BUFFER_READ); mp_get_buffer_raise(vals[5].u_obj, &public_key, MP_BUFFER_READ); @@ -129,7 +131,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type, size /// ''' STATIC mp_obj_t mod_trezorcrypto_HDNode_derive(size_t n_args, const mp_obj_t *args) { mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(args[0]); - uint32_t i = mp_obj_get_int_truncated(args[1]); + uint32_t i = trezor_obj_get_uint(args[1]); uint32_t fp = hdnode_fingerprint(&o->hdnode); bool public = n_args > 2 && args[2] == mp_const_true; @@ -172,7 +174,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_derive_path(mp_obj_t self, mp_obj_t path uint32_t pi; uint32_t pints[plen]; for (pi = 0; pi < plen; pi++) { - pints[pi] = mp_obj_get_int_truncated(pitems[pi]); + pints[pi] = trezor_obj_get_uint(pitems[pi]); } if (!hdnode_private_ckd_cached(&o->hdnode, pints, plen, &o->fingerprint)) { @@ -207,7 +209,7 @@ STATIC mp_obj_t serialize_public_private(mp_obj_t self, bool use_public, uint32_ /// Serialize the public info from HD node to base58 string. /// ''' STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_public(mp_obj_t self, mp_obj_t version) { - uint32_t ver = mp_obj_get_int_truncated(version); + uint32_t ver = trezor_obj_get_uint(version); return serialize_public_private(self, true, ver); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_public_obj, mod_trezorcrypto_HDNode_serialize_public); @@ -217,7 +219,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_public_obj, m /// Serialize the private info HD node to base58 string. /// ''' STATIC mp_obj_t mod_trezorcrypto_HDNode_serialize_private(mp_obj_t self, mp_obj_t version) { - uint32_t ver = mp_obj_get_int_truncated(version); + uint32_t ver = trezor_obj_get_uint(version); return serialize_public_private(self, false, ver); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_private_obj, mod_trezorcrypto_HDNode_serialize_private); @@ -304,7 +306,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_public_key_obj, mod_tre STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self, mp_obj_t version) { mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self); - uint32_t v = mp_obj_get_int_truncated(version); + uint32_t v = trezor_obj_get_uint(version); char address[ADDRESS_MAXLEN]; hdnode_get_address(&o->hdnode, v, address, ADDRESS_MAXLEN); return mp_obj_new_str(address, strlen(address), false); @@ -359,8 +361,8 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_deserialize(mp_obj_t value, mp_obj_t vers if (valueb.len == 0) { mp_raise_ValueError("Invalid value"); } - uint32_t vpub = mp_obj_get_int_truncated(version_public); - uint32_t vpriv = mp_obj_get_int_truncated(version_private); + uint32_t vpub = trezor_obj_get_uint(version_public); + uint32_t vpriv = trezor_obj_get_uint(version_private); HDNode hdnode; uint32_t fingerprint; if (hdnode_deserialize(valueb.buf, vpub, vpriv, SECP256K1_NAME, &hdnode, &fingerprint) < 0) { diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h index 55250d7be7..29b4df4180 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-crc.h @@ -18,12 +18,15 @@ */ #include "py/objstr.h" + +#include "embed/extmod/trezorobj.h" + #include "crc.h" mp_obj_t mod_trezorcrypto_crc_crc32(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); - uint32_t crc = (n_args > 1) ? mp_obj_get_int_truncated(args[1]) : 0; + uint32_t crc = (n_args > 1) ? trezor_obj_get_uint(args[1]) : 0; crc = crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff); return mp_obj_new_int_from_uint(crc ^ 0xffffffff); } diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h index 0c6783e18f..4484dae7e2 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h @@ -19,6 +19,8 @@ #include "py/objstr.h" +#include "embed/extmod/trezorobj.h" + #include "rand.h" /// def uniform(n: int) -> int: @@ -26,7 +28,7 @@ /// Compute uniform random number from interval 0 ... n - 1. /// ''' STATIC mp_obj_t mod_trezorcrypto_random_uniform(mp_obj_t n) { - uint32_t nn = mp_obj_get_int_truncated(n); + uint32_t nn = trezor_obj_get_uint(n); if (nn == 0) { mp_raise_ValueError("Maximum can't be zero"); } diff --git a/embed/extmod/modtrezorio/modtrezorio-poll.h b/embed/extmod/modtrezorio/modtrezorio-poll.h index d32171ef9d..67fbd3f54f 100644 --- a/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -20,6 +20,8 @@ #include #include +#include "embed/extmod/trezorobj.h" + #include "usb.h" #define TOUCH_IFACE (255) @@ -57,7 +59,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t t mp_obj_t iter = mp_getiter(ifaces, &iterbuf); mp_obj_t item; while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { - const mp_uint_t i = mp_obj_int_get_truncated(item); + const mp_uint_t i = trezor_obj_get_uint(item); const mp_uint_t iface = i & 0x00FF; const mp_uint_t mode = i & 0xFF00; From 648ec675f46ddb4735ad6d33ec988a376c792219 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Wed, 16 May 2018 14:27:51 +0200 Subject: [PATCH 3/4] embed/extmod: add trezor_obj_get_uint8 --- embed/extmod/trezorobj.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/embed/extmod/trezorobj.h b/embed/extmod/trezorobj.h index 2bd3a2fe97..76ff4d4c5d 100644 --- a/embed/extmod/trezorobj.h +++ b/embed/extmod/trezorobj.h @@ -69,4 +69,12 @@ static inline mp_uint_t trezor_obj_get_uint(mp_obj_t obj) { } } +static inline uint8_t trezor_obj_get_uint8(mp_obj_t obj) { + mp_uint_t u = trezor_obj_get_uint(obj); + if (u > 0xFF) { + mp_raise_msg(&mp_type_OverflowError, "value does not fit into byte type"); + } + return u; +} + #endif From bf7729ab9f937c093c15d7b87da616cc4300d2ee Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Wed, 16 May 2018 14:42:41 +0200 Subject: [PATCH 4/4] embed/extmod: use trezor_obj_get_uint{8} where it makes sense --- embed/extmod/modtrezorconfig/modtrezorconfig.c | 18 ++++++++++-------- .../modtrezorcrypto/modtrezorcrypto-blake2b.h | 6 ++++-- .../modtrezorcrypto/modtrezorcrypto-blake2s.h | 6 ++++-- .../modtrezorcrypto/modtrezorcrypto-pbkdf2.h | 2 +- .../modtrezorcrypto/modtrezorcrypto-random.h | 2 +- embed/extmod/modtrezorio/modtrezorio-flash.h | 14 ++++++++------ embed/extmod/modtrezorio/modtrezorio-poll.h | 2 +- embed/extmod/modtrezorio/modtrezorio-sdcard.h | 8 ++++++-- embed/extmod/modtrezorutils/modtrezorutils.c | 18 ++++++------------ 9 files changed, 41 insertions(+), 35 deletions(-) diff --git a/embed/extmod/modtrezorconfig/modtrezorconfig.c b/embed/extmod/modtrezorconfig/modtrezorconfig.c index 0f50612caf..c45dafab5f 100644 --- a/embed/extmod/modtrezorconfig/modtrezorconfig.c +++ b/embed/extmod/modtrezorconfig/modtrezorconfig.c @@ -23,6 +23,8 @@ #if MICROPY_PY_TREZORCONFIG +#include "embed/extmod/trezorobj.h" + #include "norcow.h" #include "storage.h" @@ -46,7 +48,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_init_obj, mod_trezorconfig_ini /// Check the given PIN. Returns True on success, False on failure. /// ''' STATIC mp_obj_t mod_trezorconfig_check_pin(mp_obj_t pin, mp_obj_t waitcallback) { - uint32_t pin_i = mp_obj_get_int(pin); + uint32_t pin_i = trezor_obj_get_uint(pin); if (sectrue != storage_check_pin(pin_i, waitcallback)) { return mp_const_false; } @@ -60,7 +62,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_check_pin_obj, mod_trezorconfi /// success, False on failure. /// ''' STATIC mp_obj_t mod_trezorconfig_unlock(mp_obj_t pin, mp_obj_t waitcallback) { - uint32_t pin_i = mp_obj_get_int(pin); + uint32_t pin_i = trezor_obj_get_uint(pin); if (sectrue != storage_unlock(pin_i, waitcallback)) { return mp_const_false; } @@ -85,8 +87,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_has_pin_obj, mod_trezorconfig_ /// Change PIN. Returns True on success, False on failure. /// ''' STATIC mp_obj_t mod_trezorconfig_change_pin(mp_obj_t pin, mp_obj_t newpin, mp_obj_t waitcallback) { - uint32_t pin_i = mp_obj_get_int(pin); - uint32_t newpin_i = mp_obj_get_int(newpin); + uint32_t pin_i = trezor_obj_get_uint(pin); + uint32_t newpin_i = trezor_obj_get_uint(newpin); if (sectrue != storage_change_pin(pin_i, newpin_i, waitcallback)) { return mp_const_false; } @@ -99,8 +101,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorconfig_change_pin_obj, mod_trezorconf /// Gets a value of given key for given app (or empty bytes if not set). /// ''' STATIC mp_obj_t mod_trezorconfig_get(size_t n_args, const mp_obj_t *args) { - uint8_t app = mp_obj_get_int(args[0]) & 0x7F; - uint8_t key = mp_obj_get_int(args[1]); + uint8_t app = trezor_obj_get_uint8(args[0]) & 0x7F; + uint8_t key = trezor_obj_get_uint8(args[1]); if (n_args > 2 && args[2] == mp_const_true) { app |= 0x80; } @@ -119,8 +121,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_get_obj, 2, 3, mod_t /// Sets a value of given key for given app. /// ''' STATIC mp_obj_t mod_trezorconfig_set(size_t n_args, const mp_obj_t *args) { - uint8_t app = mp_obj_get_int(args[0]) & 0x7F; - uint8_t key = mp_obj_get_int(args[1]); + uint8_t app = trezor_obj_get_uint8(args[0]) & 0x7F; + uint8_t key = trezor_obj_get_uint8(args[1]); if (n_args > 3 && args[3] == mp_const_true) { app |= 0x80; } diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h index a686508a32..a8d88e1930 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2b.h @@ -19,6 +19,8 @@ #include "py/objstr.h" +#include "embed/extmod/trezorobj.h" + #include "blake2b.h" #include "memzero.h" @@ -44,12 +46,12 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2b_make_new(const mp_obj_type_t *type, siz int res = 0; // constructor called with key argument set if (n_args == 3) { - size_t outlen = mp_obj_get_int(args[1]); + size_t outlen = trezor_obj_get_uint(args[1]); mp_buffer_info_t key; mp_get_buffer_raise(args[2], &key, MP_BUFFER_READ); res = blake2b_InitKey(&(o->ctx), outlen, key.buf, key.len); } else if (n_args == 2) { - size_t outlen = mp_obj_get_int(args[1]); + size_t outlen = trezor_obj_get_uint(args[1]); res = blake2b_Init(&(o->ctx), outlen); } else { res = blake2b_Init(&(o->ctx), BLAKE2B_DIGEST_LENGTH); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h index 79ce0f326d..1f30c8d34f 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-blake2s.h @@ -19,6 +19,8 @@ #include "py/objstr.h" +#include "embed/extmod/trezorobj.h" + #include "blake2s.h" #include "memzero.h" @@ -44,12 +46,12 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2s_make_new(const mp_obj_type_t *type, siz int res = 0; // constructor called with key argument set if (n_args == 3) { - size_t outlen = mp_obj_get_int(args[1]); + size_t outlen = trezor_obj_get_uint(args[1]); mp_buffer_info_t key; mp_get_buffer_raise(args[2], &key, MP_BUFFER_READ); res = blake2s_InitKey(&(o->ctx), outlen, key.buf, key.len); } else if (n_args == 2) { - size_t outlen = mp_obj_get_int(args[1]); + size_t outlen = trezor_obj_get_uint(args[1]); res = blake2s_Init(&(o->ctx), outlen); } else { res = blake2s_Init(&(o->ctx), BLAKE2S_DIGEST_LENGTH); diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h index 6448f25fde..eb678189db 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-pbkdf2.h @@ -85,7 +85,7 @@ STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_make_new(const mp_obj_type_t *type, size /// ''' STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_update(mp_obj_t self, mp_obj_t iterations) { mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self); - uint32_t iter = mp_obj_get_int(iterations); + uint32_t iter = trezor_obj_get_uint(iterations); if (o->prf == 256) { pbkdf2_hmac_sha256_Update(&(o->ctx256), iter); } diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h index 4484dae7e2..288d8cbac6 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-random.h @@ -41,7 +41,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezor /// Generate random bytes sequence of length len. /// ''' STATIC mp_obj_t mod_trezorcrypto_random_bytes(mp_obj_t len) { - uint32_t l = mp_obj_get_int(len); + uint32_t l = trezor_obj_get_uint(len); if (l > 1024) { mp_raise_ValueError("Maximum requested size is 1024"); } diff --git a/embed/extmod/modtrezorio/modtrezorio-flash.h b/embed/extmod/modtrezorio/modtrezorio-flash.h index d0ff438008..07bf949951 100644 --- a/embed/extmod/modtrezorio/modtrezorio-flash.h +++ b/embed/extmod/modtrezorio/modtrezorio-flash.h @@ -19,6 +19,8 @@ #include "flash.h" +#include "embed/extmod/trezorobj.h" + /// class FlashOTP: /// ''' /// ''' @@ -41,8 +43,8 @@ STATIC mp_obj_t mod_trezorio_FlashOTP_make_new(const mp_obj_type_t *type, size_t /// Writes data to OTP flash /// ''' STATIC mp_obj_t mod_trezorio_FlashOTP_write(size_t n_args, const mp_obj_t *args) { - uint8_t block = mp_obj_get_int(args[1]); - uint8_t offset = mp_obj_get_int(args[2]); + uint8_t block = trezor_obj_get_uint8(args[1]); + uint8_t offset = trezor_obj_get_uint8(args[2]); mp_buffer_info_t data; mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ); if (sectrue != flash_otp_write(block, offset, data.buf, data.len)) { @@ -57,8 +59,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_FlashOTP_write_obj, 4, 4 /// Reads data from OTP flash /// ''' STATIC mp_obj_t mod_trezorio_FlashOTP_read(size_t n_args, const mp_obj_t *args) { - uint8_t block = mp_obj_get_int(args[1]); - uint8_t offset = mp_obj_get_int(args[2]); + uint8_t block = trezor_obj_get_uint8(args[1]); + uint8_t offset = trezor_obj_get_uint8(args[2]); mp_buffer_info_t data; mp_get_buffer_raise(args[3], &data, MP_BUFFER_WRITE); if (sectrue != flash_otp_read(block, offset, data.buf, data.len)) { @@ -73,7 +75,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorio_FlashOTP_read_obj, 4, 4, /// Lock OTP flash block /// ''' STATIC mp_obj_t mod_trezorio_FlashOTP_lock(mp_obj_t self, mp_obj_t block) { - uint8_t b = mp_obj_get_int(block); + uint8_t b = trezor_obj_get_uint8(block); if (sectrue != flash_otp_lock(b)) { mp_raise_ValueError("lock failed"); } @@ -86,7 +88,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_FlashOTP_lock_obj, mod_trezorio_Fl /// Is OTP flash block locked? /// ''' STATIC mp_obj_t mod_trezorio_FlashOTP_is_locked(mp_obj_t self, mp_obj_t block) { - uint8_t b = mp_obj_get_int(block); + uint8_t b = trezor_obj_get_uint8(block); return flash_otp_is_locked(b) ? mp_const_true : mp_const_false; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_FlashOTP_is_locked_obj, mod_trezorio_FlashOTP_is_locked); diff --git a/embed/extmod/modtrezorio/modtrezorio-poll.h b/embed/extmod/modtrezorio/modtrezorio-poll.h index 67fbd3f54f..1b121f3bf3 100644 --- a/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -51,7 +51,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t t mp_raise_TypeError("invalid list_ref"); } - const mp_uint_t timeout = mp_obj_get_int(timeout_us); + const mp_uint_t timeout = trezor_obj_get_uint(timeout_us); const mp_uint_t deadline = mp_hal_ticks_us() + timeout; mp_obj_iter_buf_t iterbuf; diff --git a/embed/extmod/modtrezorio/modtrezorio-sdcard.h b/embed/extmod/modtrezorio/modtrezorio-sdcard.h index 635b7853a1..b5ef5af197 100644 --- a/embed/extmod/modtrezorio/modtrezorio-sdcard.h +++ b/embed/extmod/modtrezorio/modtrezorio-sdcard.h @@ -19,6 +19,8 @@ #include "sdcard.h" +#include "embed/extmod/trezorobj.h" + /// class SDCard: /// ''' /// ''' @@ -79,9 +81,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_SDCard_capacity_obj, mod_trezorio_ /// Returns True if in case of success, False otherwise. /// ''' STATIC mp_obj_t mod_trezorio_SDCard_read(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + uint32_t block = trezor_obj_get_uint(block_num); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); - return mp_obj_new_bool(sdcard_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE)); + return mp_obj_new_bool(sdcard_read_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE)); } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_read_obj, mod_trezorio_SDCard_read); @@ -92,9 +95,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_read_obj, mod_trezorio_SDCa /// Returns True if in case of success, False otherwise. /// ''' STATIC mp_obj_t mod_trezorio_SDCard_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + uint32_t block = trezor_obj_get_uint(block_num); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); - return mp_obj_new_bool(sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE)); + return mp_obj_new_bool(sdcard_write_blocks(bufinfo.buf, block, bufinfo.len / SDCARD_BLOCK_SIZE)); } STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_SDCard_write_obj, mod_trezorio_SDCard_write); diff --git a/embed/extmod/modtrezorutils/modtrezorutils.c b/embed/extmod/modtrezorutils/modtrezorutils.c index d609c03156..ca33c3e266 100644 --- a/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/embed/extmod/modtrezorutils/modtrezorutils.c @@ -23,6 +23,8 @@ #if MICROPY_PY_TREZORUTILS +#include "embed/extmod/trezorobj.h" + #include #include "common.h" @@ -67,22 +69,14 @@ STATIC mp_obj_t mod_trezorutils_memcpy(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t dst; mp_get_buffer_raise(args[0], &dst, MP_BUFFER_WRITE); - int dst_ofs = mp_obj_get_int(args[1]); - if (dst_ofs < 0) { - mp_raise_ValueError("Invalid dst offset (has to be >= 0)"); - } + uint32_t dst_ofs = trezor_obj_get_uint(args[1]); mp_buffer_info_t src; mp_get_buffer_raise(args[2], &src, MP_BUFFER_READ); - int src_ofs = mp_obj_get_int(args[3]); - if (src_ofs < 0) { - mp_raise_ValueError("Invalid src offset (has to be >= 0)"); - } + uint32_t src_ofs = trezor_obj_get_uint(args[3]); + + uint32_t n = trezor_obj_get_uint(args[4]); - int n = mp_obj_get_int(args[4]); - if (n < 0) { - mp_raise_ValueError("Invalid byte count (has to be >= 0)"); - } size_t dst_rem = (dst_ofs < dst.len) ? dst.len - dst_ofs : 0; size_t src_rem = (src_ofs < src.len) ? src.len - src_ofs : 0; size_t ncpy = MIN(n, MIN(src_rem, dst_rem));