From 6e9ab7f31bca08989864b0b83a6f06cb71b1aa56 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 24 Jun 2024 13:19:03 +0200 Subject: [PATCH] fix json string encoding --- app/ethereum/eip712.c | 48 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/app/ethereum/eip712.c b/app/ethereum/eip712.c index 38785db..cf109b2 100644 --- a/app/ethereum/eip712.c +++ b/app/ethereum/eip712.c @@ -372,7 +372,53 @@ static app_err_t eip712_encode_field(uint8_t out[32], uint8_t* heap, size_t heap __DECL_SHA3_CTX(); - keccak_Update(sha3, (uint8_t*) &ctx->json[ctx->tokens[field_val].start], ctx->tokens[field_val].end - ctx->tokens[field_val].start); + for (int i = ctx->tokens[field_val].start; i < ctx->tokens[field_val].end; i++) { + char c = ctx->json[i]; + if (c == '\\') { + c = ctx->json[++i]; + switch(c) { + case 'b': + c = '\b'; + break; + case 'f': + c = '\f'; + break; + case 'n': + c = '\n'; + break; + case 'r': + c = '\r'; + break; + case 't': + c = '\t'; + break; + case 'u': + uint16_t codepoint; + base16_decode(&ctx->json[++i], (uint8_t*) &codepoint, 4); + i += 3; + codepoint = rev16(codepoint); + if (codepoint < 0x80) { + c = codepoint & 0x7f; + } else if (codepoint < 0x0800) { + c = 0xc0 | (codepoint >> 6); + keccak_Update(sha3, (uint8_t*) &c, 1); + c = 0x80 | (codepoint & 0x3f); + } else { + c = 0xe0 | (codepoint >> 12); + keccak_Update(sha3, (uint8_t*) &c, 1); + c = 0x80 | (codepoint >> 6); + keccak_Update(sha3, (uint8_t*) &c, 1); + c = 0x80 | (uint8_t) (codepoint & 0x3f); + } + break; + default: + break; + } + } + + keccak_Update(sha3, (uint8_t*) &c, 1); + } + keccak_Final(sha3, out); } else if (field_type->len == 5 && !strncmp(field_type->str, "bytes", 5)) { if (ctx->tokens[field_val].type != JSMN_STRING) {