mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-23 06:48:16 +00:00
core: enable finalizers on extmod classes
This commit is contained in:
parent
123b07e3ad
commit
050936d0d7
@ -57,7 +57,7 @@ STATIC mp_obj_t mod_trezorcrypto_AES_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 2, 3, false);
|
||||
mp_obj_AES_t *o = m_new_obj(mp_obj_AES_t);
|
||||
mp_obj_AES_t *o = m_new_obj_with_finaliser(mp_obj_AES_t);
|
||||
o->base.type = type;
|
||||
o->mode = mp_obj_get_int(args[0]);
|
||||
if (o->mode < ECB || o->mode > CTR) {
|
||||
|
@ -123,7 +123,7 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_make_new(const mp_obj_type_t *type,
|
||||
mp_raise_ValueError("curve_name is invalid");
|
||||
}
|
||||
|
||||
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
|
||||
mp_obj_HDNode_t *o = m_new_obj_with_finaliser(mp_obj_HDNode_t);
|
||||
o->base.type = type;
|
||||
o->fingerprint = fingerprint;
|
||||
o->hdnode.depth = depth;
|
||||
@ -286,7 +286,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_serialize_public_obj,
|
||||
/// """
|
||||
STATIC mp_obj_t mod_trezorcrypto_HDNode_clone(mp_obj_t self) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
mp_obj_HDNode_t *copy = m_new_obj(mp_obj_HDNode_t);
|
||||
mp_obj_HDNode_t *copy = m_new_obj_with_finaliser(mp_obj_HDNode_t);
|
||||
copy->base.type = &mod_trezorcrypto_HDNode_type;
|
||||
copy->hdnode = o->hdnode;
|
||||
copy->fingerprint = o->fingerprint;
|
||||
@ -572,7 +572,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_seed(mp_obj_t seed,
|
||||
mp_raise_ValueError("Failed to derive the root node");
|
||||
}
|
||||
|
||||
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
|
||||
mp_obj_HDNode_t *o = m_new_obj_with_finaliser(mp_obj_HDNode_t);
|
||||
o->base.type = &mod_trezorcrypto_HDNode_type;
|
||||
o->hdnode = hdnode;
|
||||
o->fingerprint = 0;
|
||||
@ -615,7 +615,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip32_from_mnemonic_cardano(
|
||||
mp_raise_ValueError("Invalid mnemonic");
|
||||
}
|
||||
|
||||
mp_obj_HDNode_t *o = m_new_obj(mp_obj_HDNode_t);
|
||||
mp_obj_HDNode_t *o = m_new_obj_with_finaliser(mp_obj_HDNode_t);
|
||||
o->base.type = &mod_trezorcrypto_HDNode_type;
|
||||
o->hdnode = hdnode;
|
||||
o->fingerprint = 0;
|
||||
|
@ -45,7 +45,7 @@ STATIC mp_obj_t mod_trezorcrypto_Blake256_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Blake256_t *o = m_new_obj(mp_obj_Blake256_t);
|
||||
mp_obj_Blake256_t *o = m_new_obj_with_finaliser(mp_obj_Blake256_t);
|
||||
o->base.type = type;
|
||||
blake256_Init(&(o->ctx));
|
||||
// constructor called with bytes/str as first parameter
|
||||
|
@ -85,7 +85,7 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2b_make_new(const mp_obj_type_t *type,
|
||||
"time");
|
||||
}
|
||||
|
||||
mp_obj_Blake2b_t *o = m_new_obj(mp_obj_Blake2b_t);
|
||||
mp_obj_Blake2b_t *o = m_new_obj_with_finaliser(mp_obj_Blake2b_t);
|
||||
o->base.type = type;
|
||||
int res = 0;
|
||||
|
||||
|
@ -85,7 +85,7 @@ STATIC mp_obj_t mod_trezorcrypto_Blake2s_make_new(const mp_obj_type_t *type,
|
||||
"time");
|
||||
}
|
||||
|
||||
mp_obj_Blake2s_t *o = m_new_obj(mp_obj_Blake2s_t);
|
||||
mp_obj_Blake2s_t *o = m_new_obj_with_finaliser(mp_obj_Blake2s_t);
|
||||
o->base.type = type;
|
||||
int res = 0;
|
||||
|
||||
|
@ -43,7 +43,8 @@ STATIC mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_make_new(
|
||||
const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 2, 2, false);
|
||||
mp_obj_ChaCha20Poly1305_t *o = m_new_obj(mp_obj_ChaCha20Poly1305_t);
|
||||
mp_obj_ChaCha20Poly1305_t *o =
|
||||
m_new_obj_with_finaliser(mp_obj_ChaCha20Poly1305_t);
|
||||
o->base.type = type;
|
||||
mp_buffer_info_t key, nonce;
|
||||
mp_get_buffer_raise(args[0], &key, MP_BUFFER_READ);
|
||||
|
@ -49,7 +49,7 @@ STATIC mp_obj_t mod_trezorcrypto_Groestl512_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Groestl512_t *o = m_new_obj(mp_obj_Groestl512_t);
|
||||
mp_obj_Groestl512_t *o = m_new_obj_with_finaliser(mp_obj_Groestl512_t);
|
||||
o->base.type = type;
|
||||
groestl512_Init(&(o->ctx));
|
||||
if (n_args == 1) {
|
||||
|
@ -117,7 +117,7 @@ static uint64_t mp_obj_get_uint64(mp_const_obj_t arg) {
|
||||
}
|
||||
|
||||
STATIC mp_obj_t mp_obj_new_scalar() {
|
||||
mp_obj_bignum256modm_t *o = m_new_obj(mp_obj_bignum256modm_t);
|
||||
mp_obj_bignum256modm_t *o = m_new_obj_with_finaliser(mp_obj_bignum256modm_t);
|
||||
o->base.type = &mod_trezorcrypto_monero_bignum256modm_type;
|
||||
set256_modm(o->p, 0);
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
@ -133,7 +133,7 @@ STATIC mp_obj_t mp_obj_new_scalar_r(mp_obj_t r) {
|
||||
}
|
||||
|
||||
STATIC mp_obj_t mp_obj_new_ge25519() {
|
||||
mp_obj_ge25519_t *o = m_new_obj(mp_obj_ge25519_t);
|
||||
mp_obj_ge25519_t *o = m_new_obj_with_finaliser(mp_obj_ge25519_t);
|
||||
o->base.type = &mod_trezorcrypto_monero_ge25519_type;
|
||||
ge25519_set_neutral(&o->p);
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
@ -189,7 +189,7 @@ STATIC mp_obj_t mod_trezorcrypto_monero_ge25519_make_new(
|
||||
const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_ge25519_t *o = m_new_obj(mp_obj_ge25519_t);
|
||||
mp_obj_ge25519_t *o = m_new_obj_with_finaliser(mp_obj_ge25519_t);
|
||||
o->base.type = type;
|
||||
|
||||
if (n_args == 0 || args[0] == mp_const_none) {
|
||||
@ -228,7 +228,7 @@ STATIC mp_obj_t mod_trezorcrypto_monero_bignum256modm_make_new(
|
||||
const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_bignum256modm_t *o = m_new_obj(mp_obj_bignum256modm_t);
|
||||
mp_obj_bignum256modm_t *o = m_new_obj_with_finaliser(mp_obj_bignum256modm_t);
|
||||
o->base.type = type;
|
||||
|
||||
if (n_args == 0 || args[0] == mp_const_none) {
|
||||
@ -286,7 +286,7 @@ STATIC mp_obj_t mod_trezorcrypto_monero_hasher_make_new(
|
||||
const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_hasher_t *o = m_new_obj(mp_obj_hasher_t);
|
||||
mp_obj_hasher_t *o = m_new_obj_with_finaliser(mp_obj_hasher_t);
|
||||
o->base.type = type;
|
||||
xmr_hasher_init(&(o->h));
|
||||
|
||||
@ -1350,7 +1350,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
|
||||
|
||||
STATIC mp_obj_t mod_trezorcrypto_monero_hasher_copy(mp_obj_t self) {
|
||||
mp_obj_hasher_t *o = MP_OBJ_TO_PTR(self);
|
||||
mp_obj_hasher_t *cp = m_new_obj(mp_obj_hasher_t);
|
||||
mp_obj_hasher_t *cp = m_new_obj_with_finaliser(mp_obj_hasher_t);
|
||||
cp->base.type = o->base.type;
|
||||
memcpy(&(cp->h), &(o->h), sizeof(Hasher));
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
|
@ -59,7 +59,7 @@ STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 3, 4, false);
|
||||
mp_obj_Pbkdf2_t *o = m_new_obj(mp_obj_Pbkdf2_t);
|
||||
mp_obj_Pbkdf2_t *o = m_new_obj_with_finaliser(mp_obj_Pbkdf2_t);
|
||||
o->base.type = type;
|
||||
|
||||
mp_buffer_info_t password;
|
||||
|
@ -40,7 +40,7 @@ STATIC mp_obj_t mod_trezorcrypto_Rfc6979_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 2, 2, false);
|
||||
mp_obj_Rfc6979_t *o = m_new_obj(mp_obj_Rfc6979_t);
|
||||
mp_obj_Rfc6979_t *o = m_new_obj_with_finaliser(mp_obj_Rfc6979_t);
|
||||
o->base.type = type;
|
||||
mp_buffer_info_t pkey, hash;
|
||||
mp_get_buffer_raise(args[0], &pkey, MP_BUFFER_READ);
|
||||
|
@ -45,7 +45,7 @@ STATIC mp_obj_t mod_trezorcrypto_Ripemd160_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Ripemd160_t *o = m_new_obj(mp_obj_Ripemd160_t);
|
||||
mp_obj_Ripemd160_t *o = m_new_obj_with_finaliser(mp_obj_Ripemd160_t);
|
||||
o->base.type = type;
|
||||
ripemd160_Init(&(o->ctx));
|
||||
// constructor called with bytes/str as first parameter
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "py/gc.h"
|
||||
#include "py/objstr.h"
|
||||
|
||||
#include "vendor/secp256k1-zkp/include/secp256k1.h"
|
||||
@ -25,6 +26,13 @@
|
||||
#include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
|
||||
#include "vendor/secp256k1-zkp/include/secp256k1_recovery.h"
|
||||
|
||||
// "maybe" = do not fail if allocation fails
|
||||
// "with_finaliser" = pass true to gc_alloc
|
||||
// combination of "malloc_maybe" and "malloc_with_finaliser" does not exist in
|
||||
// malloc.c, so we need to define our own version here.
|
||||
#define m_new_obj_var_maybe_with_finaliser(obj_type, var_type, var_num) \
|
||||
gc_alloc(sizeof(obj_type) + sizeof(var_type) * (var_num), true)
|
||||
|
||||
void secp256k1_default_illegal_callback_fn(const char *str, void *data) {
|
||||
(void)data;
|
||||
mp_raise_ValueError(str);
|
||||
@ -64,7 +72,7 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_context_make_new(
|
||||
const size_t secp256k1_ctx_size = secp256k1_context_preallocated_size(
|
||||
SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
|
||||
|
||||
mp_obj_secp256k1_context_t *o = m_new_obj_var_maybe(
|
||||
mp_obj_secp256k1_context_t *o = m_new_obj_var_maybe_with_finaliser(
|
||||
mp_obj_secp256k1_context_t, uint8_t, secp256k1_ctx_size);
|
||||
if (!o) {
|
||||
mp_raise_ValueError("secp256k1_zkp context is too large");
|
||||
|
@ -45,7 +45,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha1_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Sha1_t *o = m_new_obj(mp_obj_Sha1_t);
|
||||
mp_obj_Sha1_t *o = m_new_obj_with_finaliser(mp_obj_Sha1_t);
|
||||
o->base.type = type;
|
||||
sha1_Init(&(o->ctx));
|
||||
// constructor called with bytes/str as first parameter
|
||||
|
@ -45,7 +45,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha256_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Sha256_t *o = m_new_obj(mp_obj_Sha256_t);
|
||||
mp_obj_Sha256_t *o = m_new_obj_with_finaliser(mp_obj_Sha256_t);
|
||||
o->base.type = type;
|
||||
sha256_Init(&(o->ctx));
|
||||
// constructor called with bytes/str as first parameter
|
||||
|
@ -46,7 +46,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_256_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, true);
|
||||
mp_obj_Sha3_256_t *o = m_new_obj(mp_obj_Sha3_256_t);
|
||||
mp_obj_Sha3_256_t *o = m_new_obj_with_finaliser(mp_obj_Sha3_256_t);
|
||||
o->base.type = type;
|
||||
o->keccak = 0;
|
||||
sha3_256_Init(&(o->ctx));
|
||||
@ -111,7 +111,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_256_digest_obj,
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha3_256_copy(size_t n_args,
|
||||
const mp_obj_t *args) {
|
||||
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(args[0]);
|
||||
mp_obj_Sha3_256_t *out = m_new_obj(mp_obj_Sha3_256_t);
|
||||
mp_obj_Sha3_256_t *out = m_new_obj_with_finaliser(mp_obj_Sha3_256_t);
|
||||
out->base.type = o->base.type;
|
||||
out->keccak = o->keccak;
|
||||
memcpy(&(out->ctx), &(o->ctx), sizeof(SHA3_CTX));
|
||||
|
@ -46,7 +46,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha3_512_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, true);
|
||||
mp_obj_Sha3_512_t *o = m_new_obj(mp_obj_Sha3_512_t);
|
||||
mp_obj_Sha3_512_t *o = m_new_obj_with_finaliser(mp_obj_Sha3_512_t);
|
||||
o->base.type = type;
|
||||
o->keccak = 0;
|
||||
sha3_512_Init(&(o->ctx));
|
||||
@ -111,7 +111,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha3_512_digest_obj,
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_copy(size_t n_args,
|
||||
const mp_obj_t *args) {
|
||||
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
|
||||
mp_obj_Sha3_512_t *out = m_new_obj(mp_obj_Sha3_512_t);
|
||||
mp_obj_Sha3_512_t *out = m_new_obj_with_finaliser(mp_obj_Sha3_512_t);
|
||||
out->base.type = o->base.type;
|
||||
out->keccak = o->keccak;
|
||||
memcpy(&(out->ctx), &(o->ctx), sizeof(SHA3_CTX));
|
||||
|
@ -45,7 +45,7 @@ STATIC mp_obj_t mod_trezorcrypto_Sha512_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||
mp_obj_Sha512_t *o = m_new_obj(mp_obj_Sha512_t);
|
||||
mp_obj_Sha512_t *o = m_new_obj_with_finaliser(mp_obj_Sha512_t);
|
||||
o->base.type = type;
|
||||
sha512_Init(&(o->ctx));
|
||||
if (n_args == 1) {
|
||||
|
@ -135,7 +135,7 @@ STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type,
|
||||
mp_raise_ValueError("interface is invalid");
|
||||
}
|
||||
|
||||
mp_obj_USB_t *o = m_new_obj(mp_obj_USB_t);
|
||||
mp_obj_USB_t *o = m_new_obj_with_finaliser(mp_obj_USB_t);
|
||||
o->base.type = type;
|
||||
|
||||
o->state = USB_CLOSED;
|
||||
|
Loading…
Reference in New Issue
Block a user