Skip to content

Commit 446e0a1

Browse files
authored
Refactoring (#183)
1 parent 1b3e5d8 commit 446e0a1

23 files changed

+374
-412
lines changed

cli/commands/add/add.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
struct TotpAddContext {
1212
FuriString* args;
1313
Cli* cli;
14-
uint8_t* iv;
15-
uint8_t crypto_version;
16-
uint8_t crypto_key_slot;
14+
const CryptoSettings* crypto_settings;
1715
};
1816

1917
enum TotpIteratorUpdateTokenResultsEx {
@@ -70,9 +68,7 @@ static TotpIteratorUpdateTokenResult
7068
furi_string_get_cstr(temp_str),
7169
furi_string_size(temp_str),
7270
token_secret_encoding,
73-
context_t->iv,
74-
context_t->crypto_version,
75-
context_t->crypto_key_slot);
71+
context_t->crypto_settings);
7672

7773
furi_string_secure_free(temp_str);
7874

@@ -171,11 +167,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
171167
TOTP_CLI_LOCK_UI(plugin_state);
172168

173169
struct TotpAddContext add_context = {
174-
.args = args,
175-
.cli = cli,
176-
.iv = &plugin_state->iv[0],
177-
.crypto_version = plugin_state->crypto_version,
178-
.crypto_key_slot = plugin_state->crypto_key_slot};
170+
.args = args, .cli = cli, .crypto_settings = &plugin_state->crypto_settings};
179171
TotpIteratorUpdateTokenResult add_result =
180172
totp_token_info_iterator_add_new_token(iterator_context, &add_token_handler, &add_context);
181173

cli/commands/pin/pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
105105

106106
bool do_change = false;
107107
bool do_remove = false;
108-
uint8_t crypto_key_slot = plugin_state->crypto_key_slot;
108+
uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
109109

110110
bool arguments_parsed = true;
111111
while(args_read_string_and_trim(args, temp_str)) {

cli/commands/update/update.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
struct TotpUpdateContext {
1414
FuriString* args;
1515
Cli* cli;
16-
uint8_t* iv;
17-
uint8_t crypto_version;
18-
uint8_t crypto_key_slot;
16+
const CryptoSettings* crypto_settings;
1917
};
2018

2119
enum TotpIteratorUpdateTokenResultsEx {
@@ -98,9 +96,7 @@ static TotpIteratorUpdateTokenResult
9896
furi_string_get_cstr(temp_str),
9997
furi_string_size(temp_str),
10098
token_secret_encoding,
101-
context_t->iv,
102-
context_t->crypto_version,
103-
context_t->crypto_key_slot)) {
99+
context_t->crypto_settings)) {
104100
furi_string_secure_free(temp_str);
105101
return TotpIteratorUpdateTokenResultInvalidSecret;
106102
}
@@ -155,11 +151,7 @@ void totp_cli_command_update_handle(PluginState* plugin_state, FuriString* args,
155151
totp_token_info_iterator_go_to(iterator_context, token_number - 1);
156152

157153
struct TotpUpdateContext update_context = {
158-
.args = args,
159-
.cli = cli,
160-
.iv = &plugin_state->iv[0],
161-
.crypto_version = plugin_state->crypto_version,
162-
.crypto_key_slot = plugin_state->crypto_key_slot};
154+
.args = args, .cli = cli, .crypto_settings = &plugin_state->crypto_settings};
163155
TotpIteratorUpdateTokenResult update_result = totp_token_info_iterator_update_current_token(
164156
iterator_context, &update_token_handler, &update_context);
165157

services/config/config.c

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ bool totp_config_file_load(PluginState* const plugin_state) {
375375
break;
376376
}
377377

378-
plugin_state->crypto_version = tmp_uint32;
378+
plugin_state->crypto_settings.crypto_version = tmp_uint32;
379379

380380
if(!flipper_format_rewind(fff_data_file)) {
381381
break;
@@ -388,7 +388,7 @@ bool totp_config_file_load(PluginState* const plugin_state) {
388388
break;
389389
}
390390

391-
plugin_state->crypto_key_slot = tmp_uint32;
391+
plugin_state->crypto_settings.crypto_key_slot = tmp_uint32;
392392

393393
if(!flipper_format_rewind(fff_data_file)) {
394394
break;
@@ -397,7 +397,7 @@ bool totp_config_file_load(PluginState* const plugin_state) {
397397
if(!flipper_format_read_hex(
398398
fff_data_file,
399399
TOTP_CONFIG_KEY_BASE_IV,
400-
&plugin_state->base_iv[0],
400+
&plugin_state->crypto_settings.base_iv[0],
401401
CRYPTO_IV_LENGTH)) {
402402
FURI_LOG_D(LOGGING_TAG, "Missing base IV");
403403
}
@@ -410,22 +410,23 @@ bool totp_config_file_load(PluginState* const plugin_state) {
410410
if(flipper_format_get_value_count(
411411
fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
412412
crypto_size > 0) {
413-
plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
414-
furi_check(plugin_state->crypto_verify_data != NULL);
415-
plugin_state->crypto_verify_data_length = crypto_size;
413+
plugin_state->crypto_settings.crypto_verify_data =
414+
malloc(sizeof(uint8_t) * crypto_size);
415+
furi_check(plugin_state->crypto_settings.crypto_verify_data != NULL);
416+
plugin_state->crypto_settings.crypto_verify_data_length = crypto_size;
416417
if(!flipper_format_read_hex(
417418
fff_data_file,
418419
TOTP_CONFIG_KEY_CRYPTO_VERIFY,
419-
plugin_state->crypto_verify_data,
420+
plugin_state->crypto_settings.crypto_verify_data,
420421
crypto_size)) {
421422
FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
422-
free(plugin_state->crypto_verify_data);
423-
plugin_state->crypto_verify_data = NULL;
424-
plugin_state->crypto_verify_data_length = 0;
423+
free(plugin_state->crypto_settings.crypto_verify_data);
424+
plugin_state->crypto_settings.crypto_verify_data = NULL;
425+
plugin_state->crypto_settings.crypto_verify_data_length = 0;
425426
}
426427
} else {
427-
plugin_state->crypto_verify_data = NULL;
428-
plugin_state->crypto_verify_data_length = 0;
428+
plugin_state->crypto_settings.crypto_verify_data = NULL;
429+
plugin_state->crypto_settings.crypto_verify_data_length = 0;
429430
}
430431

431432
if(!flipper_format_rewind(fff_data_file)) {
@@ -443,8 +444,11 @@ bool totp_config_file_load(PluginState* const plugin_state) {
443444
}
444445

445446
if(!flipper_format_read_bool(
446-
fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
447-
plugin_state->pin_set = true;
447+
fff_data_file,
448+
TOTP_CONFIG_KEY_PINSET,
449+
&plugin_state->crypto_settings.pin_required,
450+
1)) {
451+
plugin_state->crypto_settings.pin_required = true;
448452
}
449453

450454
if(!flipper_format_rewind(fff_data_file)) {
@@ -498,9 +502,7 @@ bool totp_config_file_load(PluginState* const plugin_state) {
498502
totp_token_info_iterator_alloc(
499503
storage,
500504
plugin_state->config_file_context->config_file,
501-
plugin_state->iv,
502-
plugin_state->crypto_version,
503-
plugin_state->crypto_key_slot);
505+
&plugin_state->crypto_settings);
504506
result = true;
505507
} while(false);
506508

@@ -513,33 +515,39 @@ bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state)
513515
flipper_format_rewind(config_file);
514516
bool update_result = false;
515517
do {
516-
uint32_t tmp_uint32 = plugin_state->crypto_version;
518+
uint32_t tmp_uint32 = plugin_state->crypto_settings.crypto_version;
517519
if(!flipper_format_insert_or_update_uint32(
518520
config_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1)) {
519521
break;
520522
}
521523

522-
tmp_uint32 = plugin_state->crypto_key_slot;
524+
tmp_uint32 = plugin_state->crypto_settings.crypto_key_slot;
523525
if(!flipper_format_insert_or_update_uint32(
524526
config_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1)) {
525527
break;
526528
}
527529

528530
if(!flipper_format_insert_or_update_hex(
529-
config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, CRYPTO_IV_LENGTH)) {
531+
config_file,
532+
TOTP_CONFIG_KEY_BASE_IV,
533+
plugin_state->crypto_settings.base_iv,
534+
CRYPTO_IV_LENGTH)) {
530535
break;
531536
}
532537

533538
if(!flipper_format_insert_or_update_hex(
534539
config_file,
535540
TOTP_CONFIG_KEY_CRYPTO_VERIFY,
536-
plugin_state->crypto_verify_data,
537-
plugin_state->crypto_verify_data_length)) {
541+
plugin_state->crypto_settings.crypto_verify_data,
542+
plugin_state->crypto_settings.crypto_verify_data_length)) {
538543
break;
539544
}
540545

541546
if(!flipper_format_insert_or_update_bool(
542-
config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
547+
config_file,
548+
TOTP_CONFIG_KEY_PINSET,
549+
&plugin_state->crypto_settings.pin_required,
550+
1)) {
543551
break;
544552
}
545553

@@ -581,24 +589,20 @@ bool totp_config_file_update_encryption(
581589
return false;
582590
}
583591

584-
uint8_t old_iv[CRYPTO_IV_LENGTH];
585-
memcpy(&old_iv[0], &plugin_state->iv[0], CRYPTO_IV_LENGTH);
586-
587-
uint8_t old_crypto_key_slot = plugin_state->crypto_key_slot;
588-
uint8_t old_crypto_version = plugin_state->crypto_version;
592+
CryptoSettings old_crypto_settings = plugin_state->crypto_settings;
589593

590-
memset(&plugin_state->iv[0], 0, CRYPTO_IV_LENGTH);
591-
memset(&plugin_state->base_iv[0], 0, CRYPTO_IV_LENGTH);
592-
if(plugin_state->crypto_verify_data != NULL) {
593-
free(plugin_state->crypto_verify_data);
594-
plugin_state->crypto_verify_data = NULL;
594+
memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
595+
memset(&plugin_state->crypto_settings.base_iv[0], 0, CRYPTO_IV_LENGTH);
596+
if(plugin_state->crypto_settings.crypto_verify_data != NULL) {
597+
free(plugin_state->crypto_settings.crypto_verify_data);
598+
plugin_state->crypto_settings.crypto_verify_data = NULL;
595599
}
596600

597-
plugin_state->crypto_key_slot = new_crypto_key_slot;
598-
plugin_state->crypto_version = CRYPTO_LATEST_VERSION;
601+
plugin_state->crypto_settings.crypto_key_slot = new_crypto_key_slot;
602+
plugin_state->crypto_settings.crypto_version = CRYPTO_LATEST_VERSION;
599603

600-
CryptoSeedIVResult seed_result =
601-
totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
604+
CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
605+
&plugin_state->crypto_settings, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
602606
if(seed_result & CryptoSeedIVResultFlagSuccess &&
603607
seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData &&
604608
!totp_config_file_update_crypto_signatures(plugin_state)) {
@@ -649,21 +653,14 @@ bool totp_config_file_update_encryption(
649653

650654
size_t plain_token_length;
651655
uint8_t* plain_token = totp_crypto_decrypt(
652-
encrypted_token,
653-
secret_bytes_count,
654-
&old_iv[0],
655-
old_crypto_version,
656-
old_crypto_key_slot,
657-
&plain_token_length);
656+
encrypted_token, secret_bytes_count, &old_crypto_settings, &plain_token_length);
658657

659658
free(encrypted_token);
660659
size_t encrypted_token_length;
661660
encrypted_token = totp_crypto_encrypt(
662661
plain_token,
663662
plain_token_length,
664-
&plugin_state->iv[0],
665-
plugin_state->crypto_version,
666-
plugin_state->crypto_key_slot,
663+
&plugin_state->crypto_settings,
667664
&encrypted_token_length);
668665

669666
memset_s(plain_token, plain_token_length, 0, plain_token_length);
@@ -700,12 +697,12 @@ bool totp_config_file_ensure_latest_encryption(
700697
const uint8_t* pin,
701698
uint8_t pin_length) {
702699
bool result = true;
703-
if(plugin_state->crypto_version < CRYPTO_LATEST_VERSION) {
700+
if(plugin_state->crypto_settings.crypto_version < CRYPTO_LATEST_VERSION) {
704701
FURI_LOG_I(LOGGING_TAG, "Migration to crypto v%d is needed", CRYPTO_LATEST_VERSION);
705702
char* backup_path = totp_config_file_backup(plugin_state);
706703
if(backup_path != NULL) {
707704
free(backup_path);
708-
uint8_t crypto_key_slot = plugin_state->crypto_key_slot;
705+
uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
709706
if(!totp_crypto_check_key_slot(crypto_key_slot)) {
710707
crypto_key_slot = DEFAULT_CRYPTO_KEY_SLOT;
711708
}

services/config/token_info_iterator.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <flipper_format/flipper_format_stream.h>
55
#include <toolbox/stream/file_stream.h>
66
#include "../../types/common.h"
7+
#include "../../types/crypto_settings.h"
78

89
#define CONFIG_FILE_PART_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf.part"
910
#define STREAM_COPY_BUFFER_SIZE 128
@@ -15,9 +16,7 @@ struct TokenInfoIteratorContext {
1516
size_t last_seek_index;
1617
TokenInfo* current_token;
1718
FlipperFormat* config_file;
18-
uint8_t* iv;
19-
uint8_t crypto_version;
20-
uint8_t crypto_key_slot;
19+
CryptoSettings* crypto_settings;
2120
Storage* storage;
2221
};
2322

@@ -242,9 +241,7 @@ static bool
242241
TokenInfoIteratorContext* totp_token_info_iterator_alloc(
243242
Storage* storage,
244243
FlipperFormat* config_file,
245-
uint8_t* iv,
246-
uint8_t crypto_version,
247-
uint8_t crypto_key_slot) {
244+
CryptoSettings* crypto_settings) {
248245
Stream* stream = flipper_format_get_raw_stream(config_file);
249246
stream_rewind(stream);
250247
size_t tokens_count = 0;
@@ -262,9 +259,7 @@ TokenInfoIteratorContext* totp_token_info_iterator_alloc(
262259
context->total_count = tokens_count;
263260
context->current_token = token_info_alloc();
264261
context->config_file = config_file;
265-
context->iv = iv;
266-
context->crypto_version = crypto_version;
267-
context->crypto_key_slot = crypto_key_slot;
262+
context->crypto_settings = crypto_settings;
268263
context->storage = storage;
269264
return context;
270265
}
@@ -461,9 +456,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
461456
furi_string_get_cstr(temp_str),
462457
furi_string_size(temp_str),
463458
PlainTokenSecretEncodingBase32,
464-
context->iv,
465-
context->crypto_version,
466-
context->crypto_key_slot)) {
459+
context->crypto_settings)) {
467460
FURI_LOG_W(
468461
LOGGING_TAG,
469462
"Token \"%s\" has plain secret",

services/config/token_info_iterator.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@ enum TotpIteratorUpdateTokenResults {
2828
* @brief Initializes a new token info iterator
2929
* @param storage storage reference
3030
* @param config_file config file to use
31-
* @param iv initialization vector (IV) to be used for encryption\decryption
32-
* @param crypto_version crypto algorithm version to be used
33-
* @param crypto_key_slot crypto key slot to be used
31+
* @param crypto_settings crypto settings
3432
* @return Token info iterator context
3533
*/
3634
TokenInfoIteratorContext* totp_token_info_iterator_alloc(
3735
Storage* storage,
3836
FlipperFormat* config_file,
39-
uint8_t* iv,
40-
uint8_t crypto_version,
41-
uint8_t crypto_key_slot);
37+
CryptoSettings* crypto_settings);
4238

4339
/**
4440
* @brief Navigates iterator to the token with given index

0 commit comments

Comments
 (0)