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
15 changes: 11 additions & 4 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ tz_exc read_path_with_curve(derivation_type_t derivation_type,
cx_ecfp_public_key_t* pubkey) {
tz_exc exc = SW_OK;
cx_err_t error = CX_OK;
bip32_path_with_curve_t tmp_path_with_curve = {0};

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));
tmp_path_with_curve.derivation_type = derivation_type;
TZ_ASSERT(read_bip32_path(buf, &tmp_path_with_curve.bip32_path), EXC_WRONG_VALUES);

// Do not derive the public key if the two path_with_curve are equal
if (!bip32_path_with_curve_eq(path_with_curve, &tmp_path_with_curve)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems my previous comment is addressed here. you can still think if a setup_path_with_curve makes sense.

memmove(path_with_curve, &tmp_path_with_curve, sizeof(bip32_path_with_curve_t));
if (pubkey != NULL) {
CX_CHECK(generate_public_key(pubkey, path_with_curve));
}
}

end:
TZ_CONVERT_CX();
Expand Down
11 changes: 7 additions & 4 deletions src/apdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ static inline int io_send_apdu_err(uint16_t sw) {

/**
* @brief Reads a path with curve and derive the public key.
* Set [pubkey] to NULL to not deriving the public key.
* Will not derive the public key if the path with curve read
* is the same as the one provided.
*
* @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.
* @param[in] derivation_type: Derivation type of the key.
* @param[in] buf: Buffer that should contains a bip32 path.
* @param[in/out] path_with_curve: Buffer to store the path with curve.
* @param[out] pubkey: Buffer to store the pubkey. Can be NULL
* @return tz_exc: exception, SW_OK if none
*/
tz_exc read_path_with_curve(derivation_type_t derivation_type,
Expand Down
11 changes: 7 additions & 4 deletions src/apdu_pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ int handle_get_public_key(buffer_t *cdata,
TZ_ASSERT_NOT_NULL(cdata);

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));
// Do not derive the public key if the two path_with_curve are equal
if (!bip32_path_with_curve_eq(&global.path_with_curve, &g_hwm.baking_key)) {
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_CHECK(read_path_with_curve(derivation_type,
cdata,
Expand Down
8 changes: 7 additions & 1 deletion src/ui_bagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ tz_exc calculate_idle_screen_authorized_key(void) {
"No Key Authorized"),
EXC_WRONG_LENGTH);
} else {
CX_CHECK(generate_public_key(authorized_pk, &g_hwm.baking_key));
// Do not derive the public key if the two path_with_curve are equal
if (!bip32_path_with_curve_eq(&global.path_with_curve, &g_hwm.baking_key)) {
CX_CHECK(generate_public_key((cx_ecfp_public_key_t *) authorized_pk,
&global.path_with_curve));
} else {
memmove(authorized_pk, &global.public_key, sizeof(tz_ecfp_public_key_t));
}

TZ_CHECK(pk_to_pkh_string(home_context.authorized_key,
sizeof(home_context.authorized_key),
Expand Down
8 changes: 7 additions & 1 deletion src/ui_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ static void initInfo(void) {
TZ_ASSERT(copy_string(infoContentsBridge[PKH_IDX], MAX_LENGTH, "No Key Authorized"),
EXC_WRONG_LENGTH);
} else {
CX_CHECK(generate_public_key(authorized_pk, &g_hwm.baking_key));
// Do not derive the public key if the two path_with_curve are equal
if (!bip32_path_with_curve_eq(&global.path_with_curve, &g_hwm.baking_key)) {
CX_CHECK(generate_public_key((cx_ecfp_public_key_t*) authorized_pk,
&global.path_with_curve));
} else {
memmove(authorized_pk, &global.public_key, sizeof(tz_ecfp_public_key_t));
}

TZ_CHECK(pk_to_pkh_string(infoContentsBridge[PKH_IDX], MAX_LENGTH, authorized_pk));
}
Expand Down
Loading