From 1155bd2df0d3d2a78dd1cac47b1e78bc0c5f459a Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Mon, 15 Jan 2024 18:50:25 +0100 Subject: [PATCH] Move rlp utils to dedicated file --- src/ethUstream.c | 2 +- src/ethUtils.c | 83 ---------------------------------- src/ethUtils.h | 15 ------- src/rlp_utils.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++ src/rlp_utils.h | 37 ++++++++++++++++ 5 files changed, 151 insertions(+), 99 deletions(-) create mode 100644 src/rlp_utils.c create mode 100644 src/rlp_utils.h diff --git a/src/ethUstream.c b/src/ethUstream.c index 81e607d..90f2f99 100644 --- a/src/ethUstream.c +++ b/src/ethUstream.c @@ -19,7 +19,7 @@ #include #include "ethUstream.h" -#include "ethUtils.h" +#include "rlp_utils.h" #include "utils.h" #define MAX_INT256 32 diff --git a/src/ethUtils.c b/src/ethUtils.c index 38d21c9..02c5797 100644 --- a/src/ethUtils.c +++ b/src/ethUtils.c @@ -34,89 +34,6 @@ #include "ethUstream.h" #include "network.h" -bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) { - if (*buffer <= 0x7f) { - } else if (*buffer <= 0xb7) { - } else if (*buffer <= 0xbf) { - if (bufferLength < (1 + (*buffer - 0xb7))) { - return false; - } - if (*buffer > 0xbb) { - *valid = false; // arbitrary 32 bits length limitation - return true; - } - } else if (*buffer <= 0xf7) { - } else { - if (bufferLength < (1 + (*buffer - 0xf7))) { - return false; - } - if (*buffer > 0xfb) { - *valid = false; // arbitrary 32 bits length limitation - return true; - } - } - *valid = true; - return true; -} - -bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list) { - if (*buffer <= 0x7f) { - *offset = 0; - *fieldLength = 1; - *list = false; - } else if (*buffer <= 0xb7) { - *offset = 1; - *fieldLength = *buffer - 0x80; - *list = false; - } else if (*buffer <= 0xbf) { - *offset = 1 + (*buffer - 0xb7); - *list = false; - switch (*buffer) { - case 0xb8: - *fieldLength = *(buffer + 1); - break; - case 0xb9: - *fieldLength = (*(buffer + 1) << 8) + *(buffer + 2); - break; - case 0xba: - *fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3); - break; - case 0xbb: - *fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) + - (*(buffer + 3) << 8) + *(buffer + 4); - break; - default: - return false; // arbitrary 32 bits length limitation - } - } else if (*buffer <= 0xf7) { - *offset = 1; - *fieldLength = *buffer - 0xc0; - *list = true; - } else { - *offset = 1 + (*buffer - 0xf7); - *list = true; - switch (*buffer) { - case 0xf8: - *fieldLength = *(buffer + 1); - break; - case 0xf9: - *fieldLength = (*(buffer + 1) << 8) + *(buffer + 2); - break; - case 0xfa: - *fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3); - break; - case 0xfb: - *fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) + - (*(buffer + 3) << 8) + *(buffer + 4); - break; - default: - return false; // arbitrary 32 bits length limitation - } - } - - return true; -} - bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) { uint8_t hashAddress[INT256_LENGTH]; diff --git a/src/ethUtils.h b/src/ethUtils.h index 89a3c63..dce493a 100644 --- a/src/ethUtils.h +++ b/src/ethUtils.h @@ -25,21 +25,6 @@ #define KECCAK256_HASH_BYTESIZE 32 -/** - * @brief Decode an RLP encoded field - see - * https://github.com/ethereum/wiki/wiki/RLP - * @param [in] buffer buffer containing the RLP encoded field to decode - * @param [out] fieldLength length of the RLP encoded field - * @param [out] offset offset to the beginning of the RLP encoded field from the - * buffer - * @param [out] list true if the field encodes a list, false if it encodes a - * string - * @return true if the RLP header is consistent - */ -bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list); - -bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid); - bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context); bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, diff --git a/src/rlp_utils.c b/src/rlp_utils.c new file mode 100644 index 0000000..b8b3a8f --- /dev/null +++ b/src/rlp_utils.c @@ -0,0 +1,113 @@ +/******************************************************************************* + * Ledger Ethereum App + * (c) 2016-2019 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + +/** + * @brief Utilities for an Ethereum Hardware Wallet logic + * @file ethUtils.h + * @author Ledger Firmware Team + * @version 1.0 + * @date 8th of March 2016 + */ + +#include +#include +#include + +#include "rlp_utils.h" + +bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) { + if (*buffer <= 0x7f) { + } else if (*buffer <= 0xb7) { + } else if (*buffer <= 0xbf) { + if (bufferLength < (1 + (*buffer - 0xb7))) { + return false; + } + if (*buffer > 0xbb) { + *valid = false; // arbitrary 32 bits length limitation + return true; + } + } else if (*buffer <= 0xf7) { + } else { + if (bufferLength < (1 + (*buffer - 0xf7))) { + return false; + } + if (*buffer > 0xfb) { + *valid = false; // arbitrary 32 bits length limitation + return true; + } + } + *valid = true; + return true; +} + +bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list) { + if (*buffer <= 0x7f) { + *offset = 0; + *fieldLength = 1; + *list = false; + } else if (*buffer <= 0xb7) { + *offset = 1; + *fieldLength = *buffer - 0x80; + *list = false; + } else if (*buffer <= 0xbf) { + *offset = 1 + (*buffer - 0xb7); + *list = false; + switch (*buffer) { + case 0xb8: + *fieldLength = *(buffer + 1); + break; + case 0xb9: + *fieldLength = (*(buffer + 1) << 8) + *(buffer + 2); + break; + case 0xba: + *fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3); + break; + case 0xbb: + *fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) + + (*(buffer + 3) << 8) + *(buffer + 4); + break; + default: + return false; // arbitrary 32 bits length limitation + } + } else if (*buffer <= 0xf7) { + *offset = 1; + *fieldLength = *buffer - 0xc0; + *list = true; + } else { + *offset = 1 + (*buffer - 0xf7); + *list = true; + switch (*buffer) { + case 0xf8: + *fieldLength = *(buffer + 1); + break; + case 0xf9: + *fieldLength = (*(buffer + 1) << 8) + *(buffer + 2); + break; + case 0xfa: + *fieldLength = (*(buffer + 1) << 16) + (*(buffer + 2) << 8) + *(buffer + 3); + break; + case 0xfb: + *fieldLength = (*(buffer + 1) << 24) + (*(buffer + 2) << 16) + + (*(buffer + 3) << 8) + *(buffer + 4); + break; + default: + return false; // arbitrary 32 bits length limitation + } + } + + return true; +} diff --git a/src/rlp_utils.h b/src/rlp_utils.h new file mode 100644 index 0000000..7da1588 --- /dev/null +++ b/src/rlp_utils.h @@ -0,0 +1,37 @@ +/******************************************************************************* + * Ledger Ethereum App + * (c) 2016-2019 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + +#pragma once + +#include +#include +#include + +/** + * @brief Decode an RLP encoded field - see + * https://github.com/ethereum/wiki/wiki/RLP + * @param [in] buffer buffer containing the RLP encoded field to decode + * @param [out] fieldLength length of the RLP encoded field + * @param [out] offset offset to the beginning of the RLP encoded field from the + * buffer + * @param [out] list true if the field encodes a list, false if it encodes a + * string + * @return true if the RLP header is consistent + */ +bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, bool *list); + +bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid);