Skip to content

Commit

Permalink
Also support derivation paths of length 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jul 23, 2024
1 parent 07885ed commit 525f02a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/src/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ zxerr_t addr_getItem(int8_t displayIdx,

snprintf(outKey, outKeyLen, "Your Path");
char buffer[300];
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
bip32_to_str(buffer, sizeof(buffer), hdPath, hdPathLen);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
return zxerr_ok;
}
Expand Down
18 changes: 12 additions & 6 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ __Z_INLINE void extractHDPath(uint32_t rx, uint32_t offset) {
ZEMU_LOGF(50, "Extract HDPath\n")
tx_initialized = false;

const uint8_t pathLength = G_io_apdu_buffer[offset];
hdPathLen = G_io_apdu_buffer[offset];
offset++;

if (pathLength != HDPATH_LEN_DEFAULT || (rx - offset) != sizeof(uint32_t) * pathLength) {
if ((hdPathLen != HDPATH_LEN_DEFAULT && hdPathLen != IDENTITY_DER_PATH_LEN) || (rx - offset) != sizeof(uint32_t) * hdPathLen) {
THROW(APDU_CODE_WRONG_LENGTH);
}

memcpy(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * HDPATH_LEN_DEFAULT);
memcpy(hdPath, G_io_apdu_buffer + offset, sizeof(uint32_t) * hdPathLen);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT &&
const bool default_mainnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_DEFAULT;

const bool testnet = hdPath[0] == HDPATH_0_DEFAULT &&
const bool default_testnet = hdPath[0] == HDPATH_0_DEFAULT &&
hdPath[1] == HDPATH_1_TESTNET;

if (!mainnet && !testnet) {
const bool identity_mainnet = hdPath[0] == HDPATH_0_IDENTITY &&
hdPath[1] == HDPATH_1_DEFAULT;

const bool identity_testnet = hdPath[0] == HDPATH_0_IDENTITY &&
hdPath[1] == HDPATH_1_TESTNET;

if (!default_mainnet && !default_testnet && !identity_mainnet && !identity_testnet) {
THROW(APDU_CODE_DATA_INVALID);
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ extern "C" {
#define CLA 0x57

#define HDPATH_LEN_DEFAULT 5
#define IDENTITY_DER_PATH_LEN 3
#define HDPATH_0_DEFAULT (0x80000000u | 0x2cu) //44
#define HDPATH_0_IDENTITY (0x80000000u | 0x20u) //32

#define HDPATH_1_DEFAULT (0x80000000u | 0x36d) //877
#define HDPATH_1_TESTNET (0x80000000u | 0x01) //1
Expand Down
6 changes: 3 additions & 3 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static zxerr_t crypto_extractPublicKey_ed25519(uint8_t *pubKey, uint16_t pubKeyL
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
hdPathLen,
privateKeyData,
NULL,
NULL,
Expand Down Expand Up @@ -111,7 +111,7 @@ static zxerr_t crypto_sign_ed25519(uint8_t *output, uint16_t outputLen, const ui
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_ED25519_SLIP10,
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
hdPathLen,
privateKeyData,
NULL,
NULL,
Expand Down Expand Up @@ -550,7 +550,7 @@ zxerr_t crypto_computeSaplingSeed(uint8_t spendingKey[static KEY_LENGTH]) {
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL,
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
hdPathLen,
privateKeyData,
NULL, NULL, 0));
memcpy(spendingKey, privateKeyData, KEY_LENGTH);
Expand Down
1 change: 1 addition & 0 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#include "parser_txdef.h"

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];
extern uint8_t hdPathLen;

zxerr_t crypto_fillAddress(signing_key_type_e addressKind, uint8_t *buffer, uint16_t bufferLen, uint16_t *cmdResponseLen);
zxerr_t crypto_sign(const parser_tx_t *txObj, uint8_t *output, uint16_t outputLen);
Expand Down
1 change: 1 addition & 0 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "blake2.h"

uint32_t hdPath[HDPATH_LEN_DEFAULT];
uint8_t hdPathLen;

uint8_t bech32_hrp_len;
char bech32_hrp[MAX_BECH32_HRP_LEN + 1];
Expand Down
6 changes: 3 additions & 3 deletions app/src/review_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ zxerr_t getItemPublicAddress(int8_t displayIdx, char *outKey, uint16_t outKeyLen
case 1: {
snprintf(outKey, outKeyLen, "HD Path");
char buffer[200] = {0};
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
bip32_to_str(buffer, sizeof(buffer), hdPath, hdPathLen);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
break;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ zxerr_t getItemProofGenerationKey(int8_t displayIdx, char *outKey, uint16_t outK
case 2: {
snprintf(outKey, outKeyLen, "HD Path");
char buffer[200] = {0};
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
bip32_to_str(buffer, sizeof(buffer), hdPath, hdPathLen);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
break;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ zxerr_t getItemViewKey(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char
case 3: {
snprintf(outKey, outKeyLen, "HD Path");
char buffer[200] = {0};
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
bip32_to_str(buffer, sizeof(buffer), hdPath, hdPathLen);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
break;
}
Expand Down

0 comments on commit 525f02a

Please sign in to comment.