Skip to content

Commit

Permalink
Merge pull request #16 from Zondax/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
ftheirs authored May 25, 2022
2 parents e247ad6 + c454551 commit 51b53d0
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 20 deletions.
1 change: 0 additions & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ SDK_SOURCE_PATH += lib_ux
.PHONY: rust
rust:
@echo "No rust code"
# cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release

# Before linking, we need to be sure rust lib is there
bin/app.elf: rust
Expand Down
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=0
# This is the patch version of this release
APPVERSION_P=0
APPVERSION_P=1
6 changes: 6 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
}

case INS_GET_ADDR_SECP256K1: {
if( os_global_pin_is_validated() != BOLOS_UX_OK ) {
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
}
handleGetAddrSecp256K1(flags, tx, rx);
break;
}

case INS_SIGN_SECP256K1: {
if( os_global_pin_is_validated() != BOLOS_UX_OK ) {
THROW(APDU_CODE_COMMAND_NOT_ALLOWED);
}
handleSignSecp256K1(flags, tx, rx);
break;
}
Expand Down
43 changes: 25 additions & 18 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
return zxerr_invalid_crypto_settings;
}

zxerr_t err = zxerr_ok;
BEGIN_TRY
{
TRY {
Expand All @@ -50,10 +51,21 @@ zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &cx_privateKey);
cx_ecfp_init_public_key(CX_CURVE_256K1, NULL, 0, &cx_publicKey);
cx_ecfp_generate_pair(CX_CURVE_256K1, &cx_publicKey, &cx_privateKey, 1);

// Format pubkey
for (int i = 0; i < 32; i++) {
pubKey[i] = cx_publicKey.W[64 - i];
}
cx_publicKey.W[0] = cx_publicKey.W[64] & 1 ? 0x03 : 0x02; // "Compress" public key in place
if ((cx_publicKey.W[32] & 1) != 0) {
pubKey[31] |= 0x80;
}
//////////////////////
MEMCPY(pubKey, cx_publicKey.W, PK_LEN_SECP256K1);

}
CATCH_OTHER(e) {
CLOSE_TRY;
return zxerr_ledger_api_error;
CATCH_ALL {
err = zxerr_ledger_api_error;
}
FINALLY {
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
Expand All @@ -62,18 +74,7 @@ zxerr_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
}
END_TRY;

// Format pubkey
for (int i = 0; i < 32; i++) {
pubKey[i] = cx_publicKey.W[64 - i];
}
cx_publicKey.W[0] = cx_publicKey.W[64] & 1 ? 0x03 : 0x02; // "Compress" public key in place
if ((cx_publicKey.W[32] & 1) != 0) {
pubKey[31] |= 0x80;
}
//////////////////////
MEMCPY(pubKey, cx_publicKey.W, PK_LEN_SECP256K1);

return zxerr_ok;
return err;
}

zxerr_t crypto_sign(uint8_t *signature,
Expand All @@ -91,6 +92,8 @@ zxerr_t crypto_sign(uint8_t *signature,
uint8_t privateKeyData[32];
unsigned int info = 0;
int signatureLength = 0;

zxerr_t err = zxerr_ok;
BEGIN_TRY
{
TRY
Expand All @@ -113,6 +116,10 @@ zxerr_t crypto_sign(uint8_t *signature,
signatureMaxlen,
&info);
}
CATCH_ALL {
signatureLength = 0;
err = zxerr_ledger_api_error;
}
FINALLY {
MEMZERO(&cx_privateKey, sizeof(cx_privateKey));
MEMZERO(privateKeyData, 32);
Expand All @@ -121,7 +128,7 @@ zxerr_t crypto_sign(uint8_t *signature,
END_TRY;

*sigSize = signatureLength;
return zxerr_ok;
return err;
}

#else
Expand Down Expand Up @@ -182,7 +189,7 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrR
}

// extract pubkey
crypto_extractPublicKey(hdPath, buffer, buffer_len);
CHECK_ZXERR(crypto_extractPublicKey(hdPath, buffer, buffer_len))

// Hash it
uint8_t hashed1_pk[CX_SHA256_SIZE];
Expand All @@ -192,7 +199,7 @@ zxerr_t crypto_fillAddress(uint8_t *buffer, uint16_t buffer_len, uint16_t *addrR
ripemd160_32(hashed2_pk, hashed1_pk);

char *addr = (char *) (buffer + PK_LEN_SECP256K1);
bech32EncodeFromBytes(addr, buffer_len - PK_LEN_SECP256K1, bech32_hrp, hashed2_pk, CX_RIPEMD160_SIZE, 1);
CHECK_ZXERR(bech32EncodeFromBytes(addr, buffer_len - PK_LEN_SECP256K1, bech32_hrp, hashed2_pk, CX_RIPEMD160_SIZE, 1))

*addrResponseLen = PK_LEN_SECP256K1 + strlen(addr);

Expand Down
59 changes: 59 additions & 0 deletions tests/testcases/manual.json
Original file line number Diff line number Diff line change
Expand Up @@ -1517,5 +1517,64 @@
"6 | Fee : 0.000600 AXL"
],
"expert": false
},
{
"name": "completeTransferExpertAxl",
"tx": {
"account_number": "0",
"chain_id": "axelar-dojo-1",
"fee": {
"amount": [
{
"amount": "150",
"denom": "uaxl"
}
],
"gas": "10000"
},
"memo": "testmemo",
"msgs": [
{
"inputs": [
{
"address": "axelaraccaddr1d9h8qat5e4ehc5",
"coins": [
{
"amount": "50",
"denom": "axl"
}
]
}
],
"outputs": [
{
"address": "axelaraccaddr1da6hgur4wse3jx32",
"coins": [
{
"amount": "10",
"denom": "axl"
}
]
}
]
}
],
"sequence": "1"
},
"parsingErr": "No error",
"validationErr": "No error",
"expected": [
"0 | Chain ID : axelar-dojo-1",
"1 | Account : 0",
"2 | Sequence : 1",
"3 | Source Address : axelaraccaddr1d9h8qat5e4ehc5",
"4 | Source Coins : 50 axl",
"5 | Dest Address : axelaraccaddr1da6hgur4wse3jx32",
"6 | Dest Coins : 10 axl",
"7 | Memo : testmemo",
"8 | Fee : 150 uaxl",
"9 | Gas : 10000"
],
"expert": true
}
]
Binary file modified tests_zemu/snapshots/s-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/s-mainmenu/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/sp-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/sp-mainmenu/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-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/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 51b53d0

Please sign in to comment.