Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.2 #8

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Language: Cpp
ColumnLimit: 160
PointerAlignment: Right
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
SortIncludes: false
SpaceAfterCStyleCast: true
AllowShortCaseLabelsOnASingleLine: false
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2023-02-16

### Feature

- Message signing command

### Fixed

- use varint when necessary
- add empty extensions encoders for some operation types

## [1.1.0] - 2022-04-13

### Feature
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ APP_LOAD_PARAMS += $(COMMON_LOAD_PARAMS)

APPNAME = "Hive"
APPVERSION_M = 1
APPVERSION_N = 1
APPVERSION_N = 2
APPVERSION_P = 0
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

Expand Down
19 changes: 19 additions & 0 deletions src/apdu/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "handler/get_settings.h"
#include "handler/sign_tx.h"
#include "handler/sign_hash.h"
#include "handler/sign_message.h"

int apdu_dispatcher(const command_t *cmd) {
if (cmd->cla != CLA) {
Expand All @@ -47,12 +48,14 @@ int apdu_dispatcher(const command_t *cmd) {
}

return handler_get_version();

case GET_APP_NAME:
if (cmd->p1 != 0 || cmd->p2 != 0) {
return io_send_sw(SW_WRONG_P1P2);
}

return handler_get_app_name();

case GET_PUBLIC_KEY:
if (cmd->p1 > 1 || cmd->p2 > 0) {
return io_send_sw(SW_WRONG_P1P2);
Expand All @@ -67,6 +70,7 @@ int apdu_dispatcher(const command_t *cmd) {
buf.offset = 0;

return handler_get_public_key(&buf, (bool) cmd->p1);

case SIGN_TRANSACTION:
if (cmd->p1 != P1_FIRST_CHUNK && cmd->p1 != P1_SUBSEQUENT_CHUNK && cmd->p2 != P2_LAST && cmd->p2 != P2_MORE) {
return io_send_sw(SW_WRONG_P1P2);
Expand Down Expand Up @@ -95,6 +99,21 @@ int apdu_dispatcher(const command_t *cmd) {
buf.offset = 0;
return handler_sign_hash(&buf);

case SIGN_MESSAGE:
if (cmd->p1 != P1_FIRST_CHUNK && cmd->p1 != P1_SUBSEQUENT_CHUNK && cmd->p2 != P2_LAST && cmd->p2 != P2_MORE) {
return io_send_sw(SW_WRONG_P1P2);
}

if (!cmd->data) {
return io_send_sw(SW_WRONG_DATA_LENGTH);
}

buf.ptr = cmd->data;
buf.size = cmd->lc;
buf.offset = 0;

return handler_sign_message(&buf, cmd->p1, (bool) (cmd->p2 & P2_MORE));

case GET_SETTINGS:
if (cmd->p1 != 0 || cmd->p2 != 0) {
return io_send_sw(SW_WRONG_P1P2);
Expand Down
19 changes: 17 additions & 2 deletions src/common/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string.h> // memmove

#include "buffer.h"
#include "varint.h"
#include "read.h"
#include "bip32.h"

Expand Down Expand Up @@ -117,6 +118,20 @@ bool buffer_read_u64(buffer_t *buffer, uint64_t *value, endianness_t endianness)
return true;
}

bool buffer_read_varint(buffer_t *buffer, uint64_t *value) {
int8_t length = varint_read(buffer->ptr + buffer->offset, buffer->size - buffer->offset, value);

if (length < 0) {
*value = 0;

return false;
}

buffer_seek_cur(buffer, (size_t) length);

return true;
}

bool buffer_read_bip32_path(buffer_t *buffer, uint32_t *out, size_t out_len) {
if (!bip32_path_read(buffer->ptr + buffer->offset, buffer->size - buffer->offset, out, out_len)) {
return false;
Expand Down Expand Up @@ -158,7 +173,7 @@ bool buffer_copy(const buffer_t *buffer, uint8_t *out, size_t out_len) {
return true;
}

bool buffer_copy_partial(const buffer_t *buffer, uint8_t *out, size_t out_len, uint8_t length) {
bool buffer_copy_partial(const buffer_t *buffer, uint8_t *out, size_t out_len, size_t length) {
if (length > out_len || buffer->size - buffer->offset < length) {
return false;
}
Expand All @@ -177,7 +192,7 @@ bool buffer_move(buffer_t *buffer, uint8_t *out, size_t out_len) {
return true;
}

bool buffer_move_partial(buffer_t *buffer, uint8_t *out, size_t out_len, uint8_t length) {
bool buffer_move_partial(buffer_t *buffer, uint8_t *out, size_t out_len, size_t length) {
if (!buffer_copy_partial(buffer, out, out_len, length)) {
return false;
}
Expand Down
19 changes: 17 additions & 2 deletions src/common/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ bool buffer_read_u32(buffer_t *buffer, uint32_t *value, endianness_t endianness)
*/
bool buffer_read_u64(buffer_t *buffer, uint64_t *value, endianness_t endianness);

/**
* Read Bitcoin-like varint from buffer into uint64_t.
*
* @see https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
*
* @param[in,out] buffer
* Pointer to input buffer struct.
* @param[out] value
* Pointer to 64-bit unsigned integer read from buffer.
*
* @return true if success, false otherwise.
*
*/
bool buffer_read_varint(buffer_t *buffer, uint64_t *value);

/**
* Read BIP32 path from buffer.
*
Expand Down Expand Up @@ -176,7 +191,7 @@ bool buffer_copy(const buffer_t *buffer, uint8_t *out, size_t out_len);
* @return true if success, false otherwise.
*
*/
bool buffer_copy_partial(const buffer_t *buffer, uint8_t *out, size_t out_len, uint8_t length);
bool buffer_copy_partial(const buffer_t *buffer, uint8_t *out, size_t out_len, size_t length);

/**
* Move bytes from buffer.
Expand Down Expand Up @@ -207,7 +222,7 @@ bool buffer_move(buffer_t *buffer, uint8_t *out, size_t out_len);
* @return true if success, false otherwise.
*
*/
bool buffer_move_partial(buffer_t *buffer, uint8_t *out, size_t out_len, uint8_t length);
bool buffer_move_partial(buffer_t *buffer, uint8_t *out, size_t out_len, size_t length);

/**
* Read TLV (Type–length–value) field endoded using asn1 DER standard
Expand Down
10 changes: 5 additions & 5 deletions src/common/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool format_timestamp(uint32_t timestamp, char *out, size_t out_len) {
return true;
}

bool format_i64(const int64_t i, char *out, uint8_t out_len) {
bool format_i64(const int64_t i, char *out, size_t out_len) {
char temp[] = "-9223372036854775808";

char *ptr = temp;
Expand All @@ -101,9 +101,9 @@ bool format_i64(const int64_t i, char *out, uint8_t out_len) {
*ptr++ = '0';
}

int distance = (ptr - temp) + 1;
size_t distance = (ptr - temp) + 1;

if ((int) out_len < distance) {
if (out_len < distance) {
return false;
}

Expand All @@ -118,7 +118,7 @@ bool format_i64(const int64_t i, char *out, uint8_t out_len) {
return true;
}

bool format_u64(const uint64_t i, char *out, uint8_t out_len) {
bool format_u64(const uint64_t i, char *out, size_t out_len) {
if (out_len < MAX_U64_LEN) {
return false;
}
Expand All @@ -142,7 +142,7 @@ bool format_u64(const uint64_t i, char *out, uint8_t out_len) {
return true;
}

static bool insert_string(char *out, uint8_t out_len, const char *source, size_t position) {
static bool insert_string(char *out, size_t out_len, const char *source, size_t position) {
if (strlen(source) + position > out_len - 1) { // need space for null character
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool format_timestamp(uint32_t timestamp, char *out, size_t out_size);
* @return true if success, false otherwise.
*
*/
bool format_i64(const int64_t i, char *out, uint8_t out_len);
bool format_i64(const int64_t i, char *out, size_t out_len);

/**
* Format 64-bit unsigned integer as string.
Expand All @@ -65,7 +65,7 @@ bool format_i64(const int64_t i, char *out, uint8_t out_len);
* @return true if success, false otherwise.
*
*/
bool format_u64(const uint64_t i, char *out, uint8_t out_len);
bool format_u64(const uint64_t i, char *out, size_t out_len);

/**
* Convert serialized asset into readable form and replace STEEM/SBD with HIVE equivalents
Expand Down
41 changes: 41 additions & 0 deletions src/common/varint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*****************************************************************************
* Ledger App Hive
* (c) 2022 Bartłomiej (@engrave) Górnicki
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

#include "varint.h"

static const uint8_t MSB = 0x80;
static const uint8_t REST = 0x7F;

int8_t varint_read(const uint8_t *in, size_t in_len, uint64_t *value) {
uint32_t shift = 0;
const uint8_t *ptr = in;
uint64_t ll;
*value = 0;
while (*ptr & MSB) {
ll = *ptr;
*value += ((ll & REST) << shift);
ptr++;
shift += 7;
if ((size_t)(ptr - in) >= in_len) {
return -1;
}
}
ll = *ptr;
*value += ((ll & REST) << shift);

return ptr - in + 1;
}
20 changes: 20 additions & 0 deletions src/common/varint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <stdint.h> // uint*_t
#include <stddef.h> // size_t
#include <stdbool.h> // bool

/**
* Read a protobuf-syle-like varint from byte buffer.
*
* @param[in] in
* Pointer to input byte buffer.
* @param[in] in_len
* Length of the input byte buffer.
* @param[out] value
* Pointer to 64-bit unsigned integer to output varint.
*
* @return number of bytes read, -1 otherwise.
*
*/
int8_t varint_read(const uint8_t *in, size_t in_len, uint64_t *value);
19 changes: 17 additions & 2 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Maximum transaction length (bytes) - four chunks
*/
#define MAX_TRANSACTION_LEN (3 * MAX_DATA_CHUNK_LEN)
#define MAX_TRANSACTION_LEN (4 * MAX_DATA_CHUNK_LEN)

/**
* Maximum DER encoded signature length (bytes).
Expand Down Expand Up @@ -64,4 +64,19 @@
#define PUBKEY_COMPRESSED_LEN 33

// [wif (53)][\0]
#define PUBKEY_WIF_STR_LEN 54
#define PUBKEY_WIF_STR_LEN 54

/**
* Maximum message length (bytes) - three chunks
*/
#define MAX_RAW_MESSAGE_LEN (3 * MAX_DATA_CHUNK_LEN)

/**
* Maximum length of the screen text.
*/
#define MAX_SCREEN_TEXT_LEN 512

/**
* Maximum length of the screen title.
*/
#define MAX_SCREEN_TITLE_LEN 60
2 changes: 1 addition & 1 deletion src/handler/sign_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "os.h"
#include "cx.h"

#include "sign_tx.h"
#include "sign_hash.h"
#include "sw.h"
#include "globals.h"
#include "crypto.h"
Expand Down
Loading