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

Optimize bls #122

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
#include <stdint.h>
#include <string.h>

tz_exc read_path_with_curve(derivation_type_t derivation_type,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The derivation type is selected at the time of setup. It can not be changed during signing. so it makes sense to derivce the public key when read_path_with_curve is called from handle_setup. Rather create a new function setup_path_with_curve and derive public key there along with a call to read_path_with_curve. Call setup_path_with_curve from handle_setup and read_path_with_curve from other places.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SETUP instruction is not required to sign:

  • AUTHORIZE_BAKING will setup the authorized key too.
  • If the authorized key was present in the RAM, SIGN with index 0x00 will tell which public key to sign with

The aims of this function is too make sure that, every time the stored Bip32-path-with-curve is updated, the stored PK is updated to.

buffer_t* buf,
bip32_path_with_curve_t* path_with_curve,
cx_ecfp_public_key_t* pubkey) {
tz_exc exc = SW_OK;
cx_err_t error = CX_OK;

TZ_ASSERT_NOT_NULL(buf);
TZ_ASSERT_NOT_NULL(path_with_curve);
TZ_ASSERT_NOT_NULL(pubkey);

path_with_curve->derivation_type = derivation_type;
TZ_ASSERT(read_bip32_path(buf, &path_with_curve->bip32_path), EXC_WRONG_VALUES);
CX_CHECK(generate_public_key(pubkey, path_with_curve));

end:
TZ_CONVERT_CX();
return exc;
}

int provide_pubkey(bip32_path_with_curve_t const* const path_with_curve) {
tz_exc exc = SW_OK;
cx_err_t error = CX_OK;
Expand Down
14 changes: 14 additions & 0 deletions src/apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ static inline int io_send_apdu_err(uint16_t sw) {
return io_send_sw(sw);
}

/**
* @brief Reads a path with curve and derive the public key.
*
* @param[in] derivation_type: Derivation type of the key.
* @param[in] buf: Buffer that should contains a bip32 path.
* @param[out] path_with_curve: Buffer to store the path with curve.
* @param[out] pubkey: Buffer to store the pubkey.
* @return tz_exc: exception, SW_OK if none
*/
tz_exc read_path_with_curve(derivation_type_t derivation_type,
buffer_t* buf,
bip32_path_with_curve_t* path_with_curve,
cx_ecfp_public_key_t* pubkey);

/**
* @brief Provides the public key in the apdu response
*
Expand Down
10 changes: 8 additions & 2 deletions src/apdu_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ int handle_get_public_key(buffer_t *cdata,
bool authorize,
bool prompt) {
tz_exc exc = SW_OK;
cx_err_t error = CX_OK;

TZ_ASSERT_NOT_NULL(cdata);

global.path_with_curve.derivation_type = derivation_type;
if ((cdata->size == 0u) && authorize) {
TZ_ASSERT(copy_bip32_path_with_curve(&global.path_with_curve, &(g_hwm.baking_key)),
EXC_MEMORY_ERROR);
CX_CHECK(generate_public_key((cx_ecfp_public_key_t *) &global.public_key,
&global.path_with_curve));
} else {
TZ_ASSERT(read_bip32_path(cdata, &global.path_with_curve.bip32_path), EXC_WRONG_VALUES);
TZ_CHECK(read_path_with_curve(derivation_type,
cdata,
&global.path_with_curve,
(cx_ecfp_public_key_t *) &global.public_key));
}

TZ_ASSERT(cdata->size == cdata->offset, EXC_WRONG_LENGTH);
Expand All @@ -100,5 +105,6 @@ int handle_get_public_key(buffer_t *cdata,
}

end:
TZ_CONVERT_CX();
return io_send_apdu_err(exc);
}
10 changes: 6 additions & 4 deletions src/apdu_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ int handle_setup(buffer_t *cdata, derivation_type_t derivation_type) {

TZ_ASSERT_NOT_NULL(cdata);

global.path_with_curve.derivation_type = derivation_type;

TZ_ASSERT(buffer_read_u32(cdata, &G.main_chain_id.v, BE) && // chain id
buffer_read_u32(cdata, &G.hwm.main, BE) && // main hwm level
buffer_read_u32(cdata, &G.hwm.test, BE) && // test hwm level
read_bip32_path(cdata, &global.path_with_curve.bip32_path),
buffer_read_u32(cdata, &G.hwm.test, BE), // test hwm level
EXC_WRONG_VALUES);

TZ_CHECK(read_path_with_curve(derivation_type,
cdata,
&global.path_with_curve,
(cx_ecfp_public_key_t *) &global.public_key));

TZ_ASSERT(cdata->size == cdata->offset, EXC_WRONG_LENGTH);

return prompt_setup(ok, reject);
Expand Down
7 changes: 4 additions & 3 deletions src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ int select_signing_key(buffer_t *cdata, derivation_type_t derivation_type) {

clear_data();

TZ_ASSERT(read_bip32_path(cdata, &global.path_with_curve.bip32_path), EXC_WRONG_VALUES);
TZ_CHECK(read_path_with_curve(derivation_type,
cdata,
&global.path_with_curve,
(cx_ecfp_public_key_t *) &global.public_key));

TZ_ASSERT(cdata->size == cdata->offset, EXC_WRONG_LENGTH);

global.path_with_curve.derivation_type = derivation_type;

return io_send_sw(SW_OK);

end:
Expand Down
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ typedef struct {
} dynamic_display;

bip32_path_with_curve_t path_with_curve; ///< holds the bip32 path and curve of the current key
tz_ecfp_public_key_t public_key; ///< holds the current public key

/// apdu handling state
struct {
Expand Down