Now uses some of the old EIP712 code as common code between the two implementations + approve/reject button now works

This commit is contained in:
Alexandre Paillier
2022-05-12 17:30:04 +02:00
parent 9f29a7caa2
commit 9cd39e2c02
10 changed files with 61 additions and 55 deletions

View File

@@ -53,12 +53,14 @@
#endif #endif
#define OFFSET_CLA 0 enum {
#define OFFSET_INS 1 OFFSET_CLA = 0,
#define OFFSET_P1 2 OFFSET_INS,
#define OFFSET_P2 3 OFFSET_P1,
#define OFFSET_LC 4 OFFSET_P2,
#define OFFSET_CDATA 5 OFFSET_LC,
OFFSET_CDATA
};
void handleGetPublicKey(uint8_t p1, void handleGetPublicKey(uint8_t p1,
uint8_t p2, uint8_t p2,
@@ -99,7 +101,7 @@ void handleSignPersonalMessage(uint8_t p1,
void handleSignEIP712Message_v0(uint8_t p1, void handleSignEIP712Message_v0(uint8_t p1,
uint8_t p2, uint8_t p2,
const uint8_t *dataBuffer, const uint8_t *dataBuffer,
uint16_t dataLength, uint8_t dataLength,
unsigned int *flags, unsigned int *flags,
unsigned int *tx); unsigned int *tx);

View File

@@ -675,6 +675,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) {
} }
else else
{ {
*flags |= IO_ASYNCH_REPLY;
handle_eip712_sign(G_io_apdu_buffer); handle_eip712_sign(G_io_apdu_buffer);
} }
break; break;

View File

@@ -14,8 +14,6 @@ unsigned int io_seproxyhal_touch_signMessage_ok(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_signMessage_cancel(const bagl_element_t *e); unsigned int io_seproxyhal_touch_signMessage_cancel(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_data_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_data_ok(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_data_cancel(const bagl_element_t *e); unsigned int io_seproxyhal_touch_data_cancel(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_signMessage712_v0_ok(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_signMessage712_v0_cancel(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_eth2_address_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_eth2_address_ok(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_privacy_ok(const bagl_element_t *e); unsigned int io_seproxyhal_touch_privacy_ok(const bagl_element_t *e);
unsigned int io_seproxyhal_touch_privacy_cancel(const bagl_element_t *e); unsigned int io_seproxyhal_touch_privacy_cancel(const bagl_element_t *e);

View File

@@ -4,15 +4,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
enum {
OFFSET_CLA = 0,
OFFSET_INS,
OFFSET_P1,
OFFSET_P2,
OFFSET_LC,
OFFSET_DATA
};
typedef enum typedef enum
{ {
// contract defined struct // contract defined struct

View File

@@ -3,6 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "apdu_constants.h"
#include "eip712.h" #include "eip712.h"
#include "mem.h" #include "mem.h"
#include "type_hash.h" #include "type_hash.h"
@@ -12,6 +13,7 @@
#include "path.h" #include "path.h"
#include "shared_context.h" #include "shared_context.h"
#include "ui_logic.h" #include "ui_logic.h"
#include "common_712.h"
// lib functions // lib functions
@@ -249,7 +251,7 @@ bool set_struct_name(const uint8_t *const data)
{ {
return false; return false;
} }
memmove(name_ptr, &data[OFFSET_DATA], data[OFFSET_LC]); memmove(name_ptr, &data[OFFSET_CDATA], data[OFFSET_LC]);
// initialize number of fields // initialize number of fields
if ((current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL) if ((current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
@@ -264,7 +266,7 @@ bool set_struct_name(const uint8_t *const data)
// TODO: Handle partial sends // TODO: Handle partial sends
bool set_struct_field(const uint8_t *const data) bool set_struct_field(const uint8_t *const data)
{ {
uint8_t data_idx = OFFSET_DATA; uint8_t data_idx = OFFSET_CDATA;
uint8_t *type_desc_ptr; uint8_t *type_desc_ptr;
uint8_t *type_size_ptr; uint8_t *type_size_ptr;
uint8_t *typename_len_ptr; uint8_t *typename_len_ptr;
@@ -403,16 +405,16 @@ bool handle_eip712_struct_impl(const uint8_t *const apdu_buf)
{ {
case P2_NAME: case P2_NAME:
// set root type // set root type
ret = path_set_root((char*)&apdu_buf[OFFSET_DATA], ret = path_set_root((char*)&apdu_buf[OFFSET_CDATA],
apdu_buf[OFFSET_LC]); apdu_buf[OFFSET_LC]);
break; break;
case P2_FIELD: case P2_FIELD:
ret = field_hash(&apdu_buf[OFFSET_DATA], ret = field_hash(&apdu_buf[OFFSET_CDATA],
apdu_buf[OFFSET_LC], apdu_buf[OFFSET_LC],
apdu_buf[OFFSET_P1] != P1_COMPLETE); apdu_buf[OFFSET_P1] != P1_COMPLETE);
break; break;
case P2_ARRAY: case P2_ARRAY:
ret = path_new_array_depth(apdu_buf[OFFSET_DATA]); ret = path_new_array_depth(apdu_buf[OFFSET_CDATA]);
break; break;
default: default:
PRINTF("Unknown P2 0x%x for APDU 0x%x\n", PRINTF("Unknown P2 0x%x for APDU 0x%x\n",
@@ -432,22 +434,12 @@ bool handle_eip712_struct_impl(const uint8_t *const apdu_buf)
bool handle_eip712_sign(const uint8_t *const apdu_buf) bool handle_eip712_sign(const uint8_t *const apdu_buf)
{ {
uint8_t i; if (parseBip32(&apdu_buf[OFFSET_CDATA],
&apdu_buf[OFFSET_LC],
if (apdu_buf[OFFSET_LC] < 1) { &tmpCtx.messageSigningContext.bip32) == NULL)
PRINTF("Invalid data\n"); {
THROW(0x6a80); return false;
} }
tmpCtx.messageSigningContext712.pathLength = apdu_buf[OFFSET_DATA];
if ((tmpCtx.messageSigningContext712.pathLength < 0x01) ||
(tmpCtx.messageSigningContext712.pathLength > MAX_BIP32_PATH)) {
PRINTF("Invalid path\n");
THROW(0x6a80);
}
for (i = 0; i < tmpCtx.messageSigningContext712.pathLength; i++) {
tmpCtx.messageSigningContext712.bip32Path[i] = U4BE(apdu_buf + OFFSET_LC + 1 + (i * 4), 0);
}
ui_712_end_sign(); ui_712_end_sign();
return true; return true;
} }

View File

@@ -1,6 +1,7 @@
#include "ui_flow_712.h" #include "ui_flow_712.h"
#include "ui_logic.h" #include "ui_logic.h"
#include "shared_context.h" // strings #include "shared_context.h" // strings
#include "common_712.h"
// clang-format off // clang-format off
UX_STEP_NOCB( UX_STEP_NOCB(
@@ -30,7 +31,7 @@ UX_STEP_INIT(
UX_STEP_CB( UX_STEP_CB(
ux_712_step_approve, ux_712_step_approve,
pb, pb,
NULL,//io_seproxyhal_touch_signMessage712_ok(NULL), ui_712_approve_cb(NULL),
{ {
&C_icon_validate_14, &C_icon_validate_14,
"Approve", "Approve",
@@ -38,7 +39,7 @@ UX_STEP_CB(
UX_STEP_CB( UX_STEP_CB(
ux_712_step_reject, ux_712_step_reject,
pb, pb,
NULL,//io_seproxyhal_touch_signMessage712_cancel(NULL), ui_712_reject_cb(NULL),
{ {
&C_icon_crossmark, &C_icon_crossmark,
"Reject", "Reject",

View File

@@ -1,16 +1,19 @@
#include "os_io_seproxyhal.h"
#include "shared_context.h" #include "shared_context.h"
#include "os_io_seproxyhal.h"
#include "ui_callbacks.h" #include "ui_callbacks.h"
#include "common_712.h"
static const uint8_t EIP_712_MAGIC[] = {0x19, 0x01}; static const uint8_t EIP_712_MAGIC[] = {0x19, 0x01};
unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused)) unsigned int ui_712_approve_cb(const bagl_element_t *e)
const bagl_element_t *e) { {
uint8_t privateKeyData[INT256_LENGTH]; uint8_t privateKeyData[INT256_LENGTH];
uint8_t hash[INT256_LENGTH]; uint8_t hash[INT256_LENGTH];
uint8_t signature[100]; uint8_t signature[100];
cx_ecfp_private_key_t privateKey; cx_ecfp_private_key_t privateKey;
uint32_t tx = 0; uint32_t tx = 0;
(void)e;
io_seproxyhal_io_heartbeat(); io_seproxyhal_io_heartbeat();
cx_keccak_init(&global_sha3, 256); cx_keccak_init(&global_sha3, 256);
cx_hash((cx_hash_t *) &global_sha3, cx_hash((cx_hash_t *) &global_sha3,
@@ -53,10 +56,12 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused))
&info); &info);
explicit_bzero(&privateKey, sizeof(privateKey)); explicit_bzero(&privateKey, sizeof(privateKey));
G_io_apdu_buffer[0] = 27; G_io_apdu_buffer[0] = 27;
if (info & CX_ECCINFO_PARITY_ODD) { if (info & CX_ECCINFO_PARITY_ODD)
{
G_io_apdu_buffer[0]++; G_io_apdu_buffer[0]++;
} }
if (info & CX_ECCINFO_xGTn) { if (info & CX_ECCINFO_xGTn)
{
G_io_apdu_buffer[0] += 2; G_io_apdu_buffer[0] += 2;
} }
format_signature_out(signature); format_signature_out(signature);
@@ -71,8 +76,9 @@ unsigned int io_seproxyhal_touch_signMessage712_v0_ok(__attribute__((unused))
return 0; // do not redraw the widget return 0; // do not redraw the widget
} }
unsigned int io_seproxyhal_touch_signMessage712_v0_cancel(__attribute__((unused)) unsigned int ui_712_reject_cb(const bagl_element_t *e)
const bagl_element_t *e) { {
(void)e;
reset_app_context(); reset_app_context();
G_io_apdu_buffer[0] = 0x69; G_io_apdu_buffer[0] = 0x69;
G_io_apdu_buffer[1] = 0x85; G_io_apdu_buffer[1] = 0x85;

View File

@@ -0,0 +1,10 @@
#ifndef COMMON_EIP712_H_
#define COMMON_EIP712_H_
#include <stdint.h>
#include "ux.h"
unsigned int ui_712_approve_cb(const bagl_element_t *e);
unsigned int ui_712_reject_cb(const bagl_element_t *e);
#endif // COMMON_EIP712_H_

View File

@@ -2,15 +2,15 @@
#include "apdu_constants.h" #include "apdu_constants.h"
#include "utils.h" #include "utils.h"
#include "ui_flow.h" #include "ui_flow.h"
#include "eip712.h"
#include "common_712.h"
void handleSignEIP712Message_v0(uint8_t p1, void handleSignEIP712Message_v0(uint8_t p1,
uint8_t p2, uint8_t p2,
const uint8_t *workBuffer, const uint8_t *workBuffer,
uint16_t dataLength, uint8_t dataLength,
unsigned int *flags, unsigned int *flags,
unsigned int *tx) { unsigned int *tx) {
uint8_t i;
UNUSED(tx); UNUSED(tx);
if ((p1 != 00) || (p2 != 00)) { if ((p1 != 00) || (p2 != 00)) {
THROW(0x6B00); THROW(0x6B00);
@@ -21,11 +21,15 @@ void handleSignEIP712Message_v0(uint8_t p1,
workBuffer = parseBip32(workBuffer, &dataLength, &tmpCtx.messageSigningContext.bip32); workBuffer = parseBip32(workBuffer, &dataLength, &tmpCtx.messageSigningContext.bip32);
if ((workBuffer == NULL) || (dataLength < (32 + 32))) { if ((workBuffer == NULL) || (dataLength < (KECCAK256_HASH_BYTESIZE * 2))) {
THROW(0x6a80); THROW(0x6a80);
} }
memmove(tmpCtx.messageSigningContext712.domainHash, workBuffer, 32); memmove(tmpCtx.messageSigningContext712.domainHash,
memmove(tmpCtx.messageSigningContext712.messageHash, workBuffer + 32, 32); workBuffer,
KECCAK256_HASH_BYTESIZE);
memmove(tmpCtx.messageSigningContext712.messageHash,
workBuffer + KECCAK256_HASH_BYTESIZE,
KECCAK256_HASH_BYTESIZE);
#ifdef NO_CONSENT #ifdef NO_CONSENT
io_seproxyhal_touch_signMessage_ok(NULL); io_seproxyhal_touch_signMessage_ok(NULL);

View File

@@ -1,5 +1,6 @@
#include "shared_context.h" #include "shared_context.h"
#include "ui_callbacks.h" #include "ui_callbacks.h"
#include "common_712.h"
void prepare_domain_hash_v0() { void prepare_domain_hash_v0() {
snprintf(strings.tmp.tmp, 70, "0x%.*H", 32, tmpCtx.messageSigningContext712.domainHash); snprintf(strings.tmp.tmp, 70, "0x%.*H", 32, tmpCtx.messageSigningContext712.domainHash);
@@ -37,7 +38,7 @@ UX_STEP_NOCB_INIT(
UX_STEP_CB( UX_STEP_CB(
ux_sign_712_v0_flow_4_step, ux_sign_712_v0_flow_4_step,
pbb, pbb,
io_seproxyhal_touch_signMessage712_v0_ok(NULL), ui_712_approve_cb(NULL),
{ {
&C_icon_validate_14, &C_icon_validate_14,
"Sign", "Sign",
@@ -46,7 +47,7 @@ UX_STEP_CB(
UX_STEP_CB( UX_STEP_CB(
ux_sign_712_v0_flow_5_step, ux_sign_712_v0_flow_5_step,
pbb, pbb,
io_seproxyhal_touch_signMessage712_v0_cancel(NULL), ui_712_reject_cb(NULL),
{ {
&C_icon_crossmark, &C_icon_crossmark,
"Cancel", "Cancel",