Skip to content

Commit

Permalink
replace array_hexstr and '%*H' format by sdk function format_hex
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed Apr 15, 2024
1 parent 2df3eb5 commit da31e63
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
########################################
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/uint_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/uint_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#define _UINT_COMMON_H_

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "format.h"

#define UPPER_P(x) x->elements[0]
#define LOWER_P(x) x->elements[1]
Expand All @@ -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_
6 changes: 5 additions & 1 deletion src_bagl/ui_flow_getEth2PublicKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions src_bagl/ui_flow_signMessage712_v0.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions src_features/signTx/logic_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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++] = ':';
Expand Down

0 comments on commit da31e63

Please sign in to comment.