2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "fsm.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2019-01-23 16:00:01 +00:00
|
|
|
#include "memzero.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "messages.h"
|
|
|
|
#include "trezor.h"
|
|
|
|
#include "util.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "messages.pb.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "pb_decode.h"
|
|
|
|
#include "pb_encode.h"
|
|
|
|
|
|
|
|
struct MessagesMap_t {
|
2019-03-29 16:10:31 +00:00
|
|
|
char type; // n = normal, d = debug
|
|
|
|
char dir; // i = in, o = out
|
|
|
|
uint16_t msg_id;
|
2019-12-23 15:33:34 +00:00
|
|
|
const pb_msgdesc_t *fields;
|
2019-03-29 16:10:31 +00:00
|
|
|
void (*process_func)(const void *ptr);
|
2014-04-29 12:26:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct MessagesMap_t MessagesMap[] = {
|
2017-05-29 11:46:32 +00:00
|
|
|
#include "messages_map.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
// end
|
|
|
|
{0, 0, 0, 0, 0}};
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-02-04 12:53:05 +00:00
|
|
|
#include "messages_map_limits.h"
|
|
|
|
|
2019-12-23 15:33:34 +00:00
|
|
|
const pb_msgdesc_t *MessageFields(char type, char dir, uint16_t msg_id) {
|
2019-03-29 16:10:31 +00:00
|
|
|
const struct MessagesMap_t *m = MessagesMap;
|
|
|
|
while (m->type) {
|
|
|
|
if (type == m->type && dir == m->dir && msg_id == m->msg_id) {
|
|
|
|
return m->fields;
|
|
|
|
}
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
return 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void MessageProcessFunc(char type, char dir, uint16_t msg_id, void *ptr) {
|
|
|
|
const struct MessagesMap_t *m = MessagesMap;
|
|
|
|
while (m->type) {
|
|
|
|
if (type == m->type && dir == m->dir && msg_id == m->msg_id) {
|
|
|
|
m->process_func(ptr);
|
2023-01-02 19:25:37 +00:00
|
|
|
fsm_postMsgCleanup(msg_id);
|
2019-03-29 16:10:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m++;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 19:26:55 +00:00
|
|
|
// Buffer for outgoing USB packets.
|
2014-04-29 12:26:51 +00:00
|
|
|
static uint32_t msg_out_start = 0;
|
|
|
|
static uint32_t msg_out_end = 0;
|
|
|
|
static uint32_t msg_out_cur = 0;
|
2021-02-10 19:26:55 +00:00
|
|
|
static uint8_t msg_out[MSG_OUT_BUFFER_SIZE];
|
|
|
|
_Static_assert(MSG_OUT_BUFFER_SIZE % USB_PACKET_SIZE == 0,
|
|
|
|
"MSG_OUT_BUFFER_SIZE");
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
|
|
|
static uint32_t msg_debug_out_start = 0;
|
|
|
|
static uint32_t msg_debug_out_end = 0;
|
|
|
|
static uint32_t msg_debug_out_cur = 0;
|
2021-02-10 19:26:55 +00:00
|
|
|
static uint8_t msg_debug_out[MSG_DEBUG_OUT_BUFFER_SIZE];
|
|
|
|
_Static_assert(MSG_DEBUG_OUT_BUFFER_SIZE % USB_PACKET_SIZE == 0,
|
|
|
|
"MSG_DEBUG_OUT_BUFFER_SIZE");
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline void msg_out_append(uint8_t c) {
|
|
|
|
if (msg_out_cur == 0) {
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_out[msg_out_end * USB_PACKET_SIZE] = '?';
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_out_cur = 1;
|
|
|
|
}
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_out[msg_out_end * USB_PACKET_SIZE + msg_out_cur] = c;
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_out_cur++;
|
2021-02-09 18:07:48 +00:00
|
|
|
if (msg_out_cur == USB_PACKET_SIZE) {
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_out_cur = 0;
|
2021-02-10 19:26:55 +00:00
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline void msg_debug_out_append(uint8_t c) {
|
|
|
|
if (msg_debug_out_cur == 0) {
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE] = '?';
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_debug_out_cur = 1;
|
|
|
|
}
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE + msg_debug_out_cur] = c;
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_debug_out_cur++;
|
2021-02-09 18:07:48 +00:00
|
|
|
if (msg_debug_out_cur == USB_PACKET_SIZE) {
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_debug_out_cur = 0;
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_debug_out_end =
|
2021-02-10 19:26:55 +00:00
|
|
|
(msg_debug_out_end + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline void msg_out_pad(void) {
|
|
|
|
if (msg_out_cur == 0) return;
|
2021-02-09 18:07:48 +00:00
|
|
|
while (msg_out_cur < USB_PACKET_SIZE) {
|
|
|
|
msg_out[msg_out_end * USB_PACKET_SIZE + msg_out_cur] = 0;
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_out_cur++;
|
|
|
|
}
|
|
|
|
msg_out_cur = 0;
|
2021-02-10 19:26:55 +00:00
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static inline void msg_debug_out_pad(void) {
|
|
|
|
if (msg_debug_out_cur == 0) return;
|
2021-02-09 18:07:48 +00:00
|
|
|
while (msg_debug_out_cur < USB_PACKET_SIZE) {
|
|
|
|
msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE + msg_debug_out_cur] = 0;
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_debug_out_cur++;
|
|
|
|
}
|
|
|
|
msg_debug_out_cur = 0;
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_debug_out_end =
|
2021-02-10 19:26:55 +00:00
|
|
|
(msg_debug_out_end + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static bool pb_callback_out(pb_ostream_t *stream, const uint8_t *buf,
|
|
|
|
size_t count) {
|
|
|
|
(void)stream;
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
msg_out_append(buf[i]);
|
|
|
|
}
|
|
|
|
return true;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
static bool pb_debug_callback_out(pb_ostream_t *stream, const uint8_t *buf,
|
|
|
|
size_t count) {
|
|
|
|
(void)stream;
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
msg_debug_out_append(buf[i]);
|
|
|
|
}
|
|
|
|
return true;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
bool msg_write_common(char type, uint16_t msg_id, const void *msg_ptr) {
|
2019-12-23 15:33:34 +00:00
|
|
|
const pb_msgdesc_t *fields = MessageFields(type, 'o', msg_id);
|
2019-03-29 16:10:31 +00:00
|
|
|
if (!fields) { // unknown message
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-30 15:34:38 +00:00
|
|
|
size_t len = 0;
|
2019-03-29 16:10:31 +00:00
|
|
|
if (!pb_get_encoded_size(&len, fields, msg_ptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-30 15:34:38 +00:00
|
|
|
void (*append)(uint8_t) = NULL;
|
2019-03-29 16:10:31 +00:00
|
|
|
bool (*pb_callback)(pb_ostream_t *, const uint8_t *, size_t);
|
|
|
|
|
|
|
|
if (type == 'n') {
|
|
|
|
append = msg_out_append;
|
|
|
|
pb_callback = pb_callback_out;
|
|
|
|
} else
|
2014-04-29 12:26:51 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
if (type == 'd') {
|
|
|
|
append = msg_debug_out_append;
|
|
|
|
pb_callback = pb_debug_callback_out;
|
|
|
|
} else
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
2019-03-29 16:10:31 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
append('#');
|
|
|
|
append('#');
|
|
|
|
append((msg_id >> 8) & 0xFF);
|
|
|
|
append(msg_id & 0xFF);
|
|
|
|
append((len >> 24) & 0xFF);
|
|
|
|
append((len >> 16) & 0xFF);
|
|
|
|
append((len >> 8) & 0xFF);
|
|
|
|
append(len & 0xFF);
|
|
|
|
pb_ostream_t stream = {pb_callback, 0, SIZE_MAX, 0, 0};
|
|
|
|
bool status = pb_encode(&stream, fields, msg_ptr);
|
|
|
|
if (type == 'n') {
|
|
|
|
msg_out_pad();
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
else if (type == 'd') {
|
|
|
|
msg_debug_out_pad();
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
2019-03-29 16:10:31 +00:00
|
|
|
return status;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
2019-03-29 16:10:31 +00:00
|
|
|
READSTATE_IDLE,
|
|
|
|
READSTATE_READING,
|
2014-04-29 12:26:51 +00:00
|
|
|
};
|
|
|
|
|
2019-12-23 15:33:34 +00:00
|
|
|
void msg_process(char type, uint16_t msg_id, const pb_msgdesc_t *fields,
|
2021-02-10 19:26:55 +00:00
|
|
|
uint8_t *msg_encoded, uint32_t msg_encoded_size) {
|
|
|
|
static uint8_t msg_decoded[MSG_IN_DECODED_SIZE];
|
|
|
|
memzero(msg_decoded, sizeof(msg_decoded));
|
|
|
|
pb_istream_t stream = pb_istream_from_buffer(msg_encoded, msg_encoded_size);
|
|
|
|
bool status = pb_decode(&stream, fields, msg_decoded);
|
2019-03-29 16:10:31 +00:00
|
|
|
if (status) {
|
2021-02-10 19:26:55 +00:00
|
|
|
MessageProcessFunc(type, 'i', msg_id, msg_decoded);
|
2019-03-29 16:10:31 +00:00
|
|
|
} else {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, stream.errmsg);
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void msg_read_common(char type, const uint8_t *buf, uint32_t len) {
|
|
|
|
static char read_state = READSTATE_IDLE;
|
2021-02-10 19:26:55 +00:00
|
|
|
static uint8_t msg_encoded[MSG_IN_ENCODED_SIZE];
|
2019-03-29 16:10:31 +00:00
|
|
|
static uint16_t msg_id = 0xFFFF;
|
2021-02-10 19:26:55 +00:00
|
|
|
static uint32_t msg_encoded_size = 0;
|
2019-03-29 16:10:31 +00:00
|
|
|
static uint32_t msg_pos = 0;
|
2019-12-23 15:33:34 +00:00
|
|
|
static const pb_msgdesc_t *fields = 0;
|
2019-03-29 16:10:31 +00:00
|
|
|
|
2021-02-09 18:07:48 +00:00
|
|
|
if (len != USB_PACKET_SIZE) return;
|
2019-03-29 16:10:31 +00:00
|
|
|
|
|
|
|
if (read_state == READSTATE_IDLE) {
|
|
|
|
if (buf[0] != '?' || buf[1] != '#' ||
|
|
|
|
buf[2] != '#') { // invalid start - discard
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msg_id = (buf[3] << 8) + buf[4];
|
2021-02-10 19:26:55 +00:00
|
|
|
msg_encoded_size =
|
2019-03-29 16:10:31 +00:00
|
|
|
((uint32_t)buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
|
|
|
|
|
|
|
|
fields = MessageFields(type, 'i', msg_id);
|
|
|
|
if (!fields) { // unknown message
|
|
|
|
fsm_sendFailure(FailureType_Failure_UnexpectedMessage,
|
|
|
|
_("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
2021-02-10 19:26:55 +00:00
|
|
|
if (msg_encoded_size > MSG_IN_ENCODED_SIZE) { // message is too big :(
|
2019-03-29 16:10:31 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Message too big"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_state = READSTATE_READING;
|
|
|
|
|
2021-02-10 19:26:55 +00:00
|
|
|
memcpy(msg_encoded, buf + MSG_HEADER_SIZE, len - MSG_HEADER_SIZE);
|
2021-02-09 18:07:48 +00:00
|
|
|
msg_pos = len - MSG_HEADER_SIZE;
|
2019-03-29 16:10:31 +00:00
|
|
|
} else if (read_state == READSTATE_READING) {
|
|
|
|
if (buf[0] != '?') { // invalid contents
|
|
|
|
read_state = READSTATE_IDLE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* raw data starts at buf + 1 with len - 1 bytes */
|
|
|
|
buf++;
|
2021-02-10 19:26:55 +00:00
|
|
|
len = MIN(len - 1, MSG_IN_ENCODED_SIZE - msg_pos);
|
2019-03-29 16:10:31 +00:00
|
|
|
|
2021-02-10 19:26:55 +00:00
|
|
|
memcpy(msg_encoded + msg_pos, buf, len);
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_pos += len;
|
|
|
|
}
|
|
|
|
|
2021-02-10 19:26:55 +00:00
|
|
|
if (msg_pos >= msg_encoded_size) {
|
|
|
|
msg_process(type, msg_id, fields, msg_encoded, msg_encoded_size);
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_pos = 0;
|
|
|
|
read_state = READSTATE_IDLE;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
const uint8_t *msg_out_data(void) {
|
|
|
|
if (msg_out_start == msg_out_end) return 0;
|
2021-02-09 18:07:48 +00:00
|
|
|
uint8_t *data = msg_out + (msg_out_start * USB_PACKET_SIZE);
|
2021-02-10 19:26:55 +00:00
|
|
|
msg_out_start = (msg_out_start + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
debugLog(0, "", "msg_out_data");
|
|
|
|
return data;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
const uint8_t *msg_debug_out_data(void) {
|
|
|
|
if (msg_debug_out_start == msg_debug_out_end) return 0;
|
2021-02-09 18:07:48 +00:00
|
|
|
uint8_t *data = msg_debug_out + (msg_debug_out_start * USB_PACKET_SIZE);
|
|
|
|
msg_debug_out_start =
|
2021-02-10 19:26:55 +00:00
|
|
|
(msg_debug_out_start + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
2019-03-29 16:10:31 +00:00
|
|
|
debugLog(0, "", "msg_debug_out_data");
|
|
|
|
return data;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-02-09 18:19:15 +00:00
|
|
|
// msg_tiny needs to be large enough to hold the C struct decoded from a single
|
|
|
|
// 64 byte USB packet. The decoded struct can be larger than the encoded
|
|
|
|
// protobuf message. However, 128 bytes should be more than enough.
|
2018-03-20 13:48:40 +00:00
|
|
|
CONFIDENTIAL uint8_t msg_tiny[128];
|
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(Cancel), "msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + Cancel_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(Initialize), "msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + Initialize_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(PassphraseAck), "msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + PassphraseAck_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(ButtonAck), "msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + ButtonAck_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(PinMatrixAck), "msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + PinMatrixAck_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(DebugLinkDecision),
|
|
|
|
"msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + DebugLinkDecision_size,
|
|
|
|
"msg_tiny too tiny");
|
2019-03-29 16:10:31 +00:00
|
|
|
_Static_assert(sizeof(msg_tiny) >= sizeof(DebugLinkGetState),
|
|
|
|
"msg_tiny too tiny");
|
2021-02-09 18:08:38 +00:00
|
|
|
_Static_assert(USB_PACKET_SIZE >= MSG_HEADER_SIZE + DebugLinkGetState_size,
|
|
|
|
"msg_tiny too tiny");
|
2018-03-20 13:48:40 +00:00
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
uint16_t msg_tiny_id = 0xFFFF;
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void msg_read_tiny(const uint8_t *buf, int len) {
|
2021-02-09 18:19:15 +00:00
|
|
|
if (len != USB_PACKET_SIZE || buf[0] != '?' || buf[1] != '#' ||
|
|
|
|
buf[2] != '#') {
|
|
|
|
// Ignore unexpected packets. This is helpful when two applications are
|
|
|
|
// attempting to communicate with Trezor at the same time.
|
2019-03-29 16:10:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:19:15 +00:00
|
|
|
const pb_msgdesc_t *fields = NULL;
|
|
|
|
uint16_t msg_id = (buf[3] << 8) + buf[4];
|
2019-03-29 16:10:31 +00:00
|
|
|
switch (msg_id) {
|
|
|
|
case MessageType_MessageType_PinMatrixAck:
|
|
|
|
fields = PinMatrixAck_fields;
|
|
|
|
break;
|
|
|
|
case MessageType_MessageType_ButtonAck:
|
|
|
|
fields = ButtonAck_fields;
|
|
|
|
break;
|
|
|
|
case MessageType_MessageType_PassphraseAck:
|
|
|
|
fields = PassphraseAck_fields;
|
|
|
|
break;
|
|
|
|
case MessageType_MessageType_Cancel:
|
|
|
|
fields = Cancel_fields;
|
|
|
|
break;
|
|
|
|
case MessageType_MessageType_Initialize:
|
|
|
|
fields = Initialize_fields;
|
|
|
|
break;
|
2014-04-29 12:26:51 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
case MessageType_MessageType_DebugLinkDecision:
|
|
|
|
fields = DebugLinkDecision_fields;
|
|
|
|
break;
|
|
|
|
case MessageType_MessageType_DebugLinkGetState:
|
|
|
|
fields = DebugLinkGetState_fields;
|
|
|
|
break;
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
2021-02-09 18:19:15 +00:00
|
|
|
default:
|
|
|
|
// Ignore unexpected messages.
|
|
|
|
return;
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2021-02-09 18:19:15 +00:00
|
|
|
|
|
|
|
uint32_t msg_size =
|
|
|
|
((uint32_t)buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
|
|
|
|
|
|
|
|
if (msg_size > sizeof(msg_tiny) / 2 ||
|
|
|
|
msg_size > (uint32_t)len - MSG_HEADER_SIZE) {
|
|
|
|
// There is a risk that the struct decoded from the message won't fit into
|
|
|
|
// msg_tiny or the encoded message does not fit into the buffer. The first
|
|
|
|
// is a fail-safe in case of a forgotten _Static_assert above.
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Message too big"));
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pb_istream_t stream = pb_istream_from_buffer(buf + MSG_HEADER_SIZE, msg_size);
|
|
|
|
bool status = pb_decode(&stream, fields, msg_tiny);
|
|
|
|
if (status) {
|
|
|
|
msg_tiny_id = msg_id;
|
2019-03-29 16:10:31 +00:00
|
|
|
} else {
|
2021-02-09 18:19:15 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, stream.errmsg);
|
2019-03-29 16:10:31 +00:00
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|