From 9e1de26753d1ffaad7d4538715c159d226fc1501 Mon Sep 17 00:00:00 2001 From: Sarah GLINER Date: Mon, 20 Nov 2023 10:20:58 +0100 Subject: [PATCH] Fix: clang analyzer --- src/btchip_apdu_hash_sign.c | 24 ++++++++++++++++++++---- src/btchip_helpers.c | 2 +- src/btchip_transaction.c | 6 +++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/btchip_apdu_hash_sign.c b/src/btchip_apdu_hash_sign.c index 8e006af9..32163794 100644 --- a/src/btchip_apdu_hash_sign.c +++ b/src/btchip_apdu_hash_sign.c @@ -165,20 +165,36 @@ unsigned short btchip_apdu_hash_sign() { void btchip_bagl_user_action_signtx(unsigned char confirming, unsigned char direct) { unsigned short sw = BTCHIP_SW_OK; + int error = 0; // confirm and finish the apdu exchange //spaghetti if (confirming) { unsigned char hash[32]; if (btchip_context_D.usingOverwinter) { - cx_hash_no_throw(&btchip_context_D.transactionHashFull.blake2b.header, CX_LAST, hash, 0, hash, 32); + error = cx_hash_no_throw(&btchip_context_D.transactionHashFull.blake2b.header, CX_LAST, hash, 0, hash, 32); } else { - cx_hash_no_throw(&btchip_context_D.transactionHashFull.sha256.header, CX_LAST, + error = cx_hash_no_throw(&btchip_context_D.transactionHashFull.sha256.header, CX_LAST, hash, 0, hash, 32); PRINTF("Hash1\n%.*H\n", sizeof(hash), hash); - // Rehash - cx_hash_sha256(hash, sizeof(hash), hash, 32); + if (!error) { + // Rehash + cx_hash_sha256(hash, sizeof(hash), hash, 32); + + } + } + + if (error) { + sw = BTCHIP_SW_CONDITIONS_OF_USE_NOT_SATISFIED; + btchip_context_D.outLength = 0; + G_io_apdu_buffer[btchip_context_D.outLength++] = sw >> 8; + G_io_apdu_buffer[btchip_context_D.outLength++] = sw; + + io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, btchip_context_D.outLength); + ui_transaction_error(); + return; } + PRINTF("Hash2\n%.*H\n", sizeof(hash), hash); // Sign size_t out_len = sizeof(G_io_apdu_buffer); diff --git a/src/btchip_helpers.c b/src/btchip_helpers.c index 343ce74d..74de2a37 100644 --- a/src/btchip_helpers.c +++ b/src/btchip_helpers.c @@ -215,7 +215,7 @@ void btchip_public_key_hash160(unsigned char *in, unsigned short inlen, unsigned char buffer[32]; cx_hash_sha256(in, inlen, buffer, 32); cx_ripemd160_init(&riprip); - cx_hash_no_throw(&riprip.header, CX_LAST, buffer, 32, out, 20); + (void)cx_hash_no_throw(&riprip.header, CX_LAST, buffer, 32, out, 20); } void btchip_compute_checksum(unsigned char* in, unsigned short inlen, unsigned char * output) { diff --git a/src/btchip_transaction.c b/src/btchip_transaction.c index 9cd29e61..345a3016 100644 --- a/src/btchip_transaction.c +++ b/src/btchip_transaction.c @@ -87,17 +87,17 @@ void transaction_offset(unsigned char value) { if ((btchip_context_D.transactionHashOption & TRANSACTION_HASH_FULL) != 0) { PRINTF("--- ADD TO HASH FULL:\n%.*H\n", value, btchip_context_D.transactionBufferPointer); if (btchip_context_D.usingOverwinter) { - cx_hash_no_throw(&btchip_context_D.transactionHashFull.blake2b.header, 0, btchip_context_D.transactionBufferPointer, value, NULL, 0); + (void)cx_hash_no_throw(&btchip_context_D.transactionHashFull.blake2b.header, 0, btchip_context_D.transactionBufferPointer, value, NULL, 0); } else { - cx_hash_no_throw(&btchip_context_D.transactionHashFull.sha256.header, 0, + (void)cx_hash_no_throw(&btchip_context_D.transactionHashFull.sha256.header, 0, btchip_context_D.transactionBufferPointer, value, NULL, 0); } } if ((btchip_context_D.transactionHashOption & TRANSACTION_HASH_AUTHORIZATION) != 0) { PRINTF("--- ADD TO HASH AUTH:\n%.*H\n", value, btchip_context_D.transactionBufferPointer); - cx_hash_no_throw(&btchip_context_D.transactionHashAuthorization.header, 0, + (void)cx_hash_no_throw(&btchip_context_D.transactionHashAuthorization.header, 0, btchip_context_D.transactionBufferPointer, value, NULL, 0); } }