From 785f001e3fafd8514d146b2f4d9b9cb541ba3e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Martins?= Date: Wed, 8 Jan 2025 17:40:25 +0000 Subject: [PATCH 1/3] Added the swap_handle_check_addres. Added initial checks on parameters. --- src/swap/handle_check_address.c | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/swap/handle_check_address.c diff --git a/src/swap/handle_check_address.c b/src/swap/handle_check_address.c new file mode 100644 index 0000000..8503b46 --- /dev/null +++ b/src/swap/handle_check_address.c @@ -0,0 +1,36 @@ +#ifdef HAVE_SWAP +#include "swap.h" + +#define ADDRESS_LENGTH 32 + +/* Set params.result to 0 on error, 1 otherwise */ +void swap_handle_check_address(check_address_parameters_t *params) { + PRINTF("Inside Aptos swap_handle_check_address\n"); + params->result = 0; + + // Checking that parameters are correct + if (params->address_parameters == NULL || params->address_parameters_length == 0) { + PRINTF("address_parameters is empty\n"); + return; + } + + PRINTF("address_parameters: %.*H", params->address_parameters_length, params->address_parameters); + + if (params->address_to_check == NULL) { + PRINTF("address_to_check is empty\n"); + return; + } + + PRINTF("address_to_check: %s", params->address_to_check); + if (strlen(params->address_to_check) != ADDRESS_LENGTH) { + PRINTF("address_to_check length should be %d, not", ADDRESS_LENGTH, strlen(params->address_to_check)); + return; + } + + // Check that the address to check is in the list of addresses in the device + + PRINTF("addess_to_check mathces within the addresses in the device\n"); + params->result = 1; +} + +#endif \ No newline at end of file From d1e2b8b6fb9148d01e8efd5095258e16311e3daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Martins?= Date: Tue, 14 Jan 2025 18:43:05 +0000 Subject: [PATCH 2/3] Initial function dev. --- src/handler/get_public_key.c | 35 +++++++++++++++++++++------------ src/swap/handle_check_address.c | 6 ++++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/handler/get_public_key.c b/src/handler/get_public_key.c index b0bdcf9..3118a34 100644 --- a/src/handler/get_public_key.c +++ b/src/handler/get_public_key.c @@ -34,21 +34,17 @@ #include "../ui/display.h" #include "../helper/send_response.h" -int handler_get_public_key(buffer_t *cdata, bool display) { - explicit_bzero(&G_context, sizeof(G_context)); - G_context.req_type = CONFIRM_ADDRESS; - +int get_public_key(buffer_t *cdata) { cx_ecfp_private_key_t private_key = {0}; cx_ecfp_public_key_t public_key = {0}; + explicit_bzero(&G_context, sizeof(G_context)); if (!buffer_read_u8(cdata, &G_context.bip32_path_len) || !buffer_read_bip32_path(cdata, G_context.bip32_path, (size_t) G_context.bip32_path_len)) { - G_context.req_type = REQUEST_UNDEFINED; return io_send_sw(SW_WRONG_DATA_LENGTH); } if (!validate_aptos_bip32_path(G_context.bip32_path, G_context.bip32_path_len)) { - G_context.req_type = REQUEST_UNDEFINED; return io_send_sw(SW_GET_PUB_KEY_FAIL); } @@ -60,7 +56,8 @@ int handler_get_public_key(buffer_t *cdata, bool display) { if (error != CX_OK) { explicit_bzero(&private_key, sizeof(private_key)); PRINTF("crypto_derive_private_key error code: %x.\n", error); - G_context.req_type = REQUEST_UNDEFINED; + // reset private key + explicit_bzero(&private_key, sizeof(private_key)); return io_send_sw(SW_GET_PUB_KEY_FAIL); } @@ -70,19 +67,31 @@ int handler_get_public_key(buffer_t *cdata, bool display) { if (error != CX_OK) { explicit_bzero(&private_key, sizeof(private_key)); PRINTF("crypto_init_public_key error code: %x.\n", error); - G_context.req_type = REQUEST_UNDEFINED; + // reset private key + explicit_bzero(&private_key, sizeof(private_key)); return io_send_sw(SW_GET_PUB_KEY_FAIL); } - // reset private key explicit_bzero(&private_key, sizeof(private_key)); + return 0; +} + + +int handler_get_public_key(buffer_t *cdata, bool display) { + G_context.req_type = CONFIRM_ADDRESS; + + int result = get_public_key(cdata); + + // All the work has been done, so set the context to undefined. + // NOTE: not sure if ui_display_address() should be taken into account for the G_context reset. + G_context.req_type = REQUEST_UNDEFINED; + if (result != 0) { + return io_send_sw(result); + } if (display) { - int ui_status = ui_display_address(); - G_context.req_type = REQUEST_UNDEFINED; // all the work is done, reset the context - return ui_status; + return ui_display_address(); } - G_context.req_type = REQUEST_UNDEFINED; // all the work is done, reset the context return helper_send_response_pubkey(); } diff --git a/src/swap/handle_check_address.c b/src/swap/handle_check_address.c index 8503b46..cff792e 100644 --- a/src/swap/handle_check_address.c +++ b/src/swap/handle_check_address.c @@ -1,5 +1,6 @@ #ifdef HAVE_SWAP #include "swap.h" +#include "get_public_key.h" #define ADDRESS_LENGTH 32 @@ -28,6 +29,11 @@ void swap_handle_check_address(check_address_parameters_t *params) { } // Check that the address to check is in the list of addresses in the device + if (get_public_key() != 0) { + PRINTF("get_public_key failed\n"); + return; + } + PRINTF("addess_to_check mathces within the addresses in the device\n"); params->result = 1; From 87862bfc28e4b23af8b3927ef5143e5c2ebf18da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Martins?= Date: Fri, 17 Jan 2025 17:28:43 +0000 Subject: [PATCH 3/3] Fixing some functions and configs. --- Makefile | 6 +++++- src/handler/get_public_key.h | 2 ++ src/swap/handle_check_address.c | 17 +++++++++++------ src/swap/handle_get_printable_amount.c | 14 ++++++++++++++ src/swap/handle_swap_sign_transaction.c | 20 ++++++++++++++++++++ src/swap/handle_swap_sign_transaction.h | 0 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/swap/handle_get_printable_amount.c create mode 100644 src/swap/handle_swap_sign_transaction.c create mode 100644 src/swap/handle_swap_sign_transaction.h diff --git a/Makefile b/Makefile index 9d9fdfc..8fc76b5 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,11 @@ VARIANT_PARAM = COIN VARIANT_VALUES = APTOS # Enabling DEBUG flag will enable PRINTF and disable optimizations -#DEBUG = 1 +DEBUG = 1 +TEST_PUBLIC_KEY = 1 +TESTING = 1 + +ENABLE_SWAP = 1 ######################################## # Application custom permissions # diff --git a/src/handler/get_public_key.h b/src/handler/get_public_key.h index 3cb538a..93275a7 100644 --- a/src/handler/get_public_key.h +++ b/src/handler/get_public_key.h @@ -24,3 +24,5 @@ * */ int handler_get_public_key(buffer_t *cdata, bool display); + +int get_public_key(buffer_t *cdata); \ No newline at end of file diff --git a/src/swap/handle_check_address.c b/src/swap/handle_check_address.c index cff792e..51c6d0f 100644 --- a/src/swap/handle_check_address.c +++ b/src/swap/handle_check_address.c @@ -1,6 +1,8 @@ #ifdef HAVE_SWAP +#include #include "swap.h" -#include "get_public_key.h" +#include "../handler/get_public_key.h" +#include "os.h" #define ADDRESS_LENGTH 32 @@ -15,27 +17,30 @@ void swap_handle_check_address(check_address_parameters_t *params) { return; } - PRINTF("address_parameters: %.*H", params->address_parameters_length, params->address_parameters); + PRINTF("address_parameters: %.*H\n", params->address_parameters_length, params->address_parameters); if (params->address_to_check == NULL) { PRINTF("address_to_check is empty\n"); return; } - PRINTF("address_to_check: %s", params->address_to_check); + PRINTF("address_to_check: %s\n", params->address_to_check); if (strlen(params->address_to_check) != ADDRESS_LENGTH) { - PRINTF("address_to_check length should be %d, not", ADDRESS_LENGTH, strlen(params->address_to_check)); + PRINTF("address_to_check length should be %d, not %d\n", ADDRESS_LENGTH, strlen(params->address_to_check)); return; } // Check that the address to check is in the list of addresses in the device - if (get_public_key() != 0) { + buffer_t cdata; + cdata.ptr = params->address_parameters; + cdata.size = params->address_parameters_length; + if (get_public_key(&cdata) != 0) { PRINTF("get_public_key failed\n"); return; } - PRINTF("addess_to_check mathces within the addresses in the device\n"); + PRINTF("addess_to_check matches within the addresses in the device\n"); params->result = 1; } diff --git a/src/swap/handle_get_printable_amount.c b/src/swap/handle_get_printable_amount.c new file mode 100644 index 0000000..6e1566b --- /dev/null +++ b/src/swap/handle_get_printable_amount.c @@ -0,0 +1,14 @@ +#ifdef HAVE_SWAP + +#include // memset, explicit_bzero +#include "handle_swap_sign_transaction.h" +#include "swap.h" +#include "os.h" +#include "constants.h" + +/* Set empty printable_amount on error, printable amount otherwise */ +void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params) { + PRINTF("TODO: swap_handle_get_printable_amount\n"); +} + +#endif \ No newline at end of file diff --git a/src/swap/handle_swap_sign_transaction.c b/src/swap/handle_swap_sign_transaction.c new file mode 100644 index 0000000..75d29a4 --- /dev/null +++ b/src/swap/handle_swap_sign_transaction.c @@ -0,0 +1,20 @@ +#ifdef HAVE_SWAP + +#include "handle_swap_sign_transaction.h" +#include "display.h" +#include "swap.h" +#include "string.h" +#include "os_lib.h" +#include "constants.h" +#include "os_utils.h" +#include "globals.h" + +bool swap_copy_transaction_parameters(create_transaction_parameters_t* params) { + PRINTF("TODO: swap_copy_transaction_parameters\n"); +} + +void __attribute__((noreturn)) swap_finalize_exchange_sign_transaction(bool is_success) { + PRINTF("TODO: swap_finalize_exchange_sign_transaction\n"); +} + +#endif \ No newline at end of file diff --git a/src/swap/handle_swap_sign_transaction.h b/src/swap/handle_swap_sign_transaction.h new file mode 100644 index 0000000..e69de29