Skip to content

Commit

Permalink
Merge pull request #26 from Zondax/no-throw
Browse files Browse the repository at this point in the history
Use no-throw SDK methods
  • Loading branch information
ftheirs authored Jul 4, 2023
2 parents 95b9258 + dbd8c2c commit f201ec1
Show file tree
Hide file tree
Showing 108 changed files with 73 additions and 94 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=8
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
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
2 changes: 1 addition & 1 deletion deps/nanosplus-secure-sdk
2 changes: 1 addition & 1 deletion deps/nanox-secure-sdk
Submodule nanox-secure-sdk updated 1179 files
2 changes: 1 addition & 1 deletion deps/stax-secure-sdk
Submodule stax-secure-sdk updated 1402 files
2 changes: 1 addition & 1 deletion tests_zemu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@zondax/ledger-algorand": "../js",
"@zondax/zemu": "^0.41.0"
"@zondax/zemu": "^0.43.1"
},
"devDependencies": {
"@types/jest": "^29.4.0",
Expand Down
Binary file modified tests_zemu/snapshots/s-mainmenu/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-mainmenu/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address_reject/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address_reject/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-show_address_reject/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00011.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-sign_application/00012.png
Binary file modified tests_zemu/snapshots/x-sign_application/00013.png
Binary file modified tests_zemu/snapshots/x-sign_application/00014.png
Binary file modified tests_zemu/snapshots/x-sign_application/00015.png
Binary file modified tests_zemu/snapshots/x-sign_application/00016.png
Binary file modified tests_zemu/snapshots/x-sign_application/00017.png
Binary file modified tests_zemu/snapshots/x-sign_application/00020.png
Binary file modified tests_zemu/snapshots/x-sign_application/00021.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00002.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00003.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00005.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00006.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00008.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00010.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00011.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00012.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00013.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00014.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00015.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00016.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00017.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00018.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00019.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00020.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00021.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00022.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00023.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00024.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00025.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00026.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00027.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00028.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00029.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00032.png
Binary file modified tests_zemu/snapshots/x-sign_application_big/00033.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00001.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00002.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00003.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00005.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00006.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00007.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00009.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00011.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00013.png
Binary file modified tests_zemu/snapshots/x-sign_asset_config/00014.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00001.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00002.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00003.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00005.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00006.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00007.png
Binary file modified tests_zemu/snapshots/x-sign_asset_freeze/00008.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00001.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00002.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00003.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00005.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00006.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00007.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00008.png
Binary file modified tests_zemu/snapshots/x-sign_asset_transfer/00009.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00002.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00003.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00005.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00007.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00008.png
Binary file modified tests_zemu/snapshots/x-sign_keyreg/00009.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00002.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00003.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00005.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00006.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00008.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00011.png
Binary file modified tests_zemu/snapshots/x-sign_payment/00012.png

0 comments on commit f201ec1

Please sign in to comment.