Eip191 review (#314)

* Avoid using a global for is_ascii

* Fix unused var, missing init, and use size_t for length

* Use snprintf where possible
This commit is contained in:
yhql
2022-06-10 09:45:12 +02:00
committed by GitHub
parent fb677fc187
commit 6768ccaf78
2 changed files with 23 additions and 26 deletions

View File

@@ -79,7 +79,7 @@ all: default
############ ############
DEFINES += OS_IO_SEPROXYHAL DEFINES += OS_IO_SEPROXYHAL
DEFINES += HAVE_BAGL HAVE_SPRINTF DEFINES += HAVE_BAGL HAVE_SPRINTF HAVE_SNPRINTF_FORMAT_U
DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=4 IO_HID_EP_LENGTH=64 HAVE_USB_APDU DEFINES += HAVE_IO_USB HAVE_L4_USBLIB IO_USB_MAX_ENDPOINTS=4 IO_HID_EP_LENGTH=64 HAVE_USB_APDU
DEFINES += LEDGER_MAJOR_VERSION=$(APPVERSION_M) LEDGER_MINOR_VERSION=$(APPVERSION_N) LEDGER_PATCH_VERSION=$(APPVERSION_P) DEFINES += LEDGER_MAJOR_VERSION=$(APPVERSION_M) LEDGER_MINOR_VERSION=$(APPVERSION_N) LEDGER_PATCH_VERSION=$(APPVERSION_P)

View File

@@ -25,7 +25,7 @@ static inline bool is_char_special(char c) {
* @param[in] the length of the input data * @param[in] the length of the input data
* @return wether the data is fully ASCII or not * @return wether the data is fully ASCII or not
*/ */
static bool is_data_ascii(const uint8_t *const data, uint8_t length) { static bool is_data_ascii(const uint8_t *const data, size_t length) {
for (uint8_t idx = 0; idx < length; ++idx) { for (uint8_t idx = 0; idx < length; ++idx) {
if (!is_char_special(data[idx]) && ((data[idx] < 0x20) || (data[idx] > 0x7e))) { if (!is_char_special(data[idx]) && ((data[idx] < 0x20) || (data[idx] > 0x7e))) {
return false; return false;
@@ -47,6 +47,13 @@ static void init_value_str(bool is_ascii) {
} }
} }
/**
* @return Whether the currently stored data is initialized as ASCII or not
*/
static bool is_value_str_ascii() {
return (memcmp(strings.tmp.tmp, "0x", 2) != 0);
}
/** /**
* Update the global UI string variable by formatting & appending the new data to it * Update the global UI string variable by formatting & appending the new data to it
* *
@@ -54,7 +61,7 @@ static void init_value_str(bool is_ascii) {
* @param[in] length the data length * @param[in] length the data length
* @param[in] is_ascii wether the data is ASCII or not * @param[in] is_ascii wether the data is ASCII or not
*/ */
static void feed_value_str(const uint8_t *const data, uint8_t length, bool is_ascii) { static void feed_value_str(const uint8_t *const data, size_t length, bool is_ascii) {
uint16_t value_strlen = strlen(strings.tmp.tmp); uint16_t value_strlen = strlen(strings.tmp.tmp);
if ((value_strlen + 1) < sizeof(strings.tmp.tmp)) { if ((value_strlen + 1) < sizeof(strings.tmp.tmp)) {
@@ -92,11 +99,14 @@ static void feed_value_str(const uint8_t *const data, uint8_t length, bool is_as
sizeof(marker)); sizeof(marker));
} }
} else { } else {
snprintf(strings.tmp.tmp + value_strlen, // truncate to strings.tmp.tmp 's size
sizeof(strings.tmp.tmp) - value_strlen, length = MIN(length, (sizeof(strings.tmp.tmp) - value_strlen)/2);
"%.*H", for (size_t i = 0; i < length; i++ ) {
length, snprintf(strings.tmp.tmp + value_strlen + 2*i,
data); sizeof(strings.tmp.tmp) - value_strlen - 2*i,
"%02X",
data[i]);
}
} }
} }
} }
@@ -107,16 +117,11 @@ void handleSignPersonalMessage(uint8_t p1,
uint16_t dataLength, uint16_t dataLength,
unsigned int *flags, unsigned int *flags,
unsigned int *tx) { unsigned int *tx) {
// Point to this unused global variable for persistency
bool *is_ascii = (bool *) &strings.tmp.tmp2[0];
UNUSED(tx); UNUSED(tx);
uint8_t hashMessage[INT256_LENGTH]; uint8_t hashMessage[INT256_LENGTH];
if (p1 == P1_FIRST) { if (p1 == P1_FIRST) {
char tmp[11]; char tmp[11] = {0};
uint32_t index;
uint32_t base = 10;
uint8_t pos = 0;
uint32_t i; uint32_t i;
if (dataLength < 1) { if (dataLength < 1) {
PRINTF("Invalid data\n"); PRINTF("Invalid data\n");
@@ -159,19 +164,11 @@ void handleSignPersonalMessage(uint8_t p1,
sizeof(SIGN_MAGIC) - 1, sizeof(SIGN_MAGIC) - 1,
NULL, NULL,
0); 0);
for (index = 1; (((index * base) <= tmpCtx.messageSigningContext.remainingLength) && snprintf(tmp, sizeof(tmp), "%u", tmpCtx.messageSigningContext.remainingLength);
(((index * base) / base) == index)); cx_hash((cx_hash_t *) &global_sha3, 0, (uint8_t *) tmp, strlen(tmp), NULL, 0);
index *= base)
;
for (; index; index /= base) {
tmp[pos++] = '0' + ((tmpCtx.messageSigningContext.remainingLength / index) % base);
}
tmp[pos] = '\0';
cx_hash((cx_hash_t *) &global_sha3, 0, (uint8_t *) tmp, pos, NULL, 0);
cx_sha256_init(&tmpContent.sha2); cx_sha256_init(&tmpContent.sha2);
*is_ascii = is_data_ascii(workBuffer, dataLength); init_value_str(is_data_ascii(workBuffer, dataLength));
init_value_str(*is_ascii);
} else if (p1 != P1_MORE) { } else if (p1 != P1_MORE) {
THROW(0x6B00); THROW(0x6B00);
@@ -191,7 +188,7 @@ void handleSignPersonalMessage(uint8_t p1,
cx_hash((cx_hash_t *) &tmpContent.sha2, 0, workBuffer, dataLength, NULL, 0); cx_hash((cx_hash_t *) &tmpContent.sha2, 0, workBuffer, dataLength, NULL, 0);
tmpCtx.messageSigningContext.remainingLength -= dataLength; tmpCtx.messageSigningContext.remainingLength -= dataLength;
feed_value_str(workBuffer, dataLength, *is_ascii); feed_value_str(workBuffer, dataLength, is_value_str_ascii());
if (tmpCtx.messageSigningContext.remainingLength == 0) { if (tmpCtx.messageSigningContext.remainingLength == 0) {
cx_hash((cx_hash_t *) &global_sha3, cx_hash((cx_hash_t *) &global_sha3,