From b2fe2f26ba2d8a2800bcaa3de979aa04bc43cb4a Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 2 May 2022 15:32:45 +0200 Subject: [PATCH] Implements the hashing part of typeHash --- src_features/signMessageEIP712/type_hash.c | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src_features/signMessageEIP712/type_hash.c b/src_features/signMessageEIP712/type_hash.c index e3069ce..ee471fc 100644 --- a/src_features/signMessageEIP712/type_hash.c +++ b/src_features/signMessageEIP712/type_hash.c @@ -4,6 +4,7 @@ #include "mem.h" #include "encode_type.h" #include "type_hash.h" +#include "sha3.h" const uint8_t *type_hash(const void *const structs_array, const char *const struct_name, @@ -12,12 +13,19 @@ const uint8_t *type_hash(const void *const structs_array, const void *const mem_loc_bak = mem_alloc(0); // backup the memory location const char *typestr; uint16_t length; + sha3_context ctx; + const uint8_t *hash; + uint8_t *hash_ptr; typestr = encode_type(structs_array, struct_name, struct_name_length, &length); if (typestr == NULL) { return NULL; } + sha3_Init256(&ctx); + sha3_SetFlags(&ctx, SHA3_FLAGS_KECCAK); + sha3_Update(&ctx, typestr, length); + hash = sha3_Finalize(&ctx); #ifdef DEBUG fwrite(typestr, sizeof(char), length, stdout); @@ -27,5 +35,23 @@ const uint8_t *type_hash(const void *const structs_array, // restore the memory location mem_dealloc(mem_alloc(0) - mem_loc_bak); - return NULL; + // copy hash into memory + if ((hash_ptr = mem_alloc(KECCAK256_HASH_LENGTH)) == NULL) + { + return NULL; + } +#ifdef DEBUG + printf("-> 0x"); +#endif + for (int idx = 0; idx < KECCAK256_HASH_LENGTH; ++idx) + { + hash_ptr[idx] = hash[idx]; +#ifdef DEBUG + printf("%.02x", hash[idx]); +#endif + } +#ifdef DEBUG + printf("\n"); +#endif + return hash_ptr; }