2016-05-24 21:22:30 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2016-05-24 21:22:30 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Alex Beregszaszi <alex@rtfs.hu>
|
2016-08-17 11:26:54 +00:00
|
|
|
* Copyright (C) 2016 Pavol Rusnak <stick@satoshilabs.com>
|
2016-08-19 20:25:01 +00:00
|
|
|
* Copyright (C) 2016 Jochen Hoenicke <hoenicke@gmail.com>
|
2016-05-24 21:22:30 +00:00
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ethereum.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "layout2.h"
|
|
|
|
#include "messages.h"
|
|
|
|
#include "transaction.h"
|
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "protect.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "secp256k1.h"
|
|
|
|
#include "sha3.h"
|
2017-07-11 19:30:01 +00:00
|
|
|
#include "address.h"
|
2016-05-25 14:33:23 +00:00
|
|
|
#include "util.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2017-05-16 12:00:55 +00:00
|
|
|
#include "ethereum_tokens.h"
|
2018-07-30 15:40:07 +00:00
|
|
|
#include "ethereum_networks.h"
|
2018-01-18 14:21:48 +00:00
|
|
|
#include "memzero.h"
|
2018-07-10 14:58:00 +00:00
|
|
|
#include "messages.pb.h"
|
2016-05-24 21:22:30 +00:00
|
|
|
|
2017-08-31 11:28:50 +00:00
|
|
|
/* maximum supported chain id. v must fit in an uint32_t. */
|
2018-08-20 13:51:17 +00:00
|
|
|
#define MAX_CHAIN_ID 2147483629
|
2017-08-31 11:28:50 +00:00
|
|
|
|
2016-08-19 20:25:01 +00:00
|
|
|
static bool ethereum_signing = false;
|
2016-08-17 11:26:54 +00:00
|
|
|
static uint32_t data_total, data_left;
|
2017-07-02 14:12:56 +00:00
|
|
|
static EthereumTxRequest msg_tx_request;
|
2017-08-08 10:59:39 +00:00
|
|
|
static CONFIDENTIAL uint8_t privkey[32];
|
2017-08-31 11:28:50 +00:00
|
|
|
static uint32_t chain_id;
|
2018-05-02 14:33:22 +00:00
|
|
|
static uint32_t tx_type;
|
2016-05-24 23:38:32 +00:00
|
|
|
struct SHA3_CTX keccak_ctx;
|
2016-05-24 21:22:30 +00:00
|
|
|
|
2016-08-17 11:26:54 +00:00
|
|
|
static inline void hash_data(const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
sha3_Update(&keccak_ctx, buf, size);
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
/*
|
2016-08-17 11:26:54 +00:00
|
|
|
* Push an RLP encoded length to the hash buffer.
|
2016-05-24 23:38:32 +00:00
|
|
|
*/
|
2016-08-17 11:26:54 +00:00
|
|
|
static void hash_rlp_length(uint32_t length, uint8_t firstbyte)
|
2016-05-24 23:38:32 +00:00
|
|
|
{
|
2016-08-17 11:26:54 +00:00
|
|
|
uint8_t buf[4];
|
2016-08-19 20:25:01 +00:00
|
|
|
if (length == 1 && firstbyte <= 0x7f) {
|
2016-08-25 10:59:40 +00:00
|
|
|
/* empty length header */
|
2016-05-24 23:38:32 +00:00
|
|
|
} else if (length <= 55) {
|
2016-08-16 12:06:05 +00:00
|
|
|
buf[0] = 0x80 + length;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 1);
|
2016-08-16 12:06:05 +00:00
|
|
|
} else if (length <= 0xff) {
|
|
|
|
buf[0] = 0xb7 + 1;
|
|
|
|
buf[1] = length;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 2);
|
2016-08-16 12:06:05 +00:00
|
|
|
} else if (length <= 0xffff) {
|
|
|
|
buf[0] = 0xb7 + 2;
|
|
|
|
buf[1] = length >> 8;
|
|
|
|
buf[2] = length & 0xff;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 3);
|
2016-08-16 12:06:05 +00:00
|
|
|
} else {
|
|
|
|
buf[0] = 0xb7 + 3;
|
|
|
|
buf[1] = length >> 16;
|
|
|
|
buf[2] = length >> 8;
|
|
|
|
buf[3] = length & 0xff;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 4);
|
2016-08-16 12:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-17 11:26:54 +00:00
|
|
|
* Push an RLP encoded list length to the hash buffer.
|
2016-08-16 12:06:05 +00:00
|
|
|
*/
|
2016-08-17 11:26:54 +00:00
|
|
|
static void hash_rlp_list_length(uint32_t length)
|
2016-08-16 12:06:05 +00:00
|
|
|
{
|
2016-08-17 11:26:54 +00:00
|
|
|
uint8_t buf[4];
|
2016-08-16 12:06:05 +00:00
|
|
|
if (length <= 55) {
|
|
|
|
buf[0] = 0xc0 + length;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 1);
|
2016-05-24 23:38:32 +00:00
|
|
|
} else if (length <= 0xff) {
|
2016-08-16 12:06:05 +00:00
|
|
|
buf[0] = 0xf7 + 1;
|
2016-05-24 23:38:32 +00:00
|
|
|
buf[1] = length;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 2);
|
2016-05-24 23:38:32 +00:00
|
|
|
} else if (length <= 0xffff) {
|
2016-08-16 12:06:05 +00:00
|
|
|
buf[0] = 0xf7 + 2;
|
2016-05-24 23:38:32 +00:00
|
|
|
buf[1] = length >> 8;
|
|
|
|
buf[2] = length & 0xff;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 3);
|
2016-05-24 23:38:32 +00:00
|
|
|
} else {
|
2016-08-16 12:06:05 +00:00
|
|
|
buf[0] = 0xf7 + 3;
|
2016-05-24 23:38:32 +00:00
|
|
|
buf[1] = length >> 16;
|
|
|
|
buf[2] = length >> 8;
|
|
|
|
buf[3] = length & 0xff;
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_data(buf, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Push an RLP encoded length field and data to the hash buffer.
|
|
|
|
*/
|
|
|
|
static void hash_rlp_field(const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
hash_rlp_length(size, buf[0]);
|
2016-08-25 10:59:40 +00:00
|
|
|
hash_data(buf, size);
|
2016-05-24 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 11:38:50 +00:00
|
|
|
/*
|
|
|
|
* Push an RLP encoded number to the hash buffer.
|
|
|
|
* Ethereum yellow paper says to convert to big endian and strip leading zeros.
|
|
|
|
*/
|
|
|
|
static void hash_rlp_number(uint32_t number)
|
|
|
|
{
|
|
|
|
if (!number) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t data[4];
|
|
|
|
data[0] = (number >> 24) & 0xff;
|
|
|
|
data[1] = (number >> 16) & 0xff;
|
|
|
|
data[2] = (number >> 8) & 0xff;
|
|
|
|
data[3] = (number) & 0xff;
|
|
|
|
int offset = 0;
|
|
|
|
while (!data[offset]) {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
hash_rlp_field(data + offset, 4 - offset);
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
/*
|
|
|
|
* Calculate the number of bytes needed for an RLP length header.
|
|
|
|
* NOTE: supports up to 16MB of data (how unlikely...)
|
|
|
|
* FIXME: improve
|
|
|
|
*/
|
2016-08-17 11:26:54 +00:00
|
|
|
static int rlp_calculate_length(int length, uint8_t firstbyte)
|
|
|
|
{
|
2016-05-24 23:38:32 +00:00
|
|
|
if (length == 1 && firstbyte <= 0x7f) {
|
|
|
|
return 1;
|
|
|
|
} else if (length <= 55) {
|
2016-05-25 08:28:53 +00:00
|
|
|
return 1 + length;
|
2016-05-24 23:38:32 +00:00
|
|
|
} else if (length <= 0xff) {
|
2016-05-25 08:28:53 +00:00
|
|
|
return 2 + length;
|
2016-05-24 23:38:32 +00:00
|
|
|
} else if (length <= 0xffff) {
|
2016-05-25 08:28:53 +00:00
|
|
|
return 3 + length;
|
2016-06-28 18:44:54 +00:00
|
|
|
} else {
|
2016-05-25 08:28:53 +00:00
|
|
|
return 4 + length;
|
2016-06-28 18:44:54 +00:00
|
|
|
}
|
2016-05-24 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:10:14 +00:00
|
|
|
static int rlp_calculate_number_length(uint32_t number)
|
|
|
|
{
|
|
|
|
if (number <= 0x7f) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (number <= 0xff) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (number <= 0xffff) {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (number <= 0xffffff) {
|
|
|
|
return 4;
|
|
|
|
} else {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 14:40:23 +00:00
|
|
|
static void send_request_chunk(void)
|
2016-05-24 23:38:32 +00:00
|
|
|
{
|
2016-08-22 19:58:57 +00:00
|
|
|
int progress = 1000 - (data_total > 1000000
|
|
|
|
? data_left / (data_total/800)
|
|
|
|
: data_left * 800 / data_total);
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing"), progress);
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.has_data_length = true;
|
|
|
|
msg_tx_request.data_length = data_left <= 1024 ? data_left : 1024;
|
|
|
|
msg_write(MessageType_MessageType_EthereumTxRequest, &msg_tx_request);
|
2016-05-24 23:38:32 +00:00
|
|
|
}
|
2016-05-24 21:22:30 +00:00
|
|
|
|
2016-10-06 14:58:05 +00:00
|
|
|
static int ethereum_is_canonic(uint8_t v, uint8_t signature[64])
|
|
|
|
{
|
|
|
|
(void) signature;
|
|
|
|
return (v & 2) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:41:10 +00:00
|
|
|
static void send_signature(void)
|
|
|
|
{
|
2016-10-06 14:58:05 +00:00
|
|
|
uint8_t hash[32], sig[64];
|
|
|
|
uint8_t v;
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing"), 1000);
|
2017-01-22 16:53:48 +00:00
|
|
|
|
|
|
|
/* eip-155 replay protection */
|
2018-05-02 14:33:22 +00:00
|
|
|
if (chain_id) {
|
2017-01-22 16:53:48 +00:00
|
|
|
/* hash v=chain_id, r=0, s=0 */
|
2017-08-31 11:38:50 +00:00
|
|
|
hash_rlp_number(chain_id);
|
2017-01-22 16:53:48 +00:00
|
|
|
hash_rlp_length(0, 0);
|
|
|
|
hash_rlp_length(0, 0);
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:41:10 +00:00
|
|
|
keccak_Final(&keccak_ctx, hash);
|
2016-10-06 14:58:05 +00:00
|
|
|
if (ecdsa_sign_digest(&secp256k1, privkey, hash, sig, &v, ethereum_is_canonic) != 0) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Signing failed"));
|
2016-05-25 01:41:10 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-18 14:21:48 +00:00
|
|
|
memzero(privkey, sizeof(privkey));
|
2016-05-25 01:41:10 +00:00
|
|
|
|
|
|
|
/* Send back the result */
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.has_data_length = false;
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.has_signature_v = true;
|
2018-08-14 17:45:44 +00:00
|
|
|
if (chain_id > MAX_CHAIN_ID) {
|
|
|
|
msg_tx_request.signature_v = v;
|
|
|
|
} else if (chain_id) {
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.signature_v = v + 2 * chain_id + 35;
|
2017-01-22 16:53:48 +00:00
|
|
|
} else {
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.signature_v = v + 27;
|
2017-01-22 16:53:48 +00:00
|
|
|
}
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.has_signature_r = true;
|
|
|
|
msg_tx_request.signature_r.size = 32;
|
|
|
|
memcpy(msg_tx_request.signature_r.bytes, sig, 32);
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_tx_request.has_signature_s = true;
|
|
|
|
msg_tx_request.signature_s.size = 32;
|
|
|
|
memcpy(msg_tx_request.signature_s.bytes, sig + 32, 32);
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2017-07-02 14:12:56 +00:00
|
|
|
msg_write(MessageType_MessageType_EthereumTxRequest, &msg_tx_request);
|
2016-05-25 01:41:10 +00:00
|
|
|
|
|
|
|
ethereum_signing_abort();
|
|
|
|
}
|
2016-08-23 16:44:46 +00:00
|
|
|
/* Format a 256 bit number (amount in wei) into a human readable format
|
|
|
|
* using standard ethereum units.
|
|
|
|
* The buffer must be at least 25 bytes.
|
|
|
|
*/
|
2017-05-18 14:19:10 +00:00
|
|
|
static void ethereumFormatAmount(const bignum256 *amnt, const TokenType *token, char *buf, int buflen)
|
2016-05-25 14:33:23 +00:00
|
|
|
{
|
2017-05-18 14:19:10 +00:00
|
|
|
bignum256 bn1e9;
|
|
|
|
bn_read_uint32(1000000000, &bn1e9);
|
|
|
|
const char *suffix = NULL;
|
|
|
|
int decimals = 18;
|
2017-07-05 12:22:00 +00:00
|
|
|
if (token == UnknownToken) {
|
2017-07-14 16:41:48 +00:00
|
|
|
strlcpy(buf, "Unknown token value", buflen);
|
2017-07-05 12:22:00 +00:00
|
|
|
return;
|
|
|
|
} else
|
2017-05-18 14:19:10 +00:00
|
|
|
if (token != NULL) {
|
|
|
|
suffix = token->ticker;
|
|
|
|
decimals = token->decimals;
|
|
|
|
} else
|
|
|
|
if (bn_is_less(amnt, &bn1e9)) {
|
|
|
|
suffix = " Wei";
|
|
|
|
decimals = 0;
|
2016-06-30 12:03:12 +00:00
|
|
|
} else {
|
2018-06-21 13:52:23 +00:00
|
|
|
if (tx_type == 1 || tx_type == 6) {
|
|
|
|
suffix = " WAN";
|
|
|
|
} else {
|
2018-07-30 15:40:07 +00:00
|
|
|
ASSIGN_ETHEREUM_SUFFIX(suffix, chain_id);
|
2018-06-21 13:52:23 +00:00
|
|
|
}
|
2016-06-30 12:03:12 +00:00
|
|
|
}
|
2017-07-26 15:54:04 +00:00
|
|
|
bn_format(amnt, NULL, suffix, decimals, 0, false, buf, buflen);
|
2016-08-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:00:55 +00:00
|
|
|
static void layoutEthereumConfirmTx(const uint8_t *to, uint32_t to_len, const uint8_t *value, uint32_t value_len, const TokenType *token)
|
2016-08-23 16:44:46 +00:00
|
|
|
{
|
|
|
|
bignum256 val;
|
|
|
|
uint8_t pad_val[32];
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(pad_val, sizeof(pad_val));
|
2016-08-23 16:44:46 +00:00
|
|
|
memcpy(pad_val + (32 - value_len), value, value_len);
|
|
|
|
bn_read_be(pad_val, &val);
|
|
|
|
|
2017-05-18 14:19:10 +00:00
|
|
|
char amount[32];
|
2017-05-16 12:00:55 +00:00
|
|
|
if (token == NULL) {
|
|
|
|
if (bn_is_zero(&val)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
strcpy(amount, _("message"));
|
2017-05-16 12:00:55 +00:00
|
|
|
} else {
|
2017-05-18 14:19:10 +00:00
|
|
|
ethereumFormatAmount(&val, NULL, amount, sizeof(amount));
|
2017-05-16 12:00:55 +00:00
|
|
|
}
|
2016-08-23 16:44:46 +00:00
|
|
|
} else {
|
2017-05-18 14:19:10 +00:00
|
|
|
ethereumFormatAmount(&val, token, amount, sizeof(amount));
|
2016-08-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 19:30:01 +00:00
|
|
|
char _to1[] = "to 0x__________";
|
|
|
|
char _to2[] = "_______________";
|
|
|
|
char _to3[] = "_______________?";
|
2016-05-25 14:33:23 +00:00
|
|
|
|
2016-08-22 19:58:57 +00:00
|
|
|
if (to_len) {
|
2017-07-11 19:30:01 +00:00
|
|
|
char to_str[41];
|
2018-06-28 09:27:27 +00:00
|
|
|
|
|
|
|
bool rskip60 = false;
|
|
|
|
// constants from trezor-common/defs/ethereum/networks.json
|
|
|
|
switch (chain_id) {
|
|
|
|
case 30: rskip60 = true; break;
|
|
|
|
case 31: rskip60 = true; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ethereum_address_checksum(to, to_str, rskip60, chain_id);
|
2017-07-11 19:30:01 +00:00
|
|
|
memcpy(_to1 + 5, to_str, 10);
|
|
|
|
memcpy(_to2, to_str + 10, 15);
|
|
|
|
memcpy(_to3, to_str + 25, 15);
|
2016-06-30 12:03:12 +00:00
|
|
|
} else {
|
2017-06-18 20:22:33 +00:00
|
|
|
strlcpy(_to1, _("to new contract?"), sizeof(_to1));
|
2016-06-30 12:03:12 +00:00
|
|
|
strlcpy(_to2, "", sizeof(_to2));
|
2016-08-10 17:15:29 +00:00
|
|
|
strlcpy(_to3, "", sizeof(_to3));
|
2016-06-30 12:03:12 +00:00
|
|
|
}
|
2016-05-25 14:33:23 +00:00
|
|
|
|
2016-08-18 14:32:51 +00:00
|
|
|
layoutDialogSwipe(&bmp_icon_question,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Cancel"),
|
|
|
|
_("Confirm"),
|
2016-05-25 14:33:23 +00:00
|
|
|
NULL,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Send"),
|
2016-08-23 16:44:46 +00:00
|
|
|
amount,
|
2016-05-25 14:33:23 +00:00
|
|
|
_to1,
|
2016-08-10 17:15:29 +00:00
|
|
|
_to2,
|
|
|
|
_to3,
|
|
|
|
NULL
|
2016-05-25 14:33:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-22 19:58:57 +00:00
|
|
|
static void layoutEthereumData(const uint8_t *data, uint32_t len, uint32_t total_len)
|
|
|
|
{
|
|
|
|
char hexdata[3][17];
|
|
|
|
char summary[20];
|
|
|
|
uint32_t printed = 0;
|
2017-07-23 20:20:51 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2016-08-22 19:58:57 +00:00
|
|
|
uint32_t linelen = len - printed;
|
2017-07-23 20:20:51 +00:00
|
|
|
if (linelen > 8) {
|
2016-08-22 19:58:57 +00:00
|
|
|
linelen = 8;
|
2017-07-23 20:20:51 +00:00
|
|
|
}
|
2016-08-22 19:58:57 +00:00
|
|
|
data2hex(data, linelen, hexdata[i]);
|
|
|
|
data += linelen;
|
|
|
|
printed += linelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(summary, "... bytes");
|
|
|
|
char *p = summary + 11;
|
|
|
|
uint32_t number = total_len;
|
|
|
|
while (number > 0) {
|
|
|
|
*p-- = '0' + number % 10;
|
|
|
|
number = number / 10;
|
|
|
|
}
|
|
|
|
char *summarystart = summary;
|
|
|
|
if (total_len == printed)
|
|
|
|
summarystart = summary + 4;
|
|
|
|
|
|
|
|
layoutDialogSwipe(&bmp_icon_question,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Cancel"),
|
|
|
|
_("Confirm"),
|
2016-08-22 19:58:57 +00:00
|
|
|
NULL,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Transaction data:"),
|
2016-08-22 19:58:57 +00:00
|
|
|
hexdata[0],
|
|
|
|
hexdata[1],
|
|
|
|
hexdata[2],
|
|
|
|
summarystart,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:44:46 +00:00
|
|
|
static void layoutEthereumFee(const uint8_t *value, uint32_t value_len,
|
|
|
|
const uint8_t *gas_price, uint32_t gas_price_len,
|
2017-05-16 12:00:55 +00:00
|
|
|
const uint8_t *gas_limit, uint32_t gas_limit_len,
|
|
|
|
bool is_token)
|
2016-08-23 16:44:46 +00:00
|
|
|
{
|
|
|
|
bignum256 val, gas;
|
|
|
|
uint8_t pad_val[32];
|
2017-05-18 14:19:10 +00:00
|
|
|
char tx_value[32];
|
|
|
|
char gas_value[32];
|
2016-08-23 16:44:46 +00:00
|
|
|
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(pad_val, sizeof(pad_val));
|
2016-08-23 16:44:46 +00:00
|
|
|
memcpy(pad_val + (32 - gas_price_len), gas_price, gas_price_len);
|
|
|
|
bn_read_be(pad_val, &val);
|
|
|
|
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(pad_val, sizeof(pad_val));
|
2016-08-23 16:44:46 +00:00
|
|
|
memcpy(pad_val + (32 - gas_limit_len), gas_limit, gas_limit_len);
|
|
|
|
bn_read_be(pad_val, &gas);
|
|
|
|
bn_multiply(&val, &gas, &secp256k1.prime);
|
|
|
|
|
2017-05-18 14:19:10 +00:00
|
|
|
ethereumFormatAmount(&gas, NULL, gas_value, sizeof(gas_value));
|
2016-08-23 16:44:46 +00:00
|
|
|
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(pad_val, sizeof(pad_val));
|
2016-08-23 16:44:46 +00:00
|
|
|
memcpy(pad_val + (32 - value_len), value, value_len);
|
|
|
|
bn_read_be(pad_val, &val);
|
|
|
|
|
|
|
|
if (bn_is_zero(&val)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
strcpy(tx_value, is_token ? _("token") : _("message"));
|
2016-08-23 16:44:46 +00:00
|
|
|
} else {
|
2017-05-18 14:19:10 +00:00
|
|
|
ethereumFormatAmount(&val, NULL, tx_value, sizeof(tx_value));
|
2016-08-23 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layoutDialogSwipe(&bmp_icon_question,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Cancel"),
|
|
|
|
_("Confirm"),
|
2016-08-23 16:44:46 +00:00
|
|
|
NULL,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("Really send"),
|
2016-08-23 16:44:46 +00:00
|
|
|
tx_value,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("paying up to"),
|
2016-08-23 16:44:46 +00:00
|
|
|
gas_value,
|
2017-06-18 20:22:33 +00:00
|
|
|
_("for gas?"),
|
2016-08-23 16:44:46 +00:00
|
|
|
NULL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
/*
|
|
|
|
* RLP fields:
|
|
|
|
* - nonce (0 .. 32)
|
|
|
|
* - gas_price (0 .. 32)
|
|
|
|
* - gas_limit (0 .. 32)
|
|
|
|
* - to (0, 20)
|
|
|
|
* - value (0 .. 32)
|
|
|
|
* - data (0 ..)
|
|
|
|
*/
|
|
|
|
|
2019-01-25 16:35:16 +00:00
|
|
|
static bool ethereum_signing_check(const EthereumSignTx *msg)
|
2016-08-17 15:18:13 +00:00
|
|
|
{
|
2017-01-21 17:00:01 +00:00
|
|
|
if (!msg->has_gas_price || !msg->has_gas_limit) {
|
2016-08-23 16:44:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-25 16:35:16 +00:00
|
|
|
size_t tolen = msg->has_to ? strlen(msg->to) : 0;
|
|
|
|
|
|
|
|
if (tolen != 42 && tolen != 40 && tolen != 0) {
|
2016-08-23 16:44:46 +00:00
|
|
|
/* Address has wrong length */
|
|
|
|
return false;
|
2016-08-17 15:18:13 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 16:44:46 +00:00
|
|
|
// sending transaction to address 0 (contract creation) without a data field
|
2019-01-25 16:35:16 +00:00
|
|
|
if (tolen == 0 && (!msg->has_data_length || msg->data_length == 0)) {
|
2016-08-17 15:18:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-23 16:44:46 +00:00
|
|
|
|
|
|
|
if (msg->gas_price.size + msg->gas_limit.size > 30) {
|
|
|
|
// sanity check that fee doesn't overflow
|
2016-08-17 15:18:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
void ethereum_signing_init(EthereumSignTx *msg, const HDNode *node)
|
|
|
|
{
|
2016-08-19 20:25:01 +00:00
|
|
|
ethereum_signing = true;
|
2016-05-24 23:38:32 +00:00
|
|
|
sha3_256_Init(&keccak_ctx);
|
|
|
|
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(&msg_tx_request, sizeof(EthereumTxRequest));
|
2016-08-22 19:58:57 +00:00
|
|
|
/* set fields to 0, to avoid conditions later */
|
|
|
|
if (!msg->has_value)
|
|
|
|
msg->value.size = 0;
|
|
|
|
if (!msg->has_data_initial_chunk)
|
|
|
|
msg->data_initial_chunk.size = 0;
|
2019-01-25 16:35:16 +00:00
|
|
|
bool toset;
|
|
|
|
uint8_t pubkeyhash[20];
|
|
|
|
if (msg->has_to && ethereum_parse(msg->to, pubkeyhash)) {
|
|
|
|
toset = true;
|
|
|
|
} else {
|
|
|
|
msg->to[0] = 0;
|
|
|
|
toset = false;
|
|
|
|
memzero(pubkeyhash, sizeof(pubkeyhash));
|
|
|
|
}
|
2017-01-21 17:00:01 +00:00
|
|
|
if (!msg->has_nonce)
|
|
|
|
msg->nonce.size = 0;
|
2016-08-22 19:58:57 +00:00
|
|
|
|
2017-01-22 16:53:48 +00:00
|
|
|
/* eip-155 chain id */
|
|
|
|
if (msg->has_chain_id) {
|
2018-08-14 17:45:44 +00:00
|
|
|
if (msg->chain_id < 1) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Chain Id out of bounds"));
|
2017-01-22 16:53:48 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2017-08-31 11:28:50 +00:00
|
|
|
chain_id = msg->chain_id;
|
2017-01-22 16:53:48 +00:00
|
|
|
} else {
|
|
|
|
chain_id = 0;
|
|
|
|
}
|
|
|
|
|
2018-06-21 13:52:23 +00:00
|
|
|
/* Wanchain txtype */
|
2018-05-02 14:33:22 +00:00
|
|
|
if (msg->has_tx_type) {
|
|
|
|
if (msg->tx_type == 1 || msg->tx_type == 6) {
|
2018-06-21 13:52:23 +00:00
|
|
|
tx_type = msg->tx_type;
|
2018-05-02 14:33:22 +00:00
|
|
|
} else {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Txtype out of bounds"));
|
2018-05-02 14:33:22 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tx_type = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-21 17:00:01 +00:00
|
|
|
if (msg->has_data_length && msg->data_length > 0) {
|
2016-08-10 16:37:30 +00:00
|
|
|
if (!msg->has_data_initial_chunk || msg->data_initial_chunk.size == 0) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Data length provided, but no initial chunk"));
|
2016-08-10 16:37:30 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-19 20:25:01 +00:00
|
|
|
/* Our encoding only supports transactions up to 2^24 bytes. To
|
|
|
|
* prevent exceeding the limit we use a stricter limit on data length.
|
|
|
|
*/
|
|
|
|
if (msg->data_length > 16000000) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Data length exceeds limit"));
|
2016-08-10 16:37:30 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
2016-05-25 14:33:12 +00:00
|
|
|
}
|
2016-08-17 11:26:54 +00:00
|
|
|
data_total = msg->data_length;
|
|
|
|
} else {
|
|
|
|
data_total = 0;
|
2016-05-25 14:33:12 +00:00
|
|
|
}
|
2016-08-19 20:25:01 +00:00
|
|
|
if (msg->data_initial_chunk.size > data_total) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Invalid size of initial chunk"));
|
2016-08-19 20:25:01 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2016-05-25 14:33:12 +00:00
|
|
|
|
2016-08-17 15:18:13 +00:00
|
|
|
// safety checks
|
|
|
|
if (!ethereum_signing_check(msg)) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Safety check failed"));
|
2016-08-17 15:18:13 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:00:55 +00:00
|
|
|
const TokenType *token = NULL;
|
|
|
|
|
|
|
|
// detect ERC-20 token
|
2019-01-25 16:35:16 +00:00
|
|
|
if (toset && msg->value.size == 0 && data_total == 68 && msg->data_initial_chunk.size == 68
|
2017-05-16 12:00:55 +00:00
|
|
|
&& memcmp(msg->data_initial_chunk.bytes, "\xa9\x05\x9c\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) == 0) {
|
2019-01-25 16:35:16 +00:00
|
|
|
token = tokenByChainAddress(chain_id, pubkeyhash);
|
2017-05-16 12:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token != NULL) {
|
|
|
|
layoutEthereumConfirmTx(msg->data_initial_chunk.bytes + 16, 20, msg->data_initial_chunk.bytes + 36, 32, token);
|
|
|
|
} else {
|
2019-01-25 16:35:16 +00:00
|
|
|
layoutEthereumConfirmTx(pubkeyhash, 20, msg->value.bytes, msg->value.size, NULL);
|
2017-05-16 12:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 17:06:11 +00:00
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2016-05-25 14:33:23 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:00:55 +00:00
|
|
|
if (token == NULL && data_total > 0) {
|
2016-08-22 19:58:57 +00:00
|
|
|
layoutEthereumData(msg->data_initial_chunk.bytes, msg->data_initial_chunk.size, data_total);
|
2018-08-27 17:06:11 +00:00
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2016-08-22 19:58:57 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:44:46 +00:00
|
|
|
layoutEthereumFee(msg->value.bytes, msg->value.size,
|
|
|
|
msg->gas_price.bytes, msg->gas_price.size,
|
2017-05-16 12:00:55 +00:00
|
|
|
msg->gas_limit.bytes, msg->gas_limit.size, token != NULL);
|
2018-08-27 17:06:11 +00:00
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2016-08-23 16:44:46 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
/* Stage 1: Calculate total RLP length */
|
2016-08-17 11:26:54 +00:00
|
|
|
uint32_t rlp_length = 0;
|
2016-05-24 23:38:32 +00:00
|
|
|
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing"), 0);
|
2016-05-24 23:38:32 +00:00
|
|
|
|
2016-08-22 19:58:57 +00:00
|
|
|
rlp_length += rlp_calculate_length(msg->nonce.size, msg->nonce.bytes[0]);
|
|
|
|
rlp_length += rlp_calculate_length(msg->gas_price.size, msg->gas_price.bytes[0]);
|
|
|
|
rlp_length += rlp_calculate_length(msg->gas_limit.size, msg->gas_limit.bytes[0]);
|
2019-01-25 16:35:16 +00:00
|
|
|
rlp_length += rlp_calculate_length(toset ? 20 : 0, pubkeyhash[0]);
|
2016-08-22 19:58:57 +00:00
|
|
|
rlp_length += rlp_calculate_length(msg->value.size, msg->value.bytes[0]);
|
2016-08-19 20:25:01 +00:00
|
|
|
rlp_length += rlp_calculate_length(data_total, msg->data_initial_chunk.bytes[0]);
|
2018-06-21 13:52:23 +00:00
|
|
|
if (tx_type) {
|
2018-10-19 14:10:14 +00:00
|
|
|
rlp_length += rlp_calculate_number_length(tx_type);
|
2018-06-21 13:52:23 +00:00
|
|
|
}
|
2017-01-22 16:53:48 +00:00
|
|
|
if (chain_id) {
|
2018-10-19 14:10:14 +00:00
|
|
|
rlp_length += rlp_calculate_number_length(chain_id);
|
2017-01-22 16:53:48 +00:00
|
|
|
rlp_length += rlp_calculate_length(0, 0);
|
|
|
|
rlp_length += rlp_calculate_length(0, 0);
|
|
|
|
}
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
/* Stage 2: Store header fields */
|
2016-08-17 11:26:54 +00:00
|
|
|
hash_rlp_list_length(rlp_length);
|
2016-05-24 23:38:32 +00:00
|
|
|
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing"), 100);
|
2016-05-25 01:41:10 +00:00
|
|
|
|
2018-06-21 13:52:23 +00:00
|
|
|
if (tx_type) {
|
|
|
|
hash_rlp_number(tx_type);
|
|
|
|
}
|
2016-08-22 19:58:57 +00:00
|
|
|
hash_rlp_field(msg->nonce.bytes, msg->nonce.size);
|
|
|
|
hash_rlp_field(msg->gas_price.bytes, msg->gas_price.size);
|
|
|
|
hash_rlp_field(msg->gas_limit.bytes, msg->gas_limit.size);
|
2019-01-25 16:35:16 +00:00
|
|
|
hash_rlp_field(pubkeyhash, toset ? 20 : 0);
|
2016-08-22 19:58:57 +00:00
|
|
|
hash_rlp_field(msg->value.bytes, msg->value.size);
|
2016-08-19 20:25:01 +00:00
|
|
|
hash_rlp_length(data_total, msg->data_initial_chunk.bytes[0]);
|
2016-08-25 10:59:40 +00:00
|
|
|
hash_data(msg->data_initial_chunk.bytes, msg->data_initial_chunk.size);
|
2016-08-19 20:25:01 +00:00
|
|
|
data_left = data_total - msg->data_initial_chunk.size;
|
2016-05-24 21:22:30 +00:00
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
memcpy(privkey, node->private_key, 32);
|
|
|
|
|
2016-08-19 20:25:01 +00:00
|
|
|
if (data_left > 0) {
|
2016-05-25 14:40:23 +00:00
|
|
|
send_request_chunk();
|
2016-05-25 01:41:10 +00:00
|
|
|
} else {
|
|
|
|
send_signature();
|
2016-05-24 23:38:32 +00:00
|
|
|
}
|
2016-05-24 21:22:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 12:40:29 +00:00
|
|
|
void ethereum_signing_txack(const EthereumTxAck *tx)
|
2016-05-24 21:22:30 +00:00
|
|
|
{
|
2016-08-19 20:25:01 +00:00
|
|
|
if (!ethereum_signing) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, _("Not in Ethereum signing mode"));
|
2016-05-24 21:22:30 +00:00
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:25:01 +00:00
|
|
|
if (tx->data_chunk.size > data_left) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Too much data"));
|
2016-08-19 20:25:01 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:38:32 +00:00
|
|
|
if (data_left > 0 && (!tx->has_data_chunk || tx->data_chunk.size == 0)) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Empty data chunk received"));
|
2016-05-24 23:38:32 +00:00
|
|
|
ethereum_signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_data(tx->data_chunk.bytes, tx->data_chunk.size);
|
|
|
|
|
|
|
|
data_left -= tx->data_chunk.size;
|
|
|
|
|
|
|
|
if (data_left > 0) {
|
2016-05-25 14:40:23 +00:00
|
|
|
send_request_chunk();
|
2016-05-24 23:38:32 +00:00
|
|
|
} else {
|
2016-05-25 01:41:10 +00:00
|
|
|
send_signature();
|
2016-05-24 23:38:32 +00:00
|
|
|
}
|
2016-05-24 21:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ethereum_signing_abort(void)
|
|
|
|
{
|
2016-08-19 20:25:01 +00:00
|
|
|
if (ethereum_signing) {
|
2018-01-18 14:21:48 +00:00
|
|
|
memzero(privkey, sizeof(privkey));
|
2016-05-24 21:22:30 +00:00
|
|
|
layoutHome();
|
2016-08-19 20:25:01 +00:00
|
|
|
ethereum_signing = false;
|
2016-05-24 21:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-02 14:12:56 +00:00
|
|
|
|
|
|
|
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);
|
2017-07-12 15:51:34 +00:00
|
|
|
sha3_Update(&ctx, (const uint8_t *)"\x19" "Ethereum Signed Message:\n", 26);
|
2018-05-25 12:22:54 +00:00
|
|
|
uint8_t c;
|
|
|
|
if (message_len > 1000000000) { c = '0' + message_len / 1000000000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 100000000) { c = '0' + message_len / 100000000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 10000000) { c = '0' + message_len / 10000000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 1000000) { c = '0' + message_len / 1000000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 100000) { c = '0' + message_len / 100000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 10000) { c = '0' + message_len / 10000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 1000) { c = '0' + message_len / 1000 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 100) { c = '0' + message_len / 100 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
if (message_len > 10) { c = '0' + message_len / 10 % 10; sha3_Update(&ctx, &c, 1); }
|
|
|
|
c = '0' + message_len % 10; sha3_Update(&ctx, &c, 1);
|
2017-07-02 14:12:56 +00:00
|
|
|
sha3_Update(&ctx, message, message_len);
|
|
|
|
keccak_Final(&ctx, hash);
|
|
|
|
}
|
|
|
|
|
2018-09-14 12:40:29 +00:00
|
|
|
void ethereum_message_sign(const EthereumSignMessage *msg, const HDNode *node, EthereumMessageSignature *resp)
|
2017-07-02 14:12:56 +00:00
|
|
|
{
|
2019-01-25 16:35:16 +00:00
|
|
|
uint8_t pubkeyhash[20];
|
|
|
|
if (!hdnode_get_ethereum_pubkeyhash(node, pubkeyhash)) {
|
2017-07-02 14:12:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-25 16:35:16 +00:00
|
|
|
|
2017-07-02 14:12:56 +00:00
|
|
|
resp->has_address = true;
|
2019-01-25 16:35:16 +00:00
|
|
|
resp->address[0] = '0';
|
|
|
|
resp->address[1] = 'x';
|
|
|
|
ethereum_address_checksum(pubkeyhash, resp->address + 2, false, 0);
|
|
|
|
// ethereum_address_checksum adds trailing zero
|
|
|
|
|
|
|
|
uint8_t hash[32];
|
2017-07-02 14:12:56 +00:00
|
|
|
ethereum_message_hash(msg->message.bytes, msg->message.size, hash);
|
|
|
|
|
|
|
|
uint8_t v;
|
|
|
|
if (ecdsa_sign_digest(&secp256k1, node->private_key, hash, resp->signature.bytes, &v, ethereum_is_canonic) != 0) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Signing failed"));
|
2017-07-02 14:12:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resp->has_signature = true;
|
|
|
|
resp->signature.bytes[64] = 27 + v;
|
|
|
|
resp->signature.size = 65;
|
|
|
|
msg_write(MessageType_MessageType_EthereumMessageSignature, resp);
|
|
|
|
}
|
|
|
|
|
2018-09-14 12:40:29 +00:00
|
|
|
int ethereum_message_verify(const EthereumVerifyMessage *msg)
|
2017-07-02 14:12:56 +00:00
|
|
|
{
|
2019-01-25 16:35:16 +00:00
|
|
|
if (msg->signature.size != 65) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Malformed signature"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pubkeyhash[20];
|
|
|
|
if (!ethereum_parse(msg->address, pubkeyhash)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Malformed address"));
|
2017-07-02 14:12:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t pubkey[65];
|
|
|
|
uint8_t hash[32];
|
|
|
|
|
|
|
|
ethereum_message_hash(msg->message.bytes, msg->message.size, hash);
|
|
|
|
|
|
|
|
/* v should be 27, 28 but some implementations use 0,1. We are
|
|
|
|
* compatible with both.
|
|
|
|
*/
|
|
|
|
uint8_t v = msg->signature.bytes[64];
|
2017-07-12 15:51:34 +00:00
|
|
|
if (v >= 27) {
|
2017-07-02 14:12:56 +00:00
|
|
|
v -= 27;
|
2017-07-12 15:51:34 +00:00
|
|
|
}
|
2017-07-02 14:12:56 +00:00
|
|
|
if (v >= 2 ||
|
2018-09-13 13:01:58 +00:00
|
|
|
ecdsa_recover_pub_from_sig(&secp256k1, pubkey, msg->signature.bytes, hash, v) != 0) {
|
2017-07-02 14:12:56 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SHA3_CTX ctx;
|
|
|
|
sha3_256_Init(&ctx);
|
|
|
|
sha3_Update(&ctx, pubkey + 1, 64);
|
|
|
|
keccak_Final(&ctx, hash);
|
|
|
|
|
|
|
|
/* result are the least significant 160 bits */
|
2019-01-25 16:35:16 +00:00
|
|
|
if (memcmp(pubkeyhash, hash + 12, 20) != 0) {
|
2017-07-02 14:12:56 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-25 16:35:16 +00:00
|
|
|
|
|
|
|
bool ethereum_parse(const char *address, uint8_t pubkeyhash[20])
|
|
|
|
{
|
|
|
|
memzero(pubkeyhash, 20);
|
|
|
|
size_t len = strlen(address);
|
|
|
|
if (len == 40) {
|
|
|
|
// do nothing
|
|
|
|
} else
|
|
|
|
if (len == 42) {
|
|
|
|
// check for "0x" prefix and strip it when required
|
|
|
|
if (address[0] != '0') return false;
|
|
|
|
if (address[1] != 'x' && address[1] != 'X') return false;
|
|
|
|
address += 2;
|
|
|
|
len -= 2;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (address[i] >= '0' && address[i] <= '9') {
|
|
|
|
pubkeyhash[i / 2] |= (address[i] - '0') << ((1 - (i % 2)) * 4);
|
|
|
|
} else
|
|
|
|
if (address[i] >= 'a' && address[i] <= 'f') {
|
|
|
|
pubkeyhash[i / 2] |= ((address[i] - 'a') + 10) << ((1 - (i % 2)) * 4);
|
|
|
|
} else
|
|
|
|
if (address[i] >= 'A' && address[i] <= 'F') {
|
|
|
|
pubkeyhash[i / 2] |= ((address[i] - 'A') + 10) << ((1 - (i % 2)) * 4);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|