From 525f02a34a7c1779fdbff4c8969431c34e5a7f91 Mon Sep 17 00:00:00 2001 From: Murisi Tarusenga Date: Tue, 23 Jul 2024 08:34:03 +0200 Subject: [PATCH] Also support derivation paths of length 3. --- app/src/addr.c | 2 +- app/src/apdu_handler.c | 18 ++++++++++++------ app/src/coin.h | 2 ++ app/src/crypto.c | 6 +++--- app/src/crypto.h | 1 + app/src/crypto_helper.c | 1 + app/src/review_keys.c | 6 +++--- 7 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/src/addr.c b/app/src/addr.c index 769b1db0..c24a3718 100644 --- a/app/src/addr.c +++ b/app/src/addr.c @@ -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; } diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index 0410c884..cd8b54e4 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -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); } } diff --git a/app/src/coin.h b/app/src/coin.h index 39e32a56..e46c05c7 100644 --- a/app/src/coin.h +++ b/app/src/coin.h @@ -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 diff --git a/app/src/crypto.c b/app/src/crypto.c index 4bf45d5a..ce079558 100644 --- a/app/src/crypto.c +++ b/app/src/crypto.c @@ -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, @@ -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, @@ -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); diff --git a/app/src/crypto.h b/app/src/crypto.h index 6c859a2f..88a07ca6 100644 --- a/app/src/crypto.h +++ b/app/src/crypto.h @@ -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); diff --git a/app/src/crypto_helper.c b/app/src/crypto_helper.c index 99b9eae5..40d4a8a8 100644 --- a/app/src/crypto_helper.c +++ b/app/src/crypto_helper.c @@ -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]; diff --git a/app/src/review_keys.c b/app/src/review_keys.c index e8b2d4c0..e3a6e0d8 100644 --- a/app/src/review_keys.c +++ b/app/src/review_keys.c @@ -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; } @@ -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; } @@ -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; }