Now uses an eip712 context struct instead of having multiple global variables => reduced bss footprint
This commit is contained in:
@@ -5,15 +5,13 @@
|
|||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "eip712.h"
|
#include "eip712.h"
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
|
#include "mem_utils.h"
|
||||||
#include "sol_typenames.h"
|
#include "sol_typenames.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
#include "field_hash.h"
|
#include "field_hash.h"
|
||||||
#include "ui_logic.h"
|
#include "ui_logic.h"
|
||||||
|
|
||||||
uint8_t *typenames_array;
|
s_eip712_context *eip712_context = NULL;
|
||||||
uint8_t *structs_array;
|
|
||||||
uint8_t *current_struct_fields_array;
|
|
||||||
bool eip712_context_initialized = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -24,6 +22,11 @@ bool eip712_context_init(void)
|
|||||||
// init global variables
|
// init global variables
|
||||||
mem_init();
|
mem_init();
|
||||||
|
|
||||||
|
if ((eip712_context = MEM_ALLOC_AND_ALIGN_TO_TYPE(sizeof(*eip712_context), *eip712_context)) == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (sol_typenames_init() == false)
|
if (sol_typenames_init() == false)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -45,15 +48,13 @@ bool eip712_context_init(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set types pointer
|
// set types pointer
|
||||||
if ((structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
if ((eip712_context->structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create len(types)
|
// create len(types)
|
||||||
*structs_array = 0;
|
*(eip712_context->structs_array) = 0;
|
||||||
|
|
||||||
eip712_context_initialized = true;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -64,7 +65,7 @@ void eip712_context_deinit(void)
|
|||||||
field_hash_deinit();
|
field_hash_deinit();
|
||||||
ui_712_deinit();
|
ui_712_deinit();
|
||||||
mem_reset();
|
mem_reset();
|
||||||
eip712_context_initialized = false;
|
eip712_context = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,15 +5,18 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
extern uint8_t *typenames_array;
|
typedef struct
|
||||||
extern uint8_t *structs_array;
|
{
|
||||||
extern uint8_t *current_struct_fields_array;
|
uint8_t *typenames_array;
|
||||||
|
uint8_t *structs_array;
|
||||||
|
uint8_t *current_struct_fields_array;
|
||||||
|
} s_eip712_context;
|
||||||
|
|
||||||
|
extern s_eip712_context *eip712_context;
|
||||||
|
|
||||||
bool eip712_context_init(void);
|
bool eip712_context_init(void);
|
||||||
void eip712_context_deinit(void);
|
void eip712_context_deinit(void);
|
||||||
|
|
||||||
extern bool eip712_context_initialized;
|
|
||||||
|
|
||||||
#endif // HAVE_EIP712_FULL_SUPPORT
|
#endif // HAVE_EIP712_FULL_SUPPORT
|
||||||
|
|
||||||
#endif // EIP712_CTX_H_
|
#endif // EIP712_CTX_H_
|
||||||
|
|||||||
@@ -239,7 +239,7 @@ bool set_struct_name(const uint8_t *const data)
|
|||||||
char *name_ptr;
|
char *name_ptr;
|
||||||
|
|
||||||
// increment number of structs
|
// increment number of structs
|
||||||
*structs_array += 1;
|
*(eip712_context->structs_array) += 1;
|
||||||
|
|
||||||
// copy length
|
// copy length
|
||||||
if ((length_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
if ((length_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||||
@@ -256,11 +256,11 @@ bool set_struct_name(const uint8_t *const data)
|
|||||||
memmove(name_ptr, &data[OFFSET_CDATA], data[OFFSET_LC]);
|
memmove(name_ptr, &data[OFFSET_CDATA], data[OFFSET_LC]);
|
||||||
|
|
||||||
// initialize number of fields
|
// initialize number of fields
|
||||||
if ((current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
if ((eip712_context->current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*current_struct_fields_array = 0;
|
*(eip712_context->current_struct_fields_array) = 0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +280,7 @@ bool set_struct_field(const uint8_t *const data)
|
|||||||
char *fieldname_ptr;
|
char *fieldname_ptr;
|
||||||
|
|
||||||
// increment number of struct fields
|
// increment number of struct fields
|
||||||
*current_struct_fields_array += 1;
|
*(eip712_context->current_struct_fields_array) += 1;
|
||||||
|
|
||||||
// copy TypeDesc
|
// copy TypeDesc
|
||||||
if ((type_desc_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
if ((type_desc_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||||
@@ -369,7 +369,7 @@ bool handle_eip712_struct_def(const uint8_t *const apdu_buf)
|
|||||||
{
|
{
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
if (!eip712_context_initialized)
|
if (eip712_context == NULL)
|
||||||
{
|
{
|
||||||
if (!eip712_context_init())
|
if (!eip712_context_init())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ static const void *get_nth_field_from_path(uint8_t *const fields_count_ptr,
|
|||||||
if (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
if (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||||
{
|
{
|
||||||
typename = get_struct_field_typename(field_ptr, &length);
|
typename = get_struct_field_typename(field_ptr, &length);
|
||||||
if ((struct_ptr = get_structn(structs_array, typename, length)) == NULL)
|
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, length)) == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -273,7 +273,7 @@ static bool path_update(void)
|
|||||||
while (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
while (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||||
{
|
{
|
||||||
typename = get_struct_field_typename(field_ptr, &typename_len);
|
typename = get_struct_field_typename(field_ptr, &typename_len);
|
||||||
if ((struct_ptr = get_structn(structs_array, typename, typename_len)) == NULL)
|
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -293,7 +293,7 @@ static bool path_update(void)
|
|||||||
}
|
}
|
||||||
cx_keccak_init(hash_ctx, 256); // initialize it
|
cx_keccak_init(hash_ctx, 256); // initialize it
|
||||||
// get the struct typehash
|
// get the struct typehash
|
||||||
if ((thash_ptr = type_hash(structs_array, typename, typename_len)) == NULL)
|
if ((thash_ptr = type_hash(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -326,7 +326,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
path_struct->root_struct = get_structn(structs_array, struct_name, name_length);
|
path_struct->root_struct = get_structn(eip712_context->structs_array, struct_name, name_length);
|
||||||
|
|
||||||
if (path_struct->root_struct == NULL)
|
if (path_struct->root_struct == NULL)
|
||||||
{
|
{
|
||||||
@@ -347,7 +347,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
cx_keccak_init(hash_ctx, 256); // init hash
|
cx_keccak_init(hash_ctx, 256); // init hash
|
||||||
if ((thash_ptr = type_hash(structs_array, struct_name, name_length)) == NULL)
|
if ((thash_ptr = type_hash(eip712_context->structs_array, struct_name, name_length)) == NULL)
|
||||||
{
|
{
|
||||||
PRINTF("Memory allocation failed!\n");
|
PRINTF("Memory allocation failed!\n");
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -64,11 +64,11 @@ bool sol_typenames_init(void)
|
|||||||
uint8_t *typename_len_ptr;
|
uint8_t *typename_len_ptr;
|
||||||
char *typename_ptr;
|
char *typename_ptr;
|
||||||
|
|
||||||
if ((typenames_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
if ((eip712_context->typenames_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*typenames_array = 0;
|
*(eip712_context->typenames_array) = 0;
|
||||||
// loop over typenames
|
// loop over typenames
|
||||||
for (uint8_t s_idx = 0; s_idx < ARRAY_SIZE(typenames); ++s_idx)
|
for (uint8_t s_idx = 0; s_idx < ARRAY_SIZE(typenames); ++s_idx)
|
||||||
{
|
{
|
||||||
@@ -90,7 +90,7 @@ bool sol_typenames_init(void)
|
|||||||
memcpy(typename_ptr, PIC(typenames[s_idx]), *typename_len_ptr);
|
memcpy(typename_ptr, PIC(typenames[s_idx]), *typename_len_ptr);
|
||||||
}
|
}
|
||||||
// increment array size
|
// increment array size
|
||||||
*typenames_array += 1;
|
*(eip712_context->typenames_array) += 1;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ const char *get_struct_field_sol_typename(const uint8_t *field_ptr,
|
|||||||
bool typename_found;
|
bool typename_found;
|
||||||
|
|
||||||
field_type = struct_field_type(field_ptr);
|
field_type = struct_field_type(field_ptr);
|
||||||
typename_ptr = get_array_in_mem(typenames_array, &typenames_count);
|
typename_ptr = get_array_in_mem(eip712_context->typenames_array, &typenames_count);
|
||||||
typename_found = false;
|
typename_found = false;
|
||||||
while (typenames_count-- > 0)
|
while (typenames_count-- > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user