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

Fix/LDG-599-enable-flex-stax-compilation #20

Merged
merged 2 commits into from
Dec 4, 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
161 changes: 81 additions & 80 deletions src/common/mainHelpers.c → src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "mainHelpers.h"

#include "responseCodes.h"
#include "handler.h"

keyDerivationPath_t path;
tx_state_t global_tx_state;
Expand All @@ -43,11 +44,11 @@
#define OFFSET_LC 0x04
#define OFFSET_CDATA 0x05

void *global_state;

// Main entry of application that listens for APDU commands that will be received from the
// computer. The APDU commands control what flow is activated, i.e. which control flow is initiated.
void concordium_main(
void (*handler)(uint8_t, uint8_t *, uint8_t, uint8_t, uint8_t, volatile unsigned int *, bool),
void *global_state) {
void app_main() {
volatile unsigned int rx = 0;
volatile unsigned int tx = 0;
volatile unsigned int flags = 0;
Expand Down Expand Up @@ -134,80 +135,80 @@
// https://github.com/LedgerHQ/ledger-sample-apps/blob/master/blue-app-helloworld/src/main.c.
// unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B];

void io_seproxyhal_display(const bagl_element_t *element) {
io_seproxyhal_display_default((bagl_element_t *) element);
}

unsigned char io_event(__attribute__((unused)) unsigned char channel) {
// can't have more than one tag in the reply, not supported yet.
switch (G_io_seproxyhal_spi_buffer[0]) {
case SEPROXYHAL_TAG_FINGER_EVENT:
UX_FINGER_EVENT(G_io_seproxyhal_spi_buffer);
break;

case SEPROXYHAL_TAG_BUTTON_PUSH_EVENT:
UX_BUTTON_PUSH_EVENT(G_io_seproxyhal_spi_buffer);
break;

case SEPROXYHAL_TAG_STATUS_EVENT:
if (G_io_apdu_media == IO_APDU_MEDIA_USB_HID &&
!(U4BE(G_io_seproxyhal_spi_buffer, 3) &
SEPROXYHAL_TAG_STATUS_EVENT_FLAG_USB_POWERED)) {
THROW(EXCEPTION_IO_RESET);
}
UX_DEFAULT_EVENT();
break;

case SEPROXYHAL_TAG_DISPLAY_PROCESSED_EVENT:
UX_DISPLAYED_EVENT({});
break;

case SEPROXYHAL_TAG_TICKER_EVENT:
UX_TICKER_EVENT(G_io_seproxyhal_spi_buffer, {});
break;

default:
UX_DEFAULT_EVENT();
break;
}

// close the event if not done previously (by a display or whatever)
if (!io_seproxyhal_spi_is_status_sent()) {
io_seproxyhal_general_status();
}

// command has been processed, DO NOT reset the current APDU transport
return 1;
}

unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) {
switch (channel & ~(IO_FLAGS)) {
case CHANNEL_KEYBOARD:
break;
// multiplexed io exchange over a SPI channel and TLV encapsulated protocol
case CHANNEL_SPI:
if (tx_len) {
io_seproxyhal_spi_send(G_io_apdu_buffer, tx_len);
if (channel & IO_RESET_AFTER_REPLIED) {
reset();
}
return 0; // nothing received from the master so far (it's a tx transaction)
} else {
return io_seproxyhal_spi_recv(G_io_apdu_buffer, sizeof(G_io_apdu_buffer), 0);
}
default:
THROW(INVALID_PARAMETER);
}
return 0;
}

void app_exit(void) {
BEGIN_TRY_L(exit) {
TRY_L(exit) {
os_sched_exit(-1);
}
FINALLY_L(exit) {
}
}
END_TRY_L(exit);
}
// void io_seproxyhal_display(const bagl_element_t *element) {
// io_seproxyhal_display_default((bagl_element_t *) element);
// }
Comment on lines +138 to +140

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// unsigned char io_event(__attribute__((unused)) unsigned char channel) {
// // can't have more than one tag in the reply, not supported yet.
// switch (G_io_seproxyhal_spi_buffer[0]) {
// case SEPROXYHAL_TAG_FINGER_EVENT:
// UX_FINGER_EVENT(G_io_seproxyhal_spi_buffer);
// break;
Comment on lines +142 to +147

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// case SEPROXYHAL_TAG_BUTTON_PUSH_EVENT:
// UX_BUTTON_PUSH_EVENT(G_io_seproxyhal_spi_buffer);
// break;
Comment on lines +149 to +151

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// case SEPROXYHAL_TAG_STATUS_EVENT:
// if (G_io_apdu_media == IO_APDU_MEDIA_USB_HID &&
// !(U4BE(G_io_seproxyhal_spi_buffer, 3) &
// SEPROXYHAL_TAG_STATUS_EVENT_FLAG_USB_POWERED)) {
// THROW(EXCEPTION_IO_RESET);
// }
// UX_DEFAULT_EVENT();
// break;
Comment on lines +153 to +160

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// case SEPROXYHAL_TAG_DISPLAY_PROCESSED_EVENT:
// UX_DISPLAYED_EVENT({});
// break;
Comment on lines +162 to +164

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// case SEPROXYHAL_TAG_TICKER_EVENT:
// UX_TICKER_EVENT(G_io_seproxyhal_spi_buffer, {});
// break;
Comment on lines +166 to +168

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// default:
// UX_DEFAULT_EVENT();
// break;
// }
Comment on lines +170 to +173

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// // close the event if not done previously (by a display or whatever)
// if (!io_seproxyhal_spi_is_status_sent()) {
// io_seproxyhal_general_status();
// }
Comment on lines +175 to +178

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// // command has been processed, DO NOT reset the current APDU transport
// return 1;
// }
Comment on lines +180 to +182

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) {
// switch (channel & ~(IO_FLAGS)) {
// case CHANNEL_KEYBOARD:
// break;
// // multiplexed io exchange over a SPI channel and TLV encapsulated protocol
// case CHANNEL_SPI:
// if (tx_len) {
// io_seproxyhal_spi_send(G_io_apdu_buffer, tx_len);
// if (channel & IO_RESET_AFTER_REPLIED) {
// reset();
// }
// return 0; // nothing received from the master so far (it's a tx transaction)
// } else {
// return io_seproxyhal_spi_recv(G_io_apdu_buffer, sizeof(G_io_apdu_buffer), 0);
// }
// default:
// THROW(INVALID_PARAMETER);
// }
// return 0;
// }
Comment on lines +184 to +203

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// void app_exit(void) {
// BEGIN_TRY_L(exit) {
// TRY_L(exit) {
// os_sched_exit(-1);
// }
// FINALLY_L(exit) {
// }
// }
// END_TRY_L(exit);
// }
Comment on lines +205 to +214

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
36 changes: 5 additions & 31 deletions src/common/getPublicKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,14 @@
#include "responseCodes.h"
#include "util.h"
#include "ux.h"
#include "display.h"

static keyDerivationPath_t *keyPath = &path;
static exportPublicKeyContext_t *ctx = &global.exportPublicKeyContext;
static tx_state_t *tx_state = &global_tx_state;
static const uint32_t HARDENED_OFFSET = 0x80000000;

void sendPublicKey(bool compare);

UX_STEP_VALID(ux_decline_step, pb, sendUserRejection(), {&C_icon_crossmark, "Decline"});

// UI definitions for the approval of the generation of a public-key. This prompts the user to
// accept that a public-key will be generated and returned to the computer.
UX_STEP_VALID(ux_generate_public_flow_0_step,
pnn,
sendPublicKey(true),
{&C_icon_validate_14, "Public key", (char *) global.exportPublicKeyContext.display});
UX_FLOW(ux_generate_public_flow, &ux_generate_public_flow_0_step, &ux_decline_step, FLOW_LOOP);

// UI definitions for comparison of public-key on the device
// with the public-key that the caller received.
UX_STEP_NOCB(ux_sign_compare_public_key_0_step,
bnnn_paging,
{.title = "Compare", .text = (char *) global.exportPublicKeyContext.publicKey});
UX_STEP_CB(ux_compare_accept_step, pb, ui_menu_main(), {&C_icon_validate_14, "Accept"});
UX_STEP_CB(ux_compare_decline_step, pb, ui_menu_main(), {&C_icon_crossmark, "Decline"});
UX_FLOW(ux_sign_compare_public_key,
&ux_sign_compare_public_key_0_step,
&ux_compare_accept_step,
&ux_compare_decline_step);
instructionContext global;

/**
* Derive the public-key for the given path, and then write it to
Expand Down Expand Up @@ -66,7 +45,8 @@ void sendPublicKey(bool compare) {
toPaginatedHex(publicKey, sizeof(publicKey), ctx->publicKey, sizeof(ctx->publicKey));
// Allow for receiving a new instruction even while comparing public keys.
tx_state->currentInstruction = -1;
ux_flow_init(0, ux_sign_compare_public_key, NULL);
uiComparePubkey();

} else {
sendSuccess(tx);
}
Expand Down Expand Up @@ -127,12 +107,6 @@ void handleGetPublicKey(uint8_t *cdata, uint8_t p1, uint8_t p2, volatile unsigne
accountIndex);
}
}

// Display the UI for the public-key flow, where the user can validate that the
// public-key being generated is the expected one.
ux_flow_init(0, ux_generate_public_flow, NULL);

// Tell the main process to wait for a button press.
*flags |= IO_ASYNCH_REPLY;
uiGeneratePubkey(flags);
}
}
9 changes: 5 additions & 4 deletions src/common/getPublicKey.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef _CONCORDIUM_APP_GET_PUBLIC_KEY_H_
#define _CONCORDIUM_APP_GET_PUBLIC_KEY_H_
#pragma once

#include <stdbool.h>
#include <stdint.h>

/**
* Handles the derivation and export of account and governance public keys.
Expand All @@ -10,11 +12,10 @@
* export the signature on the public-key signed with the corresponding private-key.
*/
void handleGetPublicKey(uint8_t *cdata, uint8_t p1, uint8_t p2, volatile unsigned int *flags);
void sendPublicKey(bool compare);

typedef struct {
uint8_t display[21];
char publicKey[68];
bool signPublicKey;
} exportPublicKeyContext_t;

#endif
81 changes: 81 additions & 0 deletions src/common/handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

#ifndef _HANDLER_C_
#define _HANDLER_C_

#include <stdbool.h>
#include <string.h>

#include "responseCodes.h"
#include "globals.h"
#include "handler.h"
#include "getAppName.h"

void handler(uint8_t INS,
uint8_t *cdata,
uint8_t p1,
uint8_t p2,
uint8_t lc,
volatile unsigned int *flags,
bool isInitialCall) {
switch (INS) {
case INS_GET_PUBLIC_KEY:
handleGetPublicKey(cdata, p1, p2, flags);
break;
case INS_VERIFY_ADDRESS:
handleVerifyAddress(cdata, flags);
break;
case INS_SIGN_TRANSFER:
handleSignTransfer(cdata, flags);
break;
case INS_SIGN_TRANSFER_WITH_MEMO:
handleSignTransferWithMemo(cdata, p1, lc, flags, isInitialCall);
break;
case INS_SIGN_TRANSFER_WITH_SCHEDULE:
handleSignTransferWithSchedule(cdata, p1, flags, isInitialCall);
break;
case INS_SIGN_TRANSFER_WITH_SCHEDULE_AND_MEMO:
handleSignTransferWithScheduleAndMemo(cdata, p1, lc, flags, isInitialCall);
break;
case INS_CREDENTIAL_DEPLOYMENT:
handleSignCredentialDeployment(cdata, p1, p2, flags, isInitialCall);
break;
case INS_EXPORT_PRIVATE_KEY:
handleExportPrivateKey(cdata, p1, p2, flags);
break;
case INS_TRANSFER_TO_ENCRYPTED:
handleSignTransferToEncrypted(cdata, flags);
break;
case INS_ENCRYPTED_AMOUNT_TRANSFER:
handleSignEncryptedAmountTransfer(cdata, p1, lc, flags, isInitialCall);
break;
case INS_ENCRYPTED_AMOUNT_TRANSFER_WITH_MEMO:
handleSignEncryptedAmountTransferWithMemo(cdata, p1, lc, flags, isInitialCall);
break;
case INS_TRANSFER_TO_PUBLIC:
handleSignTransferToPublic(cdata, p1, lc, flags, isInitialCall);
break;
case INS_REGISTER_DATA:
handleSignRegisterData(cdata, p1, lc, flags, isInitialCall);
break;
case INS_PUBLIC_INFO_FOR_IP:
handleSignPublicInformationForIp(cdata, p1, flags, isInitialCall);
break;
case INS_CONFIGURE_BAKER:
handleSignConfigureBaker(cdata, p1, lc, flags, isInitialCall);
break;
case INS_CONFIGURE_DELEGATION:
handleSignConfigureDelegation(cdata, lc, flags);
break;
case INS_SIGN_UPDATE_CREDENTIAL:
handleSignUpdateCredential(cdata, p1, p2, flags, isInitialCall);
break;
case INS_GET_APP_NAME:
handleGetAppName(flags);
break;
default:
THROW(ERROR_INVALID_INSTRUCTION);
break;
}
}

#endif
Loading
Loading