Skip to content

Commit

Permalink
Reduced code size to avoid weird issue with COMPACT=1 DEBUG=0 build (#19
Browse files Browse the repository at this point in the history
)
  • Loading branch information
akopachov authored Nov 4, 2022
1 parent a11332c commit bd0e3c9
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 277 deletions.
3 changes: 3 additions & 0 deletions scenes/add_new_token/totp_input_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion scenes/add_new_token/totp_scene_add_new_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ 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);
scene_state->token_secret = "Secret";
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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions scenes/app_settings/totp_app_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions scenes/authenticate/totp_scene_authenticate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 7 additions & 13 deletions scenes/generate_token/totp_scene_generate_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions scenes/token_menu/totp_scene_token_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion services/cli/cli_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion services/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 5 additions & 2 deletions services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++;
}
Expand Down
7 changes: 6 additions & 1 deletion services/crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion services/crypto/memset_s.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
107 changes: 26 additions & 81 deletions services/hmac/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit bd0e3c9

Please sign in to comment.