Skip to content

Commit

Permalink
Stop using CX_THROW
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Aug 8, 2024
1 parent 20e8a05 commit 4c7183f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void handle_apdu_get_wallet_id(uint8_t __attribute__((unused)) instruction) {

int rv = 0;
cx_blake2b_t hashState;
CX_THROW(cx_blake2b_init_no_throw(&hashState, 512));
CX_ASSERT(cx_blake2b_init_no_throw(&hashState, 512));

WITH_KEY_PAIR(id_path, key_pair, size_t, ({
PRINTF("\nPublic Key: %.*h\n", key_pair->public_key.W_len, key_pair->public_key.W);
Expand All @@ -73,7 +73,7 @@ void handle_apdu_get_wallet_id(uint8_t __attribute__((unused)) instruction) {
// Stubbed until we have the sign step working.
// rv = cx_hash((cx_hash_t*) &hashState, CX_LAST, signedToken, sizeof(signedToken),
// G_io_apdu_buffer, sizeof(G_io_apdu_buffer));
CX_THROW(cx_hash_no_throw((cx_hash_t *)&hashState, CX_LAST, (uint8_t *)key_pair->public_key.W,
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&hashState, CX_LAST, (uint8_t *)key_pair->public_key.W,
key_pair->public_key.W_len, G_io_apdu_buffer, sizeof(G_io_apdu_buffer)));
rv = cx_hash_get_size((cx_hash_t *)&hashState);
}));
Expand Down
6 changes: 3 additions & 3 deletions src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
static inline void conditional_init_hash_state(blake2b_hash_state_t *const state) {
check_null(state);
if (!state->initialized) {
CX_THROW(cx_blake2b_init2_no_throw(&state->state, SIGN_HASH_SIZE * 8, NULL, 0, (uint8_t *)blake2b_personalization,
CX_ASSERT(cx_blake2b_init2_no_throw(&state->state, SIGN_HASH_SIZE * 8, NULL, 0, (uint8_t *)blake2b_personalization,
sizeof(blake2b_personalization) - 1));
state->initialized = true;
}
Expand All @@ -39,7 +39,7 @@ static void blake2b_incremental_hash(
check_null(state);

conditional_init_hash_state(state);
CX_THROW(cx_hash_no_throw((cx_hash_t *)&state->state, 0, out, out_size, NULL, 0));
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&state->state, 0, out, out_size, NULL, 0));
}

static void blake2b_finish_hash(
Expand All @@ -49,7 +49,7 @@ static void blake2b_finish_hash(
check_null(state);

conditional_init_hash_state(state);
CX_THROW(cx_hash_no_throw((cx_hash_t *)&state->state, CX_LAST, NULL, 0, out, out_size));
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&state->state, CX_LAST, NULL, 0, out, out_size));
}

static int perform_signature(bool const on_hash, bool const send_hash);
Expand Down
18 changes: 9 additions & 9 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@ key_pair_t *generate_extended_key_pair_return_global(bip32_path_t const *const b
cx_curve_t const cx_curve = CX_CURVE_SECP256K1;

unsigned char temp_privkey[64] = {0};
CX_THROW(os_derive_bip32_no_throw(cx_curve, bip32_path->components, bip32_path->length, temp_privkey, chain_code));
CX_ASSERT(os_derive_bip32_no_throw(cx_curve, bip32_path->components, bip32_path->length, temp_privkey, chain_code));
memcpy(priv->private_key_data, temp_privkey, 32);
// clear the temporary buffer
explicit_bzero(temp_privkey, sizeof(temp_privkey));
// os_perso_derive_node_bip32(cx_curve, bip32_path->components, bip32_path->length, priv->private_key_data, chain_code);

BEGIN_TRY {
TRY {
CX_THROW(cx_ecfp_init_private_key_no_throw(cx_curve, priv->private_key_data, sizeof(priv->private_key_data),
CX_ASSERT(cx_ecfp_init_private_key_no_throw(cx_curve, priv->private_key_data, sizeof(priv->private_key_data),
&priv->res.private_key));
CX_THROW(cx_ecfp_generate_pair_no_throw(cx_curve, &priv->res.public_key, &priv->res.private_key, 1));
CX_ASSERT(cx_ecfp_generate_pair_no_throw(cx_curve, &priv->res.public_key, &priv->res.private_key, 1));

if (cx_curve == CX_CURVE_Ed25519) {
CX_THROW(cx_edwards_compress_point_no_throw(CX_CURVE_Ed25519, priv->res.public_key.W, priv->res.public_key.W_len));
CX_ASSERT(cx_edwards_compress_point_no_throw(CX_CURVE_Ed25519, priv->res.public_key.W, priv->res.public_key.W_len));
priv->res.public_key.W_len = 33;
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ size_t sign(uint8_t *const out, size_t const out_size, key_pair_t const *const p
unsigned int info = 0;
size_t sig_len = sizeof(sig);

CX_THROW(cx_ecdsa_sign_no_throw(&pair->private_key, CX_LAST | CX_RND_RFC6979,
CX_ASSERT(cx_ecdsa_sign_no_throw(&pair->private_key, CX_LAST | CX_RND_RFC6979,
CX_SHA256, // historical reasons...semantically CX_NONE
(uint8_t const *const)PIC(in), in_size, sig, &sig_len, &info));

Expand Down Expand Up @@ -138,12 +138,12 @@ void generate_lock_arg_for_pubkey(const cx_ecfp_public_key_t *const key, standar

cx_blake2b_t hash_state;

CX_THROW(cx_blake2b_init2_no_throw(&hash_state, 32*8, NULL, 0, (uint8_t *)blake2b_personalization,
CX_ASSERT(cx_blake2b_init2_no_throw(&hash_state, 32*8, NULL, 0, (uint8_t *)blake2b_personalization,
sizeof(blake2b_personalization) - 1));

CX_THROW(cx_hash_no_throw((cx_hash_t *)&hash_state, 0, (uint8_t *const) & tag_byte, 1, NULL, 0));
CX_THROW(cx_hash_no_throw((cx_hash_t *)&hash_state, 0, (uint8_t *const) key->W+1, 32, NULL, 0));
CX_THROW(cx_hash_no_throw((cx_hash_t *)&hash_state, CX_LAST, NULL, 0, (uint8_t *const) temp_hash,
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&hash_state, 0, (uint8_t *const) & tag_byte, 1, NULL, 0));
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&hash_state, 0, (uint8_t *const) key->W+1, 32, NULL, 0));
CX_ASSERT(cx_hash_no_throw((cx_hash_t *)&hash_state, CX_LAST, NULL, 0, (uint8_t *const) temp_hash,
sizeof(temp_hash)));

memcpy(dest, temp_hash, sizeof(standard_lock_arg_t));
Expand Down

0 comments on commit 4c7183f

Please sign in to comment.