2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 11:11:53 +00:00
|
|
|
#include <libopencm3/stm32/flash.h>
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "trezor.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "messages.h"
|
|
|
|
#include "bip32.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "coins.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "transaction.h"
|
|
|
|
#include "rng.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "oled.h"
|
|
|
|
#include "protect.h"
|
|
|
|
#include "pinmatrix.h"
|
|
|
|
#include "layout2.h"
|
2017-01-02 20:22:59 +00:00
|
|
|
#include "address.h"
|
|
|
|
#include "base58.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "reset.h"
|
|
|
|
#include "recovery.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "signing.h"
|
2018-01-13 14:20:10 +00:00
|
|
|
#include "aes/aes.h"
|
2014-06-07 12:21:59 +00:00
|
|
|
#include "hmac.h"
|
2014-11-15 01:01:21 +00:00
|
|
|
#include "crypto.h"
|
|
|
|
#include "base58.h"
|
2014-12-08 18:58:13 +00:00
|
|
|
#include "bip39.h"
|
2014-12-10 17:04:51 +00:00
|
|
|
#include "ripemd160.h"
|
2016-04-22 15:49:00 +00:00
|
|
|
#include "curves.h"
|
2015-06-03 19:21:16 +00:00
|
|
|
#include "secp256k1.h"
|
2016-05-24 21:22:30 +00:00
|
|
|
#include "ethereum.h"
|
2017-05-29 16:07:55 +00:00
|
|
|
#include "nem.h"
|
2017-05-30 16:41:02 +00:00
|
|
|
#include "nem2.h"
|
2017-10-03 11:11:53 +00:00
|
|
|
#include "rfc6979.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2018-03-16 20:40:39 +00:00
|
|
|
#include "supervise.h"
|
2018-04-06 21:45:32 +00:00
|
|
|
#include "stellar.h"
|
2018-08-27 14:41:56 +00:00
|
|
|
#include "messages.pb.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
// message methods
|
|
|
|
|
2016-08-29 21:22:49 +00:00
|
|
|
static uint8_t msg_resp[MSG_OUT_SIZE] __attribute__ ((aligned));
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2016-11-22 19:57:45 +00:00
|
|
|
#define RESP_INIT(TYPE) \
|
|
|
|
TYPE *resp = (TYPE *) (void *) msg_resp; \
|
2015-08-24 10:47:27 +00:00
|
|
|
_Static_assert(sizeof(msg_resp) >= sizeof(TYPE), #TYPE " is too large"); \
|
|
|
|
memset(resp, 0, sizeof(TYPE));
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2016-11-22 19:57:45 +00:00
|
|
|
#define CHECK_INITIALIZED \
|
|
|
|
if (!storage_isInitialized()) { \
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_NotInitialized, NULL); \
|
2016-11-22 19:57:45 +00:00
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_NOT_INITIALIZED \
|
|
|
|
if (storage_isInitialized()) { \
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_UnexpectedMessage, _("Device is already initialized. Use Wipe first.")); \
|
2016-11-22 19:57:45 +00:00
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2016-11-22 20:06:39 +00:00
|
|
|
#define CHECK_PIN \
|
|
|
|
if (!protectPin(true)) { \
|
|
|
|
layoutHome(); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_PIN_UNCACHED \
|
|
|
|
if (!protectPin(false)) { \
|
|
|
|
layoutHome(); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2016-11-22 20:39:33 +00:00
|
|
|
#define CHECK_PARAM(cond, errormsg) \
|
|
|
|
if (!(cond)) { \
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_DataError, (errormsg)); \
|
2016-11-22 20:39:33 +00:00
|
|
|
layoutHome(); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
void fsm_sendSuccess(const char *text)
|
|
|
|
{
|
|
|
|
RESP_INIT(Success);
|
|
|
|
if (text) {
|
|
|
|
resp->has_message = true;
|
|
|
|
strlcpy(resp->message, text, sizeof(resp->message));
|
|
|
|
}
|
|
|
|
msg_write(MessageType_MessageType_Success, resp);
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:15:31 +00:00
|
|
|
#if DEBUG_LINK
|
2018-07-10 14:58:00 +00:00
|
|
|
void fsm_sendFailureDebug(Failure_FailureType code, const char *text, const char *source)
|
2017-12-11 18:15:31 +00:00
|
|
|
#else
|
2018-07-10 14:58:00 +00:00
|
|
|
void fsm_sendFailure(Failure_FailureType code, const char *text)
|
2017-12-11 18:15:31 +00:00
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2018-06-05 20:38:39 +00:00
|
|
|
if (protectAbortedByCancel) {
|
|
|
|
protectAbortedByCancel = false;
|
|
|
|
}
|
2014-05-20 13:36:35 +00:00
|
|
|
if (protectAbortedByInitialize) {
|
|
|
|
fsm_msgInitialize((Initialize *)0);
|
|
|
|
protectAbortedByInitialize = false;
|
|
|
|
return;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
RESP_INIT(Failure);
|
|
|
|
resp->has_code = true;
|
|
|
|
resp->code = code;
|
2017-06-18 20:22:33 +00:00
|
|
|
if (!text) {
|
|
|
|
switch (code) {
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_UnexpectedMessage:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Unexpected message");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_ButtonExpected:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Button expected");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_DataError:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Data error");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_ActionCancelled:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Action cancelled by user");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_PinExpected:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("PIN expected");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_PinCancelled:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("PIN cancelled");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_PinInvalid:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("PIN invalid");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_InvalidSignature:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Invalid signature");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_ProcessError:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Process error");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_NotEnoughFunds:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Not enough funds");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_NotInitialized:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Device not initialized");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_PinMismatch:
|
2018-02-20 16:48:19 +00:00
|
|
|
text = _("PIN mismatch");
|
|
|
|
break;
|
2018-07-10 14:58:00 +00:00
|
|
|
case Failure_FailureType_Failure_FirmwareError:
|
2017-06-18 20:22:33 +00:00
|
|
|
text = _("Firmware error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 18:15:31 +00:00
|
|
|
#if DEBUG_LINK
|
|
|
|
resp->has_message = true;
|
|
|
|
strlcpy(resp->message, source, sizeof(resp->message));
|
|
|
|
if (text) {
|
|
|
|
strlcat(resp->message, text, sizeof(resp->message));
|
|
|
|
}
|
|
|
|
#else
|
2014-04-29 12:26:51 +00:00
|
|
|
if (text) {
|
|
|
|
resp->has_message = true;
|
|
|
|
strlcpy(resp->message, text, sizeof(resp->message));
|
|
|
|
}
|
2017-12-11 18:15:31 +00:00
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_Failure, resp);
|
|
|
|
}
|
|
|
|
|
2017-11-14 16:53:17 +00:00
|
|
|
static const CoinInfo *fsm_getCoin(bool has_name, const char *name)
|
2015-01-26 11:51:56 +00:00
|
|
|
{
|
2017-10-30 21:13:09 +00:00
|
|
|
const CoinInfo *coin;
|
2017-04-21 10:55:14 +00:00
|
|
|
if (has_name) {
|
|
|
|
coin = coinByName(name);
|
|
|
|
} else {
|
|
|
|
coin = coinByName("Bitcoin");
|
|
|
|
}
|
2015-01-26 11:51:56 +00:00
|
|
|
if (!coin) {
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_DataError, _("Invalid coin name"));
|
2015-01-26 11:51:56 +00:00
|
|
|
layoutHome();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return coin;
|
|
|
|
}
|
|
|
|
|
2018-03-07 13:04:37 +00:00
|
|
|
static HDNode *fsm_getDerivedNode(const char *curve, const uint32_t *address_n, size_t address_n_count, uint32_t *fingerprint)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2017-08-08 10:59:39 +00:00
|
|
|
static CONFIDENTIAL HDNode node;
|
2018-03-07 13:04:37 +00:00
|
|
|
if (fingerprint) {
|
|
|
|
*fingerprint = 0;
|
|
|
|
}
|
2016-05-19 23:49:20 +00:00
|
|
|
if (!storage_getRootNode(&node, curve, true)) {
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_NotInitialized, _("Device not initialized or passphrase request cancelled or unsupported curve"));
|
2015-01-26 11:51:56 +00:00
|
|
|
layoutHome();
|
2014-04-29 12:26:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-26 12:53:06 +00:00
|
|
|
if (!address_n || address_n_count == 0) {
|
|
|
|
return &node;
|
|
|
|
}
|
2018-03-07 13:04:37 +00:00
|
|
|
if (hdnode_private_ckd_cached(&node, address_n, address_n_count, fingerprint) == 0) {
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_ProcessError, _("Failed to derive private key"));
|
2015-01-26 19:24:07 +00:00
|
|
|
layoutHome();
|
|
|
|
return 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2015-01-26 12:53:06 +00:00
|
|
|
return &node;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 18:56:50 +00:00
|
|
|
static bool fsm_layoutAddress(const char *address, const char *desc, bool ignorecase, size_t prefixlen, const uint32_t *address_n, size_t address_n_count)
|
2018-01-06 15:47:32 +00:00
|
|
|
{
|
|
|
|
bool qrcode = false;
|
|
|
|
for (;;) {
|
2018-02-07 18:56:50 +00:00
|
|
|
const char* display_addr = address;
|
|
|
|
if (prefixlen && !qrcode) {
|
|
|
|
display_addr += prefixlen;
|
|
|
|
}
|
|
|
|
layoutAddress(display_addr, desc, qrcode, ignorecase, address_n, address_n_count);
|
2018-07-10 14:58:00 +00:00
|
|
|
if (protectButton(ButtonRequest_ButtonRequestType_ButtonRequest_Address, false)) {
|
2018-01-06 15:47:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-05 20:38:39 +00:00
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2018-07-10 14:58:00 +00:00
|
|
|
fsm_sendFailure(Failure_FailureType_Failure_ActionCancelled, NULL);
|
2018-01-06 15:47:32 +00:00
|
|
|
layoutHome();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qrcode = !qrcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:47:37 +00:00
|
|
|
#include "fsm_msg_common.h"
|
|
|
|
#include "fsm_msg_coin.h"
|
|
|
|
#include "fsm_msg_ethereum.h"
|
|
|
|
#include "fsm_msg_crypto.h"
|
|
|
|
#include "fsm_msg_nem.h"
|
2018-06-27 15:36:51 +00:00
|
|
|
#include "fsm_msg_stellar.h"
|
2018-05-03 14:47:37 +00:00
|
|
|
#include "fsm_msg_debug.h"
|