Skip to content

Commit bd0e3c9

Browse files
authored
Reduced code size to avoid weird issue with COMPACT=1 DEBUG=0 build (#19)
1 parent a11332c commit bd0e3c9

20 files changed

+104
-277
lines changed

scenes/add_new_token/totp_input_text.c

+3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ static void commit_text_input_callback(void* context) {
4242
InputTextSceneState* text_input_state = (InputTextSceneState*)context;
4343
if(text_input_state->callback != 0) {
4444
InputTextSceneCallbackResult* result = malloc(sizeof(InputTextSceneCallbackResult));
45+
furi_check(result != NULL);
4546
result->user_input_length =
4647
strnlen(text_input_state->text_input_buffer, INPUT_BUFFER_SIZE);
4748
result->user_input = malloc(result->user_input_length + 1);
49+
furi_check(result->user_input != NULL);
4850
result->callback_data = text_input_state->callback_data;
4951
strlcpy(
5052
result->user_input,
@@ -56,6 +58,7 @@ static void commit_text_input_callback(void* context) {
5658

5759
InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context) {
5860
InputTextSceneState* text_input_state = malloc(sizeof(InputTextSceneState));
61+
furi_check(text_input_state != NULL);
5962
text_input_state->text_input = text_input_alloc();
6063
text_input_state->text_input_view = text_input_get_view(text_input_state->text_input);
6164
text_input_state->callback = context->callback;

scenes/add_new_token/totp_scene_add_new_token.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,21 @@ void totp_scene_add_new_token_activate(
6868
PluginState* plugin_state,
6969
const TokenAddEditSceneContext* context) {
7070
SceneState* scene_state = malloc(sizeof(SceneState));
71+
furi_check(scene_state != NULL);
7172
plugin_state->current_scene_state = scene_state;
7273
scene_state->token_name = "Name";
7374
scene_state->token_name_length = strlen(scene_state->token_name);
7475
scene_state->token_secret = "Secret";
7576
scene_state->token_secret_length = strlen(scene_state->token_secret);
7677

7778
scene_state->token_name_input_context = malloc(sizeof(InputTextSceneContext));
79+
furi_check(scene_state->token_name_input_context != NULL);
7880
scene_state->token_name_input_context->header_text = "Enter token name";
7981
scene_state->token_name_input_context->callback_data = scene_state;
8082
scene_state->token_name_input_context->callback = on_token_name_user_comitted;
8183

8284
scene_state->token_secret_input_context = malloc(sizeof(InputTextSceneContext));
85+
furi_check(scene_state->token_secret_input_context != NULL);
8386
scene_state->token_secret_input_context->header_text = "Enter token secret";
8487
scene_state->token_secret_input_context->callback_data = scene_state;
8588
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
246249

247250
if(token_secret_set) {
248251
tokenInfo->name = malloc(scene_state->token_name_length + 1);
252+
furi_check(tokenInfo->name != NULL);
249253
strlcpy(
250254
tokenInfo->name, scene_state->token_name, scene_state->token_name_length + 1);
251255
tokenInfo->algo = scene_state->algo;
252256
tokenInfo->digits = scene_state->digits_count;
253257

254-
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);
258+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check);
255259
plugin_state->tokens_count++;
256260

257261
totp_config_file_save_new_token(tokenInfo);

scenes/app_settings/totp_app_settings.c

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void totp_scene_app_settings_activate(
2626
PluginState* plugin_state,
2727
const AppSettingsSceneContext* context) {
2828
SceneState* scene_state = malloc(sizeof(SceneState));
29+
furi_check(scene_state != NULL);
2930
plugin_state->current_scene_state = scene_state;
3031
if(context != NULL) {
3132
TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);

scenes/authenticate/totp_scene_authenticate.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void totp_scene_authenticate_init(PluginState* plugin_state) {
2525

2626
void totp_scene_authenticate_activate(PluginState* plugin_state) {
2727
SceneState* scene_state = malloc(sizeof(SceneState));
28+
furi_check(scene_state != NULL);
2829
scene_state->code_length = 0;
2930
memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
3031
plugin_state->current_scene_state = scene_state;

scenes/generate_token/totp_scene_generate_token.c

+7-13
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,17 @@ static const NotificationSequence sequence_short_vibro_and_sound = {
3737
};
3838

3939
static void i_token_to_str(uint32_t i_token_code, char* str, TokenDigitsCount len) {
40+
uint8_t str_token_length = 0;
4041
if(len == TOTP_8_DIGITS) {
4142
str[8] = '\0';
43+
str_token_length = 8;
4244
} else if(len == TOTP_6_DIGITS) {
4345
str[6] = '\0';
46+
str_token_length = 6;
4447
}
4548

46-
if(i_token_code == 0) {
47-
if(len > TOTP_6_DIGITS) {
48-
str[7] = '-';
49-
str[6] = '-';
50-
}
51-
52-
str[5] = '-';
53-
str[4] = '-';
54-
str[3] = '-';
55-
str[2] = '-';
56-
str[1] = '-';
57-
str[0] = '-';
49+
if(i_token_code == OTP_ERROR) {
50+
memset(&str[0], '-', str_token_length);
5851
} else {
5952
if(len == TOTP_8_DIGITS) {
6053
str[7] = DIGIT_TO_CHAR(i_token_code % 10);
@@ -132,6 +125,7 @@ void totp_scene_generate_token_activate(
132125
}
133126
}
134127
SceneState* scene_state = malloc(sizeof(SceneState));
128+
furi_check(scene_state != NULL);
135129
if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
136130
scene_state->current_token_index = 0;
137131
} else {
@@ -197,7 +191,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
197191
TOKEN_LIFETIME),
198192
scene_state->last_code,
199193
tokenInfo->digits);
200-
memset_s(key, sizeof(key), 0, key_length);
194+
memset_s(key, key_length, 0, key_length);
201195
free(key);
202196
} else {
203197
i_token_to_str(0, scene_state->last_code, tokenInfo->digits);

scenes/token_menu/totp_scene_token_menu.c

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void totp_scene_token_menu_activate(
3131
PluginState* plugin_state,
3232
const TokenMenuSceneContext* context) {
3333
SceneState* scene_state = malloc(sizeof(SceneState));
34+
furi_check(scene_state != NULL);
3435
plugin_state->current_scene_state = scene_state;
3536
if(context != NULL) {
3637
TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);

services/cli/cli_helpers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
1212

1313
TOTP_CLI_DELETE_LAST_LINE();
1414

15-
if(plugin_state->current_scene == TotpSceneAuthentication) {
15+
if(plugin_state->current_scene == TotpSceneAuthentication) { //-V547
1616
return false;
1717
}
1818
}

services/cli/commands/add/add.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
138138

139139
size_t temp_cstr_len = furi_string_size(temp_str);
140140
token_info->name = malloc(temp_cstr_len + 1);
141+
furi_check(token_info->name != NULL);
141142
strlcpy(token_info->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
142143

143144
// Read optional arguments
@@ -211,7 +212,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
211212
load_generate_token_scene = true;
212213
}
213214

214-
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info);
215+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check);
215216
plugin_state->tokens_count++;
216217
totp_config_file_save_new_token(token_info);
217218

services/config/config.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ void totp_config_file_load_base(PluginState* const plugin_state) {
283283
if(flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
284284
crypto_size > 0) {
285285
plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
286+
furi_check(plugin_state->crypto_verify_data != NULL);
286287
plugin_state->crypto_verify_data_length = crypto_size;
287288
if(!flipper_format_read_hex(
288289
fff_data_file,
@@ -344,7 +345,8 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state)
344345
TokenInfo* tokenInfo = token_info_alloc();
345346

346347
size_t temp_cstr_len = furi_string_size(temp_str);
347-
tokenInfo->name = (char*)malloc(temp_cstr_len + 1);
348+
tokenInfo->name = malloc(temp_cstr_len + 1);
349+
furi_check(tokenInfo->name != NULL);
348350
strlcpy(tokenInfo->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
349351

350352
uint32_t secret_bytes_count;
@@ -378,6 +380,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state)
378380
tokenInfo->token_length = secret_bytes_count;
379381
if(secret_bytes_count > 0) {
380382
tokenInfo->token = malloc(tokenInfo->token_length);
383+
furi_check(tokenInfo->token != NULL);
381384
if(!flipper_format_read_hex(
382385
fff_data_file,
383386
TOTP_CONFIG_KEY_TOKEN_SECRET,
@@ -409,7 +412,7 @@ TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state)
409412

410413
FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
411414

412-
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo);
415+
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check);
413416

414417
index++;
415418
}

services/crypto/crypto.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ uint8_t* totp_crypto_encrypt(
2020
if(remain) {
2121
size_t plain_data_aligned_length = plain_data_length - remain + CRYPTO_ALIGNMENT_FACTOR;
2222
uint8_t* plain_data_aligned = malloc(plain_data_aligned_length);
23+
furi_check(plain_data_aligned != NULL);
2324
memset(plain_data_aligned, 0, plain_data_aligned_length);
2425
memcpy(plain_data_aligned, plain_data, plain_data_length);
2526

2627
encrypted_data = malloc(plain_data_aligned_length);
28+
furi_check(encrypted_data != NULL);
2729
*encrypted_data_length = plain_data_aligned_length;
2830

2931
furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
3032
furi_hal_crypto_encrypt(plain_data_aligned, encrypted_data, plain_data_aligned_length);
3133
furi_hal_crypto_store_unload_key(CRYPTO_KEY_SLOT);
3234

33-
memset_s(plain_data_aligned, sizeof(plain_data_aligned), 0, plain_data_aligned_length);
35+
memset_s(plain_data_aligned, plain_data_aligned_length, 0, plain_data_aligned_length);
3436
free(plain_data_aligned);
3537
} else {
3638
encrypted_data = malloc(plain_data_length);
39+
furi_check(encrypted_data != NULL);
3740
*encrypted_data_length = plain_data_length;
3841

3942
furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
@@ -51,6 +54,7 @@ uint8_t* totp_crypto_decrypt(
5154
size_t* decrypted_data_length) {
5255
*decrypted_data_length = encrypted_data_length;
5356
uint8_t* decrypted_data = malloc(*decrypted_data_length);
57+
furi_check(decrypted_data != NULL);
5458
furi_hal_crypto_store_load_key(CRYPTO_KEY_SLOT, iv);
5559
furi_hal_crypto_decrypt(encrypted_data, decrypted_data, encrypted_data_length);
5660
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
9397
if(plugin_state->crypto_verify_data == NULL) {
9498
FURI_LOG_D(LOGGING_TAG, "Generating crypto verify data");
9599
plugin_state->crypto_verify_data = malloc(CRYPTO_VERIFY_KEY_LENGTH);
100+
furi_check(plugin_state->crypto_verify_data != NULL);
96101
plugin_state->crypto_verify_data_length = CRYPTO_VERIFY_KEY_LENGTH;
97102
Storage* storage = totp_open_storage();
98103
FlipperFormat* config_file = totp_open_config_file(storage);

services/crypto/memset_s.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef uint64_t rsize_t;
99
#define _RSIZE_T_DECLARED
1010
#endif
1111
#ifndef _ERRNOT_DECLARED
12-
typedef int16_t errno_t;
12+
typedef int16_t errno_t; //-V677
1313
#define _ERRNOT_DECLARED
1414
#endif
1515

services/hmac/sha1.c

+26-81
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void sha1_process_bytes(const void* buffer, size_t len, struct sha1_ctx* ctx) {
146146
#define UNALIGNED_P(p) ((uintptr_t)(p) % sizeof(uint32_t) != 0)
147147
if(UNALIGNED_P(buffer))
148148
while(len > 64) {
149-
sha1_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx);
149+
sha1_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); //-V1086
150150
buffer = (const char*)buffer + 64;
151151
len -= 64;
152152
}
@@ -232,86 +232,31 @@ void sha1_process_block(const void* buffer, size_t len, struct sha1_ctx* ctx) {
232232
words++;
233233
}
234234

235-
R(a, b, c, d, e, F1, K1, x[0]);
236-
R(e, a, b, c, d, F1, K1, x[1]);
237-
R(d, e, a, b, c, F1, K1, x[2]);
238-
R(c, d, e, a, b, F1, K1, x[3]);
239-
R(b, c, d, e, a, F1, K1, x[4]);
240-
R(a, b, c, d, e, F1, K1, x[5]);
241-
R(e, a, b, c, d, F1, K1, x[6]);
242-
R(d, e, a, b, c, F1, K1, x[7]);
243-
R(c, d, e, a, b, F1, K1, x[8]);
244-
R(b, c, d, e, a, F1, K1, x[9]);
245-
R(a, b, c, d, e, F1, K1, x[10]);
246-
R(e, a, b, c, d, F1, K1, x[11]);
247-
R(d, e, a, b, c, F1, K1, x[12]);
248-
R(c, d, e, a, b, F1, K1, x[13]);
249-
R(b, c, d, e, a, F1, K1, x[14]);
250-
R(a, b, c, d, e, F1, K1, x[15]);
251-
R(e, a, b, c, d, F1, K1, M(16));
252-
R(d, e, a, b, c, F1, K1, M(17));
253-
R(c, d, e, a, b, F1, K1, M(18));
254-
R(b, c, d, e, a, F1, K1, M(19));
255-
R(a, b, c, d, e, F2, K2, M(20));
256-
R(e, a, b, c, d, F2, K2, M(21));
257-
R(d, e, a, b, c, F2, K2, M(22));
258-
R(c, d, e, a, b, F2, K2, M(23));
259-
R(b, c, d, e, a, F2, K2, M(24));
260-
R(a, b, c, d, e, F2, K2, M(25));
261-
R(e, a, b, c, d, F2, K2, M(26));
262-
R(d, e, a, b, c, F2, K2, M(27));
263-
R(c, d, e, a, b, F2, K2, M(28));
264-
R(b, c, d, e, a, F2, K2, M(29));
265-
R(a, b, c, d, e, F2, K2, M(30));
266-
R(e, a, b, c, d, F2, K2, M(31));
267-
R(d, e, a, b, c, F2, K2, M(32));
268-
R(c, d, e, a, b, F2, K2, M(33));
269-
R(b, c, d, e, a, F2, K2, M(34));
270-
R(a, b, c, d, e, F2, K2, M(35));
271-
R(e, a, b, c, d, F2, K2, M(36));
272-
R(d, e, a, b, c, F2, K2, M(37));
273-
R(c, d, e, a, b, F2, K2, M(38));
274-
R(b, c, d, e, a, F2, K2, M(39));
275-
R(a, b, c, d, e, F3, K3, M(40));
276-
R(e, a, b, c, d, F3, K3, M(41));
277-
R(d, e, a, b, c, F3, K3, M(42));
278-
R(c, d, e, a, b, F3, K3, M(43));
279-
R(b, c, d, e, a, F3, K3, M(44));
280-
R(a, b, c, d, e, F3, K3, M(45));
281-
R(e, a, b, c, d, F3, K3, M(46));
282-
R(d, e, a, b, c, F3, K3, M(47));
283-
R(c, d, e, a, b, F3, K3, M(48));
284-
R(b, c, d, e, a, F3, K3, M(49));
285-
R(a, b, c, d, e, F3, K3, M(50));
286-
R(e, a, b, c, d, F3, K3, M(51));
287-
R(d, e, a, b, c, F3, K3, M(52));
288-
R(c, d, e, a, b, F3, K3, M(53));
289-
R(b, c, d, e, a, F3, K3, M(54));
290-
R(a, b, c, d, e, F3, K3, M(55));
291-
R(e, a, b, c, d, F3, K3, M(56));
292-
R(d, e, a, b, c, F3, K3, M(57));
293-
R(c, d, e, a, b, F3, K3, M(58));
294-
R(b, c, d, e, a, F3, K3, M(59));
295-
R(a, b, c, d, e, F4, K4, M(60));
296-
R(e, a, b, c, d, F4, K4, M(61));
297-
R(d, e, a, b, c, F4, K4, M(62));
298-
R(c, d, e, a, b, F4, K4, M(63));
299-
R(b, c, d, e, a, F4, K4, M(64));
300-
R(a, b, c, d, e, F4, K4, M(65));
301-
R(e, a, b, c, d, F4, K4, M(66));
302-
R(d, e, a, b, c, F4, K4, M(67));
303-
R(c, d, e, a, b, F4, K4, M(68));
304-
R(b, c, d, e, a, F4, K4, M(69));
305-
R(a, b, c, d, e, F4, K4, M(70));
306-
R(e, a, b, c, d, F4, K4, M(71));
307-
R(d, e, a, b, c, F4, K4, M(72));
308-
R(c, d, e, a, b, F4, K4, M(73));
309-
R(b, c, d, e, a, F4, K4, M(74));
310-
R(a, b, c, d, e, F4, K4, M(75));
311-
R(e, a, b, c, d, F4, K4, M(76));
312-
R(d, e, a, b, c, F4, K4, M(77));
313-
R(c, d, e, a, b, F4, K4, M(78));
314-
R(b, c, d, e, a, F4, K4, M(79));
235+
for (int i = 0; i < 80; i++) {
236+
uint32_t xx = i < 16 ? x[i] : M(i);
237+
uint32_t ki = i / 20;
238+
switch (ki) {
239+
case 0:
240+
R(a, b, c, d, e, F1, K1, xx);
241+
break;
242+
case 1:
243+
R(a, b, c, d, e, F2, K2, xx);
244+
break;
245+
case 2:
246+
R(a, b, c, d, e, F3, K3, xx);
247+
break;
248+
default:
249+
R(a, b, c, d, e, F4, K4, xx);
250+
break;
251+
}
252+
253+
uint32_t tt = a;
254+
a = e;
255+
e = d;
256+
d = c;
257+
c = b;
258+
b = tt;
259+
}
315260

316261
a = ctx->A += a;
317262
b = ctx->B += b;

0 commit comments

Comments
 (0)