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

Several updates #4

Merged
merged 8 commits into from
Mar 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ifeq ($(BOLOS_SDK),)
# In this case, there is not predefined SDK and we run dockerized
# When not using the SDK, we override and build the XL complete app

# ZXLIB_COMPILE_STAX ?= 1
ZXLIB_COMPILE_STAX ?= 1
include $(CURDIR)/deps/ledger-zxlib/dockerized_build.mk

else
Expand Down
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=0
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=3
APPVERSION_P=4
Binary file modified app/glyphs/icon_app.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/glyphs/icon_stax_32.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/glyphs/icon_stax_64.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/nanos_icon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/nanox_icon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 0 additions & 70 deletions app/src/addr.c

This file was deleted.

32 changes: 21 additions & 11 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <ux.h>

#include "actions.h"
#include "addr.h"
#include "review_keys.h"
#include "app_main.h"
#include "coin.h"
#include "crypto.h"
Expand All @@ -35,15 +35,13 @@ static bool tx_initialized = false;
void extractHDPath(uint32_t rx, uint32_t offset) {
tx_initialized = false;

if ((rx - offset) < sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
if ((rx - offset) != sizeof(uint32_t) * HDPATH_LEN_DEFAULT) {
THROW(APDU_CODE_WRONG_LENGTH);
}

memcpy(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);

// #{TODO} --> testnet necessary?
const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT && hdPath[1] == HDPATH_1_DEFAULT;

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}
Expand Down Expand Up @@ -90,22 +88,32 @@ __Z_INLINE bool process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx) {
THROW(APDU_CODE_INVALIDP1P2);
}

__Z_INLINE void handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
__Z_INLINE void handleGetKeys(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
extractHDPath(rx, OFFSET_DATA);
if (G_io_apdu_buffer[OFFSET_P2] >= InvalidKey) {
THROW(APDU_CODE_INVALIDP1P2);
}

const uint8_t requireConfirmation = G_io_apdu_buffer[OFFSET_P1];
zxerr_t zxerr = app_fill_address();
const key_kind_e requestedKeys = (key_kind_e) G_io_apdu_buffer[OFFSET_P2];

// ViewKey will require explicit user confirmation to leave the device
if (!requireConfirmation && requestedKeys == ViewKeys) {
THROW(APDU_CODE_INVALIDP1P2);
}

zxerr_t zxerr = app_fill_keys(requestedKeys);
if (zxerr != zxerr_ok) {
*tx = 0;
THROW(APDU_CODE_DATA_INVALID);
}

if (requireConfirmation) {
view_review_init(addr_getItem, addr_getNumItems, app_reply_address);
view_review_show(REVIEW_ADDRESS);
review_keys_menu(requestedKeys);
*flags |= IO_ASYNCH_REPLY;
return;
}
*tx = action_addrResponseLen;
*tx = cmdResponseLen;
THROW(APDU_CODE_OK);
}

Expand Down Expand Up @@ -179,9 +187,9 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

case INS_GET_ADDR: {
case INS_GET_KEYS: {
CHECK_PIN_VALIDATED()
handleGetAddr(flags, tx, rx);
handleGetKeys(flags, tx, rx);
break;
}

Expand All @@ -191,6 +199,8 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}



#if defined(APP_TESTING)
case INS_TEST: {
handleTest(flags, tx, rx);
Expand Down
21 changes: 13 additions & 8 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ extern "C" {
#endif

// #{TODO} ---> Replace CLA, Token symbol, HDPATH, etc etc
#define CLA 0x80
#define CLA 0x59

#define HDPATH_LEN_DEFAULT 5
#define HDPATH_0_DEFAULT (0x80000000u | 0x2c) // 44
#define HDPATH_1_DEFAULT (0x80000000u | 0x85) // 133
// This instruction will work for requesting any of the sapling keys
#define INS_GET_KEYS 0x01

#define HDPATH_2_DEFAULT (0x80000000u | 0u)
#define HDPATH_3_DEFAULT (0u)
#define HDPATH_4_DEFAULT (0u)
#define HDPATH_LEN_DEFAULT 3
#define HDPATH_0_DEFAULT (0x80000000u | 0x2c) // 44
#define HDPATH_1_DEFAULT (0x80000000u | 0x53a) // 1338

#define SECP256K1_PK_LEN 65u

Expand All @@ -40,12 +39,18 @@ extern "C" {

#define PK_LEN_25519 32u

typedef enum {
PublicAddress = 0,
ViewKeys = 1,
ProofGenerationKey = 2,
InvalidKey,
} key_kind_e;

#define COIN_AMOUNT_DECIMAL_PLACES 6
#define COIN_TICKER "IRON "

#define MENU_MAIN_APP_LINE1 "Ironfish"
#define MENU_MAIN_APP_LINE2 "Ready"
#define MENU_MAIN_APP_LINE2_SECRET "???"
#define APPVERSION_LINE1 "Ironfish"
#define APPVERSION_LINE2 "v" APPVERSION

Expand Down
2 changes: 1 addition & 1 deletion app/src/common/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

#include "actions.h"

uint16_t action_addrResponseLen;
uint16_t cmdResponseLen;
16 changes: 8 additions & 8 deletions app/src/common/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
#include "tx.h"
#include "zxerror.h"

extern uint16_t action_addrResponseLen;
extern uint16_t cmdResponseLen;

__Z_INLINE zxerr_t app_fill_address() {
__Z_INLINE zxerr_t app_fill_keys(key_kind_e requestedKey) {
// Put data directly in the apdu buffer
MEMZERO(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE);

action_addrResponseLen = 0;
const zxerr_t err = crypto_fillAddress(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE, &action_addrResponseLen);
cmdResponseLen = 0;
const zxerr_t err = crypto_fillKeys(G_io_apdu_buffer, IO_APDU_BUFFER_SIZE, requestedKey, &cmdResponseLen);

if (err != zxerr_ok || action_addrResponseLen == 0) {
if (err != zxerr_ok || cmdResponseLen == 0) {
THROW(APDU_CODE_EXECUTION_ERROR);
}

Expand Down Expand Up @@ -61,9 +61,9 @@ __Z_INLINE void app_reject() {
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
}

__Z_INLINE void app_reply_address() {
set_code(G_io_apdu_buffer, action_addrResponseLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, action_addrResponseLen + 2);
__Z_INLINE void app_reply_cmd() {
set_code(G_io_apdu_buffer, cmdResponseLen, APDU_CODE_OK);
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, cmdResponseLen + 2);
}

__Z_INLINE void app_reply_error() {
Expand Down
68 changes: 59 additions & 9 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,45 @@ static zxerr_t computeKeys(keys_t * saplingKeys) {
return zxerr_ok;
}

zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen) {
if (output == NULL || outputLen < 3 * KEY_LENGTH) {
__Z_INLINE zxerr_t copyKeys(keys_t *saplingKeys, key_kind_e requestedKeys, uint8_t *output, uint16_t outputLen) {
if (saplingKeys == NULL || output == NULL) {
return zxerr_no_data;
}

switch (requestedKeys) {
case PublicAddress:
if (outputLen < KEY_LENGTH) {
return zxerr_buffer_too_small;
}
memcpy(output, saplingKeys->address, KEY_LENGTH);
break;

case ViewKeys:
if (outputLen < 4 * KEY_LENGTH) {
return zxerr_buffer_too_small;
}
memcpy(output, saplingKeys->ak, KEY_LENGTH);
memcpy(output + KEY_LENGTH, saplingKeys->nk, KEY_LENGTH);
memcpy(output + 2 * KEY_LENGTH, saplingKeys->ovk, KEY_LENGTH);
memcpy(output + 3 * KEY_LENGTH, saplingKeys->ivk, KEY_LENGTH);
break;

case ProofGenerationKey:
if (outputLen < 2 * KEY_LENGTH) {
return zxerr_buffer_too_small;
}
memcpy(output, saplingKeys->ak, KEY_LENGTH);
memcpy(output + KEY_LENGTH, saplingKeys->nsk, KEY_LENGTH);
break;

default:
return zxerr_invalid_crypto_settings;
}
return zxerr_ok;
}

zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen, key_kind_e requestedKey) {
if (output == NULL) {
return zxerr_buffer_too_small;
}

Expand All @@ -79,9 +116,7 @@ zxerr_t crypto_generateSaplingKeys(uint8_t *output, uint16_t outputLen) {

// Copy keys
if (error == zxerr_ok) {
memcpy(output, saplingKeys.address, KEY_LENGTH);
memcpy(output + KEY_LENGTH, saplingKeys.ivk, KEY_LENGTH);
memcpy(output + 2*KEY_LENGTH, saplingKeys.ovk, KEY_LENGTH);
error = copyKeys(&saplingKeys, requestedKey, output, outputLen);
}

catch_cx_error:
Expand Down Expand Up @@ -159,14 +194,29 @@ zxerr_t crypto_sign(uint8_t *signature, uint16_t signatureMaxlen, const uint8_t
return error;
}

zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrResponseLen) {
if (buffer == NULL || addrResponseLen == NULL) {
zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requestedKey, uint16_t *cmdResponseLen) {
if (buffer == NULL || cmdResponseLen == NULL) {
return zxerr_unknown;
}

MEMZERO(buffer, bufferLen);
CHECK_ZXERR(crypto_generateSaplingKeys(buffer, bufferLen));
*addrResponseLen = 3 * KEY_LENGTH;
CHECK_ZXERR(crypto_generateSaplingKeys(buffer, bufferLen, requestedKey));
switch (requestedKey) {
case PublicAddress:
*cmdResponseLen = KEY_LENGTH;
break;

case ViewKeys:
*cmdResponseLen = 4 * KEY_LENGTH;
break;

case ProofGenerationKey:
*cmdResponseLen = 2 * KEY_LENGTH;
break;

default:
return zxerr_out_of_bounds;
}

return zxerr_ok;
}
5 changes: 2 additions & 3 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,8 +28,7 @@ extern "C" {

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];

zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrResponseLen);

zxerr_t crypto_fillKeys(uint8_t *buffer, uint16_t bufferLen, key_kind_e requestedKey, uint16_t *cmdResponseLen);
zxerr_t crypto_sign(uint8_t *signature, uint16_t signatureMaxlen, const uint8_t *message, uint16_t messageLen);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ parser_error_t convertKey(const uint8_t spendingKey[KEY_LENGTH], const uint8_t m
}

parser_error_t generate_key(const uint8_t expandedKey[KEY_LENGTH], constant_key_t keyType, uint8_t output[KEY_LENGTH]) {
if (keyType >= InvalidKey) {
if (keyType >= PointInvalidKey) {
return parser_value_out_of_range;
}
uint8_t tmpExpandedKey[KEY_LENGTH] = {0};
Expand Down
8 changes: 3 additions & 5 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef enum {
SpendingKeyGenerator,
ProofGenerationKeyGenerator,
PublicKeyGenerator,
InvalidKey,
PointInvalidKey,
} constant_key_t;

#define KEY_LENGTH 32
Expand All @@ -54,10 +54,8 @@ typedef struct {
ask_t ask;
ak_t ak;
};
union {
nsk_t nsk;
nk_t nk;
};
nsk_t nsk;
nk_t nk;

ivk_t ivk;
ovk_t ovk;
Expand Down
Loading
Loading