Move rlp utils to dedicated file

This commit is contained in:
Francois Beutin
2024-01-15 18:50:25 +01:00
parent 04d0fde47a
commit 1155bd2df0
5 changed files with 151 additions and 99 deletions

View File

@@ -19,7 +19,7 @@
#include <string.h>
#include "ethUstream.h"
#include "ethUtils.h"
#include "rlp_utils.h"
#include "utils.h"
#define MAX_INT256 32

View File

@@ -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];

View File

@@ -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,

113
src/rlp_utils.c Normal file
View File

@@ -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 <hello@ledger.fr>
* @version 1.0
* @date 8th of March 2016
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#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;
}

37
src/rlp_utils.h Normal file
View File

@@ -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 <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/**
* @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);