Skip to content

Commit

Permalink
refactor: resolve warnings (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepdefic1t authored Aug 17, 2023
1 parent 61d757c commit abb0104
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 97 deletions.
14 changes: 12 additions & 2 deletions src/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "os.h"

#include "constants.h"
#include "sw.h"

bool address_from_pubkey(const uint8_t public_key[static 33],
uint8_t *out,
Expand All @@ -52,9 +53,18 @@ bool address_from_pubkey(const uint8_t public_key[static 33],
return false;
}

cx_ripemd160_init(&ctx);
if (cx_ripemd160_init_no_throw(&ctx) != CX_OK) {
return false;
}

cx_hash((cx_hash_t *) &ctx, CX_LAST, public_key, PUBLIC_KEY_LEN, address, ADDRESS_HASH_LEN);
if (cx_hash_no_throw((cx_hash_t *) &ctx,
CX_LAST,
public_key,
PUBLIC_KEY_LEN,
address,
ADDRESS_HASH_LEN) != CX_OK) {
return false;
}

memmove(out + 1, address, ADDRESS_HASH_LEN - 1);
out[0] = network;
Expand Down
66 changes: 25 additions & 41 deletions src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

#include "io.h"
#include "os.h"
#include "sw.h"
#include "ux.h"

#include "context.h"
#include "globals.h"
#include "sw.h"
#include "types.h"

#include "apdu/dispatcher.h"

#include "ui/menu.h"

global_ctx_t G_context;
Expand All @@ -49,49 +50,32 @@ void app_main() {
reset_app_context();

for (;;) {
BEGIN_TRY {
TRY {
// Reset structured APDU command
memset(&cmd, 0, sizeof(cmd));

// Receive command bytes in G_io_apdu_buffer
if ((input_len = io_recv_command()) < 0) {
CLOSE_TRY;
return;
}
// Receive command bytes in G_io_apdu_buffer
if ((input_len = io_recv_command()) < 0) {
PRINTF("=> io_recv_command failure\n");
return;
}

// Parse APDU command from G_io_apdu_buffer
if (!apdu_parser(&cmd, G_io_apdu_buffer, input_len)) {
PRINTF("=> /!\\ BAD LENGTH: %.*H\n", input_len, G_io_apdu_buffer);
io_send_sw(SW_WRONG_DATA_LENGTH);
CLOSE_TRY;
continue;
}
// Parse APDU command from G_io_apdu_buffer
if (!apdu_parser(&cmd, G_io_apdu_buffer, input_len)) {
PRINTF("=> /!\\ BAD LENGTH: %.*H\n", input_len, G_io_apdu_buffer);
io_send_sw(SW_WRONG_DATA_LENGTH);
continue;
}

PRINTF("=> CLA=%02X | INS=%02X | P1=%02X | P2=%02X | Lc=%02X | CData=%.*H\n",
cmd.cla,
cmd.ins,
cmd.p1,
cmd.p2,
cmd.lc,
cmd.lc,
cmd.data);
PRINTF("=> CLA=%02X | INS=%02X | P1=%02X | P2=%02X | Lc=%02X | CData=%.*H\n",
cmd.cla,
cmd.ins,
cmd.p1,
cmd.p2,
cmd.lc,
cmd.lc,
cmd.data);

// Dispatch structured APDU command to handler
if (apdu_dispatcher(&cmd) < 0) {
CLOSE_TRY;
return;
}
}
CATCH(EXCEPTION_IO_RESET) {
THROW(EXCEPTION_IO_RESET);
}
CATCH_OTHER(e) {
io_send_sw(e);
}
FINALLY {
}
END_TRY;
// Dispatch structured APDU command to handler
if (apdu_dispatcher(&cmd) < 0) {
PRINTF("=> apdu_dispatcher failure\n");
return;
}
}
}
2 changes: 2 additions & 0 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <string.h> // explicit_bzero

#include "os.h" // PRINTF

#include "globals.h"

void reset_app_context() {
Expand Down
63 changes: 20 additions & 43 deletions src/crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <stdint.h> // uint*_t
#include <string.h> // memset, explicit_bzero

#include "crypto_helpers.h"

#include "crypto.h"

#include "globals.h"
Expand All @@ -39,30 +41,13 @@ int crypto_derive_private_key(cx_ecfp_private_key_t *private_key,
uint8_t chain_code[32],
const uint32_t *bip32_path,
uint8_t bip32_path_len) {
uint8_t raw_private_key[32] = {0};

BEGIN_TRY {
TRY {
// derive the seed with bip32_path
os_perso_derive_node_bip32(CX_CURVE_256K1,
bip32_path,
bip32_path_len,
raw_private_key,
chain_code);
// new private_key from raw
cx_ecfp_init_private_key(CX_CURVE_256K1,
raw_private_key,
sizeof(raw_private_key),
private_key);
}
CATCH_OTHER(e) {
THROW(e);
}
FINALLY {
explicit_bzero(&raw_private_key, sizeof(raw_private_key));
}
if (bip32_derive_init_privkey_256(CX_CURVE_256K1,
bip32_path,
bip32_path_len,
private_key,
chain_code) != CX_OK) {
return -1;
}
END_TRY;

return 0;
}
Expand All @@ -71,7 +56,9 @@ int crypto_init_public_key(cx_ecfp_private_key_t *private_key,
cx_ecfp_public_key_t *public_key,
uint8_t raw_public_key[33]) {
// generate corresponding public key
cx_ecfp_generate_pair(CX_CURVE_256K1, public_key, private_key, 1);
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, public_key, private_key, true) != CX_OK) {
return -1;
}

raw_public_key[0] = ((*(public_key->W + 64) & 1) ? 0x03 : 0x02);
memmove(raw_public_key + 1, public_key->W + 1, 32);
Expand All @@ -82,29 +69,19 @@ int crypto_init_public_key(cx_ecfp_private_key_t *private_key,
int crypto_sign_message() {
cx_ecfp_private_key_t private_key = {0};
size_t signature_len = sizeof(G_context.tx_info.signature);
cx_err_t error = CX_INTERNAL_ERROR;

// derive private key according to BIP32 path
crypto_derive_private_key(&private_key, NULL, G_context.bip32_path, G_context.bip32_path_len);

BEGIN_TRY {
TRY {
error = cx_ecschnorr_sign_no_throw(&private_key,
CX_ECSCHNORR_BIP0340 | CX_RND_TRNG,
CX_SHA256,
G_context.tx_info.m_hash,
sizeof(G_context.tx_info.m_hash),
G_context.tx_info.signature,
&signature_len);
}
CATCH_OTHER(e) {
THROW(e);
}
FINALLY {
explicit_bzero(&private_key, sizeof(private_key));
}
}
END_TRY;
cx_err_t error = cx_ecschnorr_sign_no_throw(&private_key,
CX_ECSCHNORR_BIP0340 | CX_RND_TRNG,
CX_SHA256,
G_context.tx_info.m_hash,
sizeof(G_context.tx_info.m_hash),
G_context.tx_info.signature,
&signature_len);

explicit_bzero(&private_key, sizeof(private_key));

if (error != CX_OK) {
return -1;
Expand Down
24 changes: 13 additions & 11 deletions src/handler/sign_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,19 @@ int handler_sign_tx(buffer_t *cdata, uint8_t chunk, bool more, bool is_message)
G_context.state = STATE_PARSED;

cx_sha256_t sha256;
cx_sha256_init(&sha256);
cx_hash(&sha256.header,
CX_LAST,
(G_context.req_type == CONFIRM_MESSAGE) ? G_context.tx_info.raw_tx + 2
: G_context.tx_info.raw_tx,
(G_context.req_type == CONFIRM_MESSAGE) ? G_context.tx_info.raw_tx_len - 2
: G_context.tx_info.raw_tx_len,
G_context.tx_info.m_hash,
sizeof(G_context.tx_info.m_hash));

PRINTF("Hash: %.*H\n", sizeof(G_context.tx_info.m_hash), G_context.tx_info.m_hash);

if (cx_sha256_init_no_throw(&sha256) != CX_OK) {
return io_send_sw(SW_TX_HASH_FAIL);
}

if (cx_hash_no_throw((cx_hash_t *) &sha256,
CX_LAST,
G_context.tx_info.raw_tx,
G_context.tx_info.raw_tx_len,
G_context.tx_info.m_hash,
HASH_32_LEN) != CX_OK) {
return io_send_sw(SW_TX_HASH_FAIL);
}

if (G_context.req_type == CONFIRM_TRANSACTION) {
return ui_display_transaction();
Expand Down

0 comments on commit abb0104

Please sign in to comment.