EIP-712 code linting

This commit is contained in:
Alexandre Paillier
2022-07-19 11:49:18 +02:00
parent 0cf21cdf73
commit de9e895ad9
44 changed files with 778 additions and 1402 deletions

View File

@@ -8,10 +8,10 @@
#include "mem_utils.h"
#include "shared_context.h"
#include "ui_logic.h"
#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE
#include "context.h" // contract_addr
#include "utils.h" // u64_from_BE
#include "apdu_constants.h" // APDU response codes
#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE
#include "context.h" // contract_addr
#include "utils.h" // u64_from_BE
#include "apdu_constants.h" // APDU response codes
#include "typed_data.h"
#include "commands_712.h"
#include "hash_bytes.h"
@@ -23,12 +23,9 @@ static s_field_hashing *fh = NULL;
*
* @return whether the initialization was successful or not
*/
bool field_hash_init(void)
{
if (fh == NULL)
{
if ((fh = MEM_ALLOC_AND_ALIGN_TYPE(*fh)) == NULL)
{
bool field_hash_init(void) {
if (fh == NULL) {
if ((fh = MEM_ALLOC_AND_ALIGN_TYPE(*fh)) == NULL) {
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
return false;
}
@@ -40,8 +37,7 @@ bool field_hash_init(void)
/**
* Deinitialize the field hash context
*/
void field_hash_deinit(void)
{
void field_hash_deinit(void) {
fh = NULL;
}
@@ -55,18 +51,16 @@ void field_hash_deinit(void)
*/
static const uint8_t *field_hash_prepare(const void *const field_ptr,
const uint8_t *data,
uint8_t *data_length)
{
uint8_t *data_length) {
e_type field_type;
field_type = struct_field_type(field_ptr);
fh->remaining_size = __builtin_bswap16(*(uint16_t*)&data[0]); // network byte order
fh->remaining_size = __builtin_bswap16(*(uint16_t *) &data[0]); // network byte order
data += sizeof(uint16_t);
*data_length -= sizeof(uint16_t);
fh->state = FHS_WAITING_FOR_MORE;
if (IS_DYN(field_type))
{
cx_keccak_init(&global_sha3, 256); // init hash
if (IS_DYN(field_type)) {
cx_keccak_init(&global_sha3, 256); // init hash
ui_712_new_field(field_ptr, data, *data_length);
}
return data;
@@ -84,14 +78,12 @@ static const uint8_t *field_hash_prepare(const void *const field_ptr,
*/
static const uint8_t *field_hash_finalize_static(const void *const field_ptr,
const uint8_t *const data,
uint8_t data_length)
{
uint8_t data_length) {
uint8_t *value = NULL;
e_type field_type;
field_type = struct_field_type(field_ptr);
switch (field_type)
{
switch (field_type) {
case TYPE_SOL_INT:
value = encode_int(data, data_length, get_struct_field_typesize(field_ptr));
break;
@@ -105,7 +97,7 @@ static const uint8_t *field_hash_finalize_static(const void *const field_ptr,
value = encode_address(data, data_length);
break;
case TYPE_SOL_BOOL:
value = encode_boolean((bool*)data, data_length);
value = encode_boolean((bool *) data, data_length);
break;
case TYPE_CUSTOM:
default:
@@ -113,8 +105,7 @@ static const uint8_t *field_hash_finalize_static(const void *const field_ptr,
PRINTF("Unknown solidity type!\n");
}
if (value == NULL)
{
if (value == NULL) {
return NULL;
}
ui_712_new_field(field_ptr, data, data_length);
@@ -128,22 +119,15 @@ static const uint8_t *field_hash_finalize_static(const void *const field_ptr,
*
* @return pointer to the hash, \ref NULL if it failed
*/
static uint8_t *field_hash_finalize_dynamic(void)
{
static uint8_t *field_hash_finalize_dynamic(void) {
uint8_t *value;
if ((value = mem_alloc(KECCAK256_HASH_BYTESIZE)) == NULL)
{
if ((value = mem_alloc(KECCAK256_HASH_BYTESIZE)) == NULL) {
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
return NULL;
}
// copy hash into memory
cx_hash((cx_hash_t*)&global_sha3,
CX_LAST,
NULL,
0,
value,
KECCAK256_HASH_BYTESIZE);
cx_hash((cx_hash_t *) &global_sha3, CX_LAST, NULL, 0, value, KECCAK256_HASH_BYTESIZE);
return value;
}
@@ -153,24 +137,20 @@ static uint8_t *field_hash_finalize_dynamic(void)
* @param[in] field_type the struct field's type
* @param[in] hash the field hash
*/
static void field_hash_feed_parent(e_type field_type, const uint8_t *const hash)
{
static void field_hash_feed_parent(e_type field_type, const uint8_t *const hash) {
uint8_t len;
if (IS_DYN(field_type))
{
if (IS_DYN(field_type)) {
len = KECCAK256_HASH_BYTESIZE;
}
else
{
} else {
len = EIP_712_ENCODED_FIELD_LENGTH;
}
// last thing in mem is the hash of the previous field
// and just before it is the current hash context
cx_sha3_t *hash_ctx = (cx_sha3_t*)(hash - sizeof(cx_sha3_t));
cx_sha3_t *hash_ctx = (cx_sha3_t *) (hash - sizeof(cx_sha3_t));
// continue the progressive hash on it
hash_nbytes(hash, len, (cx_hash_t*)hash_ctx);
hash_nbytes(hash, len, (cx_hash_t *) hash_ctx);
// deallocate it
mem_dealloc(len);
}
@@ -187,29 +167,23 @@ static void field_hash_feed_parent(e_type field_type, const uint8_t *const hash)
*/
static bool field_hash_domain_special_fields(const void *const field_ptr,
const uint8_t *const data,
uint8_t data_length)
{
uint8_t data_length) {
const char *key;
uint8_t keylen;
key = get_struct_field_keyname(field_ptr, &keylen);
// copy contract address into context
if (strncmp(key, "verifyingContract", keylen) == 0)
{
if (data_length != sizeof(eip712_context->contract_addr))
{
if (strncmp(key, "verifyingContract", keylen) == 0) {
if (data_length != sizeof(eip712_context->contract_addr)) {
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
PRINTF("Unexpected verifyingContract length!\n");
return false;
}
memcpy(eip712_context->contract_addr, data, data_length);
}
else if (strncmp(key, "chainId", keylen) == 0)
{
} else if (strncmp(key, "chainId", keylen) == 0) {
uint64_t chainId = u64_from_BE(data, data_length);
if (chainId != chainConfig->chainId)
{
if (chainId != chainConfig->chainId) {
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
PRINTF("EIP712Domain chain ID mismatch, expected 0x%.*h, got 0x%.*h !\n",
sizeof(chainConfig->chainId),
@@ -232,35 +206,25 @@ static bool field_hash_domain_special_fields(const void *const field_ptr,
*/
static bool field_hash_finalize(const void *const field_ptr,
const uint8_t *const data,
uint8_t data_length)
{
uint8_t data_length) {
const uint8_t *value = NULL;
e_type field_type;
field_type = struct_field_type(field_ptr);
if (!IS_DYN(field_type))
{
if ((value = field_hash_finalize_static(field_ptr,
data,
data_length)) == NULL)
{
if (!IS_DYN(field_type)) {
if ((value = field_hash_finalize_static(field_ptr, data, data_length)) == NULL) {
return false;
}
}
else
{
if ((value = field_hash_finalize_dynamic()) == NULL)
{
} else {
if ((value = field_hash_finalize_dynamic()) == NULL) {
return false;
}
}
field_hash_feed_parent(field_type, value);
if (path_get_root_type() == ROOT_DOMAIN)
{
if (field_hash_domain_special_fields(field_ptr, data, data_length) == false)
{
if (path_get_root_type() == ROOT_DOMAIN) {
if (field_hash_domain_special_fields(field_ptr, data, data_length) == false) {
return false;
}
}
@@ -278,50 +242,40 @@ static bool field_hash_finalize(const void *const field_ptr,
* @param[in] partial whether there is more of that data coming later or not
* @return whether the data hashing was successful or not
*/
bool field_hash(const uint8_t *data,
uint8_t data_length,
bool partial)
{
bool field_hash(const uint8_t *data, uint8_t data_length, bool partial) {
const void *field_ptr;
e_type field_type;
if ((fh == NULL) || ((field_ptr = path_get_field()) == NULL))
{
if ((fh == NULL) || ((field_ptr = path_get_field()) == NULL)) {
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
return false;
}
field_type = struct_field_type(field_ptr);
if (fh->state == FHS_IDLE) // first packet for this frame
if (fh->state == FHS_IDLE) // first packet for this frame
{
data = field_hash_prepare(field_ptr, data, &data_length);
}
if (data_length > fh->remaining_size)
{
if (data_length > fh->remaining_size) {
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return false;
}
fh->remaining_size -= data_length;
// if a dynamic type -> continue progressive hash
if (IS_DYN(field_type))
{
hash_nbytes(data, data_length, (cx_hash_t*)&global_sha3);
if (IS_DYN(field_type)) {
hash_nbytes(data, data_length, (cx_hash_t *) &global_sha3);
}
if (fh->remaining_size == 0)
{
if (partial) // only makes sense if marked as complete
if (fh->remaining_size == 0) {
if (partial) // only makes sense if marked as complete
{
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return false;
}
if (field_hash_finalize(field_ptr, data, data_length) == false)
{
if (field_hash_finalize(field_ptr, data, data_length) == false) {
return false;
}
}
else
{
if (!partial || !IS_DYN(field_type)) // only makes sense if marked as partial
} else {
if (!partial || !IS_DYN(field_type)) // only makes sense if marked as partial
{
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return false;
@@ -332,4 +286,4 @@ bool field_hash(const uint8_t *data,
return true;
}
#endif // HAVE_EIP712_FULL_SUPPORT
#endif // HAVE_EIP712_FULL_SUPPORT