Skip to content

Commit

Permalink
Merge pull request #41 from Zondax/main
Browse files Browse the repository at this point in the history
Boxes features
  • Loading branch information
xchapron-ledger authored Aug 2, 2023
2 parents 889b9bb + f201ec1 commit 56c42a9
Show file tree
Hide file tree
Showing 114 changed files with 279,347 additions and 280,056 deletions.
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=2
# This is the `spec_version` field of `Runtime`
APPVERSION_N=1
# This is the patch version of this release
APPVERSION_P=7
APPVERSION_P=9
2 changes: 1 addition & 1 deletion app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C" {

#define SK_LEN_25519 64u
#define SCALAR_LEN_ED25519 32u
#define SIG_PLUS_TYPE_LEN 65u
#define ED25519_SIGNATURE_SIZE 64u

#define PK_LEN_25519 32u
#define SS58_ADDRESS_MAX_LEN 60u
Expand Down
149 changes: 64 additions & 85 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,108 +22,87 @@

uint32_t hdPath[HDPATH_LEN_DEFAULT];

zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen)
{
zxerr_t crypto_extractPublicKey(uint8_t *pubKey, uint16_t pubKeyLen) {
if (pubKey == NULL || pubKeyLen < PK_LEN_25519) {
return zxerr_invalid_crypto_settings;
}

zxerr_t error = zxerr_unknown;
cx_ecfp_public_key_t cx_publicKey;
cx_ecfp_private_key_t cx_privateKey;
uint8_t privateKeyData[SK_LEN_25519];
uint8_t privateKeyData[SK_LEN_25519] = {0};

if (pubKeyLen < PK_LEN_25519) {
return zxerr_invalid_crypto_settings;
// Generate keys
CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL,
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData,
NULL,
NULL,
0))

CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_Ed25519, privateKeyData, 32, &cx_privateKey))
CATCH_CXERROR(cx_ecfp_init_public_key_no_throw(CX_CURVE_Ed25519, NULL, 0, &cx_publicKey))
CATCH_CXERROR(cx_ecfp_generate_pair_no_throw(CX_CURVE_Ed25519, &cx_publicKey, &cx_privateKey, 1))

for (unsigned int i = 0; i < PK_LEN_25519; i++) {
pubKey[i] = cx_publicKey.W[64 - i];
}

zxerr_t err = zxerr_unknown;
BEGIN_TRY
{
TRY
{
// Generate keys
os_perso_derive_node_bip32(
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData,
NULL);

cx_ecfp_init_private_key(CX_CURVE_Ed25519, privateKeyData, 32, &cx_privateKey);
cx_ecfp_init_public_key(CX_CURVE_Ed25519, NULL, 0, &cx_publicKey);
cx_ecfp_generate_pair(CX_CURVE_Ed25519, &cx_publicKey, &cx_privateKey, 1);

// Reversing the public key and changing the last byte
for (unsigned int i = 0; i < PK_LEN_25519; i++) {
pubKey[i] = cx_publicKey.W[64 - i];
}
if ((cx_publicKey.W[PK_LEN_25519] & 1) != 0) {
pubKey[31] |= 0x80;
}
err = zxerr_ok;
}
CATCH_ALL
{
err = zxerr_ledger_api_error;
}
FINALLY
{
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
MEMZERO(privateKeyData, SK_LEN_25519);
}
if ((cx_publicKey.W[PK_LEN_25519] & 1) != 0) {
pubKey[31] |= 0x80;
}
END_TRY;
error = zxerr_ok;

return err;
catch_cx_error:
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
MEMZERO(privateKeyData, SK_LEN_25519);

if (error != zxerr_ok) {
MEMZERO(pubKey, pubKeyLen);
}

return error;
}

zxerr_t crypto_sign(uint8_t *signature, uint16_t signatureMaxlen, const uint8_t *message, uint16_t messageLen) {

if (message == NULL || messageLen == 0) {
return zxerr_no_data;
if (signature == NULL || message == NULL || signatureMaxlen < ED25519_SIGNATURE_SIZE || messageLen == 0) {
return zxerr_unknown;
}

cx_ecfp_private_key_t cx_privateKey;
uint8_t privateKeyData[SK_LEN_25519] = {0};

zxerr_t err = zxerr_unknown;
BEGIN_TRY
{
TRY
{
// Generate keys
os_perso_derive_node_bip32(
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData,
NULL);

cx_ecfp_init_private_key(CX_CURVE_Ed25519, privateKeyData, SCALAR_LEN_ED25519, &cx_privateKey);

// Sign
cx_eddsa_sign(&cx_privateKey,
CX_LAST,
CX_SHA512,
message,
messageLen,
NULL,
0,
signature,
signatureMaxlen,
NULL);

err = zxerr_ok;
}
CATCH_ALL
{
err = zxerr_unknown;
}
FINALLY
{
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
MEMZERO(privateKeyData, SK_LEN_25519);
}
zxerr_t error = zxerr_unknown;

CATCH_CXERROR(os_derive_bip32_with_seed_no_throw(HDW_NORMAL,
CX_CURVE_Ed25519,
hdPath,
HDPATH_LEN_DEFAULT,
privateKeyData,
NULL,
NULL,
0))

CATCH_CXERROR(cx_ecfp_init_private_key_no_throw(CX_CURVE_Ed25519, privateKeyData, 32, &cx_privateKey))
CATCH_CXERROR(cx_eddsa_sign_no_throw(&cx_privateKey,
CX_SHA512,
message,
messageLen,
signature,
signatureMaxlen))
error = zxerr_ok;

catch_cx_error:
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
MEMZERO(privateKeyData, SK_LEN_25519);

if (error != zxerr_ok) {
MEMZERO(signature, signatureMaxlen);
}
END_TRY;

return err;
return error;
}

zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t bufferLen, uint16_t *addrResponseLen)
Expand Down
45 changes: 33 additions & 12 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ static parser_error_t parser_printTxType(const parser_context_t *ctx, char *outK
return parser_ok;
}

static parser_error_t parser_printBoxes(char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen, uint8_t displayIdx,
uint8_t pageIdx, uint8_t *pageCount, txn_application *application) {
if (outKey == NULL || outVal == NULL || application ==NULL) {
return parser_unexpected_error;
}

const uint8_t tmpIdx = displayIdx - IDX_BOXES;
if (tmpIdx >= MAX_FOREIGN_APPS) return parser_unexpected_value;

snprintf(outKey, outKeyLen, "Box %d", application->boxes[tmpIdx].i);

if (application->boxes[tmpIdx].n != NULL && application->boxes[tmpIdx].n_len > 0) {

bool printable = true;
for (uint16_t j = 0; j < application->boxes[tmpIdx].n_len; j++) {
printable &= IS_PRINTABLE(*(application->boxes[tmpIdx].n + j));
}

if (printable) {
pageStringExt(outVal, outValLen, (const char*) application->boxes[tmpIdx].n,
application->boxes[tmpIdx].n_len, pageIdx, pageCount);
} else {
base64_encode((char*) application->boxes[tmpIdx].n, application->boxes[tmpIdx].n_len, outVal, outValLen);
}
} else {
char null_box[8] = {0};
base64_encode(null_box, sizeof(null_box), outVal, outValLen);
}

return parser_ok;
}

static parser_error_t parser_printCommonParams(const parser_tx_t *parser_tx_obj,
uint8_t displayIdx,
char *outKey, uint16_t outKeyLen,
Expand Down Expand Up @@ -562,18 +594,7 @@ static parser_error_t parser_printTxApplication(parser_context_t *ctx,
return parser_ok;

case IDX_BOXES: {
const uint8_t tmpIdx = displayIdx - IDX_BOXES;
// Check max index
if (tmpIdx >= MAX_FOREIGN_APPS) return parser_unexpected_value;
if (tmpIdx == 0){
snprintf(outKey, outKeyLen, "Box");
}
else{
snprintf(outKey, outKeyLen, "Box %d", tmpIdx);
}
b64hash_data((unsigned char*) application->boxes[tmpIdx].n, application->boxes[tmpIdx].n_len, buff, sizeof(buff));
pageString(outVal, outValLen, buff, pageIdx, pageCount);
return parser_ok;
return parser_printBoxes(outKey, outKeyLen, outVal, outValLen, displayIdx, pageIdx, pageCount, application);
}

case IDX_FOREIGN_APP: {
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ parser_error_t b64hash_data(unsigned char *data, size_t data_len, char *b64hash,
cx_sha256_t ctx;
memset(&ctx, 0, sizeof(ctx));
cx_sha256_init(&ctx);
cx_hash(&ctx.header, CX_LAST, data, data_len, hash, sizeof(hash));
cx_hash_no_throw(&ctx.header, CX_LAST, data, data_len, hash, sizeof(hash));
#else
picohash_ctx_t ctx;
picohash_init_sha256(&ctx);
Expand Down
Loading

0 comments on commit 56c42a9

Please sign in to comment.