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

src: Use nbgl sync API #120

Draft
wants to merge 1 commit into
base: xch/nbgl-lns
Choose a base branch
from
Draft
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
22 changes: 7 additions & 15 deletions src/apdu/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
#include "ledger_assert.h"

#include "dispatcher.h"
#include "../constants.h"
#include "../globals.h"
#include "../types.h"
#include "../sw.h"
#include "../handler/get_version.h"
#include "../handler/get_app_name.h"
#include "../handler/get_public_key.h"
#include "../handler/sign_tx.h"
#include "constants.h"
#include "sw.h"
#include "handler/get_version.h"
#include "handler/get_app_name.h"
#include "handler/get_public_key.h"
#include "handler/sign_tx.h"

int apdu_dispatcher(const command_t *cmd) {
LEDGER_ASSERT(cmd != NULL, "NULL cmd");
Expand Down Expand Up @@ -69,12 +67,6 @@ int apdu_dispatcher(const command_t *cmd) {

return handler_get_public_key(&buf, (bool) cmd->p1);
case SIGN_TX:
if ((cmd->p1 == P1_START && cmd->p2 != P2_MORE) || //
cmd->p1 > P1_MAX || //
(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);
}
Expand All @@ -83,7 +75,7 @@ int apdu_dispatcher(const command_t *cmd) {
buf.size = cmd->lc;
buf.offset = 0;

return handler_sign_tx(&buf, cmd->p1, (bool) (cmd->p2 & P2_MORE));
return handler_sign_tx(&buf, cmd->p1, cmd->p2);
default:
return io_send_sw(SW_INS_NOT_SUPPORTED);
}
Expand Down
19 changes: 0 additions & 19 deletions src/apdu/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,6 @@

#include "parser.h"

#include "../types.h"

/**
* Parameter 2 for last APDU to receive.
*/
#define P2_LAST 0x00
/**
* Parameter 2 for more APDU to receive.
*/
#define P2_MORE 0x80
/**
* Parameter 1 for first APDU number.
*/
#define P1_START 0x00
/**
* Parameter 1 for maximum APDU number.
*/
#define P1_MAX 0x03

/**
* Dispatch APDU command received to the right handler.
*
Expand Down
6 changes: 0 additions & 6 deletions src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
#include "os.h"
#include "ux.h"

#include "types.h"
#include "globals.h"
#include "io.h"
#include "sw.h"
#include "ui/menu.h"
#include "apdu/dispatcher.h"

global_ctx_t G_context;

const internal_storage_t N_storage_real;

/**
Expand All @@ -45,9 +42,6 @@ void app_main() {

ui_menu_main();

// Reset context
explicit_bzero(&G_context, sizeof(G_context));

// Initialize the NVM data if required
if (N_storage.initialized != 0x01) {
internal_storage_t storage;
Expand Down
14 changes: 14 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
*/
#define CLA 0xE0

/**
* Enumeration with expected INS of APDU commands.
*/
typedef enum {
GET_VERSION = 0x03, /// version of the application
GET_APP_NAME = 0x04, /// name of the application
GET_PUBLIC_KEY = 0x05, /// public key of corresponding BIP32 path
SIGN_TX = 0x06 /// sign transaction with BIP32 path
} command_e;

/**
* Length of APPNAME variable in the Makefile.
*/
Expand Down Expand Up @@ -34,3 +44,7 @@
* Exponent used to convert mBOL to BOL unit (N BOL = N * 10^3 mBOL).
*/
#define EXPONENT_SMALLEST_UNIT 3

#define PUBKEY_LEN 65
#define CHAINCODE_LEN 32
#define ADDRESS_LEN 20
6 changes: 0 additions & 6 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "ux.h"

#include "io.h"
#include "types.h"
#include "constants.h"

/**
Expand All @@ -23,11 +22,6 @@ extern ux_state_t G_ux;
*/
extern bolos_ux_params_t G_ux_params;

/**
* Global context for user requests.
*/
extern global_ctx_t G_context;

/**
* Global structure for NVM data storage.
*/
Expand Down
6 changes: 2 additions & 4 deletions src/handler/get_app_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
#include "buffer.h"

#include "get_app_name.h"
#include "../constants.h"
#include "../globals.h"
#include "../sw.h"
#include "../types.h"
#include "constants.h"
#include "sw.h"

int handler_get_app_name() {
_Static_assert(APPNAME_LEN < MAX_APPNAME_LEN, "APPNAME must be at most 64 characters!");
Expand Down
61 changes: 44 additions & 17 deletions src/handler/get_public_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,65 @@
#include "io.h"
#include "buffer.h"
#include "crypto_helpers.h"
#include "bip32.h"

#include "get_public_key.h"
#include "../globals.h"
#include "../types.h"
#include "../sw.h"
#include "../ui/display.h"
#include "../helper/send_response.h"
#include "sw.h"
#include "ui/display.h"

int handler_get_public_key(buffer_t *cdata, bool display) {
explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_ADDRESS;
G_context.state = STATE_NONE;
static void send_response_pubkey(const uint8_t raw_public_key[static PUBKEY_LEN], const uint8_t chain_code[static CHAINCODE_LEN]) {
uint8_t pubkey_len[1] = {PUBKEY_LEN};
uint8_t chain_code_len[1] = {CHAINCODE_LEN};

buffer_t buffers[4] = {
{.ptr = pubkey_len, .size = sizeof(pubkey_len), .offset = 0},
{.ptr = raw_public_key, .size = PUBKEY_LEN, .offset = 0},
{.ptr = chain_code_len, .size = sizeof(chain_code_len), .offset = 0},
{.ptr = chain_code, .size = CHAINCODE_LEN, .offset = 0},
};

io_send_response_buffers(buffers, 4, SW_OK);
}

if (!buffer_read_u8(cdata, &G_context.bip32_path_len) ||
!buffer_read_bip32_path(cdata, G_context.bip32_path, (size_t) G_context.bip32_path_len)) {
int handler_get_public_key(buffer_t *cdata, bool display) {
uint8_t bip32_path_len;
uint32_t bip32_path[MAX_BIP32_PATH] = {0};
if (!buffer_read_u8(cdata, &bip32_path_len) ||
!buffer_read_bip32_path(cdata, bip32_path, (size_t) bip32_path_len)) {
return io_send_sw(SW_WRONG_DATA_LENGTH);
}


uint8_t raw_public_key[PUBKEY_LEN] = {0};
uint8_t chain_code[CHAINCODE_LEN] = {0};
cx_err_t error = bip32_derive_get_pubkey_256(CX_CURVE_256K1,
G_context.bip32_path,
G_context.bip32_path_len,
G_context.pk_info.raw_public_key,
G_context.pk_info.chain_code,
bip32_path,
bip32_path_len,
raw_public_key,
chain_code,
CX_SHA512);

if (error != CX_OK) {
return io_send_sw(error);
}

ui_ret_e ret = UI_RET_APPROVED;
if (display) {
ret = ui_display_address(raw_public_key);
}

if (ret == UI_RET_APPROVED) {
send_response_pubkey(raw_public_key, chain_code);
} else if (ret == UI_RET_REJECTED) {
io_send_sw(SW_DENY);
} else {
io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
}


if (display) {
return ui_display_address();
ui_display_address_status(ret);
}

return helper_send_response_pubkey();
return 0;
}
2 changes: 0 additions & 2 deletions src/handler/get_public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "buffer.h"

#include "../types.h"

/**
* Handler for GET_PUBLIC_KEY command. If successfully parse BIP32 path,
* derive public key/chain code and send APDU response.
Expand Down
6 changes: 2 additions & 4 deletions src/handler/get_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
#include "buffer.h"

#include "get_version.h"
#include "../globals.h"
#include "../constants.h"
#include "../sw.h"
#include "../types.h"
#include "constants.h"
#include "sw.h"

int handler_get_version() {
_Static_assert(APPVERSION_LEN == 3, "Length of (MAJOR || MINOR || PATCH) must be 3!");
Expand Down
Loading
Loading