mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 23:48:12 +00:00
add EthereumSignMessage/EthereumVerifyMessage
This commit is contained in:
parent
c5e927fac2
commit
b0ac3a2af1
@ -576,8 +576,11 @@ void ethereum_signing_abort(void)
|
||||
static void ethereum_message_hash(const uint8_t *message, size_t message_len, uint8_t hash[32])
|
||||
{
|
||||
struct SHA3_CTX ctx;
|
||||
|
||||
sha3_256_Init(&ctx);
|
||||
sha3_Update(&ctx, (const uint8_t *)"\x19" "Ethereum Signed Message:\n", 26);
|
||||
uint8_t varint[5];
|
||||
uint32_t l = ser_length(message_len, varint);
|
||||
sha3_Update(&ctx, varint, l);
|
||||
sha3_Update(&ctx, message, message_len);
|
||||
keccak_Final(&ctx, hash);
|
||||
}
|
||||
@ -607,8 +610,7 @@ void ethereum_message_sign(EthereumSignMessage *msg, const HDNode *node, Ethereu
|
||||
|
||||
int ethereum_message_verify(EthereumVerifyMessage *msg)
|
||||
{
|
||||
if (msg->signature.size != 65
|
||||
|| msg->address.size != 20) {
|
||||
if (msg->signature.size != 65 || msg->address.size != 20) {
|
||||
fsm_sendFailure(FailureType_Failure_DataError, _("Malformed data"));
|
||||
return 1;
|
||||
}
|
||||
@ -622,9 +624,9 @@ int ethereum_message_verify(EthereumVerifyMessage *msg)
|
||||
* compatible with both.
|
||||
*/
|
||||
uint8_t v = msg->signature.bytes[64];
|
||||
if (v >= 27)
|
||||
if (v >= 27) {
|
||||
v -= 27;
|
||||
|
||||
}
|
||||
if (v >= 2 ||
|
||||
ecdsa_verify_digest_recover(&secp256k1, pubkey, msg->signature.bytes, hash, v) != 0) {
|
||||
return 2;
|
||||
|
@ -735,8 +735,8 @@ void fsm_msgEthereumVerifyMessage(EthereumVerifyMessage *msg)
|
||||
return;
|
||||
}
|
||||
|
||||
char address[41];
|
||||
data2hex(msg->address.bytes, 20, address);
|
||||
char address[43] = { '0', 'x' };
|
||||
ethereum_address_checksum(msg->address.bytes, address + 2);
|
||||
layoutVerifyAddress(address);
|
||||
if (!protectButton(ButtonRequestType_ButtonRequest_Other, false)) {
|
||||
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
||||
|
@ -65,6 +65,16 @@ VerifyMessage.coin_name max_size:17
|
||||
MessageSignature.address max_size:41
|
||||
MessageSignature.signature max_size:65
|
||||
|
||||
EthereumSignMessage.address_n max_count:8
|
||||
EthereumSignMessage.message max_size:1024
|
||||
|
||||
EthereumVerifyMessage.address max_size:20
|
||||
EthereumVerifyMessage.signature max_size:65
|
||||
EthereumVerifyMessage.message max_size:1024
|
||||
|
||||
EthereumMessageSignature.address max_size:20
|
||||
EthereumMessageSignature.signature max_size:65
|
||||
|
||||
# deprecated
|
||||
EncryptMessage skip_message:true
|
||||
# EncryptMessage.pubkey max_size:33
|
||||
|
@ -314,6 +314,25 @@ const pb_field_t EthereumTxAck_fields[2] = {
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
|
||||
const pb_field_t EthereumSignMessage_fields[3] = {
|
||||
PB_FIELD2( 1, UINT32 , REPEATED, STATIC , FIRST, EthereumSignMessage, address_n, address_n, 0),
|
||||
PB_FIELD2( 2, BYTES , REQUIRED, STATIC , OTHER, EthereumSignMessage, message, address_n, 0),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
|
||||
const pb_field_t EthereumVerifyMessage_fields[4] = {
|
||||
PB_FIELD2( 1, BYTES , OPTIONAL, STATIC , FIRST, EthereumVerifyMessage, address, address, 0),
|
||||
PB_FIELD2( 2, BYTES , OPTIONAL, STATIC , OTHER, EthereumVerifyMessage, signature, address, 0),
|
||||
PB_FIELD2( 3, BYTES , OPTIONAL, STATIC , OTHER, EthereumVerifyMessage, message, signature, 0),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
|
||||
const pb_field_t EthereumMessageSignature_fields[3] = {
|
||||
PB_FIELD2( 1, BYTES , OPTIONAL, STATIC , FIRST, EthereumMessageSignature, address, address, 0),
|
||||
PB_FIELD2( 2, BYTES , OPTIONAL, STATIC , OTHER, EthereumMessageSignature, signature, address, 0),
|
||||
PB_LAST_FIELD
|
||||
};
|
||||
|
||||
const pb_field_t SignIdentity_fields[5] = {
|
||||
PB_FIELD2( 1, MESSAGE , OPTIONAL, STATIC , FIRST, SignIdentity, identity, identity, &IdentityType_fields),
|
||||
PB_FIELD2( 2, BYTES , OPTIONAL, STATIC , OTHER, SignIdentity, challenge_hidden, identity, 0),
|
||||
@ -413,7 +432,7 @@ const pb_field_t DebugLinkFlashErase_fields[2] = {
|
||||
* numbers or field sizes that are larger than what can fit in 8 or 16 bit
|
||||
* field descriptors.
|
||||
*/
|
||||
STATIC_ASSERT((pb_membersize(Features, coins[0]) < 65536 && pb_membersize(PublicKey, node) < 65536 && pb_membersize(GetAddress, multisig) < 65536 && pb_membersize(LoadDevice, node) < 65536 && pb_membersize(TxRequest, details) < 65536 && pb_membersize(TxRequest, serialized) < 65536 && pb_membersize(TxAck, tx) < 65536 && pb_membersize(SignIdentity, identity) < 65536 && pb_membersize(GetECDHSessionKey, identity) < 65536 && pb_membersize(DebugLinkState, node) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_Initialize_GetFeatures_Features_ClearSession_ApplySettings_ChangePin_Ping_Success_Failure_ButtonRequest_ButtonAck_PinMatrixRequest_PinMatrixAck_Cancel_PassphraseRequest_PassphraseAck_GetEntropy_Entropy_GetPublicKey_PublicKey_GetAddress_EthereumGetAddress_Address_EthereumAddress_WipeDevice_LoadDevice_ResetDevice_BackupDevice_EntropyRequest_EntropyAck_RecoveryDevice_WordRequest_WordAck_SignMessage_VerifyMessage_MessageSignature_CipherKeyValue_CipheredKeyValue_SignTx_TxRequest_TxAck_EthereumSignTx_EthereumTxRequest_EthereumTxAck_SignIdentity_SignedIdentity_GetECDHSessionKey_ECDHSessionKey_SetU2FCounter_DebugLinkDecision_DebugLinkGetState_DebugLinkState_DebugLinkStop_DebugLinkLog_DebugLinkMemoryRead_DebugLinkMemory_DebugLinkMemoryWrite_DebugLinkFlashErase)
|
||||
STATIC_ASSERT((pb_membersize(Features, coins[0]) < 65536 && pb_membersize(PublicKey, node) < 65536 && pb_membersize(GetAddress, multisig) < 65536 && pb_membersize(LoadDevice, node) < 65536 && pb_membersize(TxRequest, details) < 65536 && pb_membersize(TxRequest, serialized) < 65536 && pb_membersize(TxAck, tx) < 65536 && pb_membersize(SignIdentity, identity) < 65536 && pb_membersize(GetECDHSessionKey, identity) < 65536 && pb_membersize(DebugLinkState, node) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_Initialize_GetFeatures_Features_ClearSession_ApplySettings_ChangePin_Ping_Success_Failure_ButtonRequest_ButtonAck_PinMatrixRequest_PinMatrixAck_Cancel_PassphraseRequest_PassphraseAck_GetEntropy_Entropy_GetPublicKey_PublicKey_GetAddress_EthereumGetAddress_Address_EthereumAddress_WipeDevice_LoadDevice_ResetDevice_BackupDevice_EntropyRequest_EntropyAck_RecoveryDevice_WordRequest_WordAck_SignMessage_VerifyMessage_MessageSignature_CipherKeyValue_CipheredKeyValue_SignTx_TxRequest_TxAck_EthereumSignTx_EthereumTxRequest_EthereumTxAck_EthereumSignMessage_EthereumVerifyMessage_EthereumMessageSignature_SignIdentity_SignedIdentity_GetECDHSessionKey_ECDHSessionKey_SetU2FCounter_DebugLinkDecision_DebugLinkGetState_DebugLinkState_DebugLinkStop_DebugLinkLog_DebugLinkMemoryRead_DebugLinkMemory_DebugLinkMemoryWrite_DebugLinkFlashErase)
|
||||
#endif
|
||||
|
||||
#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
|
||||
|
@ -72,6 +72,9 @@ typedef enum _MessageType {
|
||||
MessageType_MessageType_GetECDHSessionKey = 61,
|
||||
MessageType_MessageType_ECDHSessionKey = 62,
|
||||
MessageType_MessageType_SetU2FCounter = 63,
|
||||
MessageType_MessageType_EthereumSignMessage = 64,
|
||||
MessageType_MessageType_EthereumVerifyMessage = 65,
|
||||
MessageType_MessageType_EthereumMessageSignature = 66,
|
||||
MessageType_MessageType_DebugLinkDecision = 100,
|
||||
MessageType_MessageType_DebugLinkGetState = 101,
|
||||
MessageType_MessageType_DebugLinkState = 102,
|
||||
@ -324,6 +327,34 @@ typedef struct _EthereumGetAddress {
|
||||
bool show_display;
|
||||
} EthereumGetAddress;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[20];
|
||||
} EthereumMessageSignature_address_t;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[65];
|
||||
} EthereumMessageSignature_signature_t;
|
||||
|
||||
typedef struct _EthereumMessageSignature {
|
||||
bool has_address;
|
||||
EthereumMessageSignature_address_t address;
|
||||
bool has_signature;
|
||||
EthereumMessageSignature_signature_t signature;
|
||||
} EthereumMessageSignature;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[1024];
|
||||
} EthereumSignMessage_message_t;
|
||||
|
||||
typedef struct _EthereumSignMessage {
|
||||
size_t address_n_count;
|
||||
uint32_t address_n[8];
|
||||
EthereumSignMessage_message_t message;
|
||||
} EthereumSignMessage;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[32];
|
||||
@ -406,6 +437,30 @@ typedef struct _EthereumTxRequest {
|
||||
EthereumTxRequest_signature_s_t signature_s;
|
||||
} EthereumTxRequest;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[20];
|
||||
} EthereumVerifyMessage_address_t;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[65];
|
||||
} EthereumVerifyMessage_signature_t;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
uint8_t bytes[1024];
|
||||
} EthereumVerifyMessage_message_t;
|
||||
|
||||
typedef struct _EthereumVerifyMessage {
|
||||
bool has_address;
|
||||
EthereumVerifyMessage_address_t address;
|
||||
bool has_signature;
|
||||
EthereumVerifyMessage_signature_t signature;
|
||||
bool has_message;
|
||||
EthereumVerifyMessage_message_t message;
|
||||
} EthereumVerifyMessage;
|
||||
|
||||
typedef struct _Failure {
|
||||
bool has_code;
|
||||
FailureType code;
|
||||
@ -779,6 +834,9 @@ extern const uint32_t SignTx_lock_time_default;
|
||||
#define EthereumSignTx_init_default {0, {0, 0, 0, 0, 0, 0, 0, 0}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, 0, false, 0}
|
||||
#define EthereumTxRequest_init_default {false, 0, false, 0, false, {0, {0}}, false, {0, {0}}}
|
||||
#define EthereumTxAck_init_default {false, {0, {0}}}
|
||||
#define EthereumSignMessage_init_default {0, {0, 0, 0, 0, 0, 0, 0, 0}, {0, {0}}}
|
||||
#define EthereumVerifyMessage_init_default {false, {0, {0}}, false, {0, {0}}, false, {0, {0}}}
|
||||
#define EthereumMessageSignature_init_default {false, {0, {0}}, false, {0, {0}}}
|
||||
#define SignIdentity_init_default {false, IdentityType_init_default, false, {0, {0}}, false, "", false, ""}
|
||||
#define SignedIdentity_init_default {false, "", false, {0, {0}}, false, {0, {0}}}
|
||||
#define GetECDHSessionKey_init_default {false, IdentityType_init_default, false, {0, {0}}, false, ""}
|
||||
@ -837,6 +895,9 @@ extern const uint32_t SignTx_lock_time_default;
|
||||
#define EthereumSignTx_init_zero {0, {0, 0, 0, 0, 0, 0, 0, 0}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, {0, {0}}, false, 0, false, 0}
|
||||
#define EthereumTxRequest_init_zero {false, 0, false, 0, false, {0, {0}}, false, {0, {0}}}
|
||||
#define EthereumTxAck_init_zero {false, {0, {0}}}
|
||||
#define EthereumSignMessage_init_zero {0, {0, 0, 0, 0, 0, 0, 0, 0}, {0, {0}}}
|
||||
#define EthereumVerifyMessage_init_zero {false, {0, {0}}, false, {0, {0}}, false, {0, {0}}}
|
||||
#define EthereumMessageSignature_init_zero {false, {0, {0}}, false, {0, {0}}}
|
||||
#define SignIdentity_init_zero {false, IdentityType_init_zero, false, {0, {0}}, false, "", false, ""}
|
||||
#define SignedIdentity_init_zero {false, "", false, {0, {0}}, false, {0, {0}}}
|
||||
#define GetECDHSessionKey_init_zero {false, IdentityType_init_zero, false, {0, {0}}, false, ""}
|
||||
@ -896,6 +957,10 @@ extern const uint32_t SignTx_lock_time_default;
|
||||
#define EthereumAddress_address_tag 1
|
||||
#define EthereumGetAddress_address_n_tag 1
|
||||
#define EthereumGetAddress_show_display_tag 2
|
||||
#define EthereumMessageSignature_address_tag 1
|
||||
#define EthereumMessageSignature_signature_tag 2
|
||||
#define EthereumSignMessage_address_n_tag 1
|
||||
#define EthereumSignMessage_message_tag 2
|
||||
#define EthereumSignTx_address_n_tag 1
|
||||
#define EthereumSignTx_nonce_tag 2
|
||||
#define EthereumSignTx_gas_price_tag 3
|
||||
@ -910,6 +975,9 @@ extern const uint32_t SignTx_lock_time_default;
|
||||
#define EthereumTxRequest_signature_v_tag 2
|
||||
#define EthereumTxRequest_signature_r_tag 3
|
||||
#define EthereumTxRequest_signature_s_tag 4
|
||||
#define EthereumVerifyMessage_address_tag 1
|
||||
#define EthereumVerifyMessage_signature_tag 2
|
||||
#define EthereumVerifyMessage_message_tag 3
|
||||
#define Failure_code_tag 1
|
||||
#define Failure_message_tag 2
|
||||
#define Features_vendor_tag 1
|
||||
@ -1053,6 +1121,9 @@ extern const pb_field_t TxAck_fields[2];
|
||||
extern const pb_field_t EthereumSignTx_fields[10];
|
||||
extern const pb_field_t EthereumTxRequest_fields[5];
|
||||
extern const pb_field_t EthereumTxAck_fields[2];
|
||||
extern const pb_field_t EthereumSignMessage_fields[3];
|
||||
extern const pb_field_t EthereumVerifyMessage_fields[4];
|
||||
extern const pb_field_t EthereumMessageSignature_fields[3];
|
||||
extern const pb_field_t SignIdentity_fields[5];
|
||||
extern const pb_field_t SignedIdentity_fields[4];
|
||||
extern const pb_field_t GetECDHSessionKey_fields[4];
|
||||
@ -1113,6 +1184,9 @@ extern const pb_field_t DebugLinkFlashErase_fields[2];
|
||||
#define EthereumSignTx_size 1245
|
||||
#define EthereumTxRequest_size 80
|
||||
#define EthereumTxAck_size 1027
|
||||
#define EthereumSignMessage_size 1075
|
||||
#define EthereumVerifyMessage_size 1116
|
||||
#define EthereumMessageSignature_size 89
|
||||
#define SignIdentity_size (558 + IdentityType_size)
|
||||
#define SignedIdentity_size 145
|
||||
#define GetECDHSessionKey_size (107 + IdentityType_size)
|
||||
|
@ -40,6 +40,8 @@
|
||||
{ 'n', 'i', MessageType_MessageType_EthereumTxAck, EthereumTxAck_fields, (void (*)(void *)) fsm_msgEthereumTxAck },
|
||||
{ 'n', 'i', MessageType_MessageType_GetECDHSessionKey, GetECDHSessionKey_fields, (void (*)(void *)) fsm_msgGetECDHSessionKey },
|
||||
{ 'n', 'i', MessageType_MessageType_SetU2FCounter, SetU2FCounter_fields, (void (*)(void *)) fsm_msgSetU2FCounter },
|
||||
{ 'n', 'i', MessageType_MessageType_EthereumSignMessage, EthereumSignMessage_fields, (void (*)(void *)) fsm_msgEthereumSignMessage },
|
||||
{ 'n', 'i', MessageType_MessageType_EthereumVerifyMessage, EthereumVerifyMessage_fields, (void (*)(void *)) fsm_msgEthereumVerifyMessage },
|
||||
|
||||
// out messages
|
||||
|
||||
@ -65,6 +67,7 @@
|
||||
{ 'n', 'o', MessageType_MessageType_EthereumAddress, EthereumAddress_fields, 0 },
|
||||
{ 'n', 'o', MessageType_MessageType_EthereumTxRequest, EthereumTxRequest_fields, 0 },
|
||||
{ 'n', 'o', MessageType_MessageType_ECDHSessionKey, ECDHSessionKey_fields, 0 },
|
||||
{ 'n', 'o', MessageType_MessageType_EthereumMessageSignature, EthereumMessageSignature_fields, 0 },
|
||||
|
||||
#if DEBUG_LINK
|
||||
|
||||
|
2
vendor/trezor-common
vendored
2
vendor/trezor-common
vendored
@ -1 +1 @@
|
||||
Subproject commit c2a40f4b675ed3bf137360a6fa22ff72775f38ff
|
||||
Subproject commit b29b98d69ba43571dcbe54dc927aa3ecd2b95113
|
Loading…
Reference in New Issue
Block a user