Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Mar 4, 2024
1 parent f261ab1 commit 9c589b8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 227 deletions.
6 changes: 1 addition & 5 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ endif

APP_LOAD_PARAMS = --curve secp256k1 $(COMMON_LOAD_PARAMS) --path $(APPPATH)

NANOS_STACK_SIZE := 2050

APP_STACK_MIN_SIZE := 3100
include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.devices

$(info TARGET_NAME = [$(TARGET_NAME)])
Expand All @@ -72,9 +71,6 @@ CFLAGS += -Wvla -Wno-implicit-fallthrough
CFLAGS += -I$(MY_DIR)/../deps/tinycbor/src
APP_SOURCE_PATH += $(MY_DIR)/../deps/tinycbor-ledger

CFLAGS += -I$(MY_DIR)/../deps/nanopb/
APP_SOURCE_PATH += $(MY_DIR)/../deps/nanopb_tiny/

.PHONY: rust
rust:
@echo "No rust code"
Expand Down
18 changes: 10 additions & 8 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __Z_INLINE bool process_chunk(volatile uint32_t *tx, uint32_t rx) {
THROW(APDU_CODE_DATA_INVALID);
}

bool is_stake_tx = parser_tx_obj.special_transfer_type == neuron_stake_transaction;
const bool is_stake_tx = parser_tx_obj.special_transfer_type == neuron_stake_transaction;

uint32_t added;
switch (payloadType) {
Expand Down Expand Up @@ -128,9 +128,9 @@ __Z_INLINE bool process_chunk(volatile uint32_t *tx, uint32_t rx) {
__Z_INLINE void handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
extractHDPath(rx, OFFSET_DATA);

uint8_t requireConfirmation = G_io_apdu_buffer[OFFSET_P1];
const uint8_t requireConfirmation = G_io_apdu_buffer[OFFSET_P1];

zxerr_t zxerr = app_fill_address();
const zxerr_t zxerr = app_fill_address();
if (zxerr != zxerr_ok) {
*tx = 0;
THROW(APDU_CODE_DATA_INVALID);
Expand Down Expand Up @@ -158,7 +158,7 @@ __Z_INLINE void handleSign(volatile uint32_t *flags, volatile uint32_t *tx, uint
CHECK_APP_CANARY()

if (error_msg != NULL) {
int error_msg_length = strlen(error_msg);
const uint32_t error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
MEMCPY(G_io_apdu_buffer, error_msg, error_msg_length);
*tx += (error_msg_length);
THROW(APDU_CODE_DATA_INVALID);
Expand All @@ -181,7 +181,7 @@ __Z_INLINE void handleSignCombined(volatile uint32_t *flags, volatile uint32_t *
CHECK_APP_CANARY()

if (error_msg != NULL) {
int error_msg_length = strlen(error_msg);
const uint32_t error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
MEMCPY(G_io_apdu_buffer, error_msg, error_msg_length);
*tx += (error_msg_length);
THROW(APDU_CODE_DATA_INVALID);
Expand All @@ -202,7 +202,9 @@ __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile
G_io_apdu_buffer[1] = LEDGER_MAJOR_VERSION;
G_io_apdu_buffer[2] = LEDGER_MINOR_VERSION;
G_io_apdu_buffer[3] = LEDGER_PATCH_VERSION;
G_io_apdu_buffer[4] = !IS_UX_ALLOWED;
// sdk won't pass the apdu message if device is locked
// keeping it for backwards compatibility
G_io_apdu_buffer[4] = 0;

G_io_apdu_buffer[5] = (TARGET_ID >> 24) & 0xFF;
G_io_apdu_buffer[6] = (TARGET_ID >> 16) & 0xFF;
Expand All @@ -214,7 +216,7 @@ __Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile
}

void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
uint16_t sw = 0;
volatile uint16_t sw = 0;

BEGIN_TRY
{
Expand Down Expand Up @@ -278,7 +280,7 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}
G_io_apdu_buffer[*tx] = sw >> 8;
G_io_apdu_buffer[*tx + 1] = sw;
G_io_apdu_buffer[*tx + 1] = sw & 0xFF;
*tx += 2;
}
FINALLY
Expand Down
56 changes: 24 additions & 32 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@

uint32_t hdPath[HDPATH_LEN_DEFAULT];

bool isTestnet() {
return hdPath[0] == HDPATH_0_TESTNET &&
hdPath[1] == HDPATH_1_TESTNET;
}

uint8_t const DER_PREFIX[] = {0x30, 0x56, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01,
0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a, 0x03, 0x42, 0x00};

Expand All @@ -53,7 +48,7 @@ zxerr_t hash_sha224(uint8_t *input, uint16_t inputLen, uint8_t *output, uint16_t
}
cx_sha256_t ctx;
cx_sha224_init(&ctx);
cx_hash_no_throw(&ctx.header, CX_LAST, input, inputLen, output, 224);
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, CX_LAST, input, inputLen, output, 224));
return zxerr_ok;
}

Expand All @@ -69,11 +64,11 @@ zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
zxerr_t err = zxerr_ledger_api_error;
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL))
privateKeyData, NULL));

CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey))
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_256K1, NULL, 0, &cx_publicKey))
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1))
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey));
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_256K1, NULL, 0, &cx_publicKey));
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1));
memcpy(pubKey, cx_publicKey.W, SECP256K1_PK_LEN);
err = zxerr_ok;

Expand Down Expand Up @@ -118,36 +113,36 @@ typedef struct {
#define HASH_U64(FIELDNAME, FIELDVALUE, TMPDIGEST) { \
MEMZERO(TMPDIGEST,sizeof(TMPDIGEST)); \
cx_hash_sha256((uint8_t *)FIELDNAME, sizeof(FIELDNAME) - 1, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0)); \
uint8_t ingressbuf[10]; \
uint16_t enc_size = 0; \
CHECK_ZXERR(compressLEB128(FIELDVALUE, sizeof(ingressbuf), ingressbuf, &enc_size)); \
cx_hash_sha256((uint8_t *)ingressbuf, enc_size, tmpdigest, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0)); \
}

#define HASH_BYTES_INTERMEDIATE(FIELDNAME, FIELDVALUE, TMPDIGEST) { \
MEMZERO(TMPDIGEST,sizeof(TMPDIGEST)); \
cx_hash_sha256((uint8_t *)FIELDNAME, sizeof(FIELDNAME) - 1, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0)); \
cx_hash_sha256((uint8_t *)(FIELDVALUE).data, (FIELDVALUE).len, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0)); \
}

#define HASH_BYTES_END(FIELDNAME, FIELDVALUE, TMPDIGEST, ENDDIGEST) { \
MEMZERO(TMPDIGEST,sizeof(TMPDIGEST)); \
cx_hash_sha256((uint8_t *)FIELDNAME, sizeof(FIELDNAME) - 1, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0)); \
cx_hash_sha256((uint8_t *)(FIELDVALUE).data, (FIELDVALUE).len, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, CX_LAST, TMPDIGEST, CX_SHA256_SIZE, ENDDIGEST, CX_SHA256_SIZE); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, CX_LAST, TMPDIGEST, CX_SHA256_SIZE, ENDDIGEST, CX_SHA256_SIZE)); \
}

#define HASH_BYTES_PTR_END(FIELDNAME, FIELDVALUE, TMPDIGEST, ENDDIGEST) { \
MEMZERO(TMPDIGEST,sizeof(TMPDIGEST)); \
cx_hash_sha256((uint8_t *)FIELDNAME, sizeof(FIELDNAME) - 1, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, TMPDIGEST, CX_SHA256_SIZE, NULL, 0)); \
cx_hash_sha256((uint8_t *)(FIELDVALUE).dataPtr, (FIELDVALUE).len, TMPDIGEST, CX_SHA256_SIZE); \
cx_hash_no_throw(&ctx.header, CX_LAST, TMPDIGEST, CX_SHA256_SIZE, ENDDIGEST, CX_SHA256_SIZE); \
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, CX_LAST, TMPDIGEST, CX_SHA256_SIZE, ENDDIGEST, CX_SHA256_SIZE)); \
}

zxerr_t crypto_getDigest(uint8_t *digest, txtype_e txtype){
Expand Down Expand Up @@ -178,15 +173,15 @@ zxerr_t crypto_getDigest(uint8_t *digest, txtype_e txtype){
HASH_U64("ingress_expiry",fields->ingress_expiry, tmpdigest);

cx_hash_sha256((uint8_t *)"paths", 5, tmpdigest, CX_SHA256_SIZE);
cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0);
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0));

uint8_t arrayBuffer[PATH_MAX_ARRAY * CX_SHA256_SIZE];
for (size_t index = 0; index < fields->paths.arrayLen ; index++){
cx_hash_sha256((uint8_t *)fields->paths.paths[index].data, fields->paths.paths[index].len, arrayBuffer + index * CX_SHA256_SIZE, CX_SHA256_SIZE);
}
cx_hash_sha256(arrayBuffer, fields->paths.arrayLen*CX_SHA256_SIZE, tmpdigest, CX_SHA256_SIZE);
cx_hash_sha256(tmpdigest, CX_SHA256_SIZE, tmpdigest, CX_SHA256_SIZE);
cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0);
CHECK_CX_OK(cx_hash_no_throw(&ctx.header, 0, tmpdigest, CX_SHA256_SIZE, NULL, 0));

HASH_BYTES_END("request_type", parser_tx_obj.request_type, tmpdigest, digest);
return zxerr_ok;
Expand All @@ -205,8 +200,7 @@ zxerr_t crypto_sign(uint8_t *signatureBuffer,
return zxerr_buffer_too_small;
}

uint8_t message_digest[CX_SHA256_SIZE];
MEMZERO(message_digest,sizeof(message_digest));
uint8_t message_digest[CX_SHA256_SIZE] = {0};

signatureBuffer[0] = 0x0a;
MEMCPY(&signatureBuffer[1], (uint8_t *)"ic-request",SIGN_PREFIX_SIZE - 1);
Expand All @@ -228,9 +222,9 @@ zxerr_t crypto_sign(uint8_t *signatureBuffer,
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_SECP256K1,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL))
privateKeyData, NULL));

CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_SECP256K1, privateKeyData, 32, &cx_privateKey))
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_SECP256K1, privateKeyData, 32, &cx_privateKey));

// Sign
CATCH_CXERROR(cx_ecdsa_sign_no_throw(&cx_privateKey,
Expand All @@ -240,7 +234,7 @@ zxerr_t crypto_sign(uint8_t *signatureBuffer,
CX_SHA256_SIZE,
signature->der_signature,
&signatureLength,
&info))
&info));

err_convert_e err_c = convertDERtoRSV(signature->der_signature, info, signature->r, signature->s, &signature->v);
if (err_c != no_error) {
Expand Down Expand Up @@ -309,9 +303,9 @@ zxerr_t crypto_sign_combined(uint8_t *signatureBuffer,
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_SECP256K1,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData, NULL))
privateKeyData, NULL));

CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_SECP256K1, privateKeyData, 32, &cx_privateKey))
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_SECP256K1, privateKeyData, 32, &cx_privateKey));

// Sign request
CATCH_CXERROR(cx_ecdsa_sign_no_throw(&cx_privateKey,
Expand All @@ -321,7 +315,7 @@ zxerr_t crypto_sign_combined(uint8_t *signatureBuffer,
CX_SHA256_SIZE,
sigma.der_signature,
&sigLen,
&info))
&info));

err_convert_e err_c = convertDERtoRSV(sigma.der_signature, info, sigma.r, sigma.s, &sigma.v);
if (err_c != no_error) {
Expand All @@ -343,7 +337,7 @@ zxerr_t crypto_sign_combined(uint8_t *signatureBuffer,
CX_SHA256_SIZE,
sigma.der_signature,
&sigLen,
&info))
&info));

err_c = convertDERtoRSV(sigma.der_signature, info, sigma.r, sigma.s, &sigma.v);
if (err_c != no_error) {
Expand Down Expand Up @@ -665,9 +659,7 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrL
CHECK_ZXERR(crypto_computePrincipal(answer->publicKey, answer->principalBytes));

//For now only defeault subaccount, maybe later grab 32 bytes from the apdu buffer.
uint8_t zero_subaccount[DFINITY_SUBACCOUNT_LEN];
MEMZERO(zero_subaccount, DFINITY_SUBACCOUNT_LEN);

uint8_t zero_subaccount[DFINITY_SUBACCOUNT_LEN] = {0};
CHECK_ZXERR(crypto_principalToSubaccount(answer->principalBytes, sizeof_field(answer_t, principalBytes),
zero_subaccount, DFINITY_SUBACCOUNT_LEN, answer->subAccountBytes,
sizeof_field(answer_t, subAccountBytes)));
Expand Down
2 changes: 0 additions & 2 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ extern "C" {

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];

bool isTestnet();

zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen);

zxerr_t crypto_computePrincipal(const uint8_t *pubKey, uint8_t *principal);
Expand Down
43 changes: 5 additions & 38 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,31 +368,6 @@ parser_error_t readPayload(parser_tx_t *v, uint8_t *buffer, size_t bufferLen) {
return parser_unexpected_type;
}

static bool isCandidTransaction(parser_tx_t *v) {
char *method = v->tx_fields.call.method_name.data;
if (strcmp(method, "manage_neuron") == 0) {
return true;
}

if (strcmp(method, "update_node_provider") == 0) {
return true;
}

if (strcmp(method, "list_neurons") == 0) {
return true;
}

if (strcmp(method, "icrc1_transfer") == 0) {
return true;
}

if (strcmp(method, "transfer") == 0) {
return true;
}

return false;
}

parser_error_t readContent(CborValue *content_map, parser_tx_t *v) {
CborValue content_it;
zemu_log_stack("read content");
Expand Down Expand Up @@ -427,15 +402,11 @@ parser_error_t readContent(CborValue *content_map, parser_tx_t *v) {
READ_STRING(content_map, "method_name", fields->method_name)
READ_INT64(content_map, "ingress_expiry", fields->ingress_expiry)

if (isCandidTransaction(v)) {
READ_STRING_PTR_SIZE(content_map, "arg", fields->method_args.dataPtr, fields->method_args.len)
if (fields->method_args.dataPtr == NULL) {
return parser_no_data;
}
CHECK_PARSER_ERR(readPayload(v, fields->method_args.dataPtr, fields->method_args.len))
} else {
return parser_unexpected_type;
READ_STRING_PTR_SIZE(content_map, "arg", fields->method_args.dataPtr, fields->method_args.len)
if (fields->method_args.dataPtr == NULL) {
return parser_no_data;
}
CHECK_PARSER_ERR(readPayload(v, fields->method_args.dataPtr, fields->method_args.len))

} else if (strcmp(v->request_type.data, "read_state") == 0) {
state_read_t *fields = &v->tx_fields.stateRead;
Expand Down Expand Up @@ -669,7 +640,6 @@ uint8_t getNumItemsManageNeurons(__Z_UNUSED const parser_context_t *c, const par
case Configure_StartDissolving : {
return 2;
}
case Spawn :
case Split:
case Merge:
case Configure_RemoveHotKey :
Expand All @@ -683,10 +653,8 @@ uint8_t getNumItemsManageNeurons(__Z_UNUSED const parser_context_t *c, const par
return 3;
}
case SNS_Configure_SetDissolveDelay:
case RegisterVote :
case RegisterVoteCandid:
case DisburseCandid:
case Disburse : {
case DisburseCandid: {
return 4;
}
case SpawnCandid: {
Expand Down Expand Up @@ -752,7 +720,6 @@ uint8_t _getNumItems(__Z_UNUSED const parser_context_t *c, const parser_tx_t *v)
return 2;
}

// case pb_manageneuron :
case candid_manageneuron: {
return getNumItemsManageNeurons(c, v);
}
Expand Down
8 changes: 0 additions & 8 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <zxtypes.h>
#include <stdbool.h>

#define ZX_NO_CPP

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -36,8 +34,6 @@ extern "C" {
#define METHOD_MAX_LEN 20
#define NONCE_MAX_LEN 32

#define ARG_MAX_LEN 1000

#define PATH_MAX_LEN 40
#define PATH_MAX_ARRAY 2

Expand Down Expand Up @@ -70,9 +66,6 @@ typedef enum {
Configure_ChangeAutoStakeMaturity = 2009,

////
Disburse = 3,
Spawn = 4,
RegisterVote = 7,
Split = 11,
Merge = 1000,
SpawnCandid = 1001,
Expand Down Expand Up @@ -129,7 +122,6 @@ typedef struct {
} nonce_t;

typedef struct {
uint8_t data[ARG_MAX_LEN + 1];
uint8_t *dataPtr;
size_t len;
} method_arg_t;
Expand Down
Loading

0 comments on commit 9c589b8

Please sign in to comment.