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

Use magic byte instead of tag to differentiate between pre-attestation and attestation #112

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ Download the source code for application from github repository [App-tezos-bakin
### Building

```
$ git clone https://github.com/LedgerHQ/app-tezos.git
$ cd app-tezos
$ git clone https://github.com/trilitech/ledger-app-tezos-baking.git
$ cd ledger-app-tezos-baking
```
Then run the following command to enter into docker container provided by Ledger. You will need to have docker cli installed.
Use the docker container `ledger-app-dev-tools` provided by Ledger to build the app.
Expand Down
16 changes: 8 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,18 @@ 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);
break;
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
26 changes: 5 additions & 21 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,21 @@ 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) {
uint8_t tag;

bool parse_consensus_operation(buffer_t *buf,
parsed_baking_data_t *const out,
bool is_attestation) {
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_seek_cur(buf, sizeof(uint8_t)) || // 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
2 changes: 1 addition & 1 deletion test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
base58
bip32
GitPython
pytezos>=3.11.3
pytezos==3.11.3
ragger>=1.18.1
Loading