Skip to content

Commit

Permalink
hash sign base64
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 27, 2024
1 parent 99195bd commit d041fc9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
33 changes: 24 additions & 9 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "crypto_helper.h"
#include "items_format.h"
#include "parser_impl.h"
#include "zxformat.h"

#define INCREMENT_NUM_ITEMS() \
item_array.numOfItems++; \
Expand All @@ -36,6 +37,7 @@ static items_error_t items_validateSigners();
static items_error_t items_storePayingGas();
static items_error_t items_storeAllTransfers();
static items_error_t items_storeCaution();
static items_error_t items_storeHashWarning();
static items_error_t items_storeChainId();
static items_error_t items_storeUsingGas();
static items_error_t items_checkTxLengths();
Expand Down Expand Up @@ -97,6 +99,10 @@ items_error_t items_storeItems(tx_type_t tx_type) {
CHECK_ITEMS_ERROR(items_checkTxLengths());
}

if (tx_type == tx_type_hash) {
CHECK_ITEMS_ERROR(items_storeHashWarning());
}

CHECK_ITEMS_ERROR(items_computeHash(tx_type));

CHECK_ITEMS_ERROR(items_storeHash());
Expand Down Expand Up @@ -288,6 +294,16 @@ static items_error_t items_storeAllTransfers() {
return items_ok;
}

static items_error_t items_storeHashWarning() {
item_t *item = &item_array.items[item_array.numOfItems];

strcpy(item->key, "WARNING");
item_array.toString[item_array.numOfItems] = items_hashWarningToDisplayString;
INCREMENT_NUM_ITEMS()

return items_ok;
}

static items_error_t items_storeCaution() {
item_t *item = &item_array.items[item_array.numOfItems];

Expand Down Expand Up @@ -353,17 +369,16 @@ static items_error_t items_checkTxLengths() {
static items_error_t items_computeHash(tx_type_t tx_type) {
if (tx_type == tx_type_hash) {
tx_hash_t *hash_obj = parser_getParserHashObj();
MEMCPY(base64_hash, hash_obj->tx, hash_obj->hash_len);
return items_ok;
}

if (blake2b_hash((uint8_t *)parser_getParserJsonObj()->json.buffer, parser_getParserJsonObj()->json.bufferLen, hash) !=
zxerr_ok) {
return items_error;
base64_encode(base64_hash, 44, hash_obj->tx, hash_obj->hash_len);
} else if (tx_type == tx_type_json) {
if (blake2b_hash((uint8_t *)parser_getParserJsonObj()->json.buffer, parser_getParserJsonObj()->json.bufferLen, hash) !=
zxerr_ok) {
return items_error;
}

base64_encode(base64_hash, 44, hash, sizeof(hash));
}

base64_encode(base64_hash, 44, hash, sizeof(hash));

// Make it base64 URL safe
for (int i = 0; base64_hash[i] != '\0'; i++) {
if (base64_hash[i] == '+') {
Expand Down
9 changes: 9 additions & 0 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ items_error_t items_warningToDisplayString(__Z_UNUSED item_t item, char *outVal,
return items_ok;
}

items_error_t items_hashWarningToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen) {
uint16_t len = sizeof(HASH_WARNING_TEXT);

if (len > outValLen) return items_data_too_large;

snprintf(outVal, len, HASH_WARNING_TEXT);
return items_ok;
}

items_error_t items_cautionToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen) {
uint16_t len = sizeof(CAUTION_TEXT);

Expand Down
2 changes: 2 additions & 0 deletions app/src/items_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
#define WARNING_TEXT \
"UNSAFE TRANSACTION. This transaction's code was not recognized and does not limit capabilities for all signers. " \
"Signing this transaction may make arbitrary actions on the chain including loss of all funds."
#define HASH_WARNING_TEXT "Blind Signing a Transaction Hash is a very unusual operation. Do not continue unless you know what you are doing"
#define CAUTION_TEXT "'meta' field of transaction not recognized"
#define TX_TOO_LARGE_TEXT \
"Transaction too large for Ledger to display. PROCEED WITH GREAT CAUTION. Do you want to continue?"

items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_nothingToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_warningToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_hashWarningToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_cautionToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_txTooLargeToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_signingToDisplayString(item_t item, char *outVal, uint16_t outValLen);
Expand Down

1 comment on commit d041fc9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 7 file(s) not formatted
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/apdu_handler.c
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/common/tx.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.