From 2fa6379470b0dadefc519910193d9c062d8e0dd9 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 16 May 2022 10:59:20 +0200 Subject: [PATCH] Now only compiles full EIP-712 support for capable devices --- Makefile | 5 +++++ src/main.c | 6 ++++++ src_common/ethUtils.h | 2 ++ src_features/signMessageEIP712/context.c | 4 ++++ src_features/signMessageEIP712/context.h | 5 ++++- src_features/signMessageEIP712/eip712.h | 6 ++++-- src_features/signMessageEIP712/encode_field.c | 4 ++++ src_features/signMessageEIP712/encode_field.h | 4 ++++ src_features/signMessageEIP712/entrypoint.c | 5 +++++ src_features/signMessageEIP712/field_hash.c | 5 +++++ src_features/signMessageEIP712/field_hash.h | 5 +++++ src_features/signMessageEIP712/mem.c | 5 ++++- src_features/signMessageEIP712/mem.h | 4 ++++ src_features/signMessageEIP712/mem_utils.c | 4 ++++ src_features/signMessageEIP712/mem_utils.h | 4 ++++ src_features/signMessageEIP712/path.c | 4 ++++ src_features/signMessageEIP712/path.h | 4 ++++ src_features/signMessageEIP712/sol_typenames.c | 4 ++++ src_features/signMessageEIP712/sol_typenames.h | 4 ++++ src_features/signMessageEIP712/type_hash.c | 5 +++++ src_features/signMessageEIP712/type_hash.h | 4 ++++ src_features/signMessageEIP712/ui_flow_712.c | 4 ++++ src_features/signMessageEIP712/ui_flow_712.h | 4 ++++ src_features/signMessageEIP712/ui_logic.c | 4 ++++ src_features/signMessageEIP712/ui_logic.h | 4 ++++ src_features/signMessageEIP712_v0/cmd_signMessage712.c | 6 ++++-- 26 files changed, 109 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 05646f3..872bc15 100644 --- a/Makefile +++ b/Makefile @@ -137,6 +137,11 @@ DEFINES += HAVE_NFT_TESTING_KEY endif endif +# EIP-712 +ifneq ($(TARGET_NAME),TARGET_NANOS) +DEFINES += HAVE_EIP712_FULL_SUPPORT +endif + # Enabling debug PRINTF DEBUG:=0 ifneq ($(DEBUG),0) diff --git a/src/main.c b/src/main.c index 09f8eb2..85c49e2 100644 --- a/src/main.c +++ b/src/main.c @@ -675,8 +675,12 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { } else { +#ifdef HAVE_EIP712_FULL_SUPPORT *flags |= IO_ASYNCH_REPLY; handle_eip712_sign(G_io_apdu_buffer); +#else + THROW(0x6B00); +#endif // HAVE_EIP712_FULL_SUPPORT } break; @@ -703,6 +707,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { #endif +#ifdef HAVE_EIP712_FULL_SUPPORT case INS_EIP712_STRUCT_DEF: *flags |= IO_ASYNCH_REPLY; handle_eip712_struct_def(G_io_apdu_buffer); @@ -712,6 +717,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { *flags |= IO_ASYNCH_REPLY; handle_eip712_struct_impl(G_io_apdu_buffer); break; +#endif // HAVE_EIP712_FULL_SUPPORT #if 0 case 0xFF: // return to dashboard diff --git a/src_common/ethUtils.h b/src_common/ethUtils.h index 3c147cc..0755b13 100644 --- a/src_common/ethUtils.h +++ b/src_common/ethUtils.h @@ -23,6 +23,8 @@ #include "cx.h" #include "chainConfig.h" +#define KECCAK256_HASH_BYTESIZE 32 + /** * @brief Decode an RLP encoded field - see * https://github.com/ethereum/wiki/wiki/RLP diff --git a/src_features/signMessageEIP712/context.c b/src_features/signMessageEIP712/context.c index dbea2d6..27a937e 100644 --- a/src_features/signMessageEIP712/context.c +++ b/src_features/signMessageEIP712/context.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include "context.h" @@ -64,3 +66,5 @@ void eip712_context_deinit(void) mem_reset(); eip712_context_initialized = false; } + +#endif diff --git a/src_features/signMessageEIP712/context.h b/src_features/signMessageEIP712/context.h index 82aff85..a85f56a 100644 --- a/src_features/signMessageEIP712/context.h +++ b/src_features/signMessageEIP712/context.h @@ -1,8 +1,9 @@ #ifndef EIP712_CTX_H_ #define EIP712_CTX_H_ -#include +#ifdef HAVE_EIP712_FULL_SUPPORT +#include extern uint8_t *typenames_array; extern uint8_t *structs_array; @@ -13,4 +14,6 @@ void eip712_context_deinit(void); extern bool eip712_context_initialized; +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // EIP712_CTX_H_ diff --git a/src_features/signMessageEIP712/eip712.h b/src_features/signMessageEIP712/eip712.h index 70a5ffe..98a7b05 100644 --- a/src_features/signMessageEIP712/eip712.h +++ b/src_features/signMessageEIP712/eip712.h @@ -1,6 +1,8 @@ #ifndef EIP712_H_ #define EIP712_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -51,8 +53,6 @@ typedef enum EIP712_STRUCT_HASH } e_eip712_hash_type; -#define KECCAK256_HASH_BYTESIZE 32 - #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define DOMAIN_STRUCT_NAME "EIP712Domain" @@ -84,4 +84,6 @@ const uint8_t *get_structn(const uint8_t *const ptr, const void *get_array_in_mem(const void *ptr, uint8_t *const array_size); const char *get_string_in_mem(const uint8_t *ptr, uint8_t *const string_length); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // EIP712_H_ diff --git a/src_features/signMessageEIP712/encode_field.c b/src_features/signMessageEIP712/encode_field.c index fea2a26..4702fa4 100644 --- a/src_features/signMessageEIP712/encode_field.c +++ b/src_features/signMessageEIP712/encode_field.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include "encode_field.h" @@ -108,3 +110,5 @@ void *encode_address(const uint8_t *const value, uint8_t length) } return encode_integer(value, length); } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/encode_field.h b/src_features/signMessageEIP712/encode_field.h index acc925c..6bb58e4 100644 --- a/src_features/signMessageEIP712/encode_field.h +++ b/src_features/signMessageEIP712/encode_field.h @@ -1,6 +1,8 @@ #ifndef ENCODE_FIELD_H_ #define ENCODE_FIELD_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -11,4 +13,6 @@ void *encode_boolean(const bool *const value, uint8_t length); void *encode_address(const uint8_t *const value, uint8_t length); void *encode_bytes(const uint8_t *const value, uint8_t length); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // ENCODE_FIELD_H_ diff --git a/src_features/signMessageEIP712/entrypoint.c b/src_features/signMessageEIP712/entrypoint.c index 5b67a4f..e744ff1 100644 --- a/src_features/signMessageEIP712/entrypoint.c +++ b/src_features/signMessageEIP712/entrypoint.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include @@ -449,3 +451,6 @@ bool handle_eip712_sign(const uint8_t *const apdu_buf) ui_712_end_sign(); return true; } + + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/field_hash.c b/src_features/signMessageEIP712/field_hash.c index c8c1fb1..ef019ac 100644 --- a/src_features/signMessageEIP712/field_hash.c +++ b/src_features/signMessageEIP712/field_hash.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include "field_hash.h" #include "encode_field.h" @@ -7,6 +9,7 @@ #include "eip712.h" #include "shared_context.h" #include "ui_logic.h" +#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE static s_field_hashing *fh = NULL; @@ -170,3 +173,5 @@ bool field_hash(const uint8_t *data, return true; } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/field_hash.h b/src_features/signMessageEIP712/field_hash.h index 6a3fb4a..f184792 100644 --- a/src_features/signMessageEIP712/field_hash.h +++ b/src_features/signMessageEIP712/field_hash.h @@ -1,6 +1,8 @@ #ifndef FIELD_HASH_H_ #define FIELD_HASH_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -23,4 +25,7 @@ void field_hash_deinit(void); bool field_hash(const uint8_t *data, uint8_t data_length, bool partial); + +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // FIELD_HASH_H_ diff --git a/src_features/signMessageEIP712/mem.c b/src_features/signMessageEIP712/mem.c index e0c61c9..e4c01c4 100644 --- a/src_features/signMessageEIP712/mem.c +++ b/src_features/signMessageEIP712/mem.c @@ -1,8 +1,9 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include "mem.h" #include "shared_context.h" - #define SIZE_MEM_BUFFER 5120 static uint8_t mem_buffer[SIZE_MEM_BUFFER]; @@ -75,3 +76,5 @@ void mem_dealloc(size_t size) mem_idx -= size; } } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/mem.h b/src_features/signMessageEIP712/mem.h index d91f57f..0cb9aca 100644 --- a/src_features/signMessageEIP712/mem.h +++ b/src_features/signMessageEIP712/mem.h @@ -1,6 +1,8 @@ #ifndef MEM_H_ #define MEM_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include void mem_init(void); @@ -12,4 +14,6 @@ void mem_dealloc(size_t size); extern size_t mem_max; #endif +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // MEM_H_ diff --git a/src_features/signMessageEIP712/mem_utils.c b/src_features/signMessageEIP712/mem_utils.c index d8b5cf8..09d98cd 100644 --- a/src_features/signMessageEIP712/mem_utils.c +++ b/src_features/signMessageEIP712/mem_utils.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include @@ -77,3 +79,5 @@ void *mem_alloc_and_align(size_t size, size_t alignment) } return mem_alloc(size); } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/mem_utils.h b/src_features/signMessageEIP712/mem_utils.h index 8a89c5b..f339a92 100644 --- a/src_features/signMessageEIP712/mem_utils.h +++ b/src_features/signMessageEIP712/mem_utils.h @@ -1,6 +1,8 @@ #ifndef MEM_UTILS_H_ #define MEM_UTILS_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -11,4 +13,6 @@ void *mem_alloc_and_copy(const void *data, size_t size); char *mem_alloc_and_format_uint(uint32_t value, uint8_t *const written_chars); void *mem_alloc_and_align(size_t size, size_t alignment); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // MEM_UTILS_H_ diff --git a/src_features/signMessageEIP712/path.c b/src_features/signMessageEIP712/path.c index de8634b..28fd41b 100644 --- a/src_features/signMessageEIP712/path.c +++ b/src_features/signMessageEIP712/path.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include "path.h" @@ -614,3 +616,5 @@ void path_deinit(void) { path_struct = NULL; } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/path.h b/src_features/signMessageEIP712/path.h index 030e369..b8e0ad6 100644 --- a/src_features/signMessageEIP712/path.h +++ b/src_features/signMessageEIP712/path.h @@ -1,6 +1,8 @@ #ifndef PATH_H_ #define PATH_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -36,4 +38,6 @@ bool path_init(void); void path_deinit(void); bool path_new_array_depth(uint8_t size); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // PATH_H_ diff --git a/src_features/signMessageEIP712/sol_typenames.c b/src_features/signMessageEIP712/sol_typenames.c index f4fc562..b9cbf53 100644 --- a/src_features/signMessageEIP712/sol_typenames.c +++ b/src_features/signMessageEIP712/sol_typenames.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include "sol_typenames.h" @@ -130,3 +132,5 @@ const char *get_struct_field_sol_typename(const uint8_t *field_ptr, } return NULL; // Not found } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/sol_typenames.h b/src_features/signMessageEIP712/sol_typenames.h index bb8e259..77f744c 100644 --- a/src_features/signMessageEIP712/sol_typenames.h +++ b/src_features/signMessageEIP712/sol_typenames.h @@ -1,6 +1,8 @@ #ifndef SOL_TYPENAMES_H_ #define SOL_TYPENAMES_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include @@ -9,4 +11,6 @@ bool sol_typenames_init(void); const char *get_struct_field_sol_typename(const uint8_t *ptr, uint8_t *const length); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // SOL_TYPENAMES_H_ diff --git a/src_features/signMessageEIP712/type_hash.c b/src_features/signMessageEIP712/type_hash.c index 203608c..f9b28ca 100644 --- a/src_features/signMessageEIP712/type_hash.c +++ b/src_features/signMessageEIP712/type_hash.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include @@ -6,6 +8,7 @@ #include "eip712.h" #include "type_hash.h" #include "shared_context.h" +#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE static inline void hash_nbytes(const uint8_t *b, uint8_t n) { @@ -316,3 +319,5 @@ const uint8_t *type_hash(const void *const structs_array, KECCAK256_HASH_BYTESIZE); return hash_ptr; } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/type_hash.h b/src_features/signMessageEIP712/type_hash.h index e40ba7a..7fe8bd2 100644 --- a/src_features/signMessageEIP712/type_hash.h +++ b/src_features/signMessageEIP712/type_hash.h @@ -1,10 +1,14 @@ #ifndef TYPE_HASH_H_ #define TYPE_HASH_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include const uint8_t *type_hash(const void *const structs_array, const char *const struct_name, const uint8_t struct_name_length); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // TYPE_HASH_H_ diff --git a/src_features/signMessageEIP712/ui_flow_712.c b/src_features/signMessageEIP712/ui_flow_712.c index 37d97f1..7d06bd6 100644 --- a/src_features/signMessageEIP712/ui_flow_712.c +++ b/src_features/signMessageEIP712/ui_flow_712.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include "ui_flow_712.h" #include "ui_logic.h" #include "shared_context.h" // strings @@ -51,3 +53,5 @@ UX_FLOW(ux_712_flow, &ux_712_step_dummy, &ux_712_step_approve, &ux_712_step_reject); + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/ui_flow_712.h b/src_features/signMessageEIP712/ui_flow_712.h index 46f3120..3ace335 100644 --- a/src_features/signMessageEIP712/ui_flow_712.h +++ b/src_features/signMessageEIP712/ui_flow_712.h @@ -1,8 +1,12 @@ #ifndef UI_FLOW_712_H_ #define UI_FLOW_712_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include "ux_flow_engine.h" extern const ux_flow_step_t* const ux_712_flow[]; +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // UI_FLOW_712_H_ diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index 993e0ff..e6048ce 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -1,3 +1,5 @@ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include #include "ui_logic.h" @@ -222,3 +224,5 @@ unsigned int ui_712_reject(const bagl_element_t *e) eip712_context_deinit(); return 0; } + +#endif // HAVE_EIP712_FULL_SUPPORT diff --git a/src_features/signMessageEIP712/ui_logic.h b/src_features/signMessageEIP712/ui_logic.h index c313a9d..acfb52c 100644 --- a/src_features/signMessageEIP712/ui_logic.h +++ b/src_features/signMessageEIP712/ui_logic.h @@ -1,6 +1,8 @@ #ifndef UI_LOGIC_712_H_ #define UI_LOGIC_712_H_ +#ifdef HAVE_EIP712_FULL_SUPPORT + #include #include "ux.h" @@ -26,4 +28,6 @@ void ui_712_end_sign(void); unsigned int ui_712_approve(const bagl_element_t *e); unsigned int ui_712_reject(const bagl_element_t *e); +#endif // HAVE_EIP712_FULL_SUPPORT + #endif // UI_LOGIC_712_H_ diff --git a/src_features/signMessageEIP712_v0/cmd_signMessage712.c b/src_features/signMessageEIP712_v0/cmd_signMessage712.c index 09e3b30..6865b3a 100644 --- a/src_features/signMessageEIP712_v0/cmd_signMessage712.c +++ b/src_features/signMessageEIP712_v0/cmd_signMessage712.c @@ -4,6 +4,7 @@ #include "ui_flow.h" #include "eip712.h" #include "common_712.h" +#include "ethUtils.h" void handleSignEIP712Message_v0(uint8_t p1, uint8_t p2, @@ -11,8 +12,9 @@ void handleSignEIP712Message_v0(uint8_t p1, uint8_t dataLength, unsigned int *flags, unsigned int *tx) { - UNUSED(tx); - if ((p1 != 00) || (p2 != 00)) { + (void)tx; + (void)p2; + if (p1 != 00) { THROW(0x6B00); } if (appState != APP_STATE_IDLE) {