diff --git a/applications/plugins/totp/cli/cli.c b/applications/plugins/totp/cli/cli.c index 8f4708f4d..28d173766 100644 --- a/applications/plugins/totp/cli/cli.c +++ b/applications/plugins/totp/cli/cli.c @@ -14,7 +14,7 @@ #include "commands/reset/reset.h" static void totp_cli_print_unknown_command(const FuriString* unknown_command) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP "\" command to get list of available commands.", furi_string_get_cstr(unknown_command)); diff --git a/applications/plugins/totp/cli/cli_helpers.h b/applications/plugins/totp/cli/cli_helpers.h index 5027d7027..a35a2e599 100644 --- a/applications/plugins/totp/cli/cli_helpers.h +++ b/applications/plugins/totp/cli/cli_helpers.h @@ -22,6 +22,31 @@ _Pragma(STRINGIFY(GCC diagnostic pop)) \ } while(false) +#define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \ + do { \ + _Pragma(STRINGIFY(GCC diagnostic push)) \ + _Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \ + printf("\e[%s", color); \ + printf(format, ##__VA_ARGS__); \ + printf("\e[0m"); \ + fflush(stdout); \ + _Pragma(STRINGIFY(GCC diagnostic pop)) \ + } while(false) + +#define TOTP_CLI_COLOR_ERROR "91m" +#define TOTP_CLI_COLOR_WARNING "93m" +#define TOTP_CLI_COLOR_SUCCESS "92m" +#define TOTP_CLI_COLOR_INFO "96m" + +#define TOTP_CLI_PRINTF_ERROR(format, ...) \ + TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__) +#define TOTP_CLI_PRINTF_WARNING(format, ...) \ + TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__) +#define TOTP_CLI_PRINTF_SUCCESS(format, ...) \ + TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__) +#define TOTP_CLI_PRINTF_INFO(format, ...) \ + TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__) + #define TOTP_CLI_DELETE_LAST_LINE() \ TOTP_CLI_PRINTF("\033[A\33[2K\r"); \ fflush(stdout) @@ -35,11 +60,11 @@ fflush(stdout) #define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \ - TOTP_CLI_PRINTF( \ + TOTP_CLI_PRINTF_ERROR( \ "Invalid command arguments. use \"help\" command to get list of available commands") #define TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE() \ - TOTP_CLI_PRINTF("An error has occurred during updating config file\r\n") + TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n") /** * @brief Checks whether user is authenticated and entered correct PIN. diff --git a/applications/plugins/totp/cli/commands/add/add.c b/applications/plugins/totp/cli/commands/add/add.c index 2183d5277..fbcd58530 100644 --- a/applications/plugins/totp/cli/commands/add/add.c +++ b/applications/plugins/totp/cli/commands/add/add.c @@ -115,10 +115,10 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl bool parsed = false; if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) { if(!args_read_string_and_trim(args, temp_str)) { - TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX - "\"\r\n"); + TOTP_CLI_PRINTF_ERROR( + "Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n"); } else if(!token_info_set_algo_from_str(token_info, temp_str)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n", furi_string_get_cstr(temp_str)); @@ -128,11 +128,11 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) { uint8_t digit_value; if(!args_read_uint8_and_trim(args, &digit_value)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n"); } else if(!token_info_set_digits_from_int(token_info, digit_value)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "\"%" PRIu8 "\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n", @@ -143,11 +143,11 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX) == 0) { uint8_t duration_value; if(!args_read_uint8_and_trim(args, &duration_value)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX "\"\r\n"); } else if(!token_info_set_duration_from_int(token_info, duration_value)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "\"%" PRIu8 "\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX "\"\r\n", @@ -159,7 +159,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl mask_user_input = false; parsed = true; } else { - TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str)); + TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str)); } if(!parsed) { @@ -176,7 +176,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl if(!totp_cli_read_line(cli, temp_str, mask_user_input) || !totp_cli_ensure_authenticated(plugin_state, cli)) { TOTP_CLI_DELETE_LAST_LINE(); - TOTP_CLI_PRINTF("Cancelled by user\r\n"); + TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n"); furi_string_secure_free(temp_str); token_info_free(token_info); return; @@ -189,7 +189,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl furi_string_get_cstr(temp_str), furi_string_size(temp_str), plugin_state->iv)) { - TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n"); + TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n"); furi_string_secure_free(temp_str); token_info_free(token_info); return; @@ -206,7 +206,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check); plugin_state->tokens_count++; if(totp_config_file_save_new_token(token_info) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF("Token \"%s\" has been successfully added\r\n", token_info->name); + TOTP_CLI_PRINTF_SUCCESS("Token \"%s\" has been successfully added\r\n", token_info->name); } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); } diff --git a/applications/plugins/totp/cli/commands/delete/delete.c b/applications/plugins/totp/cli/commands/delete/delete.c index 046341693..8fe72e220 100644 --- a/applications/plugins/totp/cli/commands/delete/delete.c +++ b/applications/plugins/totp/cli/commands/delete/delete.c @@ -64,11 +64,11 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, bool confirmed = !confirm_needed; if(confirm_needed) { - TOTP_CLI_PRINTF("WARNING!\r\n"); - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_WARNING("WARNING!\r\n"); + TOTP_CLI_PRINTF_WARNING( "TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n", token_info->name); - TOTP_CLI_PRINTF("Confirm? [y/n]\r\n"); + TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n"); fflush(stdout); char user_pick; do { @@ -94,7 +94,8 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, plugin_state->tokens_count--; if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name); + TOTP_CLI_PRINTF_SUCCESS( + "Token \"%s\" has been successfully deleted\r\n", token_info->name); } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); } @@ -105,6 +106,6 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args, totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL); } } else { - TOTP_CLI_PRINTF("User not confirmed\r\n"); + TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n"); } } \ No newline at end of file diff --git a/applications/plugins/totp/cli/commands/move/move.c b/applications/plugins/totp/cli/commands/move/move.c index e2929f153..1d5a23bbf 100644 --- a/applications/plugins/totp/cli/commands/move/move.c +++ b/applications/plugins/totp/cli/commands/move/move.c @@ -65,7 +65,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C bool parsed = false; if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX) == 0) { if(!args_read_string_and_trim(args, temp_str)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX "\"\r\n"); } else { @@ -87,11 +87,11 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C } } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX) == 0) { if(!args_read_int_and_trim(args, &new_token_index)) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX "\"\r\n"); } else if(new_token_index < 1 || new_token_index > plugin_state->tokens_count) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_ERROR( "\"%" PRId16 "\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX "\"\r\n", @@ -100,7 +100,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C parsed = true; } } else { - TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str)); + TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str)); } if(!parsed) { @@ -148,7 +148,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C if(token_updated) { if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF("Token \"%s\" has been successfully updated\r\n", token_info->name); + TOTP_CLI_PRINTF_SUCCESS( + "Token \"%s\" has been successfully updated\r\n", token_info->name); } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); } diff --git a/applications/plugins/totp/cli/commands/notification/notification.c b/applications/plugins/totp/cli/commands/notification/notification.c index 5b98a857b..bbbd52703 100644 --- a/applications/plugins/totp/cli/commands/notification/notification.c +++ b/applications/plugins/totp/cli/commands/notification/notification.c @@ -28,21 +28,21 @@ void totp_cli_command_notification_docopt_arguments() { ", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n"); } -static void totp_cli_command_notification_print_method(NotificationMethod method) { +static void totp_cli_command_notification_print_method(NotificationMethod method, char* color) { bool has_previous_method = false; if(method & NotificationMethodSound) { - TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\""); + TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\""); has_previous_method = true; } if(method & NotificationMethodVibro) { if(has_previous_method) { - TOTP_CLI_PRINTF(" and "); + TOTP_CLI_PRINTF_COLORFUL(color, " and "); } - TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\""); + TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\""); } if(method == NotificationMethodNone) { - TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\""); + TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\""); } } @@ -88,8 +88,8 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* plugin_state->notification_method = new_method; if(totp_config_file_update_notification_method(new_method) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF("Notification method is set to "); - totp_cli_command_notification_print_method(new_method); + TOTP_CLI_PRINTF_SUCCESS("Notification method is set to "); + totp_cli_command_notification_print_method(new_method, TOTP_CLI_COLOR_SUCCESS); cli_nl(); } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); @@ -99,8 +99,9 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* totp_scene_director_activate_scene(plugin_state, previous_scene, NULL); } } else { - TOTP_CLI_PRINTF("Current notification method is "); - totp_cli_command_notification_print_method(plugin_state->notification_method); + TOTP_CLI_PRINTF_INFO("Current notification method is "); + totp_cli_command_notification_print_method( + plugin_state->notification_method, TOTP_CLI_COLOR_INFO); cli_nl(); } } while(false); diff --git a/applications/plugins/totp/cli/commands/pin/pin.c b/applications/plugins/totp/cli/commands/pin/pin.c index 198324e27..b5b4943d6 100644 --- a/applications/plugins/totp/cli/commands/pin/pin.c +++ b/applications/plugins/totp/cli/commands/pin/pin.c @@ -65,7 +65,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) { } } else if(c == CliSymbolAsciiETX) { TOTP_CLI_DELETE_CURRENT_LINE(); - TOTP_CLI_PRINTF("Cancelled by user\r\n"); + TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n"); return false; } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) { if(*pin_length > 0) { @@ -162,9 +162,9 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) { if(do_change) { - TOTP_CLI_PRINTF("PIN has been successfully changed\r\n"); + TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully changed\r\n"); } else if(do_remove) { - TOTP_CLI_PRINTF("PIN has been successfully removed\r\n"); + TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully removed\r\n"); } } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); diff --git a/applications/plugins/totp/cli/commands/reset/reset.c b/applications/plugins/totp/cli/commands/reset/reset.c index 3f20dc8ad..c7928db31 100644 --- a/applications/plugins/totp/cli/commands/reset/reset.c +++ b/applications/plugins/totp/cli/commands/reset/reset.c @@ -17,21 +17,21 @@ void totp_cli_command_reset_docopt_usage() { } void totp_cli_command_reset_handle(Cli* cli, FuriMessageQueue* event_queue) { - TOTP_CLI_PRINTF( + TOTP_CLI_PRINTF_WARNING( "As a result of reset all the settings and tokens will be permanently lost.\r\n"); - TOTP_CLI_PRINTF("Do you really want to reset application?\r\n"); - TOTP_CLI_PRINTF("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD - "\" and hit to confirm:\r\n"); + TOTP_CLI_PRINTF_WARNING("Do you really want to reset application?\r\n"); + TOTP_CLI_PRINTF_WARNING("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD + "\" and hit to confirm:\r\n"); FuriString* temp_str = furi_string_alloc(); bool is_confirmed = totp_cli_read_line(cli, temp_str, false) && furi_string_cmpi_str(temp_str, TOTP_CLI_RESET_CONFIRMATION_KEYWORD) == 0; furi_string_free(temp_str); if(is_confirmed) { totp_config_file_reset(); - TOTP_CLI_PRINTF("Application has been successfully reset to default.\r\n"); - TOTP_CLI_PRINTF("Now application will be closed to apply all the changes.\r\n"); + TOTP_CLI_PRINTF_SUCCESS("Application has been successfully reset to default.\r\n"); + TOTP_CLI_PRINTF_SUCCESS("Now application will be closed to apply all the changes.\r\n"); totp_cli_force_close_app(event_queue); } else { - TOTP_CLI_PRINTF("Action was not confirmed by user\r\n"); + TOTP_CLI_PRINTF_INFO("Action was not confirmed by user\r\n"); } } \ No newline at end of file diff --git a/applications/plugins/totp/cli/commands/timezone/timezone.c b/applications/plugins/totp/cli/commands/timezone/timezone.c index 4a737f6ef..9eb0cb5f6 100644 --- a/applications/plugins/totp/cli/commands/timezone/timezone.c +++ b/applications/plugins/totp/cli/commands/timezone/timezone.c @@ -30,11 +30,12 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg FuriString* temp_str = furi_string_alloc(); if(args_read_string_and_trim(args, temp_str)) { - float tz = strtof(furi_string_get_cstr(temp_str), NULL); - if(tz >= -12.75f && tz <= 12.75f) { + char* strtof_endptr; + float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr); + if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) { plugin_state->timezone_offset = tz; if(totp_config_file_update_timezone_offset(tz) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF("Timezone is set to %f\r\n", tz); + TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", tz); } else { TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); } @@ -46,10 +47,10 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings, NULL); } } else { - TOTP_CLI_PRINTF("Invalid timezone offset\r\n"); + TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n"); } } else { - TOTP_CLI_PRINTF("Current timezone offset is %f\r\n", plugin_state->timezone_offset); + TOTP_CLI_PRINTF_INFO("Current timezone offset is %f\r\n", plugin_state->timezone_offset); } furi_string_free(temp_str); } \ No newline at end of file diff --git a/applications/plugins/totp/services/hmac/sha1.c b/applications/plugins/totp/services/hmac/sha1.c index 243a6dde8..ecf22fc97 100644 --- a/applications/plugins/totp/services/hmac/sha1.c +++ b/applications/plugins/totp/services/hmac/sha1.c @@ -174,16 +174,13 @@ void sha1_process_bytes(const void* buffer, size_t len, struct sha1_ctx* ctx) { /* --- Code below is the primary difference between md5.c and sha1.c --- */ /* SHA1 round constants */ -#define K1 0x5a827999 -#define K2 0x6ed9eba1 -#define K3 0x8f1bbcdc -#define K4 0xca62c1d6 +static const int sha1_round_constants[4] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}; /* Round functions. Note that F2 is the same as F4. */ #define F1(B, C, D) (D ^ (B & (C ^ D))) -#define F2(B, C, D) (B ^ C ^ D) +#define F2_4(B, C, D) (B ^ C ^ D) #define F3(B, C, D) ((B & C) | (D & (B | C))) -#define F4(B, C, D) (B ^ C ^ D) +#define FN(I, B, C, D) (I == 0 ? F1(B, C, D) : (I == 2 ? F3(B, C, D) : F2_4(B, C, D))) /* Process LEN bytes of BUFFER, accumulating context into CTX. It is assumed that LEN % 64 == 0. @@ -213,10 +210,10 @@ void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx) { (tm = x[I & 0x0f] ^ x[(I - 14) & 0x0f] ^ x[(I - 8) & 0x0f] ^ x[(I - 3) & 0x0f], \ (x[I & 0x0f] = rol(tm, 1))) -#define R(A, B, C, D, E, F, K, M) \ - do { \ - E += rol(A, 5) + F(B, C, D) + K + M; \ - B = rol(B, 30); \ +#define R(A, B, C, D, E, F, K, M, KI) \ + do { \ + E += rol(A, 5) + F(KI, B, C, D) + K + M; \ + B = rol(B, 30); \ } while(0) while(words < endp) { @@ -227,24 +224,11 @@ void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx) { words++; } - 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; - } - + for(uint8_t i = 0; i < 80; i++) { + uint32_t m = i < 16 ? x[i] : M(i); + uint8_t ki = i / 20; + int k_const = sha1_round_constants[ki]; + R(a, b, c, d, e, FN, k_const, m, ki); uint32_t tt = a; a = e; e = d; diff --git a/applications/plugins/totp/totp_app.c b/applications/plugins/totp/totp_app.c index 322acd36c..966c9fb34 100644 --- a/applications/plugins/totp/totp_app.c +++ b/applications/plugins/totp/totp_app.c @@ -25,12 +25,10 @@ static void render_callback(Canvas* const canvas, void* ctx) { furi_assert(ctx); PluginState* plugin_state = ctx; - furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); - if(plugin_state != NULL) { + if(furi_mutex_acquire(plugin_state->mutex, 25) == FuriStatusOk) { totp_scene_director_render(canvas, plugin_state); + furi_mutex_release(plugin_state->mutex); } - - furi_mutex_release(plugin_state->mutex); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { @@ -104,6 +102,12 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) { return false; } + plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(plugin_state->mutex == NULL) { + FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n"); + return false; + } + return true; } @@ -125,6 +129,8 @@ static void totp_plugin_state_free(PluginState* plugin_state) { if(plugin_state->crypto_verify_data != NULL) { free(plugin_state->crypto_verify_data); } + + furi_mutex_free(plugin_state->mutex); free(plugin_state); } @@ -139,13 +145,6 @@ int32_t totp_app() { return 254; } - plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal); - if(!plugin_state->mutex) { - FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n"); - totp_plugin_state_free(plugin_state); - return 255; - } - TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue); totp_scene_director_init_scenes(plugin_state); if(!totp_activate_initial_scene(plugin_state)) { @@ -171,26 +170,26 @@ int32_t totp_app() { while(processing) { FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - furi_mutex_acquire(plugin_state->mutex, FuriWaitForever); + if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) { + if(event_status == FuriStatusOk) { + if(event.type == EventTypeKey) { + last_user_interaction_time = furi_get_tick(); + } - if(event_status == FuriStatusOk) { - if(event.type == EventTypeKey) { - last_user_interaction_time = furi_get_tick(); + if(event.type == EventForceCloseApp) { + processing = false; + } else { + processing = totp_scene_director_handle_event(&event, plugin_state); + } + } else if( + plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication && + furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) { + totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL); } - if(event.type == EventForceCloseApp) { - processing = false; - } else { - processing = totp_scene_director_handle_event(&event, plugin_state); - } - } else if( - plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication && - furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) { - totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL); + view_port_update(view_port); + furi_mutex_release(plugin_state->mutex); } - - view_port_update(view_port); - furi_mutex_release(plugin_state->mutex); } totp_cli_unregister_command_handler(cli_context); @@ -201,7 +200,6 @@ int32_t totp_app() { gui_remove_view_port(plugin_state->gui, view_port); view_port_free(view_port); furi_message_queue_free(event_queue); - furi_mutex_free(plugin_state->mutex); totp_plugin_state_free(plugin_state); return 0; } diff --git a/applications/plugins/totp/types/plugin_state.h b/applications/plugins/totp/types/plugin_state.h index b5e78d0cf..dee500305 100644 --- a/applications/plugins/totp/types/plugin_state.h +++ b/applications/plugins/totp/types/plugin_state.h @@ -87,5 +87,9 @@ typedef struct { * @brief Notification method */ NotificationMethod notification_method; + + /** + * @brief Main rendering loop mutex + */ FuriMutex* mutex; } PluginState; diff --git a/applications/plugins/totp/types/token_info.c b/applications/plugins/totp/types/token_info.c index 60d04245f..b69979cc7 100644 --- a/applications/plugins/totp/types/token_info.c +++ b/applications/plugins/totp/types/token_info.c @@ -12,6 +12,7 @@ TokenInfo* token_info_alloc() { furi_check(tokenInfo != NULL); tokenInfo->algo = SHA1; tokenInfo->digits = TOTP_6_DIGITS; + tokenInfo->duration = TOTP_TOKEN_DURATION_DEFAULT; return tokenInfo; } diff --git a/applications/plugins/totp/ui/scenes/authenticate/totp_scene_authenticate.c b/applications/plugins/totp/ui/scenes/authenticate/totp_scene_authenticate.c index c595b5bd0..17beb64c6 100644 --- a/applications/plugins/totp/ui/scenes/authenticate/totp_scene_authenticate.c +++ b/applications/plugins/totp/ui/scenes/authenticate/totp_scene_authenticate.c @@ -10,6 +10,8 @@ #include "../../../types/user_pin_codes.h" #define MAX_CODE_LENGTH TOTP_IV_SIZE +static const uint8_t PIN_ASTERISK_RADIUS = 3; +static const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2; typedef struct { TotpUserPinCode code_input[MAX_CODE_LENGTH]; @@ -61,8 +63,7 @@ void totp_scene_authenticate_render(Canvas* const canvas, PluginState* plugin_st AlignCenter, "Use arrow keys to enter PIN"); } - const uint8_t PIN_ASTERISK_RADIUS = 3; - const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2; + if(scene_state->code_length > 0) { uint8_t left_start_x = ((scene_state->code_length - 1) * PIN_ASTERISK_STEP) >> 1; for(uint8_t i = 0; i < scene_state->code_length; i++) { diff --git a/applications/plugins/totp/ui/scenes/generate_token/totp_scene_generate_token.c b/applications/plugins/totp/ui/scenes/generate_token/totp_scene_generate_token.c index 51c2e7702..a882ae78c 100644 --- a/applications/plugins/totp/ui/scenes/generate_token/totp_scene_generate_token.c +++ b/applications/plugins/totp/ui/scenes/generate_token/totp_scene_generate_token.c @@ -16,6 +16,9 @@ #include "../token_menu/totp_scene_token_menu.h" #include "../../../workers/type_code/type_code.h" +static const uint8_t PROGRESS_BAR_MARGIN = 3; +static const uint8_t PROGRESS_BAR_HEIGHT = 4; + typedef struct { uint16_t current_token_index; char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1]; @@ -306,14 +309,18 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_ AlignCenter, scene_state->last_code); - const uint8_t BAR_MARGIN = 3; - const uint8_t BAR_HEIGHT = 4; const uint8_t TOKEN_LIFETIME = scene_state->current_token->duration; float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME; - uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (BAR_MARGIN << 1)) * percentDone); - uint8_t barX = ((SCREEN_WIDTH - (BAR_MARGIN << 1) - barWidth) >> 1) + BAR_MARGIN; + uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * percentDone); + uint8_t barX = + ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) - barWidth) >> 1) + PROGRESS_BAR_MARGIN; - canvas_draw_box(canvas, barX, SCREEN_HEIGHT - BAR_MARGIN - BAR_HEIGHT, barWidth, BAR_HEIGHT); + canvas_draw_box( + canvas, + barX, + SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT, + barWidth, + PROGRESS_BAR_HEIGHT); if(plugin_state->tokens_count > 1) { canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9); diff --git a/applications/plugins/totp/workers/type_code/type_code.c b/applications/plugins/totp/workers/type_code/type_code.c index 06fe7897e..f2b4c9b9e 100644 --- a/applications/plugins/totp/workers/type_code/type_code.c +++ b/applications/plugins/totp/workers/type_code/type_code.c @@ -57,10 +57,8 @@ static void totp_type_code_worker_type_code(TotpTypeCodeWorkerContext* context) } static int32_t totp_type_code_worker_callback(void* context) { - furi_assert(context); - TotpTypeCodeWorkerContext* ctxx = context; - ctxx->mutex = furi_mutex_alloc(FuriMutexTypeNormal); - if(!ctxx->mutex) { + FuriMutex* context_mutex = furi_mutex_alloc(FuriMutexTypeNormal); + if(context_mutex == NULL) { return 251; } @@ -72,16 +70,16 @@ static int32_t totp_type_code_worker_callback(void* context) { furi_check((flags & FuriFlagError) == 0); //-V562 if(flags & TotpTypeCodeWorkerEventStop) break; - TotpTypeCodeWorkerContext* h_context = context; - furi_mutex_acquire(ctxx->mutex, FuriWaitForever); - if(flags & TotpTypeCodeWorkerEventType) { - totp_type_code_worker_type_code(h_context); - } + if(furi_mutex_acquire(context_mutex, FuriWaitForever) == FuriStatusOk) { + if(flags & TotpTypeCodeWorkerEventType) { + totp_type_code_worker_type_code(context); + } - furi_mutex_release(ctxx->mutex); + furi_mutex_release(context_mutex); + } } - furi_mutex_free(ctxx->mutex); + furi_mutex_free(context_mutex); return 0; } diff --git a/applications/plugins/totp/workers/type_code/type_code.h b/applications/plugins/totp/workers/type_code/type_code.h index b696420bc..27f2e02d4 100644 --- a/applications/plugins/totp/workers/type_code/type_code.h +++ b/applications/plugins/totp/workers/type_code/type_code.h @@ -12,7 +12,6 @@ typedef struct { FuriThread* thread; FuriMutex* string_sync; FuriHalUsbInterface* usb_mode_prev; - FuriMutex* mutex; } TotpTypeCodeWorkerContext; enum TotpTypeCodeWorkerEvents {