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

Swap SPL Tokens #231

Draft
wants to merge 5 commits into
base: develop
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
8 changes: 8 additions & 0 deletions src/apdu_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ static uint16_t check_instruction(uint8_t instruction, uint8_t subcommand) {
check_current_state = TRANSACTION_RECEIVED;
check_subcommand_context = true;
break;
case GET_CHALLENGE:
check_current_state = SIGNATURE_CHECKED;
check_subcommand_context = true;
break;
case SEND_TRUSTED_NAME_DESCRIPTOR:
check_current_state = CHALLENGE_SENT;
check_subcommand_context = true;
break;
case CHECK_PAYOUT_ADDRESS:
case CHECK_ASSET_IN_AND_DISPLAY:
case CHECK_ASSET_IN_NO_DISPLAY:
Expand Down
8 changes: 8 additions & 0 deletions src/command_dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "start_signing_transaction.h"
#include "check_addresses_and_amounts.h"
#include "prompt_ui_display.h"
#include "get_challenge_handler.h"
#include "trusted_name_descriptor_handler.h"

#include "io.h"

Expand Down Expand Up @@ -42,6 +44,12 @@ int dispatch_command(const command_t *cmd) {
case CHECK_TRANSACTION_SIGNATURE_COMMAND:
ret = check_tx_signature(cmd);
break;
case GET_CHALLENGE:
ret = get_challenge_handler();
break;
case SEND_TRUSTED_NAME_DESCRIPTOR:
ret = trusted_name_descriptor_handler(cmd);
break;
case CHECK_PAYOUT_ADDRESS:
case CHECK_ASSET_IN_AND_DISPLAY:
case CHECK_ASSET_IN_NO_DISPLAY:
Expand Down
2 changes: 2 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ typedef enum {
CHECK_PARTNER_COMMAND = 0x05,
PROCESS_TRANSACTION_RESPONSE_COMMAND = 0x06,
CHECK_TRANSACTION_SIGNATURE_COMMAND = 0x07,
GET_CHALLENGE = 0x10,
SEND_TRUSTED_NAME_DESCRIPTOR = 0x11,
CHECK_PAYOUT_ADDRESS = 0x08,
CHECK_ASSET_IN_LEGACY_AND_DISPLAY = 0x08, // Same ID as CHECK_PAYOUT_ADDRESS, deprecated
CHECK_ASSET_IN_AND_DISPLAY = 0x0B, // Do note the 0x0B
Expand Down
55 changes: 55 additions & 0 deletions src/get_challenge_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cx.h>
#include "get_challenge_handler.h"
#include "swap_errors.h"
#include "buffer.h"
#include "io.h"
#include "globals.h"

static uint32_t challenge;

#define LAST_BYTE_MASK 0x000000FF

/**
* Generate a new challenge from the Random Number Generator
*/
void roll_challenge(void) {
#ifdef HAVE_TRUSTED_NAME_TEST
challenge = 0xdeadbeef;
#else
challenge = cx_rng_u32();
#endif
}

/**
* Get the current challenge
*
* @return challenge
*/
uint32_t get_challenge(void) {
return challenge;
}

/**
* Send back the current challenge
*/
int get_challenge_handler(void) {
PRINTF("New challenge -> %u\n", challenge);
uint8_t output_buffer[4];
output_buffer[0] = (uint8_t) ((challenge >> 24) & LAST_BYTE_MASK);
output_buffer[1] = (uint8_t) ((challenge >> 16) & LAST_BYTE_MASK);
output_buffer[2] = (uint8_t) ((challenge >> 8) & LAST_BYTE_MASK);
output_buffer[3] = (uint8_t) (challenge & LAST_BYTE_MASK);

buffer_t output;
output.ptr = output_buffer;
output.size = 4;
output.offset = 0;

if (io_send_response_buffers(&output, 1, SUCCESS) < 0) {
return -1;
}

G_swap_ctx.state = CHALLENGE_SENT;

return 0;
}
5 changes: 5 additions & 0 deletions src/get_challenge_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

void roll_challenge(void);
uint32_t get_challenge(void);
int get_challenge_handler(void);
4 changes: 4 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swap_errors.h"
#include "apdu_parser.h"
#include "sign_result.h"
#include "get_challenge_handler.h"

#include "usbd_core.h"

Expand Down Expand Up @@ -155,6 +156,9 @@ __attribute__((section(".boot"))) int main(__attribute__((unused)) int arg0) {
BLE_power(1, NULL);
#endif

// to prevent it from having a fixed value at boot
roll_challenge();

app_main();
}
CATCH(SWO_SEC_APP_14) {
Expand Down
2 changes: 2 additions & 0 deletions src/states.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ typedef enum {
PROVIDER_CHECKED,
TRANSACTION_RECEIVED,
SIGNATURE_CHECKED,
CHALLENGE_SENT,
TRUSTED_NAME_DESCRIPTOR_RECEIVED,
PAYOUT_ADDRESS_CHECKED,
ALL_ADDRESSES_CHECKED,
WAITING_USER_VALIDATION,
Expand Down
Loading
Loading