From 4fcba0e6ffef776e7b5092376cfea28707fa1c17 Mon Sep 17 00:00:00 2001 From: tdejoigny-ledger Date: Tue, 11 Jul 2023 17:23:49 +0200 Subject: [PATCH] Fix the CI issues (use no throw functions) and remove "throw" from ETH plugin SDK --- src/utils.c | 7 ++-- src/utils.h | 2 +- src_common/ethUtils.c | 86 ++++++++++++++++++++++++++++++++----------- src_common/ethUtils.h | 10 ++--- tools/build_sdk.py | 2 +- 5 files changed, 76 insertions(+), 31 deletions(-) diff --git a/src/utils.c b/src/utils.c index 2c553b4a82..3926bf25e9 100644 --- a/src/utils.c +++ b/src/utils.c @@ -116,7 +116,7 @@ bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_ return true; } -void amountToString(const uint8_t *amount, +bool amountToString(const uint8_t *amount, uint8_t amount_size, uint8_t decimals, const char *ticker, @@ -125,7 +125,7 @@ void amountToString(const uint8_t *amount, char tmp_buffer[100] = {0}; if (uint256_to_decimal(amount, amount_size, tmp_buffer, sizeof(tmp_buffer)) == false) { - THROW(EXCEPTION_OVERFLOW); + return false; } uint8_t amount_len = strnlen(tmp_buffer, sizeof(tmp_buffer)); @@ -141,10 +141,11 @@ void amountToString(const uint8_t *amount, out_buffer + ticker_len, out_buffer_size - ticker_len - 1, decimals) == false) { - THROW(EXCEPTION_OVERFLOW); + return false; } out_buffer[out_buffer_size - 1] = '\0'; + return true; } bool parse_swap_config(const uint8_t *config, uint8_t config_len, char *ticker, uint8_t *decimals) { diff --git a/src/utils.h b/src/utils.h index 504c1b139e..c276b6dd21 100644 --- a/src/utils.h +++ b/src/utils.h @@ -34,7 +34,7 @@ uint64_t u64_from_BE(const uint8_t* in, uint8_t size); bool uint256_to_decimal(const uint8_t* value, size_t value_len, char* out, size_t out_len); -void amountToString(const uint8_t* amount, +bool amountToString(const uint8_t* amount, uint8_t amount_len, uint8_t decimals, const char* ticker, diff --git a/src_common/ethUtils.c b/src_common/ethUtils.c index e66e0d2e5d..27291b2569 100644 --- a/src_common/ethUtils.c +++ b/src_common/ethUtils.c @@ -117,30 +117,59 @@ bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, b return true; } -void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) { +bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) { uint8_t hashAddress[INT256_LENGTH]; - cx_keccak_init(sha3Context, 256); - cx_hash((cx_hash_t *) sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32); + + if(cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if(cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + publicKey->W + 1, + 64, + hashAddress, + 32) != CX_OK) { + return false; + } + memmove(out, hashAddress + 12, 20); + return true; } -void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, +bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, char *out, cx_sha3_t *sha3Context, uint64_t chainId) { uint8_t hashAddress[INT256_LENGTH]; - cx_keccak_init(sha3Context, 256); - cx_hash((cx_hash_t *) sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32); - getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId); + + if(cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if(cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + publicKey->W + 1, + 64, + hashAddress, + 32) != CX_OK) { + return false; + } + + if(!getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId)) { + return false; + } + + return true; } -void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) { +bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size) { // Copy the numbers in ASCII format. uint8_t i = 0; do { // Checking `i + 1` to make sure we have enough space for '\0'. if (i + 1 >= dst_size) { - THROW(0x6502); + return false; } dst[i] = src % 10 + '0'; src /= 10; @@ -160,9 +189,10 @@ void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) { i--; j++; } + return true; } -void getEthAddressStringFromBinary(uint8_t *address, +bool getEthAddressStringFromBinary(uint8_t *address, char *out, cx_sha3_t *sha3Context, uint64_t chainId) { @@ -182,7 +212,9 @@ void getEthAddressStringFromBinary(uint8_t *address, break; } if (eip1191) { - u64_to_string(chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp)); + if(!u64_to_string(chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp))) { + return false; + } offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); strlcat((char *) locals_union.tmp + offset, "0x", sizeof(locals_union.tmp) - offset); offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); @@ -192,13 +224,18 @@ void getEthAddressStringFromBinary(uint8_t *address, locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f]; locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f]; } - cx_keccak_init(sha3Context, 256); - cx_hash((cx_hash_t *) sha3Context, - CX_LAST, - locals_union.tmp, - offset + 40, - locals_union.hashChecksum, - 32); + if(cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if(cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + locals_union.tmp, + offset + 40, + locals_union.hashChecksum, + 32) != CX_OK) { + return false; + } for (i = 0; i < 40; i++) { uint8_t digit = address[i / 2]; if ((i % 2) == 0) { @@ -218,24 +255,31 @@ void getEthAddressStringFromBinary(uint8_t *address, } } out[40] = '\0'; + + return true; } /* Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB -> char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ) `sha3` context doesn't have have to be initialized prior to call.*/ -void getEthDisplayableAddress(uint8_t *in, +bool getEthDisplayableAddress(uint8_t *in, char *out, size_t out_len, cx_sha3_t *sha3, uint64_t chainId) { if (out_len < 43) { strlcpy(out, "ERROR", out_len); - return; + return false; } out[0] = '0'; out[1] = 'x'; - getEthAddressStringFromBinary(in, out + 2, sha3, chainId); + if(!getEthAddressStringFromBinary(in, out + 2, sha3, chainId)) { + strlcpy(out, "ERROR", out_len); + return false; + } + + return true; } bool adjustDecimals(const char *src, diff --git a/src_common/ethUtils.h b/src_common/ethUtils.h index 8702533517..2942243014 100644 --- a/src_common/ethUtils.h +++ b/src_common/ethUtils.h @@ -40,21 +40,21 @@ bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, b bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid); -void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context); +bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context); -void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, +bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, char *out, cx_sha3_t *sha3Context, uint64_t chainId); -void u64_to_string(uint64_t src, char *dst, uint8_t dst_size); +bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size); -void getEthAddressStringFromBinary(uint8_t *address, +bool getEthAddressStringFromBinary(uint8_t *address, char *out, cx_sha3_t *sha3Context, uint64_t chainId); -void getEthDisplayableAddress(uint8_t *in, +bool getEthDisplayableAddress(uint8_t *in, char *out, size_t out_len, cx_sha3_t *sha3, diff --git a/tools/build_sdk.py b/tools/build_sdk.py index 552bb25cf7..26ad40a78a 100755 --- a/tools/build_sdk.py +++ b/tools/build_sdk.py @@ -168,7 +168,7 @@ def merge_c_files(sources, nodes_to_extract): "typedef union": ["extraInfo_t"], "__attribute__((no_instrument_function)) inline": ["int allzeroes"], "const": ["HEXDIGITS"], - "fn": ["void getEthAddressStringFromBinary", "void getEthAddressFromKey", "void getEthDisplayableAddress", "bool adjustDecimals", "bool uint256_to_decimal", "void amountToString", "void u64_to_string", "void copy_address", "void copy_parameter", "bool U2BE_from_parameter", "bool U4BE_from_parameter"] + "fn": ["bool getEthAddressStringFromBinary", "bool getEthAddressFromKey", "bool getEthDisplayableAddress", "bool adjustDecimals", "bool uint256_to_decimal", "bool amountToString", "bool u64_to_string", "void copy_address", "void copy_parameter", "bool U2BE_from_parameter", "bool U4BE_from_parameter"] } merge_headers(headers_to_merge, nodes_to_extract)