Skip to content

Commit

Permalink
Operation parser: fix public key parsing
Browse files Browse the repository at this point in the history
The parse_next_type function is used to parse a wire structure.

It fills in nexttype_subparser_state.body using
nexttype_subparser_state.body.raw, then casts the data with the
expected type.

To be able to parse a wire structure using this function, the
structure must be in the body union. Otherwise, the parser may not
have enough space to contain the entire structure.

The largest structure in nexttype_subparser_state.body is
operation_group_header (32 bytes), the nexttype_subparser_state.body
can therefore contain a maximum of 32 bytes.

As the size of the public key depends on the key curve, no fixed size
structure was added to nexttype_subparser_state.body.

But since a public key can contain 33 bytes, a byte was missed in
nexttype_subparser_state.body.

This commit adds a new structure to be able to parse the public key
  • Loading branch information
spalmer25 committed Mar 19, 2024
1 parent 76708a2 commit 2c7b7ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ static inline bool parse_byte(uint8_t byte,
{
size_t klen = out->public_key.W_len;

// klen must match one of the field sizes in the public_key union

CALL_SUBPARSER(parse_next_type, byte, &(state->subparser_state.nexttype), klen);

if (memcmp(out->public_key.W, &(state->subparser_state.nexttype.body.raw), klen) !=
Expand Down
13 changes: 13 additions & 0 deletions src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ struct implicit_contract {
uint8_t pkh[HASH_SIZE]; ///< raw public key hash
} __attribute__((packed));

/**
* @brief Wire representation of implicit contract
*
*/
union public_key {
uint8_t edpk[32]; ///< raw public key for a edpk key
uint8_t sppk[33]; ///< raw public key for a sppk key
uint8_t p2pk[33]; ///< raw public key for a p2pk key
} __attribute__((packed));

/**
* @brief Wire representation of delegation
*
Expand Down Expand Up @@ -98,6 +108,9 @@ struct nexttype_subparser_state {

struct delegation_contents dc; ///< wire delegation content

// Required to read Reveal public key
union public_key pk; ///< wire public key

uint8_t raw[1]; ///< raw array to fill the body
} body;
uint32_t fill_idx; ///< current fill index
Expand Down

0 comments on commit 2c7b7ff

Please sign in to comment.