diff --git a/Makefile b/Makefile index 8f678ff023..6925267e7a 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,6 @@ endif include $(BOLOS_SDK)/Makefile.defines -# Allows to use sprintf(..., "0x%.*H", ...) -CFLAGS += -Wno-format-invalid-specifier -Wno-format-extra-args ######################################## # Mandatory configuration # ######################################## @@ -54,6 +52,7 @@ APP_SOURCE_FILES += ./ethereum-plugin-sdk/src/common_utils.c APP_SOURCE_FILES += ./ethereum-plugin-sdk/src/plugin_utils.c INCLUDES_PATH += ./ethereum-plugin-sdk/src APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c +APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/format.c INCLUDES_PATH += ${BOLOS_SDK}/lib_standard_app ifeq ($(TARGET_NAME),TARGET_STAX) diff --git a/src/uint_common.c b/src/uint_common.c index 5fe06a7c4f..331609f36f 100644 --- a/src/uint_common.c +++ b/src/uint_common.c @@ -58,3 +58,16 @@ void reverseString(char *const str, uint32_t length) { str[j] = c; } } + +int bytes_to_string(char *out, size_t outl, const void *value, size_t len) { + if (strlcpy(out, "0x", outl) != 2) { + goto err; + } + if (format_hex(value, len, out + 2, outl - 2)) { + goto err; + } + return 0; +err: + *out = '\0'; + return -1; +} diff --git a/src/uint_common.h b/src/uint_common.h index c12d9e89f5..59c726ad82 100644 --- a/src/uint_common.h +++ b/src/uint_common.h @@ -21,6 +21,9 @@ #define _UINT_COMMON_H_ #include +#include +#include +#include "format.h" #define UPPER_P(x) x->elements[0] #define LOWER_P(x) x->elements[1] @@ -32,4 +35,6 @@ void read_u64_be(const uint8_t *const in, uint64_t *const out); uint64_t readUint64BE(const uint8_t *const buffer); void reverseString(char *const str, uint32_t length); +int bytes_to_string(char *out, size_t outl, const void *value, size_t len); + #endif //_UINT_COMMON_H_ diff --git a/src_bagl/ui_flow_getEth2PublicKey.c b/src_bagl/ui_flow_getEth2PublicKey.c index af2142f94a..2eedfa00f7 100644 --- a/src_bagl/ui_flow_getEth2PublicKey.c +++ b/src_bagl/ui_flow_getEth2PublicKey.c @@ -2,9 +2,13 @@ #include "shared_context.h" #include "ui_callbacks.h" +#include "uint_common.h" void prepare_eth2_public_key() { - snprintf(strings.tmp.tmp, 100, "0x%.*H", 48, tmpCtx.publicKeyContext.publicKey.W); + bytes_to_string(strings.tmp.tmp, + sizeof(strings.tmp.tmp), + tmpCtx.publicKeyContext.publicKey.W, + 48); } // clang-format off diff --git a/src_bagl/ui_flow_signMessage712_v0.c b/src_bagl/ui_flow_signMessage712_v0.c index a06a128145..6eb88b6579 100644 --- a/src_bagl/ui_flow_signMessage712_v0.c +++ b/src_bagl/ui_flow_signMessage712_v0.c @@ -1,21 +1,20 @@ #include "shared_context.h" #include "ui_callbacks.h" #include "common_712.h" +#include "uint_common.h" void prepare_domain_hash_v0() { - snprintf(strings.tmp.tmp, - sizeof(strings.tmp.tmp), - "0x%.*H", - KECCAK256_HASH_BYTESIZE, - tmpCtx.messageSigningContext712.domainHash); + bytes_to_string(strings.tmp.tmp, + sizeof(strings.tmp.tmp), + tmpCtx.messageSigningContext712.domainHash, + KECCAK256_HASH_BYTESIZE); } void prepare_message_hash_v0() { - snprintf(strings.tmp.tmp, - sizeof(strings.tmp.tmp), - "0x%.*H", - KECCAK256_HASH_BYTESIZE, - tmpCtx.messageSigningContext712.messageHash); + bytes_to_string(strings.tmp.tmp, + sizeof(strings.tmp.tmp), + tmpCtx.messageSigningContext712.messageHash, + KECCAK256_HASH_BYTESIZE); } // clang-format off diff --git a/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c b/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c index c0120f0d0e..36f02d88d1 100644 --- a/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c +++ b/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c @@ -3,6 +3,7 @@ #include "feature_performPrivacyOperation.h" #include "common_ui.h" +#include "uint_common.h" #define P2_PUBLIC_ENCRYPTION_KEY 0x00 #define P2_SHARED_SECRET 0x01 @@ -106,11 +107,12 @@ void handlePerformPrivacyOperation(uint8_t p1, for (uint8_t i = 0; i < 32; i++) { privateKeyData[i] = tmpCtx.publicKeyContext.publicKey.W[32 - i]; } - snprintf(strings.common.fullAmount, - sizeof(strings.common.fullAmount) - 1, - "%.*H", - 32, - privateKeyData); + bytes_to_string(strings.common.fullAmount, + sizeof(strings.common.fullAmount) - 1, + privateKeyData, + 32); + + if (p2 == P2_PUBLIC_ENCRYPTION_KEY) { ui_display_privacy_public_key(); } else { diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index 1ada77086d..e2fe6d7d45 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -9,10 +9,13 @@ #include "ui_callbacks.h" #include "apdu_constants.h" #include "crypto_helpers.h" +#include "format.h" #define ERR_SILENT_MODE_CHECK_FAILED 0x6001 -uint32_t splitBinaryParameterPart(char *result, uint8_t *parameter) { +static uint32_t splitBinaryParameterPart(char *result, + size_t result_size, + uint8_t *parameter) { uint32_t i; for (i = 0; i < 8; i++) { if (parameter[i] != 0x00) { @@ -25,7 +28,7 @@ uint32_t splitBinaryParameterPart(char *result, uint8_t *parameter) { result[2] = '\0'; return 2; } else { - array_hexstr(result, parameter + i, 8 - i); + format_hex(parameter + i, 8 - i, result, result_size); return ((8 - i) * 2); } } @@ -144,7 +147,10 @@ customStatus_e customProcessor(txContext_t *context) { } dataContext.tokenContext.fieldOffset = 0; if (fieldPos == 0) { - array_hexstr(strings.tmp.tmp, dataContext.tokenContext.data, 4); + format_hex(dataContext.tokenContext.data, + 4, + strings.tmp.tmp, + sizeof(strings.tmp.tmp)); ui_confirm_selector(); } else { uint32_t offset = 0; @@ -155,6 +161,7 @@ customStatus_e customProcessor(txContext_t *context) { dataContext.tokenContext.fieldIndex); for (i = 0; i < 4; i++) { offset += splitBinaryParameterPart(strings.tmp.tmp + offset, + sizeof(strings.tmp.tmp) - offset, dataContext.tokenContext.data + 8 * i); if (i != 3) { strings.tmp.tmp[offset++] = ':';