From 82281d768d4b29752466af4f3e48932b3caca073 Mon Sep 17 00:00:00 2001 From: Xavier Chapron Date: Tue, 19 Dec 2023 17:55:20 +0100 Subject: [PATCH] src: Fix cx call WARN_UNUSED_RESULT warnings --- include/crypto.h | 10 ++++++ src/crypto.c | 17 ++++++++-- src/ctap2_client_pin.c | 18 ++++++++--- src/u2f_processing.c | 73 ++++++++++++++++++++---------------------- 4 files changed, 73 insertions(+), 45 deletions(-) diff --git a/include/crypto.h b/include/crypto.h index 323c42d8..b84a382f 100644 --- a/include/crypto.h +++ b/include/crypto.h @@ -19,6 +19,16 @@ #ifndef __CRYPTO_H__ #define __CRYPTO_H__ +/** + * Wrapper around cx_hash_no_throw with an assert in case of failure + */ +void crypto_hash(cx_hash_t *hash, + uint32_t mode, + const uint8_t *in, + size_t len, + uint8_t *out, + size_t out_len); + /** * Compare two buffer a and b. * Return true if they match, else false. diff --git a/src/crypto.c b/src/crypto.c index b550a698..d91abf8f 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -20,6 +20,7 @@ #include "os.h" #include "cx.h" +#include "ledger_assert.h" #include "config.h" #include "crypto_data.h" @@ -30,6 +31,18 @@ #define ROLE_CRED_RANDOM_KEY_UV 1 #define ROLE_CRED_RANDOM_KEY_NO_UV 2 +void crypto_hash(cx_hash_t *hash, + uint32_t mode, + const uint8_t *in, + size_t len, + uint8_t *out, + size_t out_len) { + cx_err_t cx_err; + + cx_err = cx_hash_no_throw(hash, mode, in, len, out, out_len); + LEDGER_ASSERT(cx_err == CX_OK, "cx_hash_no_throw fail"); +} + bool crypto_compare(const uint8_t *a, const uint8_t *b, uint16_t length) { uint16_t given_length = length; uint8_t status = 0; @@ -56,8 +69,8 @@ void crypto_compute_sha256(const uint8_t *in1, cx_sha256_t hash; cx_sha256_init(&hash); - cx_hash_no_throw(&hash.header, 0, in1, in1_len, NULL, 0); - cx_hash_no_throw(&hash.header, CX_LAST, in2, in2_len, out, CX_SHA256_SIZE); + crypto_hash(&hash.header, 0, in1, in1_len, NULL, 0); + crypto_hash(&hash.header, CX_LAST, in2, in2_len, out, CX_SHA256_SIZE); } int crypto_generate_private_key(const uint8_t *nonce, diff --git a/src/ctap2_client_pin.c b/src/ctap2_client_pin.c index d7cf5510..2111f185 100644 --- a/src/ctap2_client_pin.c +++ b/src/ctap2_client_pin.c @@ -18,6 +18,7 @@ #include "os.h" #include "cx.h" +#include "ledger_assert.h" #include "ctap2.h" #include "config.h" @@ -177,10 +178,19 @@ bool ctap2_client_pin_verify(int protocol, cx_hmac_sha256(key, keyLen, msg, msgLength, hmacValue, CX_SHA256_SIZE); } else { cx_hmac_sha256_t hmac; - - cx_hmac_sha256_init_no_throw(&hmac, key, keyLen); - cx_hmac_no_throw((cx_hmac_t *) &hmac, 0, msg, msgLength, NULL, 0); - cx_hmac_no_throw((cx_hmac_t *) &hmac, CX_LAST, msg2, msg2Len, hmacValue, CX_SHA256_SIZE); + cx_err_t cx_err; + + cx_err = cx_hmac_sha256_init_no_throw(&hmac, key, keyLen); + LEDGER_ASSERT(cx_err == CX_OK, "cx_hmac_sha256_init_no_throw fail"); + cx_err = cx_hmac_no_throw((cx_hmac_t *) &hmac, 0, msg, msgLength, NULL, 0); + LEDGER_ASSERT(cx_err == CX_OK, "cx_hmac_no_throw fail"); + cx_err = cx_hmac_no_throw((cx_hmac_t *) &hmac, + CX_LAST, + msg2, + msg2Len, + hmacValue, + CX_SHA256_SIZE); + LEDGER_ASSERT(cx_err == CX_OK, "cx_hmac_no_throw fail"); } if (!crypto_compare(signature, hmacValue, signatureLength)) { diff --git a/src/u2f_processing.c b/src/u2f_processing.c index a8f3d629..a92134c4 100644 --- a/src/u2f_processing.c +++ b/src/u2f_processing.c @@ -226,26 +226,26 @@ static void u2f_compute_enroll_response_hash(u2f_reg_resp_base_t *reg_resp_base, cx_sha256_t hash; cx_sha256_init(&hash); - cx_hash_no_throw(&hash.header, 0, DUMMY_ZERO, 1, NULL, 0); - cx_hash_no_throw(&hash.header, - 0, - globals_get_u2f_data()->application_param, - sizeof(globals_get_u2f_data()->application_param), - NULL, - 0); - cx_hash_no_throw(&hash.header, - 0, - globals_get_u2f_data()->challenge_param, - sizeof(globals_get_u2f_data()->challenge_param), - NULL, - 0); - cx_hash_no_throw(&hash.header, 0, reg_resp_base->key_handle, key_handle_length, NULL, 0); - cx_hash_no_throw(&hash.header, - CX_LAST, - reg_resp_base->user_key, - sizeof(reg_resp_base->user_key), - data_hash, - CX_SHA256_SIZE); + crypto_hash(&hash.header, 0, DUMMY_ZERO, 1, NULL, 0); + crypto_hash(&hash.header, + 0, + globals_get_u2f_data()->application_param, + sizeof(globals_get_u2f_data()->application_param), + NULL, + 0); + crypto_hash(&hash.header, + 0, + globals_get_u2f_data()->challenge_param, + sizeof(globals_get_u2f_data()->challenge_param), + NULL, + 0); + crypto_hash(&hash.header, 0, reg_resp_base->key_handle, key_handle_length, NULL, 0); + crypto_hash(&hash.header, + CX_LAST, + reg_resp_base->user_key, + sizeof(reg_resp_base->user_key), + data_hash, + CX_SHA256_SIZE); } static int u2f_prepare_enroll_response(void) { @@ -322,25 +322,20 @@ static void u2f_compute_sign_response_hash(u2f_auth_resp_base_t *auth_resp_base, cx_sha256_t hash; cx_sha256_init(&hash); - cx_hash_no_throw(&hash.header, - 0, - globals_get_u2f_data()->application_param, - sizeof(globals_get_u2f_data()->application_param), - NULL, - 0); - cx_hash_no_throw(&hash.header, 0, DUMMY_USER_PRESENCE, 1, NULL, 0); - cx_hash_no_throw(&hash.header, - 0, - auth_resp_base->counter, - sizeof(auth_resp_base->counter), - NULL, - 0); - cx_hash_no_throw(&hash.header, - CX_LAST, - globals_get_u2f_data()->challenge_param, - sizeof(globals_get_u2f_data()->challenge_param), - data_hash, - CX_SHA256_SIZE); + crypto_hash(&hash.header, + 0, + globals_get_u2f_data()->application_param, + sizeof(globals_get_u2f_data()->application_param), + NULL, + 0); + crypto_hash(&hash.header, 0, DUMMY_USER_PRESENCE, 1, NULL, 0); + crypto_hash(&hash.header, 0, auth_resp_base->counter, sizeof(auth_resp_base->counter), NULL, 0); + crypto_hash(&hash.header, + CX_LAST, + globals_get_u2f_data()->challenge_param, + sizeof(globals_get_u2f_data()->challenge_param), + data_hash, + CX_SHA256_SIZE); } static int u2f_prepare_sign_response(void) {