Skip to content

Commit

Permalink
Parser: allowing all handled operations
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Feb 27, 2024
1 parent 193a5dc commit 67bccb8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
29 changes: 5 additions & 24 deletions src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,6 @@ static bool sign_reject(void) {
return true; // Return to idle
}

static bool is_operation_allowed(enum operation_tag tag) {
switch (tag) {
case OPERATION_TAG_DELEGATION:
case OPERATION_TAG_REVEAL:
return true;
default:
return false;
}
}

static bool parse_allowed_operations(struct parsed_operation_group *const out,
uint8_t const *const in,
size_t const in_size,
bip32_path_with_curve_t const *const key) {
return parse_operations(out,
in,
in_size,
key->derivation_type,
&key->bip32_path,
&is_operation_allowed);
}

size_t baking_sign_complete(bool const send_hash, volatile uint32_t *flags) {
switch (G.magic_byte) {
case MAGIC_BYTE_BLOCK:
Expand Down Expand Up @@ -213,8 +191,11 @@ static size_t handle_apdu(bool const enable_hashing,
G.magic_byte = get_magic_byte_or_throw(buff, buff_size);
if (G.magic_byte == MAGIC_BYTE_UNSAFE_OP) {
// Parse the operation. It will be verified in `baking_sign_complete`.
G.maybe_ops.is_valid =
parse_allowed_operations(&G.maybe_ops.v, buff, buff_size, &global.path_with_curve);
G.maybe_ops.is_valid = parse_operations(&G.maybe_ops.v,
buff,
buff_size,
global.path_with_curve.derivation_type,
&global.path_with_curve.bip32_path);
} else {
// This should be a baking operation so parse it.
if (!parse_baking_data(&G.parsed_baking_data, buff, buff_size)) PARSE_ERROR();
Expand Down
20 changes: 5 additions & 15 deletions src/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ bool parse_operations_final(struct parse_state *const state,

static inline bool parse_byte(uint8_t byte,
struct parse_state *const state,
struct parsed_operation_group *const out,
is_operation_allowed_t is_operation_allowed) {
struct parsed_operation_group *const out) {
// OP_STEP finishes the current state transition, setting the state, and introduces the next state.
// For linear chains of states, this keeps the code structurally similar to equivalent imperative
// parsing code.
Expand Down Expand Up @@ -215,8 +214,6 @@ static inline bool parse_byte(uint8_t byte,

state->tag = NEXT_BYTE;

if (!is_operation_allowed(state->tag)) PARSE_ERROR();

OP_STEP

// Parse 'source'
Expand Down Expand Up @@ -344,15 +341,14 @@ static void parse_operations_throws_parse_error(struct parsed_operation_group *c
void const *const data,
size_t length,
derivation_type_t derivation_type,
bip32_path_t const *const bip32_path,
is_operation_allowed_t is_operation_allowed) {
bip32_path_t const *const bip32_path) {
size_t ix = 0;

parse_operations_init(out, derivation_type, bip32_path, &G.parse_state);

while (ix < length) {
uint8_t byte = ((uint8_t *) data)[ix];
parse_byte(byte, &G.parse_state, out, is_operation_allowed);
parse_byte(byte, &G.parse_state, out);
PRINTF("Byte: %x - Next op_step state: %d\n", byte, G.parse_state.op_step);
ix++;
}
Expand All @@ -364,16 +360,10 @@ bool parse_operations(struct parsed_operation_group *const out,
uint8_t const *const data,
size_t length,
derivation_type_t derivation_type,
bip32_path_t const *const bip32_path,
is_operation_allowed_t is_operation_allowed) {
bip32_path_t const *const bip32_path) {
BEGIN_TRY {
TRY {
parse_operations_throws_parse_error(out,
data,
length,
derivation_type,
bip32_path,
is_operation_allowed);
parse_operations_throws_parse_error(out, data, length, derivation_type, bip32_path);
}
CATCH(EXC_PARSE_ERROR) {
return false;
Expand Down
5 changes: 1 addition & 4 deletions src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "cx.h"
#include "types.h"

typedef bool (*is_operation_allowed_t)(enum operation_tag);

// Wire format that gets parsed into `signature_type`.
typedef struct {
uint8_t v;
Expand Down Expand Up @@ -71,8 +69,7 @@ bool parse_operations(struct parsed_operation_group *const out,
uint8_t const *const data,
size_t length,
derivation_type_t curve,
bip32_path_t const *const bip32_path,
is_operation_allowed_t is_operation_allowed);
bip32_path_t const *const bip32_path);

bool parse_operations_final(struct parse_state *const state,
struct parsed_operation_group *const out);

0 comments on commit 67bccb8

Please sign in to comment.