Skip to content

Commit

Permalink
Use magic byte instead of tag to differentiate between pre-attestatio…
Browse files Browse the repository at this point in the history
…n and attestation
  • Loading branch information
ajinkyaraj-23 committed May 28, 2024
1 parent 15e21a2 commit 750dd11
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
15 changes: 7 additions & 8 deletions src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,6 @@ static bool sign_reject(void) {
return true;
}

/// Magic byte values
/// See: https://tezos.gitlab.io/user/key-management.html#signer-requests
#define MAGIC_BYTE_UNSAFE_OP 0x03u /// magic byte of an operation
#define MAGIC_BYTE_BLOCK 0x11u /// magic byte of a block
#define MAGIC_BYTE_PREATTESTATION 0x12u /// magic byte of a pre-attestation
#define MAGIC_BYTE_ATTESTATION 0x13u /// magic byte of an attestation

/**
* @brief Carries out final checks before signing
*
Expand Down Expand Up @@ -303,11 +296,17 @@ int handle_sign(buffer_t *cdata, const bool last, const bool with_hash) {
TZ_ASSERT(G.packet_index == 1u, EXC_PARSE_ERROR);

TZ_ASSERT(buffer_read_u8(cdata, &G.magic_byte), EXC_PARSE_ERROR);
bool is_attestation = false;

switch (G.magic_byte) {
case MAGIC_BYTE_PREATTESTATION:
is_attestation = false;
TZ_ASSERT(parse_consensus_operation(cdata, &G.parsed_baking_data, is_attestation),
EXC_PARSE_ERROR);
case MAGIC_BYTE_ATTESTATION:
TZ_ASSERT(parse_consensus_operation(cdata, &G.parsed_baking_data), EXC_PARSE_ERROR);
is_attestation = true;
TZ_ASSERT(parse_consensus_operation(cdata, &G.parsed_baking_data, is_attestation),
EXC_PARSE_ERROR);
break;
case MAGIC_BYTE_BLOCK:
TZ_ASSERT(parse_block(cdata, &G.parsed_baking_data), EXC_PARSE_ERROR);
Expand Down
24 changes: 5 additions & 19 deletions src/baking_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ bool parse_block(buffer_t *buf, parsed_baking_data_t *const out) {
return true;
}

#define TAG_PREATTESTATION 20
#define TAG_ATTESTATION 21
#define TAG_ATTESTATION_DAL 23

/**
* Data:
* + (4 bytes) uint32: chain id of the block
Expand All @@ -230,33 +226,23 @@ bool parse_block(buffer_t *buf, parsed_baking_data_t *const out) {
* + (4 bytes) uint32: round of the related block
* + (32 bytes) uint8 *: hash of the related block
*/
bool parse_consensus_operation(buffer_t *buf, parsed_baking_data_t *const out) {
bool parse_consensus_operation(buffer_t *buf,
parsed_baking_data_t *const out,
bool is_attestation) {
uint8_t tag;

if (!buffer_read_u32(buf, &out->chain_id.v, BE) || // chain id
!buffer_seek_cur(buf, 32u * sizeof(uint8_t)) || // ignore branch
!buffer_read_u8(buf, &tag) || // tag
!buffer_read_u8(buf, &tag) || // ignore tag
!buffer_seek_cur(buf, sizeof(uint16_t)) || // ignore slot
!buffer_read_u32(buf, &out->level, BE) || // level
!buffer_read_u32(buf, &out->round, BE) || // round
!buffer_seek_cur(buf, 32u * sizeof(uint8_t)) // ignore hash
) {
return false;
}

switch (tag) {
case TAG_PREATTESTATION:
out->type = BAKING_TYPE_PREATTESTATION;
break;
case TAG_ATTESTATION:
case TAG_ATTESTATION_DAL:
out->type = BAKING_TYPE_ATTESTATION;
break;
default:
return false;
}
out->type = is_attestation ? BAKING_TYPE_ATTESTATION : BAKING_TYPE_PREATTESTATION;

out->is_tenderbake = true;

return true;
}
3 changes: 2 additions & 1 deletion src/baking_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool parse_block(buffer_t *buf, parsed_baking_data_t *const out);
*
* @param buf: input buffer containing the consensus operation
* @param out: baking data output
* @param is_attestation: whether its an attestation or pre-attestation.
* @return bool: returns false if it is invalid
*/
bool parse_consensus_operation(buffer_t *buf, parsed_baking_data_t *const out);
bool parse_consensus_operation(buffer_t *buf, parsed_baking_data_t *const out, bool is_attestation);
2 changes: 1 addition & 1 deletion src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ typedef struct {
blake2b_hash_state_t hash_state; ///< current blake2b hash state
uint8_t final_hash[SIGN_HASH_SIZE]; ///< buffer to hold hash of all the message

uint8_t magic_byte; ///< current magic byte read
magic_byte_t magic_byte; ///< current magic byte read
struct parse_state parse_state; ///< current parser state
} apdu_sign_state_t;

Expand Down
11 changes: 11 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ typedef enum {
BAKING_TYPE_PREATTESTATION
} baking_type_t;

/**
* @brief magic byte of operations
* See: https://tezos.gitlab.io/user/key-management.html#signer-requests
*/
typedef enum {
MAGIC_BYTE_UNSAFE_OP = 0x03u, /// magic byte of an operation
MAGIC_BYTE_BLOCK = 0x11u, /// magic byte of a block
MAGIC_BYTE_PREATTESTATION = 0x12u, /// magic byte of a pre-attestation
MAGIC_BYTE_ATTESTATION = 0x13u, /// magic byte of an attestation
} magic_byte_t;

typedef uint32_t level_t;
typedef uint32_t round_t;

Expand Down

0 comments on commit 750dd11

Please sign in to comment.