From bd0e3c900674d73ad567832023e8ead1d44b13e0 Mon Sep 17 00:00:00 2001 From: Alexander Kopachov Date: Fri, 4 Nov 2022 08:55:05 +0100 Subject: [PATCH] Reduced code size to avoid weird issue with COMPACT=1 DEBUG=0 build (#19) --- scenes/add_new_token/totp_input_text.c | 3 + .../add_new_token/totp_scene_add_new_token.c | 6 +- scenes/app_settings/totp_app_settings.c | 1 + scenes/authenticate/totp_scene_authenticate.c | 1 + .../totp_scene_generate_token.c | 20 ++-- scenes/token_menu/totp_scene_token_menu.c | 1 + services/cli/cli_helpers.c | 2 +- services/cli/commands/add/add.c | 3 +- services/config/config.c | 7 +- services/crypto/crypto.c | 7 +- services/crypto/memset_s.h | 2 +- services/hmac/sha1.c | 107 +++++------------- services/hmac/sha256.c | 79 +++---------- services/hmac/sha512.c | 95 +++------------- services/hmac/u64.c | 20 ---- services/list/list.c | 6 +- services/list/list.h | 15 +-- services/totp/totp.c | 1 + totp_app.c | 1 + types/token_info.c | 4 +- 20 files changed, 104 insertions(+), 277 deletions(-) delete mode 100644 services/hmac/u64.c diff --git a/scenes/add_new_token/totp_input_text.c b/scenes/add_new_token/totp_input_text.c index 811cd29763f..8ae8d32b019 100644 --- a/scenes/add_new_token/totp_input_text.c +++ b/scenes/add_new_token/totp_input_text.c @@ -42,9 +42,11 @@ static void commit_text_input_callback(void* context) { InputTextSceneState* text_input_state = (InputTextSceneState*)context; if(text_input_state->callback != 0) { InputTextSceneCallbackResult* result = malloc(sizeof(InputTextSceneCallbackResult)); + furi_check(result != NULL); result->user_input_length = strnlen(text_input_state->text_input_buffer, INPUT_BUFFER_SIZE); result->user_input = malloc(result->user_input_length + 1); + furi_check(result->user_input != NULL); result->callback_data = text_input_state->callback_data; strlcpy( result->user_input, @@ -56,6 +58,7 @@ static void commit_text_input_callback(void* context) { InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context) { InputTextSceneState* text_input_state = malloc(sizeof(InputTextSceneState)); + furi_check(text_input_state != NULL); text_input_state->text_input = text_input_alloc(); text_input_state->text_input_view = text_input_get_view(text_input_state->text_input); text_input_state->callback = context->callback; diff --git a/scenes/add_new_token/totp_scene_add_new_token.c b/scenes/add_new_token/totp_scene_add_new_token.c index 248b6d86e0b..2c9973789ef 100644 --- a/scenes/add_new_token/totp_scene_add_new_token.c +++ b/scenes/add_new_token/totp_scene_add_new_token.c @@ -68,6 +68,7 @@ void totp_scene_add_new_token_activate( PluginState* plugin_state, const TokenAddEditSceneContext* context) { SceneState* scene_state = malloc(sizeof(SceneState)); + furi_check(scene_state != NULL); plugin_state->current_scene_state = scene_state; scene_state->token_name = "Name"; scene_state->token_name_length = strlen(scene_state->token_name); @@ -75,11 +76,13 @@ void totp_scene_add_new_token_activate( scene_state->token_secret_length = strlen(scene_state->token_secret); scene_state->token_name_input_context = malloc(sizeof(InputTextSceneContext)); + furi_check(scene_state->token_name_input_context != NULL); scene_state->token_name_input_context->header_text = "Enter token name"; scene_state->token_name_input_context->callback_data = scene_state; scene_state->token_name_input_context->callback = on_token_name_user_comitted; scene_state->token_secret_input_context = malloc(sizeof(InputTextSceneContext)); + furi_check(scene_state->token_secret_input_context != NULL); scene_state->token_secret_input_context->header_text = "Enter token secret"; scene_state->token_secret_input_context->callback_data = scene_state; scene_state->token_secret_input_context->callback = on_token_secret_user_comitted; @@ -246,12 +249,13 @@ bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState if(token_secret_set) { tokenInfo->name = malloc(scene_state->token_name_length + 1); + furi_check(tokenInfo->name != NULL); strlcpy( tokenInfo->name, scene_state->token_name, scene_state->token_name_length + 1); tokenInfo->algo = scene_state->algo; tokenInfo->digits = scene_state->digits_count; - TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo); + TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check); plugin_state->tokens_count++; totp_config_file_save_new_token(tokenInfo); diff --git a/scenes/app_settings/totp_app_settings.c b/scenes/app_settings/totp_app_settings.c index a8ca1a167c3..980688e7377 100644 --- a/scenes/app_settings/totp_app_settings.c +++ b/scenes/app_settings/totp_app_settings.c @@ -26,6 +26,7 @@ void totp_scene_app_settings_activate( PluginState* plugin_state, const AppSettingsSceneContext* context) { SceneState* scene_state = malloc(sizeof(SceneState)); + furi_check(scene_state != NULL); plugin_state->current_scene_state = scene_state; if(context != NULL) { TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index); diff --git a/scenes/authenticate/totp_scene_authenticate.c b/scenes/authenticate/totp_scene_authenticate.c index 8c7715059df..a6458b3edd2 100644 --- a/scenes/authenticate/totp_scene_authenticate.c +++ b/scenes/authenticate/totp_scene_authenticate.c @@ -25,6 +25,7 @@ void totp_scene_authenticate_init(PluginState* plugin_state) { void totp_scene_authenticate_activate(PluginState* plugin_state) { SceneState* scene_state = malloc(sizeof(SceneState)); + furi_check(scene_state != NULL); scene_state->code_length = 0; memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH); plugin_state->current_scene_state = scene_state; diff --git a/scenes/generate_token/totp_scene_generate_token.c b/scenes/generate_token/totp_scene_generate_token.c index 0ccab0d98d3..7d52f9f5921 100644 --- a/scenes/generate_token/totp_scene_generate_token.c +++ b/scenes/generate_token/totp_scene_generate_token.c @@ -37,24 +37,17 @@ static const NotificationSequence sequence_short_vibro_and_sound = { }; static void i_token_to_str(uint32_t i_token_code, char* str, TokenDigitsCount len) { + uint8_t str_token_length = 0; if(len == TOTP_8_DIGITS) { str[8] = '\0'; + str_token_length = 8; } else if(len == TOTP_6_DIGITS) { str[6] = '\0'; + str_token_length = 6; } - if(i_token_code == 0) { - if(len > TOTP_6_DIGITS) { - str[7] = '-'; - str[6] = '-'; - } - - str[5] = '-'; - str[4] = '-'; - str[3] = '-'; - str[2] = '-'; - str[1] = '-'; - str[0] = '-'; + if(i_token_code == OTP_ERROR) { + memset(&str[0], '-', str_token_length); } else { if(len == TOTP_8_DIGITS) { str[7] = DIGIT_TO_CHAR(i_token_code % 10); @@ -132,6 +125,7 @@ void totp_scene_generate_token_activate( } } SceneState* scene_state = malloc(sizeof(SceneState)); + furi_check(scene_state != NULL); if(context == NULL || context->current_token_index > plugin_state->tokens_count) { scene_state->current_token_index = 0; } else { @@ -197,7 +191,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_ TOKEN_LIFETIME), scene_state->last_code, tokenInfo->digits); - memset_s(key, sizeof(key), 0, key_length); + memset_s(key, key_length, 0, key_length); free(key); } else { i_token_to_str(0, scene_state->last_code, tokenInfo->digits); diff --git a/scenes/token_menu/totp_scene_token_menu.c b/scenes/token_menu/totp_scene_token_menu.c index 44a429e2fac..1445c7b65ed 100644 --- a/scenes/token_menu/totp_scene_token_menu.c +++ b/scenes/token_menu/totp_scene_token_menu.c @@ -31,6 +31,7 @@ void totp_scene_token_menu_activate( PluginState* plugin_state, const TokenMenuSceneContext* context) { SceneState* scene_state = malloc(sizeof(SceneState)); + furi_check(scene_state != NULL); plugin_state->current_scene_state = scene_state; if(context != NULL) { TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index); diff --git a/services/cli/cli_helpers.c b/services/cli/cli_helpers.c index 4a0b8b35273..0bea6fd9066 100644 --- a/services/cli/cli_helpers.c +++ b/services/cli/cli_helpers.c @@ -12,7 +12,7 @@ bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) { TOTP_CLI_DELETE_LAST_LINE(); - if(plugin_state->current_scene == TotpSceneAuthentication) { + if(plugin_state->current_scene == TotpSceneAuthentication) { //-V547 return false; } } diff --git a/services/cli/commands/add/add.c b/services/cli/commands/add/add.c index 31548c59e51..fa29a912990 100644 --- a/services/cli/commands/add/add.c +++ b/services/cli/commands/add/add.c @@ -138,6 +138,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl size_t temp_cstr_len = furi_string_size(temp_str); token_info->name = malloc(temp_cstr_len + 1); + furi_check(token_info->name != NULL); strlcpy(token_info->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1); // Read optional arguments @@ -211,7 +212,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl load_generate_token_scene = true; } - TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info); + TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check); plugin_state->tokens_count++; totp_config_file_save_new_token(token_info); diff --git a/services/config/config.c b/services/config/config.c index e751f7990de..d458d682f27 100644 --- a/services/config/config.c +++ b/services/config/config.c @@ -283,6 +283,7 @@ void totp_config_file_load_base(PluginState* const plugin_state) { if(flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) && crypto_size > 0) { plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size); + furi_check(plugin_state->crypto_verify_data != NULL); plugin_state->crypto_verify_data_length = crypto_size; if(!flipper_format_read_hex( fff_data_file, @@ -344,7 +345,8 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) TokenInfo* tokenInfo = token_info_alloc(); size_t temp_cstr_len = furi_string_size(temp_str); - tokenInfo->name = (char*)malloc(temp_cstr_len + 1); + tokenInfo->name = malloc(temp_cstr_len + 1); + furi_check(tokenInfo->name != NULL); strlcpy(tokenInfo->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1); uint32_t secret_bytes_count; @@ -378,6 +380,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) tokenInfo->token_length = secret_bytes_count; if(secret_bytes_count > 0) { tokenInfo->token = malloc(tokenInfo->token_length); + furi_check(tokenInfo->token != NULL); if(!flipper_format_read_hex( fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, @@ -409,7 +412,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name); - TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo); + TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check); index++; } diff --git a/services/crypto/crypto.c b/services/crypto/crypto.c index 79e41e6fd36..794d0b0be0c 100644 --- a/services/crypto/crypto.c +++ b/services/crypto/crypto.c @@ -20,20 +20,23 @@ uint8_t* totp_crypto_encrypt( if(remain) { size_t plain_data_aligned_length = plain_data_length - remain + CRYPTO_ALIGNMENT_FACTOR; uint8_t* plain_data_aligned = malloc(plain_data_aligned_length); + furi_check(plain_data_aligned != NULL); memset(plain_data_aligned, 0, plain_data_aligned_length); memcpy(plain_data_aligned, plain_data, plain_data_length); encrypted_data = malloc(plain_data_aligned_length); + furi_check(encrypted_data != NULL); *encrypted_data_length = plain_data_aligned_length; furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv); furi_hal_crypto_encrypt(plain_data_aligned, encrypted_data, plain_data_aligned_length); furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT); - memset_s(plain_data_aligned, sizeof(plain_data_aligned), 0, plain_data_aligned_length); + memset_s(plain_data_aligned, plain_data_aligned_length, 0, plain_data_aligned_length); free(plain_data_aligned); } else { encrypted_data = malloc(plain_data_length); + furi_check(encrypted_data != NULL); *encrypted_data_length = plain_data_length; furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv); @@ -51,6 +54,7 @@ uint8_t* totp_crypto_decrypt( size_t* decrypted_data_length) { *decrypted_data_length = encrypted_data_length; uint8_t* decrypted_data = malloc(*decrypted_data_length); + furi_check(decrypted_data != NULL); furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv); furi_hal_crypto_decrypt(encrypted_data, decrypted_data, encrypted_data_length); furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT); @@ -93,6 +97,7 @@ void totp_crypto_seed_iv(PluginState* plugin_state, const uint8_t* pin, uint8_t if(plugin_state->crypto_verify_data == NULL) { FURI_LOG_D(LOGGING_TAG, "Generating crypto verify data"); plugin_state->crypto_verify_data = malloc(CRYPTO_VERIFY_KEY_LENGTH); + furi_check(plugin_state->crypto_verify_data != NULL); plugin_state->crypto_verify_data_length = CRYPTO_VERIFY_KEY_LENGTH; Storage* storage = totp_open_storage(); FlipperFormat* config_file = totp_open_config_file(storage); diff --git a/services/crypto/memset_s.h b/services/crypto/memset_s.h index 2889e23b24b..060c6642b47 100644 --- a/services/crypto/memset_s.h +++ b/services/crypto/memset_s.h @@ -9,7 +9,7 @@ typedef uint64_t rsize_t; #define _RSIZE_T_DECLARED #endif #ifndef _ERRNOT_DECLARED -typedef int16_t errno_t; +typedef int16_t errno_t; //-V677 #define _ERRNOT_DECLARED #endif diff --git a/services/hmac/sha1.c b/services/hmac/sha1.c index 5679e489d86..578fb826bb3 100644 --- a/services/hmac/sha1.c +++ b/services/hmac/sha1.c @@ -146,7 +146,7 @@ void sha1_process_bytes(const void* buffer, size_t len, struct sha1_ctx* ctx) { #define UNALIGNED_P(p) ((uintptr_t)(p) % sizeof(uint32_t) != 0) if(UNALIGNED_P(buffer)) while(len > 64) { - sha1_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); + sha1_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); //-V1086 buffer = (const char*)buffer + 64; len -= 64; } @@ -232,86 +232,31 @@ void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx) { words++; } - R(a, b, c, d, e, F1, K1, x[0]); - R(e, a, b, c, d, F1, K1, x[1]); - R(d, e, a, b, c, F1, K1, x[2]); - R(c, d, e, a, b, F1, K1, x[3]); - R(b, c, d, e, a, F1, K1, x[4]); - R(a, b, c, d, e, F1, K1, x[5]); - R(e, a, b, c, d, F1, K1, x[6]); - R(d, e, a, b, c, F1, K1, x[7]); - R(c, d, e, a, b, F1, K1, x[8]); - R(b, c, d, e, a, F1, K1, x[9]); - R(a, b, c, d, e, F1, K1, x[10]); - R(e, a, b, c, d, F1, K1, x[11]); - R(d, e, a, b, c, F1, K1, x[12]); - R(c, d, e, a, b, F1, K1, x[13]); - R(b, c, d, e, a, F1, K1, x[14]); - R(a, b, c, d, e, F1, K1, x[15]); - R(e, a, b, c, d, F1, K1, M(16)); - R(d, e, a, b, c, F1, K1, M(17)); - R(c, d, e, a, b, F1, K1, M(18)); - R(b, c, d, e, a, F1, K1, M(19)); - R(a, b, c, d, e, F2, K2, M(20)); - R(e, a, b, c, d, F2, K2, M(21)); - R(d, e, a, b, c, F2, K2, M(22)); - R(c, d, e, a, b, F2, K2, M(23)); - R(b, c, d, e, a, F2, K2, M(24)); - R(a, b, c, d, e, F2, K2, M(25)); - R(e, a, b, c, d, F2, K2, M(26)); - R(d, e, a, b, c, F2, K2, M(27)); - R(c, d, e, a, b, F2, K2, M(28)); - R(b, c, d, e, a, F2, K2, M(29)); - R(a, b, c, d, e, F2, K2, M(30)); - R(e, a, b, c, d, F2, K2, M(31)); - R(d, e, a, b, c, F2, K2, M(32)); - R(c, d, e, a, b, F2, K2, M(33)); - R(b, c, d, e, a, F2, K2, M(34)); - R(a, b, c, d, e, F2, K2, M(35)); - R(e, a, b, c, d, F2, K2, M(36)); - R(d, e, a, b, c, F2, K2, M(37)); - R(c, d, e, a, b, F2, K2, M(38)); - R(b, c, d, e, a, F2, K2, M(39)); - R(a, b, c, d, e, F3, K3, M(40)); - R(e, a, b, c, d, F3, K3, M(41)); - R(d, e, a, b, c, F3, K3, M(42)); - R(c, d, e, a, b, F3, K3, M(43)); - R(b, c, d, e, a, F3, K3, M(44)); - R(a, b, c, d, e, F3, K3, M(45)); - R(e, a, b, c, d, F3, K3, M(46)); - R(d, e, a, b, c, F3, K3, M(47)); - R(c, d, e, a, b, F3, K3, M(48)); - R(b, c, d, e, a, F3, K3, M(49)); - R(a, b, c, d, e, F3, K3, M(50)); - R(e, a, b, c, d, F3, K3, M(51)); - R(d, e, a, b, c, F3, K3, M(52)); - R(c, d, e, a, b, F3, K3, M(53)); - R(b, c, d, e, a, F3, K3, M(54)); - R(a, b, c, d, e, F3, K3, M(55)); - R(e, a, b, c, d, F3, K3, M(56)); - R(d, e, a, b, c, F3, K3, M(57)); - R(c, d, e, a, b, F3, K3, M(58)); - R(b, c, d, e, a, F3, K3, M(59)); - R(a, b, c, d, e, F4, K4, M(60)); - R(e, a, b, c, d, F4, K4, M(61)); - R(d, e, a, b, c, F4, K4, M(62)); - R(c, d, e, a, b, F4, K4, M(63)); - R(b, c, d, e, a, F4, K4, M(64)); - R(a, b, c, d, e, F4, K4, M(65)); - R(e, a, b, c, d, F4, K4, M(66)); - R(d, e, a, b, c, F4, K4, M(67)); - R(c, d, e, a, b, F4, K4, M(68)); - R(b, c, d, e, a, F4, K4, M(69)); - R(a, b, c, d, e, F4, K4, M(70)); - R(e, a, b, c, d, F4, K4, M(71)); - R(d, e, a, b, c, F4, K4, M(72)); - R(c, d, e, a, b, F4, K4, M(73)); - R(b, c, d, e, a, F4, K4, M(74)); - R(a, b, c, d, e, F4, K4, M(75)); - R(e, a, b, c, d, F4, K4, M(76)); - R(d, e, a, b, c, F4, K4, M(77)); - R(c, d, e, a, b, F4, K4, M(78)); - R(b, c, d, e, a, F4, K4, M(79)); + for (int i = 0; i < 80; i++) { + uint32_t xx = i < 16 ? x[i] : M(i); + uint32_t ki = i / 20; + switch (ki) { + case 0: + R(a, b, c, d, e, F1, K1, xx); + break; + case 1: + R(a, b, c, d, e, F2, K2, xx); + break; + case 2: + R(a, b, c, d, e, F3, K3, xx); + break; + default: + R(a, b, c, d, e, F4, K4, xx); + break; + } + + uint32_t tt = a; + a = e; + e = d; + d = c; + c = b; + b = tt; + } a = ctx->A += a; b = ctx->B += b; diff --git a/services/hmac/sha256.c b/services/hmac/sha256.c index 2ea63dd37b9..02409f0d4ad 100644 --- a/services/hmac/sha256.c +++ b/services/hmac/sha256.c @@ -194,7 +194,7 @@ void sha256_process_bytes(const void* buffer, size_t len, struct sha256_ctx* ctx #define UNALIGNED_P(p) ((uintptr_t)(p) % sizeof(uint32_t) != 0) if(UNALIGNED_P(buffer)) while(len > 64) { - sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); + sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); //-V1086 buffer = (const char*)buffer + 64; len -= 64; } @@ -299,70 +299,19 @@ void sha256_process_block(const void* buffer, size_t len, struct sha256_ctx* ctx words++; } - R(a, b, c, d, e, f, g, h, K(0), x[0]); - R(h, a, b, c, d, e, f, g, K(1), x[1]); - R(g, h, a, b, c, d, e, f, K(2), x[2]); - R(f, g, h, a, b, c, d, e, K(3), x[3]); - R(e, f, g, h, a, b, c, d, K(4), x[4]); - R(d, e, f, g, h, a, b, c, K(5), x[5]); - R(c, d, e, f, g, h, a, b, K(6), x[6]); - R(b, c, d, e, f, g, h, a, K(7), x[7]); - R(a, b, c, d, e, f, g, h, K(8), x[8]); - R(h, a, b, c, d, e, f, g, K(9), x[9]); - R(g, h, a, b, c, d, e, f, K(10), x[10]); - R(f, g, h, a, b, c, d, e, K(11), x[11]); - R(e, f, g, h, a, b, c, d, K(12), x[12]); - R(d, e, f, g, h, a, b, c, K(13), x[13]); - R(c, d, e, f, g, h, a, b, K(14), x[14]); - R(b, c, d, e, f, g, h, a, K(15), x[15]); - R(a, b, c, d, e, f, g, h, K(16), M(16)); - R(h, a, b, c, d, e, f, g, K(17), M(17)); - R(g, h, a, b, c, d, e, f, K(18), M(18)); - R(f, g, h, a, b, c, d, e, K(19), M(19)); - R(e, f, g, h, a, b, c, d, K(20), M(20)); - R(d, e, f, g, h, a, b, c, K(21), M(21)); - R(c, d, e, f, g, h, a, b, K(22), M(22)); - R(b, c, d, e, f, g, h, a, K(23), M(23)); - R(a, b, c, d, e, f, g, h, K(24), M(24)); - R(h, a, b, c, d, e, f, g, K(25), M(25)); - R(g, h, a, b, c, d, e, f, K(26), M(26)); - R(f, g, h, a, b, c, d, e, K(27), M(27)); - R(e, f, g, h, a, b, c, d, K(28), M(28)); - R(d, e, f, g, h, a, b, c, K(29), M(29)); - R(c, d, e, f, g, h, a, b, K(30), M(30)); - R(b, c, d, e, f, g, h, a, K(31), M(31)); - R(a, b, c, d, e, f, g, h, K(32), M(32)); - R(h, a, b, c, d, e, f, g, K(33), M(33)); - R(g, h, a, b, c, d, e, f, K(34), M(34)); - R(f, g, h, a, b, c, d, e, K(35), M(35)); - R(e, f, g, h, a, b, c, d, K(36), M(36)); - R(d, e, f, g, h, a, b, c, K(37), M(37)); - R(c, d, e, f, g, h, a, b, K(38), M(38)); - R(b, c, d, e, f, g, h, a, K(39), M(39)); - R(a, b, c, d, e, f, g, h, K(40), M(40)); - R(h, a, b, c, d, e, f, g, K(41), M(41)); - R(g, h, a, b, c, d, e, f, K(42), M(42)); - R(f, g, h, a, b, c, d, e, K(43), M(43)); - R(e, f, g, h, a, b, c, d, K(44), M(44)); - R(d, e, f, g, h, a, b, c, K(45), M(45)); - R(c, d, e, f, g, h, a, b, K(46), M(46)); - R(b, c, d, e, f, g, h, a, K(47), M(47)); - R(a, b, c, d, e, f, g, h, K(48), M(48)); - R(h, a, b, c, d, e, f, g, K(49), M(49)); - R(g, h, a, b, c, d, e, f, K(50), M(50)); - R(f, g, h, a, b, c, d, e, K(51), M(51)); - R(e, f, g, h, a, b, c, d, K(52), M(52)); - R(d, e, f, g, h, a, b, c, K(53), M(53)); - R(c, d, e, f, g, h, a, b, K(54), M(54)); - R(b, c, d, e, f, g, h, a, K(55), M(55)); - R(a, b, c, d, e, f, g, h, K(56), M(56)); - R(h, a, b, c, d, e, f, g, K(57), M(57)); - R(g, h, a, b, c, d, e, f, K(58), M(58)); - R(f, g, h, a, b, c, d, e, K(59), M(59)); - R(e, f, g, h, a, b, c, d, K(60), M(60)); - R(d, e, f, g, h, a, b, c, K(61), M(61)); - R(c, d, e, f, g, h, a, b, K(62), M(62)); - R(b, c, d, e, f, g, h, a, K(63), M(63)); + for (int i = 0; i < 64; i++) { + uint32_t xx = i < 16 ? x[i] : M(i); + R(a, b, c, d, e, f, g, h, K(i), xx); + uint32_t tt = a; + a = h; + h = g; + g = f; + f = e; + e = d; + d = c; + c = b; + b = tt; + } a = ctx->state[0] += a; b = ctx->state[1] += b; diff --git a/services/hmac/sha512.c b/services/hmac/sha512.c index 53d3bad2e60..8b17d271774 100644 --- a/services/hmac/sha512.c +++ b/services/hmac/sha512.c @@ -196,7 +196,7 @@ void sha512_process_bytes(const void* buffer, size_t len, struct sha512_ctx* ctx #define UNALIGNED_P(p) ((uintptr_t)(p) % sizeof(u64) != 0) if(UNALIGNED_P(buffer)) while(len > 128) { - sha512_process_block(memcpy(ctx->buffer, buffer, 128), 128, ctx); + sha512_process_block(memcpy(ctx->buffer, buffer, 128), 128, ctx); //-V1086 buffer = (const char*)buffer + 128; len -= 128; } @@ -328,86 +328,19 @@ void sha512_process_block(const void* buffer, size_t len, struct sha512_ctx* ctx words++; } - R(a, b, c, d, e, f, g, h, K(0), x[0]); - R(h, a, b, c, d, e, f, g, K(1), x[1]); - R(g, h, a, b, c, d, e, f, K(2), x[2]); - R(f, g, h, a, b, c, d, e, K(3), x[3]); - R(e, f, g, h, a, b, c, d, K(4), x[4]); - R(d, e, f, g, h, a, b, c, K(5), x[5]); - R(c, d, e, f, g, h, a, b, K(6), x[6]); - R(b, c, d, e, f, g, h, a, K(7), x[7]); - R(a, b, c, d, e, f, g, h, K(8), x[8]); - R(h, a, b, c, d, e, f, g, K(9), x[9]); - R(g, h, a, b, c, d, e, f, K(10), x[10]); - R(f, g, h, a, b, c, d, e, K(11), x[11]); - R(e, f, g, h, a, b, c, d, K(12), x[12]); - R(d, e, f, g, h, a, b, c, K(13), x[13]); - R(c, d, e, f, g, h, a, b, K(14), x[14]); - R(b, c, d, e, f, g, h, a, K(15), x[15]); - R(a, b, c, d, e, f, g, h, K(16), M(16)); - R(h, a, b, c, d, e, f, g, K(17), M(17)); - R(g, h, a, b, c, d, e, f, K(18), M(18)); - R(f, g, h, a, b, c, d, e, K(19), M(19)); - R(e, f, g, h, a, b, c, d, K(20), M(20)); - R(d, e, f, g, h, a, b, c, K(21), M(21)); - R(c, d, e, f, g, h, a, b, K(22), M(22)); - R(b, c, d, e, f, g, h, a, K(23), M(23)); - R(a, b, c, d, e, f, g, h, K(24), M(24)); - R(h, a, b, c, d, e, f, g, K(25), M(25)); - R(g, h, a, b, c, d, e, f, K(26), M(26)); - R(f, g, h, a, b, c, d, e, K(27), M(27)); - R(e, f, g, h, a, b, c, d, K(28), M(28)); - R(d, e, f, g, h, a, b, c, K(29), M(29)); - R(c, d, e, f, g, h, a, b, K(30), M(30)); - R(b, c, d, e, f, g, h, a, K(31), M(31)); - R(a, b, c, d, e, f, g, h, K(32), M(32)); - R(h, a, b, c, d, e, f, g, K(33), M(33)); - R(g, h, a, b, c, d, e, f, K(34), M(34)); - R(f, g, h, a, b, c, d, e, K(35), M(35)); - R(e, f, g, h, a, b, c, d, K(36), M(36)); - R(d, e, f, g, h, a, b, c, K(37), M(37)); - R(c, d, e, f, g, h, a, b, K(38), M(38)); - R(b, c, d, e, f, g, h, a, K(39), M(39)); - R(a, b, c, d, e, f, g, h, K(40), M(40)); - R(h, a, b, c, d, e, f, g, K(41), M(41)); - R(g, h, a, b, c, d, e, f, K(42), M(42)); - R(f, g, h, a, b, c, d, e, K(43), M(43)); - R(e, f, g, h, a, b, c, d, K(44), M(44)); - R(d, e, f, g, h, a, b, c, K(45), M(45)); - R(c, d, e, f, g, h, a, b, K(46), M(46)); - R(b, c, d, e, f, g, h, a, K(47), M(47)); - R(a, b, c, d, e, f, g, h, K(48), M(48)); - R(h, a, b, c, d, e, f, g, K(49), M(49)); - R(g, h, a, b, c, d, e, f, K(50), M(50)); - R(f, g, h, a, b, c, d, e, K(51), M(51)); - R(e, f, g, h, a, b, c, d, K(52), M(52)); - R(d, e, f, g, h, a, b, c, K(53), M(53)); - R(c, d, e, f, g, h, a, b, K(54), M(54)); - R(b, c, d, e, f, g, h, a, K(55), M(55)); - R(a, b, c, d, e, f, g, h, K(56), M(56)); - R(h, a, b, c, d, e, f, g, K(57), M(57)); - R(g, h, a, b, c, d, e, f, K(58), M(58)); - R(f, g, h, a, b, c, d, e, K(59), M(59)); - R(e, f, g, h, a, b, c, d, K(60), M(60)); - R(d, e, f, g, h, a, b, c, K(61), M(61)); - R(c, d, e, f, g, h, a, b, K(62), M(62)); - R(b, c, d, e, f, g, h, a, K(63), M(63)); - R(a, b, c, d, e, f, g, h, K(64), M(64)); - R(h, a, b, c, d, e, f, g, K(65), M(65)); - R(g, h, a, b, c, d, e, f, K(66), M(66)); - R(f, g, h, a, b, c, d, e, K(67), M(67)); - R(e, f, g, h, a, b, c, d, K(68), M(68)); - R(d, e, f, g, h, a, b, c, K(69), M(69)); - R(c, d, e, f, g, h, a, b, K(70), M(70)); - R(b, c, d, e, f, g, h, a, K(71), M(71)); - R(a, b, c, d, e, f, g, h, K(72), M(72)); - R(h, a, b, c, d, e, f, g, K(73), M(73)); - R(g, h, a, b, c, d, e, f, K(74), M(74)); - R(f, g, h, a, b, c, d, e, K(75), M(75)); - R(e, f, g, h, a, b, c, d, K(76), M(76)); - R(d, e, f, g, h, a, b, c, K(77), M(77)); - R(c, d, e, f, g, h, a, b, K(78), M(78)); - R(b, c, d, e, f, g, h, a, K(79), M(79)); + for (int i = 0; i < 80; i++) { + u64 xx = i < 16 ? x[i] : M(i); + R(a, b, c, d, e, f, g, h, K(i), xx); + u64 tt = a; + a = h; + h = g; + g = f; + f = e; + e = d; + d = c; + c = b; + b = tt; + } a = ctx->state[0] = u64plus(ctx->state[0], a); b = ctx->state[1] = u64plus(ctx->state[1], b); diff --git a/services/hmac/u64.c b/services/hmac/u64.c deleted file mode 100644 index de6257ba084..00000000000 --- a/services/hmac/u64.c +++ /dev/null @@ -1,20 +0,0 @@ -/* uint64_t-like operations that work even on hosts lacking uint64_t - - Copyright (C) 2012-2022 Free Software Foundation, Inc. - - This file is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of the - License, or (at your option) any later version. - - This file is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . */ - -#define _GL_U64_INLINE _GL_EXTERN_INLINE -#include "u64.h" -typedef int dummy; diff --git a/services/list/list.c b/services/list/list.c index 20d92e71b23..71560ff9eb0 100644 --- a/services/list/list.c +++ b/services/list/list.c @@ -1,14 +1,16 @@ #include "list.h" ListNode* list_init_head(void* data) { - ListNode* new = (ListNode*)malloc(sizeof(ListNode)); + ListNode* new = malloc(sizeof(ListNode)); + if (new == NULL) return NULL; new->data = data; new->next = NULL; return new; } ListNode* list_add(ListNode* head, void* data) { - ListNode* new = (ListNode*)malloc(sizeof(ListNode)); + ListNode* new = malloc(sizeof(ListNode)); + if (new == NULL) return NULL; new->data = data; new->next = NULL; diff --git a/services/list/list.h b/services/list/list.h index 93acb0f34db..7721b252224 100644 --- a/services/list/list.h +++ b/services/list/list.h @@ -23,11 +23,12 @@ ListNode* list_remove( ListNode* ep); /* removes element from the list and returns new head node. */ void list_free(ListNode* head); /* deletes all elements of the list. */ -#define TOTP_LIST_INIT_OR_ADD(head, item) \ - do { \ - if(head == NULL) { \ - head = list_init_head(item); \ - } else { \ - list_add(head, item); \ - } \ +#define TOTP_LIST_INIT_OR_ADD(head, item, assert) \ + do { \ + if(head == NULL) { \ + head = list_init_head(item); \ + assert(head != NULL); \ + } else { \ + assert(list_add(head, item) != NULL); \ + } \ } while(false) diff --git a/services/totp/totp.c b/services/totp/totp.c index 68531e17198..71b68480bf3 100644 --- a/services/totp/totp.c +++ b/services/totp/totp.c @@ -45,6 +45,7 @@ uint32_t otp_generate( size_t plain_secret_length, uint64_t input) { uint8_t* hmac = malloc(64); + if (hmac == NULL) return OTP_ERROR; memset(hmac, 0, 64); uint64_t input_swapped = swap_uint64(input); diff --git a/totp_app.c b/totp_app.c index 3bc85e2112d..d7d6ea84f63 100644 --- a/totp_app.c +++ b/totp_app.c @@ -111,6 +111,7 @@ static void totp_plugin_state_free(PluginState* plugin_state) { int32_t totp_app() { FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); PluginState* plugin_state = malloc(sizeof(PluginState)); + furi_check(plugin_state != NULL); if (!totp_plugin_state_init(plugin_state)) { FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n"); diff --git a/types/token_info.c b/types/token_info.c index 17b2af9aa36..6c672ab00a8 100644 --- a/types/token_info.c +++ b/types/token_info.c @@ -9,6 +9,7 @@ TokenInfo* token_info_alloc() { TokenInfo* tokenInfo = malloc(sizeof(TokenInfo)); + furi_check(tokenInfo != NULL); tokenInfo->algo = SHA1; tokenInfo->digits = TOTP_6_DIGITS; return tokenInfo; @@ -27,6 +28,7 @@ bool token_info_set_secret( size_t token_secret_length, const uint8_t* iv) { uint8_t* plain_secret = malloc(token_secret_length); + furi_check(plain_secret != NULL); int plain_secret_length = base32_decode((const uint8_t*)base32_token_secret, plain_secret, token_secret_length); bool result; @@ -38,7 +40,7 @@ bool token_info_set_secret( result = false; } - memset_s(plain_secret, sizeof(plain_secret), 0, token_secret_length); + memset_s(plain_secret, token_secret_length, 0, token_secret_length); free(plain_secret); return result; }