diff --git a/src/apdu_constants.h b/src/apdu_constants.h index a5934ce..52de586 100644 --- a/src/apdu_constants.h +++ b/src/apdu_constants.h @@ -53,12 +53,14 @@ #endif -#define OFFSET_CLA 0 -#define OFFSET_INS 1 -#define OFFSET_P1 2 -#define OFFSET_P2 3 -#define OFFSET_LC 4 -#define OFFSET_CDATA 5 +enum { + OFFSET_CLA = 0, + OFFSET_INS, + OFFSET_P1, + OFFSET_P2, + OFFSET_LC, + OFFSET_CDATA +}; void handleGetPublicKey(uint8_t p1, uint8_t p2, @@ -99,7 +101,7 @@ void handleSignPersonalMessage(uint8_t p1, void handleSignEIP712Message_v0(uint8_t p1, uint8_t p2, const uint8_t *dataBuffer, - uint16_t dataLength, + uint8_t dataLength, unsigned int *flags, unsigned int *tx); diff --git a/src/main.c b/src/main.c index 5af4e52..bd8e67b 100644 --- a/src/main.c +++ b/src/main.c @@ -675,6 +675,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { } else { + *flags |= IO_ASYNCH_REPLY; handle_eip712_sign(G_io_apdu_buffer); } break; diff --git a/src/ui_callbacks.h b/src/ui_callbacks.h index 2d7ff98..f1b1339 100644 --- a/src/ui_callbacks.h +++ b/src/ui_callbacks.h @@ -14,8 +14,6 @@ unsigned int io_seproxyhal_touch_signMessage_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_signMessage_cancel(const bagl_element_t *e); unsigned int io_seproxyhal_touch_data_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_data_cancel(const bagl_element_t *e); -unsigned int io_seproxyhal_touch_signMessage712_v0_ok(const bagl_element_t *e); -unsigned int io_seproxyhal_touch_signMessage712_v0_cancel(const bagl_element_t *e); unsigned int io_seproxyhal_touch_eth2_address_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_privacy_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_privacy_cancel(const bagl_element_t *e); diff --git a/src_features/signMessageEIP712/eip712.h b/src_features/signMessageEIP712/eip712.h index 75f63f9..70a5ffe 100644 --- a/src_features/signMessageEIP712/eip712.h +++ b/src_features/signMessageEIP712/eip712.h @@ -4,15 +4,6 @@ #include #include -enum { - OFFSET_CLA = 0, - OFFSET_INS, - OFFSET_P1, - OFFSET_P2, - OFFSET_LC, - OFFSET_DATA -}; - typedef enum { // contract defined struct diff --git a/src_features/signMessageEIP712/entrypoint.c b/src_features/signMessageEIP712/entrypoint.c index 5e00b87..368deb3 100644 --- a/src_features/signMessageEIP712/entrypoint.c +++ b/src_features/signMessageEIP712/entrypoint.c @@ -3,6 +3,7 @@ #include #include +#include "apdu_constants.h" #include "eip712.h" #include "mem.h" #include "type_hash.h" @@ -12,6 +13,7 @@ #include "path.h" #include "shared_context.h" #include "ui_logic.h" +#include "common_712.h" // lib functions @@ -249,7 +251,7 @@ bool set_struct_name(const uint8_t *const data) { return false; } - memmove(name_ptr, &data[OFFSET_DATA], data[OFFSET_LC]); + memmove(name_ptr, &data[OFFSET_CDATA], data[OFFSET_LC]); // initialize number of fields if ((current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL) @@ -264,7 +266,7 @@ bool set_struct_name(const uint8_t *const data) // TODO: Handle partial sends bool set_struct_field(const uint8_t *const data) { - uint8_t data_idx = OFFSET_DATA; + uint8_t data_idx = OFFSET_CDATA; uint8_t *type_desc_ptr; uint8_t *type_size_ptr; uint8_t *typename_len_ptr; @@ -403,16 +405,16 @@ bool handle_eip712_struct_impl(const uint8_t *const apdu_buf) { case P2_NAME: // set root type - ret = path_set_root((char*)&apdu_buf[OFFSET_DATA], + ret = path_set_root((char*)&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]); break; case P2_FIELD: - ret = field_hash(&apdu_buf[OFFSET_DATA], + ret = field_hash(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC], apdu_buf[OFFSET_P1] != P1_COMPLETE); break; case P2_ARRAY: - ret = path_new_array_depth(apdu_buf[OFFSET_DATA]); + ret = path_new_array_depth(apdu_buf[OFFSET_CDATA]); break; default: PRINTF("Unknown P2 0x%x for APDU 0x%x\n", @@ -432,22 +434,12 @@ bool handle_eip712_struct_impl(const uint8_t *const apdu_buf) bool handle_eip712_sign(const uint8_t *const apdu_buf) { - uint8_t i; - - if (apdu_buf[OFFSET_LC] < 1) { - PRINTF("Invalid data\n"); - THROW(0x6a80); + if (parseBip32(&apdu_buf[OFFSET_CDATA], + &apdu_buf[OFFSET_LC], + &tmpCtx.messageSigningContext.bip32) == NULL) + { + return false; } - tmpCtx.messageSigningContext712.pathLength = apdu_buf[OFFSET_DATA]; - if ((tmpCtx.messageSigningContext712.pathLength < 0x01) || - (tmpCtx.messageSigningContext712.pathLength > MAX_BIP32_PATH)) { - PRINTF("Invalid path\n"); - THROW(0x6a80); - } - for (i = 0; i < tmpCtx.messageSigningContext712.pathLength; i++) { - tmpCtx.messageSigningContext712.bip32Path[i] = U4BE(apdu_buf + OFFSET_LC + 1 + (i * 4), 0); - } - ui_712_end_sign(); return true; } diff --git a/src_features/signMessageEIP712/ui_flow_712.c b/src_features/signMessageEIP712/ui_flow_712.c index 6db752b..7563407 100644 --- a/src_features/signMessageEIP712/ui_flow_712.c +++ b/src_features/signMessageEIP712/ui_flow_712.c @@ -1,6 +1,7 @@ #include "ui_flow_712.h" #include "ui_logic.h" #include "shared_context.h" // strings +#include "common_712.h" // clang-format off UX_STEP_NOCB( @@ -30,7 +31,7 @@ UX_STEP_INIT( UX_STEP_CB( ux_712_step_approve, pb, - NULL,//io_seproxyhal_touch_signMessage712_ok(NULL), + ui_712_approve_cb(NULL), { &C_icon_validate_14, "Approve", @@ -38,7 +39,7 @@ UX_STEP_CB( UX_STEP_CB( ux_712_step_reject, pb, - NULL,//io_seproxyhal_touch_signMessage712_cancel(NULL), + ui_712_reject_cb(NULL), { &C_icon_crossmark, "Reject", diff --git a/src_features/signMessageEIP712_v0/ui_common_signMessage712.c b/src_features/signMessageEIP712_common/common_712.c similarity index 86% rename from src_features/signMessageEIP712_v0/ui_common_signMessage712.c rename to src_features/signMessageEIP712_common/common_712.c index e7dea8f..93ba048 100644 --- a/src_features/signMessageEIP712_v0/ui_common_signMessage712.c +++ b/src_features/signMessageEIP712_common/common_712.c @@ -1,16 +1,19 @@ -#include "os_io_seproxyhal.h" #include "shared_context.h" +#include "os_io_seproxyhal.h" #include "ui_callbacks.h" +#include "common_712.h" static const uint8_t EIP_712_MAGIC[] = {0x19, 0x01}; -unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused)) - const bagl_element_t *e) { +unsigned int ui_712_approve_cb(const bagl_element_t *e) +{ uint8_t privateKeyData[INT256_LENGTH]; uint8_t hash[INT256_LENGTH]; uint8_t signature[100]; cx_ecfp_private_key_t privateKey; uint32_t tx = 0; + + (void)e; io_seproxyhal_io_heartbeat(); cx_keccak_init(&global_sha3, 256); cx_hash((cx_hash_t *) &global_sha3, @@ -53,10 +56,12 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused)) &info); explicit_bzero(&privateKey, sizeof(privateKey)); G_io_apdu_buffer[0] = 27; - if (info & CX_ECCINFO_PARITY_ODD) { + if (info & CX_ECCINFO_PARITY_ODD) + { G_io_apdu_buffer[0]++; } - if (info & CX_ECCINFO_xGTn) { + if (info & CX_ECCINFO_xGTn) + { G_io_apdu_buffer[0] += 2; } format_signature_out(signature); @@ -71,8 +76,9 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused)) return 0; // do not redraw the widget } -unsigned int io_seproxyhal_touch_signMessage712_v0_cancel(__attribute__((unused)) - const bagl_element_t *e) { +unsigned int ui_712_reject_cb(const bagl_element_t *e) +{ + (void)e; reset_app_context(); G_io_apdu_buffer[0] = 0x69; G_io_apdu_buffer[1] = 0x85; diff --git a/src_features/signMessageEIP712_common/common_712.h b/src_features/signMessageEIP712_common/common_712.h new file mode 100644 index 0000000..8e35b15 --- /dev/null +++ b/src_features/signMessageEIP712_common/common_712.h @@ -0,0 +1,10 @@ +#ifndef COMMON_EIP712_H_ +#define COMMON_EIP712_H_ + +#include +#include "ux.h" + +unsigned int ui_712_approve_cb(const bagl_element_t *e); +unsigned int ui_712_reject_cb(const bagl_element_t *e); + +#endif // COMMON_EIP712_H_ diff --git a/src_features/signMessageEIP712_v0/cmd_signMessage712.c b/src_features/signMessageEIP712_v0/cmd_signMessage712.c index 6abd4f7..09e3b30 100644 --- a/src_features/signMessageEIP712_v0/cmd_signMessage712.c +++ b/src_features/signMessageEIP712_v0/cmd_signMessage712.c @@ -2,15 +2,15 @@ #include "apdu_constants.h" #include "utils.h" #include "ui_flow.h" +#include "eip712.h" +#include "common_712.h" void handleSignEIP712Message_v0(uint8_t p1, uint8_t p2, const uint8_t *workBuffer, - uint16_t dataLength, + uint8_t dataLength, unsigned int *flags, unsigned int *tx) { - uint8_t i; - UNUSED(tx); if ((p1 != 00) || (p2 != 00)) { THROW(0x6B00); @@ -21,11 +21,15 @@ void handleSignEIP712Message_v0(uint8_t p1, workBuffer = parseBip32(workBuffer, &dataLength, &tmpCtx.messageSigningContext.bip32); - if ((workBuffer == NULL) || (dataLength < (32 + 32))) { + if ((workBuffer == NULL) || (dataLength < (KECCAK256_HASH_BYTESIZE * 2))) { THROW(0x6a80); } - memmove(tmpCtx.messageSigningContext712.domainHash, workBuffer, 32); - memmove(tmpCtx.messageSigningContext712.messageHash, workBuffer + 32, 32); + memmove(tmpCtx.messageSigningContext712.domainHash, + workBuffer, + KECCAK256_HASH_BYTESIZE); + memmove(tmpCtx.messageSigningContext712.messageHash, + workBuffer + KECCAK256_HASH_BYTESIZE, + KECCAK256_HASH_BYTESIZE); #ifdef NO_CONSENT io_seproxyhal_touch_signMessage_ok(NULL); diff --git a/src_features/signMessageEIP712_v0/ui_flow_signMessage712.c b/src_features/signMessageEIP712_v0/ui_flow_signMessage712.c index ac239d7..02e9e14 100644 --- a/src_features/signMessageEIP712_v0/ui_flow_signMessage712.c +++ b/src_features/signMessageEIP712_v0/ui_flow_signMessage712.c @@ -1,5 +1,6 @@ #include "shared_context.h" #include "ui_callbacks.h" +#include "common_712.h" void prepare_domain_hash_v0() { snprintf(strings.tmp.tmp, 70, "0x%.*H", 32, tmpCtx.messageSigningContext712.domainHash); @@ -37,7 +38,7 @@ UX_STEP_NOCB_INIT( UX_STEP_CB( ux_sign_712_v0_flow_4_step, pbb, - io_seproxyhal_touch_signMessage712_v0_ok(NULL), + ui_712_approve_cb(NULL), { &C_icon_validate_14, "Sign", @@ -46,7 +47,7 @@ UX_STEP_CB( UX_STEP_CB( ux_sign_712_v0_flow_5_step, pbb, - io_seproxyhal_touch_signMessage712_v0_cancel(NULL), + ui_712_reject_cb(NULL), { &C_icon_crossmark, "Cancel",