Skip to content

Commit

Permalink
Cleaning and standardizing
Browse files Browse the repository at this point in the history
Remove deprecated handlers, only supported mode is wallet
U2F is no longer supported
set_check_internal_structure_integrity: remove empty function
dead code and use functions from SDk
standardize, and replace coin_config structure with defines
swap: use global variable from standard swap and move to dedicated folder
  • Loading branch information
sgliner-ledger committed Mar 29, 2024
1 parent e7a197b commit 704e8ad
Show file tree
Hide file tree
Showing 46 changed files with 861 additions and 2,073 deletions.
252 changes: 66 additions & 186 deletions Makefile

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions TODO.md

This file was deleted.

9 changes: 0 additions & 9 deletions include/apdu_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,14 @@

void commit_operation_mode(secu8 operationMode);

unsigned short apdu_setup(void);
unsigned short apdu_verify_pin(void);
unsigned short apdu_get_operation_mode(void);
unsigned short apdu_set_operation_mode(void);
unsigned short apdu_get_wallet_public_key(void);
unsigned short apdu_get_trusted_input(void);
unsigned short apdu_hash_input_start(void);
unsigned short apdu_hash_input_finalize(void);
unsigned short apdu_hash_sign(void);
unsigned short apdu_hash_input_finalize_full(void);
unsigned short apdu_import_private_key(void);
unsigned short apdu_get_public_key(void);
unsigned short apdu_derive_bip32_key(void);
unsigned short apdu_signverify_immediate(void);
unsigned short apdu_sign_message(void);

unsigned short apdu_get_random(void);
unsigned short apdu_get_firmware_version(void);

unsigned short apdu_get_coin_version(void);
Expand Down
30 changes: 0 additions & 30 deletions include/base58.h

This file was deleted.

30 changes: 0 additions & 30 deletions include/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ struct context_s {
/** (Integrity protected) transaction context */
transaction_context_t transactionContext;

/** Non protected transaction context */

/** Last U2F Token streamed by host to attempt pubkey request */
unsigned char last_token[4];
unsigned char has_valid_token;

/** Full transaction hash context */
union multi_hash transactionHashFull;
/** Authorization transaction hash context */
Expand Down Expand Up @@ -241,9 +235,6 @@ struct context_s {
unsigned char nExpiryHeight[4];
unsigned char nLockTime[4];
unsigned char sigHashType[4];

/*Is swap mode*/
unsigned char called_from_swap;
};
typedef struct context_s context_t;

Expand Down Expand Up @@ -291,27 +282,6 @@ typedef enum coin_kind_e {
COIN_KIND_HYDRA
} coin_kind_t;

typedef struct altcoin_config_s {
unsigned int bip44_coin_type;
unsigned int bip44_coin_type2;
unsigned short p2pkh_version;
unsigned short p2sh_version;
unsigned char family;
#ifdef HAVE_NBGL
unsigned char img_raw[1024];
nbgl_icon_details_t img_nbgl;
#endif // HAVE_NBGL
char coinid[14]; // used coind id for message signature prefix
char name[16]; // for ux displays
char name_short[6]; // for unit in ux displays
char native_segwit_prefix_val[5];
const char* native_segwit_prefix; // null if no segwit prefix
unsigned int forkid;
unsigned int zcash_consensus_branch_id;
coin_kind_t kind;
unsigned int flags;
} altcoin_config_t;

void context_init(void);

#endif
28 changes: 0 additions & 28 deletions include/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,16 @@ unsigned char output_script_is_op_create(unsigned char *buffer,
unsigned char output_script_is_op_call(unsigned char *buffer,
size_t size);

void sleep16(unsigned short delay);
void sleep32(unsigned long int delayEach, unsigned long int delayRepeat);

unsigned long int read_u32(unsigned char *buffer, unsigned char be,
unsigned char skipSign);

void write_u32_be(unsigned char *buffer, unsigned long int value);
void write_u32_le(unsigned char *buffer, unsigned long int value);

void perform_double_hash(unsigned char *in, unsigned short inlen,
unsigned char *out,
unsigned char hash1Algorithm,
unsigned char hash2Algorithm);

void public_key_hash160(unsigned char *in, unsigned short inlen,
unsigned char *out);
unsigned short public_key_to_encoded_base58(
unsigned char *in, unsigned short inlen, unsigned char *out,
unsigned short outlen, unsigned short version, unsigned char alreadyHashed);

unsigned short decode_base58_address(unsigned char *in,
unsigned short inlen,
unsigned char *out,
unsigned short outlen);

unsigned char bip44_derivation_guard(unsigned char *bip32Path, bool is_change_path);
unsigned char enforce_bip44_coin_type(unsigned char *bip32Path, bool for_pubkey);
unsigned char bip32_print_path(unsigned char *bip32Path, char* out, unsigned char max_out_len);

// void set_check_internal_structure_integrity(unsigned char
// setParameter);
#define set_check_internal_structure_integrity(x)
void swap_bytes(unsigned char *target, unsigned char *source,
unsigned char size);

Expand All @@ -82,12 +60,6 @@ int sign_finalhash(unsigned char *path, size_t path_len,

void transaction_add_output(unsigned char *hash160Address,
unsigned char *amount, unsigned char p2sh);
unsigned char rng_u8_modulo(unsigned char modulo);
unsigned char secure_memcmp(const void *buf1, const void *buf2,
unsigned short length);
unsigned char decrease_2fa(void);
void reset_2fa(void);
void reset_token(void);
int get_public_key(unsigned char* keyPath, size_t keyPath_len, uint8_t raw_pubkey[static 65], unsigned char* chainCode);

#endif
33 changes: 13 additions & 20 deletions src/apdu_get_coin_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,19 @@
unsigned short apdu_get_coin_version() {
uint8_t offset = 0;

SB_CHECK(N_btchip.bkp.config.operationMode);
if ((SB_GET(N_btchip.bkp.config.operationMode) ==
MODE_SETUP_NEEDED) ||
(SB_GET(N_btchip.bkp.config.operationMode) == MODE_ISSUER)) {
return SW_CONDITIONS_OF_USE_NOT_SATISFIED;
}

G_io_apdu_buffer[offset++] = G_coin_config->p2pkh_version >> 8;
G_io_apdu_buffer[offset++] = G_coin_config->p2pkh_version;
G_io_apdu_buffer[offset++] = G_coin_config->p2sh_version >> 8;
G_io_apdu_buffer[offset++] = G_coin_config->p2sh_version;
G_io_apdu_buffer[offset++] = G_coin_config->family;
G_io_apdu_buffer[offset++] = strlen(G_coin_config->coinid);
memmove(G_io_apdu_buffer + offset, G_coin_config->coinid,
strlen(G_coin_config->coinid));
offset += strlen(G_coin_config->coinid);
G_io_apdu_buffer[offset++] = strlen(G_coin_config->name_short);
memmove(G_io_apdu_buffer + offset, G_coin_config->name_short,
strlen(G_coin_config->name_short));
offset += strlen(G_coin_config->name_short);
G_io_apdu_buffer[offset++] = COIN_P2PKH_VERSION >> 8;
G_io_apdu_buffer[offset++] = COIN_P2PKH_VERSION;
G_io_apdu_buffer[offset++] = COIN_P2SH_VERSION >> 8;
G_io_apdu_buffer[offset++] = COIN_P2SH_VERSION;
G_io_apdu_buffer[offset++] = COIN_FAMILY;
G_io_apdu_buffer[offset++] = strlen(COIN_COINID);
memmove(G_io_apdu_buffer + offset, COIN_COINID,
strlen(COIN_COINID));
offset += strlen(COIN_COINID);
G_io_apdu_buffer[offset++] = strlen(COIN_COINID_SHORT);
memmove(G_io_apdu_buffer + offset, COIN_COINID_SHORT,
strlen(COIN_COINID_SHORT));
offset += strlen(COIN_COINID_SHORT);
context_D.outLength = offset;

return SW_OK;
Expand Down
7 changes: 4 additions & 3 deletions src/apdu_get_firmware_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
#define ARCH_ID 0x30

// Java Card is 0x60
#define TCS_LOADER_PATCH_VERSION 0

void get_firmware_version(unsigned char *buffer) {
buffer[0] = ARCH_ID;
buffer[1] = LEDGER_MAJOR_VERSION;
buffer[2] = LEDGER_MINOR_VERSION;
buffer[3] = LEDGER_PATCH_VERSION;
buffer[1] = MAJOR_VERSION;
buffer[2] = MINOR_VERSION;
buffer[3] = PATCH_VERSION;
buffer[4] = 1;
buffer[5] = TCS_LOADER_PATCH_VERSION;
}
Expand Down
44 changes: 0 additions & 44 deletions src/apdu_get_operation_mode.c

This file was deleted.

38 changes: 0 additions & 38 deletions src/apdu_get_random.c

This file was deleted.

18 changes: 4 additions & 14 deletions src/apdu_get_trusted_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "internal.h"
#include "apdu_constants.h"
#include "lib_standard_app/read.h"
#include "lib_standard_app/write.h"

#define GET_TRUSTED_INPUT_P1_FIRST 0x00
#define GET_TRUSTED_INPUT_P1_NEXT 0x80
Expand All @@ -26,25 +28,14 @@ unsigned short apdu_get_trusted_input() {
unsigned char dataOffset = 0;
apduLength = G_io_apdu_buffer[ISO_OFFSET_LC];

SB_CHECK(N_btchip.bkp.config.operationMode);
switch (SB_GET(N_btchip.bkp.config.operationMode)) {
case MODE_WALLET:
case MODE_RELAXED_WALLET:
case MODE_SERVER:
break;
default:
return SW_CONDITIONS_OF_USE_NOT_SATISFIED;
}

if (G_io_apdu_buffer[ISO_OFFSET_P1] == GET_TRUSTED_INPUT_P1_FIRST) {
// Initialize
context_D.transactionTargetInput =
read_u32(G_io_apdu_buffer + ISO_OFFSET_CDATA, 1, 0);
read_u32_be(G_io_apdu_buffer, ISO_OFFSET_CDATA);
context_D.transactionContext.transactionState =
TRANSACTION_NONE;
context_D.trustedInputProcessed = 0;
context_D.transactionContext.consumeP2SH = 0;
set_check_internal_structure_integrity(1);
dataOffset = 4;
context_D.transactionHashOption = TRANSACTION_HASH_FULL;
context_D.usingSegwit = 0;
Expand All @@ -67,7 +58,6 @@ unsigned short apdu_get_trusted_input() {

context_D.transactionContext.transactionState =
TRANSACTION_NONE;
set_check_internal_structure_integrity(1);
if (!context_D.trustedInputProcessed) {
// Output was not found
return SW_INCORRECT_DATA;
Expand All @@ -84,7 +74,7 @@ unsigned short apdu_get_trusted_input() {
G_io_apdu_buffer[1] = 0x00;
cx_hash_sha256(G_io_apdu_buffer + TRUSTED_INPUT_SIZE, 32, G_io_apdu_buffer + 4, 32);

write_u32_le(G_io_apdu_buffer + 4 + 32,
write_u32_le(G_io_apdu_buffer, 4 + 32,
context_D.transactionTargetInput);
memmove(G_io_apdu_buffer + 4 + 32 + 4,
context_D.transactionContext.transactionAmount, 8);
Expand Down
Loading

0 comments on commit 704e8ad

Please sign in to comment.