Skip to content

Commit

Permalink
IO: use standard io
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Mar 19, 2024
1 parent f254f37 commit 22277dc
Show file tree
Hide file tree
Showing 29 changed files with 215 additions and 239 deletions.
48 changes: 24 additions & 24 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,43 @@
#include <stdint.h>
#include <string.h>

size_t provide_pubkey(uint8_t* const io_buffer, cx_ecfp_public_key_t const* const pubkey) {
check_null(io_buffer);
int provide_pubkey(cx_ecfp_public_key_t const* const pubkey) {
check_null(pubkey);
size_t tx = 0;

// 100 = MAX(SIGNATURE_LEN)
uint8_t resp[1u + 100u] = {0};
size_t offset = 0;

// Application could be PIN-locked, and pubkey->W_len would then be 0,
// so throwing an error rather than returning an empty key
if (os_global_pin_is_validated() != BOLOS_UX_OK) {
THROW(EXC_SECURITY);
}
io_buffer[tx] = pubkey->W_len;
tx++;
memmove(io_buffer + tx, pubkey->W, pubkey->W_len);
tx += pubkey->W_len;
return finalize_successful_send(tx);

resp[offset] = pubkey->W_len;
offset++;
memmove(resp + offset, pubkey->W, pubkey->W_len);
offset += pubkey->W_len;

return io_send_response_pointer(resp, offset, SW_OK);
}

/**
* @brief Gets the version
*
* @return size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
static size_t handle_version(void) {
memcpy(G_io_apdu_buffer, &version, sizeof(version_t));
size_t tx = sizeof(version_t);
return finalize_successful_send(tx);
static int handle_version(void) {
return io_send_response_pointer((const uint8_t*) &version, sizeof(version_t), SW_OK);
}

/**
* @brief Gets the git commit
*
* @return size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
static size_t handle_git(void) {
static const char commit[] = COMMIT;
memcpy(G_io_apdu_buffer, commit, sizeof(commit));
size_t tx = sizeof(commit);
return finalize_successful_send(tx);
static int handle_git(void) {
return io_send_response_pointer((const uint8_t*) &COMMIT, sizeof(COMMIT), SW_OK);
}

#define CLA 0x80 /// The only APDU class that will be used
Expand All @@ -79,7 +79,7 @@ static size_t handle_git(void) {
#define P1_NEXT 0x01u /// Other packet
#define P1_LAST_MARKER 0x80u /// Last packet

size_t apdu_dispatcher(const command_t* cmd, volatile uint32_t* flags) {
int apdu_dispatcher(const command_t* cmd) {
check_null(cmd);

if (cmd->lc > MAX_APDU_SIZE) {
Expand Down Expand Up @@ -157,7 +157,7 @@ size_t apdu_dispatcher(const command_t* cmd, volatile uint32_t* flags) {
bool authorize = cmd->ins == INS_AUTHORIZE_BAKING;
bool prompt = (cmd->ins == INS_AUTHORIZE_BAKING) || (cmd->ins == INS_PROMPT_PUBLIC_KEY);

return handle_get_public_key(&buf, derivation_type, authorize, prompt, flags);
return handle_get_public_key(&buf, derivation_type, authorize, prompt);

case INS_DEAUTHORIZE:

Expand All @@ -173,15 +173,15 @@ size_t apdu_dispatcher(const command_t* cmd, volatile uint32_t* flags) {
READ_P2_DERIVATION_TYPE;
READ_DATA;

return handle_setup(&buf, derivation_type, flags);
return handle_setup(&buf, derivation_type);

case INS_RESET:

ASSERT_NO_P1;
ASSERT_NO_P2;
READ_DATA;

return handle_reset(&buf, flags);
return handle_reset(&buf);

case INS_QUERY_AUTH_KEY:

Expand Down Expand Up @@ -236,7 +236,7 @@ size_t apdu_dispatcher(const command_t* cmd, volatile uint32_t* flags) {
bool with_hash = cmd->ins == INS_SIGN_WITH_HASH;
bool last = (cmd->p1 & P1_LAST_MARKER) != 0;

return handle_sign(&buf, last, with_hash, flags);
return handle_sign(&buf, last, with_hash);

default:
THROW(EXC_WRONG_PARAM);
Expand Down
46 changes: 8 additions & 38 deletions src/apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "keys.h"
#include "parser.h"
#include "types.h"
#include "io.h"
#include "ui.h"

#include "os.h"
Expand Down Expand Up @@ -58,47 +59,17 @@
* @brief Dispatch APDU command received to the right handler
*
* @param cmd: structured APDU command (CLA, INS, P1, P2, Lc, Command data).
* @param flags: io flags
* @param size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
size_t apdu_dispatcher(const command_t* cmd, volatile uint32_t* flags);
int apdu_dispatcher(const command_t* cmd);

/**
* @brief Tags as successful apdu response
*
* @param offset: current offset of the apdu response
* @return size_t: updated offset of the apdu response
*/
static inline size_t finalize_successful_send(size_t offset) {
size_t tx = offset;
G_io_apdu_buffer[tx] = 0x90;
tx++;
G_io_apdu_buffer[tx] = 0x00;
tx++;
return tx;
}

/**
* @brief Sends the apdu response asynchronously
*
* @param tx: current offset of the apdu response
*/
static inline void delayed_send(size_t tx) {
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, tx);
}

/**
* @brief Sends asynchronously a reject exception
* @brief Sends a reject exception
*
* @return true
*/
static inline bool delay_reject(void) {
size_t tx = 0;
G_io_apdu_buffer[tx] = EXC_REJECT >> 8;
tx++;
G_io_apdu_buffer[tx] = EXC_REJECT & 0xFFu;
tx++;
delayed_send(tx);
static inline bool reject(void) {
io_send_sw(EXC_REJECT);
return true;
}

Expand All @@ -107,8 +78,7 @@ static inline bool delay_reject(void) {
*
* Expects validated pin
*
* @param io_buffer: apdu response buffer
* @param pubkey: public key
* @return size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
size_t provide_pubkey(uint8_t* const io_buffer, cx_ecfp_public_key_t const* const pubkey);
int provide_pubkey(cx_ecfp_public_key_t const* const pubkey);
11 changes: 6 additions & 5 deletions src/apdu_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static inline size_t hmac(uint8_t *const out,
out_size);
}

size_t handle_hmac(buffer_t *cdata, derivation_type_t derivation_type) {
int handle_hmac(buffer_t *cdata, derivation_type_t derivation_type) {
check_null(cdata);

memset(&G, 0, sizeof(G));
Expand All @@ -117,8 +117,9 @@ size_t handle_hmac(buffer_t *cdata, derivation_type_t derivation_type) {
bip32_path,
derivation_type);

size_t tx = 0;
memcpy(G_io_apdu_buffer, G.hmac, hmac_size);
tx += hmac_size;
return finalize_successful_send(tx);
uint8_t resp[CX_SHA256_SIZE] = {0};

memcpy(resp, G.hmac, hmac_size);

return io_send_response_pointer(resp, hmac_size, SW_OK);
}
4 changes: 2 additions & 2 deletions src/apdu_hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
*
* @param cdata: data containing the message and the BIP32 path of the key
* @param derivation_type: derivation_type of the key
* @return size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
size_t handle_hmac(buffer_t *cdata, derivation_type_t derivation_type);
int handle_hmac(buffer_t *cdata, derivation_type_t derivation_type);
20 changes: 8 additions & 12 deletions src/apdu_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static bool pubkey_ok(void) {
generate_public_key(&public_key,
global.path_with_curve.derivation_type,
&global.path_with_curve.bip32_path);
delayed_send(provide_pubkey(G_io_apdu_buffer, &public_key));
provide_pubkey(&public_key);
return true;
}

Expand All @@ -56,15 +56,13 @@ static bool pubkey_ok(void) {
*/
static bool baking_ok(void) {
authorize_baking(global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path);
pubkey_ok();
return true;
return pubkey_ok();
}

size_t handle_get_public_key(buffer_t *cdata,
derivation_type_t derivation_type,
bool authorize,
bool prompt,
volatile uint32_t *flags) {
int handle_get_public_key(buffer_t *cdata,
derivation_type_t derivation_type,
bool authorize,
bool prompt) {
check_null(cdata);

global.path_with_curve.derivation_type = derivation_type;
Expand All @@ -84,7 +82,7 @@ size_t handle_get_public_key(buffer_t *cdata,
&global.path_with_curve.bip32_path);

if (!prompt) {
return provide_pubkey(G_io_apdu_buffer, &public_key);
return provide_pubkey(&public_key);
} else {
// INS_PROMPT_PUBLIC_KEY || INS_AUTHORIZE_BAKING
ui_callback_t cb;
Expand All @@ -97,8 +95,6 @@ size_t handle_get_public_key(buffer_t *cdata,
cb = pubkey_ok;
bake = false;
}
prompt_pubkey(bake, cb, delay_reject);
*flags = IO_ASYNCH_REPLY;
return 0;
return prompt_pubkey(bake, cb, reject);
}
}
12 changes: 5 additions & 7 deletions src/apdu_pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
* @param derivation_type: derivation_type of the key
* @param authorize: whether to authorize the address or not
* @param prompt: whether to display address on screen or not
* @param flags: io flags
* @return size_t: offset of the apdu response
* @return int: zero or positive integer if success, negative integer otherwise.
*/
size_t handle_get_public_key(buffer_t *cdata,
derivation_type_t derivation_type,
bool authorize,
bool prompt,
volatile uint32_t *flags);
int handle_get_public_key(buffer_t *cdata,
derivation_type_t derivation_type,
bool authorize,
bool prompt);
Loading

0 comments on commit 22277dc

Please sign in to comment.