2021-07-16 15:37:48 +00:00
|
|
|
#include "subghz_keystore.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-11-01 13:11:25 +00:00
|
|
|
|
[FL-1191][FL-1524] Filesystem rework (#568)
* FS-Api: removed datetime manipulation functions and most of the file flags
* Filesystem: common proxy api
* Filesystem: renamed to Storage. Work has begun on a glue layer. Added functions for reentrance.
* Storage: sd mount and sd file open
* Storage: sd file close
* Storage: temporary test app
* Storage: free filedata on close
* Storage: sd file read and write
* Storage: added internal storage (LittleFS)
* Storage: renamed internal commands
* Storage: seek, tell, truncate, size, sync, eof
* Storage: error descriptions
* Storage: directory management api (open, close, read, rewind)
* Storage: common management api (stat, fs_stat, remove, rename, mkdir)
* Dolphin app and Notifications app now use raw storage.
* Storage: storage statuses renamed. Implemented sd card icon.
* Storage: added raw sd-card api.
* Storage settings: work started
* Assets: use new icons approach
* Storage settings: working storage settings
* Storage: completely redesigned api, no longer sticking out FS_Api
* Storage: more simplified api, getting error_id from file is hidden from user, pointer to api is hidden inside file
* Storage: cli info and format commands
* Storage-cli: file list
* Storage: a simpler and more reliable api
* FatFS: slightly lighter and faster config. Also disabled reentrancy and file locking functions. They moved to a storage service.
* Storage-cli: accommodate to the new cli api.
* Storage: filesystem api is separated into internal and common api.
* Cli: added the ability to print the list of free heap blocks
* Storage: uses a list instead of an array to store the StorageFile. Rewrote api calls to use semaphores instead of thread flags.
* Storage settings: added the ability to benchmark the SD card.
* Gui module file select: uses new storage api
* Apps: removed deprecated sd_card_test application
* Args lib: support for enquoted arguments
* Dialogs: a new gui app for simple non-asynchronous apps
* Dialogs: view holder for easy single view work
* File worker: use new storage api
* IButton and lfrrfid apps: save keys to any storage
* Apps: fix ibutton and lfrfid stack, remove sd_card_test.
* SD filesystem: app removed
* File worker: fixed api pointer type
* Subghz: loading assets using the new storage api
* NFC: use the new storage api
* Dialogs: the better api for the message element
* Archive: use new storage api
* Irda: changed assest path, changed app path
* FileWorker: removed unused file_buf_cnt
* Storage: copying and renaming files now works between storages
* Storage cli: read, copy, remove, rename commands
* Archive: removed commented code
* Storage cli: write command
* Applications: add SRV_STORAGE and SRV_DIALOGS
* Internal-storage: removed
* Storage: improved api
* Storage app: changed api pointer from StorageApp to Storage
* Storage: better file_id handling
* Storage: more consistent errors
* Loader: support for NULL icons
* Storage: do nothing with the lfs file or directory if it is not open
* Storage: fix typo
* Storage: minor float usage cleanup, rename some symbols.
* Storage: compact doxygen comments.
Co-authored-by: あく <alleteam@gmail.com>
2021-07-23 12:20:19 +00:00
|
|
|
#include <storage/storage.h>
|
2021-11-04 10:06:13 +00:00
|
|
|
#include <toolbox/hex.h>
|
2022-02-18 19:53:46 +00:00
|
|
|
#include <toolbox/stream/stream.h>
|
|
|
|
#include <flipper_format/flipper_format.h>
|
|
|
|
#include <flipper_format/flipper_format_i.h>
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "SubGhzKeystore"
|
2021-07-16 15:37:48 +00:00
|
|
|
|
|
|
|
#define FILE_BUFFER_SIZE 64
|
|
|
|
|
2021-11-01 13:11:25 +00:00
|
|
|
#define SUBGHZ_KEYSTORE_FILE_TYPE "Flipper SubGhz Keystore File"
|
2021-11-03 16:41:07 +00:00
|
|
|
#define SUBGHZ_KEYSTORE_FILE_RAW_TYPE "Flipper SubGhz Keystore RAW File"
|
2021-11-01 13:11:25 +00:00
|
|
|
#define SUBGHZ_KEYSTORE_FILE_VERSION 0
|
|
|
|
|
|
|
|
#define SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT 1
|
|
|
|
#define SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE 512
|
2021-11-03 16:41:07 +00:00
|
|
|
#define SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE (SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE * 2)
|
2021-11-01 13:11:25 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
SubGhzKeystoreEncryptionNone,
|
|
|
|
SubGhzKeystoreEncryptionAES256,
|
|
|
|
} SubGhzKeystoreEncryption;
|
|
|
|
|
2021-07-16 15:37:48 +00:00
|
|
|
struct SubGhzKeystore {
|
|
|
|
SubGhzKeyArray_t data;
|
|
|
|
};
|
|
|
|
|
|
|
|
SubGhzKeystore* subghz_keystore_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
SubGhzKeystore* instance = malloc(sizeof(SubGhzKeystore));
|
2021-07-16 15:37:48 +00:00
|
|
|
|
|
|
|
SubGhzKeyArray_init(instance->data);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_keystore_free(SubGhzKeystore* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
for
|
2021-11-03 16:41:07 +00:00
|
|
|
M_EACH(manufacture_code, instance->data, SubGhzKeyArray_t) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(manufacture_code->name);
|
2021-11-03 16:41:07 +00:00
|
|
|
manufacture_code->key = 0;
|
|
|
|
}
|
2021-07-16 15:37:48 +00:00
|
|
|
SubGhzKeyArray_clear(instance->data);
|
|
|
|
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:41:07 +00:00
|
|
|
static void subghz_keystore_add_key(
|
|
|
|
SubGhzKeystore* instance,
|
|
|
|
const char* name,
|
|
|
|
uint64_t key,
|
|
|
|
uint16_t type) {
|
2021-07-16 15:37:48 +00:00
|
|
|
SubGhzKey* manufacture_code = SubGhzKeyArray_push_raw(instance->data);
|
2022-10-05 15:15:23 +00:00
|
|
|
manufacture_code->name = furi_string_alloc_set(name);
|
2021-07-16 15:37:48 +00:00
|
|
|
manufacture_code->key = key;
|
|
|
|
manufacture_code->type = type;
|
|
|
|
}
|
|
|
|
|
2021-11-01 13:11:25 +00:00
|
|
|
static bool subghz_keystore_process_line(SubGhzKeystore* instance, char* line) {
|
2021-07-16 15:37:48 +00:00
|
|
|
uint64_t key = 0;
|
|
|
|
uint16_t type = 0;
|
|
|
|
char skey[17] = {0};
|
|
|
|
char name[65] = {0};
|
2021-11-01 13:11:25 +00:00
|
|
|
int ret = sscanf(line, "%16s:%hu:%64s", skey, &type, name);
|
2021-07-16 15:37:48 +00:00
|
|
|
key = strtoull(skey, NULL, 16);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(ret == 3) {
|
2021-07-16 15:37:48 +00:00
|
|
|
subghz_keystore_add_key(instance, name, key, type);
|
2021-11-01 13:11:25 +00:00
|
|
|
return true;
|
2021-07-16 15:37:48 +00:00
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Failed to load line: %s\r\n", line);
|
2021-11-01 13:11:25 +00:00
|
|
|
return false;
|
2021-07-16 15:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 13:11:25 +00:00
|
|
|
static void subghz_keystore_mess_with_iv(uint8_t* iv) {
|
2021-11-26 13:53:51 +00:00
|
|
|
// Alignment check for `ldrd` instruction
|
|
|
|
furi_assert(((uint32_t)iv) % 4 == 0);
|
2021-11-01 13:11:25 +00:00
|
|
|
// Please do not share decrypted manufacture keys
|
|
|
|
// Sharing them will bring some discomfort to legal owners
|
|
|
|
// And potential legal action against you
|
|
|
|
// While you reading this code think about your own personal responsibility
|
2022-04-13 20:50:25 +00:00
|
|
|
asm volatile("nani%=: \n"
|
2021-11-26 11:47:17 +00:00
|
|
|
"ldrd r0, r2, [%0, #0x0] \n"
|
|
|
|
"lsl r1, r0, #8 \n"
|
|
|
|
"lsl r3, r2, #8 \n"
|
|
|
|
"orr r3, r3, r0, lsr #24\n"
|
|
|
|
"uadd8 r1, r1, r0 \n"
|
|
|
|
"uadd8 r3, r3, r2 \n"
|
|
|
|
"strd r1, r3, [%0, #0x0] \n"
|
|
|
|
"ldrd r1, r3, [%0, #0x8] \n"
|
|
|
|
"lsl r0, r1, #8 \n"
|
|
|
|
"orr r0, r0, r2, lsr #24\n"
|
|
|
|
"lsl r2, r3, #8 \n"
|
|
|
|
"orr r2, r2, r1, lsr #24\n"
|
|
|
|
"uadd8 r1, r1, r0 \n"
|
|
|
|
"uadd8 r3, r3, r2 \n"
|
|
|
|
"strd r1, r3, [%0, #0x8] \n"
|
2021-11-03 16:41:07 +00:00
|
|
|
:
|
2021-11-01 13:11:25 +00:00
|
|
|
: "r"(iv)
|
|
|
|
: "r0", "r1", "r2", "r3", "memory");
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
static bool subghz_keystore_read_file(SubGhzKeystore* instance, Stream* stream, uint8_t* iv) {
|
2021-11-01 13:11:25 +00:00
|
|
|
bool result = true;
|
2022-02-18 19:53:46 +00:00
|
|
|
uint8_t buffer[FILE_BUFFER_SIZE];
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
2021-11-01 13:11:25 +00:00
|
|
|
size_t encrypted_line_cursor = 0;
|
|
|
|
|
|
|
|
do {
|
2022-09-08 07:18:18 +00:00
|
|
|
if(iv) {
|
|
|
|
if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to load decryption key");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-09-08 07:18:18 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
do {
|
|
|
|
ret = stream_read(stream, buffer, FILE_BUFFER_SIZE);
|
|
|
|
for(uint16_t i = 0; i < ret; i++) {
|
|
|
|
if(buffer[i] == '\n' && encrypted_line_cursor > 0) {
|
|
|
|
// Process line
|
|
|
|
if(iv) {
|
|
|
|
// Data alignment check, 32 instead of 16 because of hex encoding
|
|
|
|
size_t len = strlen(encrypted_line);
|
|
|
|
if(len % 32 == 0) {
|
|
|
|
// Inplace hex to bin conversion
|
|
|
|
for(size_t i = 0; i < len; i += 2) {
|
|
|
|
uint8_t hi_nibble = 0;
|
|
|
|
uint8_t lo_nibble = 0;
|
|
|
|
hex_char_to_hex_nibble(encrypted_line[i], &hi_nibble);
|
|
|
|
hex_char_to_hex_nibble(encrypted_line[i + 1], &lo_nibble);
|
|
|
|
encrypted_line[i / 2] = (hi_nibble << 4) | lo_nibble;
|
|
|
|
}
|
|
|
|
len /= 2;
|
|
|
|
|
|
|
|
if(furi_hal_crypto_decrypt(
|
|
|
|
(uint8_t*)encrypted_line, (uint8_t*)decrypted_line, len)) {
|
|
|
|
subghz_keystore_process_line(instance, decrypted_line);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Decryption failed");
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-01 13:11:25 +00:00
|
|
|
} else {
|
2022-09-08 07:18:18 +00:00
|
|
|
FURI_LOG_E(TAG, "Invalid encrypted data: %s", encrypted_line);
|
2021-11-01 13:11:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 07:18:18 +00:00
|
|
|
subghz_keystore_process_line(instance, encrypted_line);
|
2021-11-01 13:11:25 +00:00
|
|
|
}
|
2022-09-08 07:18:18 +00:00
|
|
|
// reset line buffer
|
|
|
|
memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
|
|
|
encrypted_line_cursor = 0;
|
|
|
|
} else if(buffer[i] == '\r' || buffer[i] == '\n') {
|
|
|
|
// do not add line endings to the buffer
|
2021-07-16 15:37:48 +00:00
|
|
|
} else {
|
2022-09-08 07:18:18 +00:00
|
|
|
if(encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
|
|
|
|
encrypted_line[encrypted_line_cursor] = buffer[i];
|
|
|
|
encrypted_line_cursor++;
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Malformed file");
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-16 15:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-08 07:18:18 +00:00
|
|
|
} while(ret > 0 && result);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-09-08 07:18:18 +00:00
|
|
|
if(iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
|
|
|
} while(false);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
|
|
|
free(encrypted_line);
|
|
|
|
free(decrypted_line);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
|
|
|
|
furi_assert(instance);
|
|
|
|
bool result = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint32_t version;
|
|
|
|
SubGhzKeystoreEncryption encryption;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* filetype;
|
|
|
|
filetype = furi_string_alloc();
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-04-07 11:47:47 +00:00
|
|
|
FURI_LOG_I(TAG, "Loading keystore %s", file_name);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-11-01 13:11:25 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(flipper_format, file_name)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_header(flipper_format, filetype, &version)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing encryption type");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(strcmp(furi_string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
|
2021-11-03 16:41:07 +00:00
|
|
|
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Type or version mismatch");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(encryption == SubGhzKeystoreEncryptionNone) {
|
2022-02-18 19:53:46 +00:00
|
|
|
result = subghz_keystore_read_file(instance, stream, NULL);
|
2021-11-03 16:41:07 +00:00
|
|
|
} else if(encryption == SubGhzKeystoreEncryptionAES256) {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing IV");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
subghz_keystore_mess_with_iv(iv);
|
2022-02-18 19:53:46 +00:00
|
|
|
result = subghz_keystore_read_file(instance, stream, iv);
|
2021-11-01 13:11:25 +00:00
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unknown encryption");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(0);
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(flipper_format);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(filetype);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8_t* iv) {
|
|
|
|
furi_assert(instance);
|
|
|
|
bool result = false;
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 19:53:46 +00:00
|
|
|
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-11-01 13:11:25 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_always(flipper_format, file_name)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to open file for write: %s", file_name);
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
flipper_format, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add header");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-15 19:55:25 +00:00
|
|
|
uint32_t encryption = SubGhzKeystoreEncryptionAES256;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(flipper_format, "Encryption", &encryption, 1)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add Encryption");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(flipper_format, "IV", iv, 16)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add IV");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
subghz_keystore_mess_with_iv(iv);
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
2021-11-01 13:11:25 +00:00
|
|
|
size_t encrypted_line_count = 0;
|
|
|
|
for
|
2021-11-03 16:41:07 +00:00
|
|
|
M_EACH(key, instance->data, SubGhzKeyArray_t) {
|
2021-11-01 13:11:25 +00:00
|
|
|
// Wipe buffer before packing
|
|
|
|
memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
|
|
|
// Form unecreypted line
|
|
|
|
int len = snprintf(
|
2021-11-03 16:41:07 +00:00
|
|
|
decrypted_line,
|
|
|
|
SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
|
2021-11-01 13:11:25 +00:00
|
|
|
"%08lX%08lX:%hu:%s",
|
2021-11-03 16:41:07 +00:00
|
|
|
(uint32_t)(key->key >> 32),
|
|
|
|
(uint32_t)key->key,
|
|
|
|
key->type,
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(key->name));
|
2021-11-01 13:11:25 +00:00
|
|
|
// Verify length and align
|
|
|
|
furi_assert(len > 0);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(len % 16 != 0) {
|
2021-11-01 13:11:25 +00:00
|
|
|
len += (16 - len % 16);
|
|
|
|
}
|
|
|
|
furi_assert(len % 16 == 0);
|
|
|
|
furi_assert(len <= SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
// Form encrypted line
|
2021-11-03 16:41:07 +00:00
|
|
|
if(!furi_hal_crypto_encrypt(
|
|
|
|
(uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Encryption failed");
|
2021-11-01 13:11:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// HEX Encode encrypted line
|
2021-11-03 16:41:07 +00:00
|
|
|
const char xx[] = "0123456789ABCDEF";
|
2022-05-06 13:37:10 +00:00
|
|
|
for(int i = 0; i < len; i++) {
|
2021-11-01 13:11:25 +00:00
|
|
|
size_t cursor = len - i - 1;
|
2021-11-03 16:41:07 +00:00
|
|
|
size_t hex_cursor = len * 2 - i * 2 - 1;
|
2021-11-01 13:11:25 +00:00
|
|
|
encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
|
2021-11-03 16:41:07 +00:00
|
|
|
encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
|
2021-11-01 13:11:25 +00:00
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_write_cstring(stream, encrypted_line);
|
|
|
|
stream_write_char(stream, '\n');
|
2021-11-01 13:11:25 +00:00
|
|
|
encrypted_line_count++;
|
|
|
|
}
|
|
|
|
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
2021-11-16 18:52:49 +00:00
|
|
|
size_t total_keys = SubGhzKeyArray_size(instance->data);
|
|
|
|
result = encrypted_line_count == total_keys;
|
2022-01-05 16:10:18 +00:00
|
|
|
if(result) {
|
2021-11-16 18:52:49 +00:00
|
|
|
FURI_LOG_I(TAG, "Success. Encrypted: %d of %d", encrypted_line_count, total_keys);
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Failure. Encrypted: %d of %d", encrypted_line_count, total_keys);
|
|
|
|
}
|
2021-11-01 13:11:25 +00:00
|
|
|
} while(0);
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(flipper_format);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
|
|
|
free(encrypted_line);
|
|
|
|
free(decrypted_line);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-11-01 13:11:25 +00:00
|
|
|
|
|
|
|
return result;
|
2021-07-16 15:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return &instance->data;
|
|
|
|
}
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
bool subghz_keystore_raw_encrypted_save(
|
|
|
|
const char* input_file_name,
|
|
|
|
const char* output_file_name,
|
|
|
|
uint8_t* iv) {
|
|
|
|
bool encrypted = false;
|
|
|
|
uint32_t version;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* filetype;
|
|
|
|
filetype = furi_string_alloc();
|
2021-11-03 16:41:07 +00:00
|
|
|
SubGhzKeystoreEncryption encryption;
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* input_flipper_format = flipper_format_file_alloc(storage);
|
2021-11-03 16:41:07 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(input_flipper_format, input_file_name)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_header(input_flipper_format, filetype, &version)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
input_flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing encryption type");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(strcmp(furi_string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
|
2021-11-03 16:41:07 +00:00
|
|
|
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Type or version mismatch");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(encryption != SubGhzKeystoreEncryptionNone) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Already encryption");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* input_stream = flipper_format_get_raw_stream(input_flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* output_flipper_format = flipper_format_file_alloc(storage);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_always(output_flipper_format, output_file_name)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_header_cstr(
|
2022-10-05 15:15:23 +00:00
|
|
|
output_flipper_format,
|
|
|
|
furi_string_get_cstr(filetype),
|
|
|
|
SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add header");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-15 19:55:25 +00:00
|
|
|
uint32_t encryption = SubGhzKeystoreEncryptionAES256;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(output_flipper_format, "Encryption", &encryption, 1)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add Encryption");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(output_flipper_format, "IV", iv, 16)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add IV");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_string_cstr(output_flipper_format, "Encrypt_data", "RAW")) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to add Encrypt_data");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
subghz_keystore_mess_with_iv(iv);
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* output_stream = flipper_format_get_raw_stream(output_flipper_format);
|
|
|
|
uint8_t buffer[FILE_BUFFER_SIZE];
|
2021-11-03 16:41:07 +00:00
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
size_t ret = 0;
|
|
|
|
furi_assert(FILE_BUFFER_SIZE % 16 == 0);
|
|
|
|
|
|
|
|
//skip the end of the previous line "\n"
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_read(input_stream, buffer, 1);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
memset(buffer, 0, FILE_BUFFER_SIZE);
|
2022-02-18 19:53:46 +00:00
|
|
|
ret = stream_read(input_stream, buffer, FILE_BUFFER_SIZE);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(ret == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
|
|
|
|
uint8_t hi_nibble = 0;
|
|
|
|
uint8_t lo_nibble = 0;
|
|
|
|
hex_char_to_hex_nibble(buffer[i], &hi_nibble);
|
|
|
|
hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
|
|
|
|
buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
|
|
|
// Form encrypted line
|
|
|
|
if(!furi_hal_crypto_encrypt(
|
|
|
|
(uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Encryption failed");
|
2021-11-03 16:41:07 +00:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEX Encode encrypted line
|
|
|
|
const char xx[] = "0123456789ABCDEF";
|
|
|
|
for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
|
|
|
|
size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
|
|
|
|
size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
|
|
|
|
encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
|
|
|
|
encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_write_cstring(output_stream, encrypted_line);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
} while(result);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(output_flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
|
|
|
|
|
|
|
if(!result) break;
|
|
|
|
|
|
|
|
encrypted = true;
|
|
|
|
} while(0);
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(input_flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
free(encrypted_line);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
return encrypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len) {
|
|
|
|
bool result = false;
|
|
|
|
uint8_t iv[16];
|
|
|
|
uint32_t version;
|
|
|
|
SubGhzKeystoreEncryption encryption;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* str_temp;
|
|
|
|
str_temp = furi_string_alloc();
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 19:53:46 +00:00
|
|
|
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
2021-11-03 16:41:07 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(flipper_format, file_name)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_header(flipper_format, str_temp, &version)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing or incorrect header");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing encryption type");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(strcmp(furi_string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
|
2021-11-03 16:41:07 +00:00
|
|
|
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Type or version mismatch");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(encryption != SubGhzKeystoreEncryptionAES256) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unknown encryption");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(offset < 16) {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing IV");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
subghz_keystore_mess_with_iv(iv);
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(flipper_format, "Encrypt_data", str_temp)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing Encrypt_data");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bufer_size;
|
|
|
|
if(len <= (16 - offset % 16)) {
|
|
|
|
bufer_size = 32;
|
|
|
|
} else {
|
|
|
|
bufer_size = (((len) / 16) + 2) * 32;
|
|
|
|
}
|
|
|
|
furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
uint8_t buffer[bufer_size];
|
2021-11-03 16:41:07 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
bool decrypted = true;
|
|
|
|
//skip the end of the previous line "\n"
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_read(stream, buffer, 1);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
size_t size = stream_size(stream);
|
|
|
|
size -= stream_tell(stream);
|
2021-11-03 16:41:07 +00:00
|
|
|
if(size < (offset * 2 + len * 2)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Seek position exceeds file size");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(offset >= 16) {
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_seek(stream, ((offset / 16) - 1) * 32, StreamOffsetFromCurrent);
|
|
|
|
ret = stream_read(stream, buffer, 32);
|
2021-11-03 16:41:07 +00:00
|
|
|
furi_assert(ret == 32);
|
|
|
|
for(uint16_t i = 0; i < ret - 1; i += 2) {
|
|
|
|
uint8_t hi_nibble = 0;
|
|
|
|
uint8_t lo_nibble = 0;
|
|
|
|
hex_char_to_hex_nibble(buffer[i], &hi_nibble);
|
|
|
|
hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
|
|
|
|
iv[i / 2] = (hi_nibble << 4) | lo_nibble;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unable to load encryption key");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
memset(buffer, 0, bufer_size);
|
2022-02-18 19:53:46 +00:00
|
|
|
ret = stream_read(stream, buffer, bufer_size);
|
2021-11-03 16:41:07 +00:00
|
|
|
furi_assert(ret == bufer_size);
|
|
|
|
for(uint16_t i = 0; i < ret - 1; i += 2) {
|
|
|
|
uint8_t hi_nibble = 0;
|
|
|
|
uint8_t lo_nibble = 0;
|
|
|
|
hex_char_to_hex_nibble(buffer[i], &hi_nibble);
|
|
|
|
hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
|
|
|
|
buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
|
|
|
|
|
|
|
if(!furi_hal_crypto_decrypt(
|
|
|
|
(uint8_t*)buffer, (uint8_t*)decrypted_line, bufer_size / 2)) {
|
|
|
|
decrypted = false;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Decryption failed");
|
2021-11-03 16:41:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(data, (uint8_t*)decrypted_line + (offset - (offset / 16) * 16), len);
|
|
|
|
|
|
|
|
} while(0);
|
|
|
|
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
|
|
|
if(decrypted) result = true;
|
|
|
|
} while(0);
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(flipper_format);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
free(decrypted_line);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(str_temp);
|
2021-11-03 16:41:07 +00:00
|
|
|
|
|
|
|
return result;
|
2022-05-06 13:37:10 +00:00
|
|
|
}
|