Skip to content

Commit

Permalink
improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed May 15, 2024
1 parent 9c617f7 commit 08e0537
Show file tree
Hide file tree
Showing 28 changed files with 396 additions and 297 deletions.
70 changes: 60 additions & 10 deletions app/src/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ zxerr_t addr_getNumItems(uint8_t *num_items) {
*num_items = 1;
if (app_mode_expert()) {
*num_items = 2;
if (hdPath.addressKind == addr_sapling_div) {
*num_items = 3;
}
}

return zxerr_ok;
}

Expand All @@ -42,26 +46,31 @@ zxerr_t addr_getItem(int8_t displayIdx,
uint8_t *pageCount) {
ZEMU_LOGF(200, "[addr_getItem] %d/%d\n", displayIdx, pageIdx)

char buffer[300];
switch (displayIdx) {
case 0:
switch (action_addrResponse.kind) {
case addr_secp256k1:
// Title
switch ( hdPath.addressKind) {
case addr_secp256k1: {
snprintf(outKey, outKeyLen, "Unshielded");
pageString(outVal, outValLen, (char *)(G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SECP256K1), pageIdx,
pageCount);
return zxerr_ok;
}

case addr_sapling:
case addr_sapling: {
snprintf(outKey, outKeyLen, "Shielded");
pageString(outVal, outValLen, (char *)(G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SAPLING), pageIdx,
pageCount);
return zxerr_ok;
}

case addr_sapling_div:
case addr_sapling_div: {
snprintf(outKey, outKeyLen, "Shielded w/div");
pageString(outVal, outValLen, (char *)(G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_SAPLING), pageIdx,
pageCount);
return zxerr_ok;
}

default:
return zxerr_no_data;
Expand All @@ -71,12 +80,53 @@ zxerr_t addr_getItem(int8_t displayIdx,
return zxerr_no_data;
}

// FIXME: in the case of sapling, the path should be ZIP32 (3, elements, etc..)
snprintf(outKey, outKeyLen, "Your Path");
char buffer[300];
bip32_to_str(buffer, sizeof(buffer), hdPath, HDPATH_LEN_DEFAULT);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);
return zxerr_ok;
switch (hdPath.addressKind) {
case addr_secp256k1: {
snprintf(outKey, outKeyLen, "BIP44 Path");

bip32_to_str(buffer, sizeof(buffer), hdPath.secp256k1_path, HDPATH_LEN_BIP44);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);

return zxerr_ok;
}

case addr_sapling: {
snprintf(outKey, outKeyLen, "ZIP32 Path");

bip32_to_str(buffer, sizeof(buffer), hdPath.sapling_path, HDPATH_LEN_SAPLING);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);

return zxerr_ok;
}

case addr_sapling_div: {
snprintf(outKey, outKeyLen, "ZIP32 Path");

bip32_to_str(buffer, sizeof(buffer), hdPath.sapling_path, HDPATH_LEN_SAPLING);
pageString(outVal, outValLen, buffer, pageIdx, pageCount);

return zxerr_ok;
}

default:
return zxerr_no_data;
}
}

case 2: {
if (!app_mode_expert()) {
return zxerr_no_data;
}
switch (hdPath.addressKind)
{
case addr_sapling_div:
snprintf(outKey, outKeyLen, "Divisifier");
array_to_hexstr(outVal, outValLen, hdPath.saplingdiv_div, DIV_SIZE);
return zxerr_ok;

default:
return zxerr_no_data;
}
}
default:
return zxerr_no_data;
Expand Down
1 change: 0 additions & 1 deletion app/src/addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ extern "C" {
#endif

typedef struct {
address_kind_e kind;
uint8_t len;
} address_state_t;

Expand Down
10 changes: 6 additions & 4 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include "app_mode.h"
#include "coin.h"
#include "crypto.h"
#include "handlers/handler_path.h"
#include "handlers/handler_addr.h"
#include "handlers/handler_keys.h"
#include "handlers/handler_signing.h"
#include "handlers/handler_version.h"
#include "key.h"
#include "nvdata.h"
#include "parser.h"
Expand All @@ -35,10 +40,7 @@
#include "view_internal.h"
#include "zxmacros.h"

#include "handlers/handler_addr.h"
#include "handlers/handler_keys.h"
#include "handlers/handler_signing.h"
#include "handlers/handler_version.h"
hdPath_t hdPath;

void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
uint16_t sw = 0;
Expand Down
42 changes: 35 additions & 7 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
********************************************************************************/
#pragma once
#include "bolos_target.h"
#include "inttypes.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CLA 0x85

#define HDPATH_LEN_DEFAULT 5
#define HDPATH_LEN_MAX 5

#define HDPATH_0_DEFAULT (0x80000000u | 0x2cu)
#define HDPATH_1_DEFAULT (0x80000000u | 0x85)
Expand All @@ -33,6 +34,10 @@ extern "C" {
#define HDPATH_0_TESTNET (0x80000000u | 0x2cu)
#define HDPATH_1_TESTNET (0x80000000u | 0x1u)

#define HDPATH_0_ZIP32 (0x80000000u | 0x20u)
#define HDPATH_1_ZIP32 (0x80000000u | 0x85u)
#define HDPATH_2_ZIP32 (0u)

// compressed key
#define PK_LEN_SECP256K1 33u

Expand Down Expand Up @@ -78,12 +83,6 @@ extern "C" {
#define INS_GET_FVK 0xf3
#define INS_CRASH_TEST 0xff

typedef enum {
addr_secp256k1 = 0,
addr_sapling = 1,
addr_sapling_div = 2,
} address_kind_e;

typedef enum { key_ivk = 0, key_ovk = 1, key_fvk = 2, nf = 3 } key_type_e;

#define VIEW_ADDRESS_OFFSET_SECP256K1 PK_LEN_SECP256K1
Expand All @@ -100,6 +99,35 @@ typedef enum { key_ivk = 0, key_ovk = 1, key_fvk = 2, nf = 3 } key_type_e;
#define COIN_AMOUNT_DECIMAL_PLACES 18
#define CRYPTO_BLOB_SKIP_BYTES 0


#define HDPATH_LEN_BIP44 5
#define HDPATH_LEN_SAPLING 3

typedef enum {
addr_not_set = 0,
addr_secp256k1 = 1,
addr_sapling = 2,
addr_sapling_div = 3,
} address_kind_e;

typedef struct {
address_kind_e addressKind;
union {
struct {
uint32_t secp256k1_path[HDPATH_LEN_MAX];
};
struct {
uint32_t sapling_path[3];
};
struct {
uint32_t saplingdiv_path[3];
uint8_t saplingdiv_div[11];
};
};
} hdPath_t;

extern hdPath_t hdPath;

#ifdef __cplusplus
}
#endif
22 changes: 12 additions & 10 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#include "zxformat.h"
#include "zxmacros.h"

uint32_t hdPath[HDPATH_LEN_DEFAULT];

#define CHECK_ZXERROR_AND_CLEAN(CALL) \
do { \
zxerr_t __zxerror = CALL; \
Expand Down Expand Up @@ -119,12 +117,16 @@ static zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
return zxerr_invalid_crypto_settings;
}

if (hdPath.addressKind != addr_secp256k1) {
return zxerr_invalid_crypto_settings;
}

cx_ecfp_public_key_t cx_publicKey = {0};
cx_ecfp_private_key_t cx_privateKey = {0};
uint8_t privateKeyData[64] = {0};

zxerr_t error = zxerr_unknown;
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, hdPath, HDPATH_LEN_DEFAULT, privateKeyData, NULL));
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, hdPath.secp256k1_path, HDPATH_LEN_BIP44, privateKeyData, NULL));
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1, privateKeyData, SK_SECP256K1_SIZE, &cx_privateKey));
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_256K1, NULL, 0, &cx_publicKey));
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1));
Expand Down Expand Up @@ -190,7 +192,7 @@ zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed) {
zemu_log_stack("crypto_fillDeviceSeed");

// Generate randomness using a fixed path related to the device mnemonic
const uint32_t path[HDPATH_LEN_DEFAULT] = {
const uint32_t path[HDPATH_LEN_BIP44] = {
HDPATH_0_DEFAULT, HDPATH_1_DEFAULT, MASK_HARDENED, MASK_HARDENED, MASK_HARDENED,
};

Expand All @@ -199,7 +201,7 @@ zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed) {

zxerr_t error = zxerr_unknown;
io_seproxyhal_io_heartbeat();
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL, CX_CURVE_Ed25519, path, HDPATH_LEN_DEFAULT, raw_privkey,
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL, CX_CURVE_Ed25519, path, HDPATH_LEN_BIP44, raw_privkey,
NULL, NULL, 0));

io_seproxyhal_io_heartbeat();
Expand Down Expand Up @@ -1083,7 +1085,7 @@ zxerr_t crypto_sign_and_check_transparent(
for (uint8_t i = 0; i < tInListLen; i++) {
const t_input_item_t *item = t_inlist_retrieve_item(i);

CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, item->path, HDPATH_LEN_DEFAULT, privateKeyData, NULL));
CATCH_CXERROR(os_derive_bip32_no_throw(CX_CURVE_256K1, item->path, HDPATH_LEN_BIP44, privateKeyData, NULL));
CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1, privateKeyData, SK_SECP256K1_SIZE, &cx_privateKey));
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_256K1, NULL, 0, &cx_publicKey));
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1));
Expand Down Expand Up @@ -1324,10 +1326,10 @@ zxerr_t crypto_nullifier_sapling(uint8_t *outputBuffer,
}

// handleGetDiversifierList
zxerr_t crypto_diversifier_with_startindex(uint8_t *buffer, uint32_t p, const uint8_t *startindex, uint16_t *replylen) {
zxerr_t crypto_diversifier_with_startindex(uint8_t *buffer, uint32_t zip32Account, const uint8_t *startindex, uint16_t *replylen) {
zemu_log_stack("crypto_get_diversifiers_sapling");

diversifier_get_list(p, startindex, buffer);
diversifier_get_list(zip32Account, startindex, buffer);
for (int i = 0; i < DIV_LIST_LENGTH; i++) {
if (!diversifier_is_valid(buffer + i * DIV_SIZE)) {
MEMZERO(buffer + i * DIV_SIZE, DIV_SIZE);
Expand Down Expand Up @@ -1356,7 +1358,7 @@ typedef struct {
} tmp_buf_addr_s;

zxerr_t crypto_fillAddress_with_diversifier_sapling(
uint8_t *buffer, uint16_t bufferLen, uint32_t p, uint8_t *div, uint16_t *replyLen) {
uint8_t *buffer, uint16_t bufferLen, uint32_t zip32Account, uint8_t *div, uint16_t *replyLen) {
if (bufferLen < sizeof(tmp_buf_addr_s)) {
return zxerr_unknown;
}
Expand All @@ -1374,7 +1376,7 @@ zxerr_t crypto_fillAddress_with_diversifier_sapling(
}

// Initialize pkd
get_pkd(p, out->diversifier, out->pkd);
get_pkd(zip32Account, out->diversifier, out->pkd);
CHECK_APP_CANARY()

// To simplify the code and avoid making copies, read the 'address_raw' variable.
Expand Down
3 changes: 0 additions & 3 deletions app/src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ extern "C" {
#include "coin.h"
#include "zxerror.h"

extern uint32_t hdPath[HDPATH_LEN_DEFAULT];

extern address_kind_e addressKind;

zxerr_t crypto_fillDeviceSeed(uint8_t *device_seed);

Expand Down

0 comments on commit 08e0537

Please sign in to comment.