SubGhz: reading keys from encrypted files (#803)
* SubGhz: add file with manufactory codes, and the ability to add your own manufactory codes for KeeLog * SubGhz: add encrypt RAW data, add decrypt and get RAW data * SubGhz: add encrypt magic_xor_atomo * SubGhz: parsing atomo using file encrypt * SubGhz: fix calculating the size of the read buffer * SubGhz: parsing Nice FLOR S using file encrypt * SubGhz: add file encrypt nice_flor_s_tx, fix name load file * SubGhz: fix checking read buffer size * Update subghz_keystore.c * SubGhz: fix calculating the size of the read buffer Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "subghz_protocol_came_atomo.h"
|
||||
#include "subghz_protocol_common.h"
|
||||
#include <lib/toolbox/manchester-decoder.h>
|
||||
#include "../subghz_keystore.h"
|
||||
|
||||
#define SUBGHZ_NO_CAME_ATOMO_RAINBOW_TABLE 0xFFFFFFFFFFFFFFFF
|
||||
|
||||
@@ -25,14 +26,8 @@ SubGhzProtocolCameAtomo* subghz_protocol_came_atomo_alloc() {
|
||||
instance->common.te_delta = 250;
|
||||
instance->common.type_protocol = SubGhzProtocolCommonTypeStatic;
|
||||
instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_came_atomo_to_str;
|
||||
// instance->common.to_save_string =
|
||||
// (SubGhzProtocolCommonGetStrSave)subghz_protocol_came_atomo_to_save_str;
|
||||
//instance->common.to_load_protocol_from_file =
|
||||
// (SubGhzProtocolCommonLoadFromFile)subghz_protocol_came_atomo_to_load_protocol_from_file;
|
||||
instance->common.to_load_protocol =
|
||||
(SubGhzProtocolCommonLoadFromRAW)subghz_decoder_came_atomo_to_load_protocol;
|
||||
// instance->common.get_upload_protocol =
|
||||
// (SubGhzProtocolCommonEncoderGetUpLoad)subghz_protocol_came_atomo_send_key;
|
||||
|
||||
return instance;
|
||||
}
|
||||
@@ -62,20 +57,14 @@ uint64_t subghz_came_atomo_get_atomo_magic_xor_in_file(
|
||||
uint32_t address = number_atomo_magic_xor * sizeof(uint64_t);
|
||||
uint64_t atomo_magic_xor = 0;
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(
|
||||
file_worker, instance->rainbow_table_file_name, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
file_worker_seek(file_worker, address, true);
|
||||
file_worker_read(file_worker, &buffer, sizeof(uint64_t));
|
||||
if(subghz_keystore_raw_get_data(
|
||||
instance->rainbow_table_file_name, address, buffer, sizeof(uint64_t))) {
|
||||
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
atomo_magic_xor = (atomo_magic_xor << 8) | buffer[i];
|
||||
}
|
||||
} else {
|
||||
atomo_magic_xor = SUBGHZ_NO_CAME_ATOMO_RAINBOW_TABLE;
|
||||
}
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
|
||||
return atomo_magic_xor;
|
||||
}
|
||||
|
||||
@@ -265,64 +254,6 @@ void subghz_protocol_came_atomo_to_str(SubGhzProtocolCameAtomo* instance, string
|
||||
instance->common.cnt);
|
||||
}
|
||||
|
||||
// void subghz_protocol_came_atomo_to_save_str(SubGhzProtocolCameAtomo* instance, string_t output) {
|
||||
// string_printf(
|
||||
// output,
|
||||
// "Protocol: %s\n"
|
||||
// "Bit: %d\n"
|
||||
// "Key: %08lX%08lX\r\n",
|
||||
// instance->common.name,
|
||||
// instance->common.code_last_count_bit,
|
||||
// (uint32_t)(instance->common.code_last_found >> 32),
|
||||
// (uint32_t)(instance->common.code_last_found & 0xFFFFFFFF));
|
||||
// }
|
||||
|
||||
// bool subghz_protocol_came_atomo_to_load_protocol_from_file(
|
||||
// FileWorker* file_worker,
|
||||
// SubGhzProtocolCameAtomo* instance,
|
||||
// const char* file_path) {
|
||||
// bool loaded = false;
|
||||
// string_t temp_str;
|
||||
// string_init(temp_str);
|
||||
// int res = 0;
|
||||
// int data = 0;
|
||||
|
||||
// do {
|
||||
// // Read and parse bit data from 2nd line
|
||||
// if(!file_worker_read_until(file_worker, temp_str, '\n')) {
|
||||
// break;
|
||||
// }
|
||||
// res = sscanf(string_get_cstr(temp_str), "Bit: %d\n", &data);
|
||||
// if(res != 1) {
|
||||
// break;
|
||||
// }
|
||||
// instance->common.code_last_count_bit = (uint8_t)data;
|
||||
|
||||
// // Read and parse key data from 3nd line
|
||||
// if(!file_worker_read_until(file_worker, temp_str, '\n')) {
|
||||
// break;
|
||||
// }
|
||||
// // strlen("Key: ") = 5
|
||||
// string_right(temp_str, 5);
|
||||
|
||||
// uint8_t buf_key[8] = {0};
|
||||
// if(!subghz_protocol_common_read_hex(temp_str, buf_key, 8)) {
|
||||
// break;
|
||||
// }
|
||||
|
||||
// for(uint8_t i = 0; i < 8; i++) {
|
||||
// instance->common.code_last_found = instance->common.code_last_found << 8 | buf_key[i];
|
||||
// }
|
||||
|
||||
// loaded = true;
|
||||
// } while(0);
|
||||
|
||||
// string_clear(temp_str);
|
||||
|
||||
// subghz_protocol_came_atomo_remote_controller(instance);
|
||||
// return loaded;
|
||||
// }
|
||||
|
||||
void subghz_decoder_came_atomo_to_load_protocol(SubGhzProtocolCameAtomo* instance, void* context) {
|
||||
furi_assert(context);
|
||||
furi_assert(instance);
|
||||
|
@@ -23,16 +23,6 @@ void subghz_protocol_came_atomo_free(SubGhzProtocolCameAtomo* instance);
|
||||
*/
|
||||
void subghz_protocol_came_atomo_name_file(SubGhzProtocolCameAtomo* instance, const char* name);
|
||||
|
||||
// /** Get upload protocol
|
||||
// *
|
||||
// * @param instance - SubGhzProtocolCameAtomo instance
|
||||
// * @param encoder - SubGhzProtocolCommonEncoder encoder
|
||||
// * @return bool
|
||||
// */
|
||||
// bool subghz_protocol_came_atomo_send_key(
|
||||
// SubGhzProtocolCameAtomo* instance,
|
||||
// SubGhzProtocolCommonEncoder* encoder);
|
||||
|
||||
/** Reset internal state
|
||||
* @param instance - SubGhzProtocolCameAtomo instance
|
||||
*/
|
||||
@@ -55,25 +45,6 @@ void subghz_protocol_came_atomo_parse(
|
||||
*/
|
||||
void subghz_protocol_came_atomo_to_str(SubGhzProtocolCameAtomo* instance, string_t output);
|
||||
|
||||
// /** Get a string to save the protocol
|
||||
// *
|
||||
// * @param instance - SubGhzProtocolCameAtomo instance
|
||||
// * @param output - the resulting string
|
||||
// */
|
||||
// void subghz_protocol_came_atomo_to_save_str(SubGhzProtocolCameAtomo* instance, string_t output);
|
||||
|
||||
// /** Loading protocol from file
|
||||
// *
|
||||
// * @param file_worker - FileWorker file_worker
|
||||
// * @param instance - SubGhzProtocolCameAtomo instance
|
||||
// * @param file_path - file path
|
||||
// * @return bool
|
||||
// */
|
||||
// bool subghz_protocol_came_atomo_to_load_protocol_from_file(
|
||||
// FileWorker* file_worker,
|
||||
// SubGhzProtocolCameAtomo* instance,
|
||||
// const char* file_path);
|
||||
|
||||
/** Loading protocol from bin data
|
||||
*
|
||||
* @param instance - SubGhzProtocolCameAtomo instance
|
||||
|
@@ -167,7 +167,7 @@ void subghz_protocol_faac_slh_to_str(SubGhzProtocolFaacSLH* instance, string_t o
|
||||
string_cat_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:0x%lX%08lX\r\n"
|
||||
"Key:%lX%08lX\r\n"
|
||||
"Fix:%08lX \r\n"
|
||||
"Hop:%08lX \r\n"
|
||||
"Sn:%07lX Btn:%lX\r\n",
|
||||
|
@@ -69,7 +69,7 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(
|
||||
M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) {
|
||||
switch(manufacture_code->type) {
|
||||
case KEELOQ_LEARNING_SIMPLE:
|
||||
//Simple Learning
|
||||
// Simple Learning
|
||||
decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
|
||||
if((decrypt >> 28 == btn) &&
|
||||
(((((uint16_t)(decrypt >> 16)) & 0x3FF) == end_serial) ||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <furi.h>
|
||||
#include "file-worker.h"
|
||||
#include "../subghz_keystore.h"
|
||||
/*
|
||||
* https://phreakerclub.com/1615
|
||||
* https://phreakerclub.com/forum/showthread.php?t=2360
|
||||
@@ -103,17 +104,13 @@ void subghz_protocol_nice_flor_s_send_key(
|
||||
uint8_t subghz_nice_flor_s_get_byte_in_file(SubGhzProtocolNiceFlorS* instance, uint32_t address) {
|
||||
if(!instance->rainbow_table_file_name) return 0;
|
||||
|
||||
uint8_t buffer = 0;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
if(file_worker_open(
|
||||
file_worker, instance->rainbow_table_file_name, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
file_worker_seek(file_worker, address, true);
|
||||
file_worker_read(file_worker, &buffer, 1);
|
||||
uint8_t buffer[1] = {0};
|
||||
if(subghz_keystore_raw_get_data(
|
||||
instance->rainbow_table_file_name, address, buffer, sizeof(uint8_t))) {
|
||||
return buffer[0];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/** Decrypt protocol Nice Flor S
|
||||
|
@@ -12,11 +12,12 @@
|
||||
#define FILE_BUFFER_SIZE 64
|
||||
|
||||
#define SUBGHZ_KEYSTORE_FILE_TYPE "Flipper SubGhz Keystore File"
|
||||
#define SUBGHZ_KEYSTORE_FILE_RAW_TYPE "Flipper SubGhz Keystore RAW File"
|
||||
#define SUBGHZ_KEYSTORE_FILE_VERSION 0
|
||||
|
||||
#define SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT 1
|
||||
#define SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE 512
|
||||
#define SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE (SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE*2)
|
||||
#define SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE (SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE * 2)
|
||||
|
||||
typedef enum {
|
||||
SubGhzKeystoreEncryptionNone,
|
||||
@@ -39,16 +40,20 @@ void subghz_keystore_free(SubGhzKeystore* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
for
|
||||
M_EACH(manufacture_code, instance->data, SubGhzKeyArray_t) {
|
||||
string_clear(manufacture_code->name);
|
||||
manufacture_code->key = 0;
|
||||
}
|
||||
M_EACH(manufacture_code, instance->data, SubGhzKeyArray_t) {
|
||||
string_clear(manufacture_code->name);
|
||||
manufacture_code->key = 0;
|
||||
}
|
||||
SubGhzKeyArray_clear(instance->data);
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static void subghz_keystore_add_key(SubGhzKeystore* instance, const char* name, uint64_t key, uint16_t type) {
|
||||
static void subghz_keystore_add_key(
|
||||
SubGhzKeystore* instance,
|
||||
const char* name,
|
||||
uint64_t key,
|
||||
uint16_t type) {
|
||||
SubGhzKey* manufacture_code = SubGhzKeyArray_push_raw(instance->data);
|
||||
string_init_set_str(manufacture_code->name, name);
|
||||
manufacture_code->key = key;
|
||||
@@ -62,7 +67,7 @@ static bool subghz_keystore_process_line(SubGhzKeystore* instance, char* line) {
|
||||
char name[65] = {0};
|
||||
int ret = sscanf(line, "%16s:%hu:%64s", skey, &type, name);
|
||||
key = strtoull(skey, NULL, 16);
|
||||
if (ret == 3) {
|
||||
if(ret == 3) {
|
||||
subghz_keystore_add_key(instance, name, key, type);
|
||||
return true;
|
||||
} else {
|
||||
@@ -76,21 +81,20 @@ static void subghz_keystore_mess_with_iv(uint8_t* iv) {
|
||||
// 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
|
||||
asm volatile(
|
||||
"movs r0, #0x0 \n"
|
||||
"movs r1, #0x0 \n"
|
||||
"movs r2, #0x0 \n"
|
||||
"movs r3, #0x0 \n"
|
||||
"nani: \n"
|
||||
"ldrb r1, [r0, %0]\n"
|
||||
"mov r2, r1 \n"
|
||||
"add r1, r3 \n"
|
||||
"mov r3, r2 \n"
|
||||
"strb r1, [r0, %0]\n"
|
||||
"adds r0, #0x1 \n"
|
||||
"cmp r0, #0xF \n"
|
||||
"bls nani \n"
|
||||
:
|
||||
asm volatile("movs r0, #0x0 \n"
|
||||
"movs r1, #0x0 \n"
|
||||
"movs r2, #0x0 \n"
|
||||
"movs r3, #0x0 \n"
|
||||
"nani: \n"
|
||||
"ldrb r1, [r0, %0]\n"
|
||||
"mov r2, r1 \n"
|
||||
"add r1, r3 \n"
|
||||
"mov r3, r2 \n"
|
||||
"strb r1, [r0, %0]\n"
|
||||
"adds r0, #0x1 \n"
|
||||
"cmp r0, #0xF \n"
|
||||
"bls nani \n"
|
||||
:
|
||||
: "r"(iv)
|
||||
: "r0", "r1", "r2", "r3", "memory");
|
||||
}
|
||||
@@ -103,29 +107,30 @@ static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint
|
||||
char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
size_t encrypted_line_cursor = 0;
|
||||
|
||||
if (iv) furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv);
|
||||
if(iv) furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv);
|
||||
|
||||
size_t ret = 0;
|
||||
do {
|
||||
ret = storage_file_read(file, buffer, FILE_BUFFER_SIZE);
|
||||
for (uint16_t i=0; i < ret; i++) {
|
||||
if (buffer[i] == '\n' && encrypted_line_cursor > 0) {
|
||||
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) {
|
||||
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;
|
||||
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;
|
||||
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)) {
|
||||
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(SUBGHZ_KEYSTORE_TAG, "Decryption failed");
|
||||
@@ -133,7 +138,8 @@ static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Invalid encrypted data: %s", encrypted_line);
|
||||
FURI_LOG_E(
|
||||
SUBGHZ_KEYSTORE_TAG, "Invalid encrypted data: %s", encrypted_line);
|
||||
}
|
||||
} else {
|
||||
subghz_keystore_process_line(instance, encrypted_line);
|
||||
@@ -142,10 +148,10 @@ static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint
|
||||
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') {
|
||||
} else if(buffer[i] == '\r' || buffer[i] == '\n') {
|
||||
// do not add line endings to the buffer
|
||||
} else {
|
||||
if (encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
|
||||
if(encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
|
||||
encrypted_line[encrypted_line_cursor] = buffer[i];
|
||||
encrypted_line_cursor++;
|
||||
} else {
|
||||
@@ -157,7 +163,7 @@ static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint
|
||||
}
|
||||
} while(ret > 0 && result);
|
||||
|
||||
if (iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
if(iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
|
||||
free(encrypted_line);
|
||||
free(decrypted_line);
|
||||
@@ -192,16 +198,16 @@ bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0
|
||||
|| version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
||||
if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
|
||||
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
|
||||
break;
|
||||
}
|
||||
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
if (encryption == SubGhzKeystoreEncryptionNone) {
|
||||
if(encryption == SubGhzKeystoreEncryptionNone) {
|
||||
result = subghz_keystore_read_file(instance, file, NULL);
|
||||
}else if (encryption == SubGhzKeystoreEncryptionAES256) {
|
||||
} else if(encryption == SubGhzKeystoreEncryptionAES256) {
|
||||
if(!flipper_file_read_hex_array(flipper_file, "IV", iv, 16)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing IV");
|
||||
break;
|
||||
@@ -237,7 +243,8 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for write: %s", file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_header_cstr(flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
if(!flipper_file_write_header_cstr(
|
||||
flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
@@ -260,43 +267,46 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
size_t encrypted_line_count = 0;
|
||||
for
|
||||
M_EACH(
|
||||
key,
|
||||
instance->data,
|
||||
SubGhzKeyArray_t) {
|
||||
M_EACH(key, instance->data, SubGhzKeyArray_t) {
|
||||
// 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(
|
||||
decrypted_line, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
|
||||
decrypted_line,
|
||||
SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
|
||||
"%08lX%08lX:%hu:%s",
|
||||
(uint32_t)(key->key>>32), (uint32_t)key->key, key->type, string_get_cstr(key->name));
|
||||
(uint32_t)(key->key >> 32),
|
||||
(uint32_t)key->key,
|
||||
key->type,
|
||||
string_get_cstr(key->name));
|
||||
// Verify length and align
|
||||
furi_assert(len > 0);
|
||||
if (len % 16 != 0) {
|
||||
if(len % 16 != 0) {
|
||||
len += (16 - len % 16);
|
||||
}
|
||||
furi_assert(len % 16 == 0);
|
||||
furi_assert(len <= SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
// Form encrypted line
|
||||
if(!furi_hal_crypto_encrypt((uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
|
||||
if(!furi_hal_crypto_encrypt(
|
||||
(uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Encryption failed");
|
||||
break;
|
||||
}
|
||||
// HEX Encode encrypted line
|
||||
const char xx[]= "0123456789ABCDEF";
|
||||
for (size_t i=0; i<len; i++) {
|
||||
const char xx[] = "0123456789ABCDEF";
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
size_t cursor = len - i - 1;
|
||||
size_t hex_cursor = len*2 - i*2 - 1;
|
||||
size_t hex_cursor = len * 2 - i * 2 - 1;
|
||||
encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
|
||||
encrypted_line[hex_cursor-1] = xx[(encrypted_line[cursor]>>4) & 0xF];
|
||||
encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
|
||||
}
|
||||
storage_file_write(file, encrypted_line, strlen(encrypted_line));
|
||||
storage_file_write(file, "\n", 1);
|
||||
encrypted_line_count++;
|
||||
|
||||
FURI_LOG_I(SUBGHZ_KEYSTORE_TAG, "Encrypted: `%s` -> `%s`", decrypted_line, encrypted_line);
|
||||
FURI_LOG_I(
|
||||
SUBGHZ_KEYSTORE_TAG, "Encrypted: `%s` -> `%s`", decrypted_line, encrypted_line);
|
||||
}
|
||||
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
result = encrypted_line_count == SubGhzKeyArray_size(instance->data);
|
||||
@@ -315,3 +325,272 @@ SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
|
||||
furi_assert(instance);
|
||||
return &instance->data;
|
||||
}
|
||||
|
||||
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;
|
||||
string_t filetype;
|
||||
string_init(filetype);
|
||||
SubGhzKeystoreEncryption encryption;
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
|
||||
char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
|
||||
FlipperFile* input_flipper_file = flipper_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_read(input_flipper_file, input_file_name)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for read: %s", input_file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing or incorrect header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing encryption type");
|
||||
break;
|
||||
}
|
||||
|
||||
if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
|
||||
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
|
||||
break;
|
||||
}
|
||||
|
||||
if(encryption != SubGhzKeystoreEncryptionNone) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Already encryption");
|
||||
break;
|
||||
}
|
||||
File* input_file = flipper_file_get_file(input_flipper_file);
|
||||
|
||||
FlipperFile* output_flipper_file = flipper_file_alloc(storage);
|
||||
|
||||
if(!flipper_file_new_write(output_flipper_file, output_file_name)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for write: %s", output_file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_header_cstr(
|
||||
output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_uint32(
|
||||
output_flipper_file, "Encryption", SubGhzKeystoreEncryptionAES256)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add Encryption");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_hex_array(output_flipper_file, "IV", iv, 16)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add IV");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_string_cstr(output_flipper_file, "Encrypt_data", "RAW")) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add Encrypt_data");
|
||||
break;
|
||||
}
|
||||
|
||||
subghz_keystore_mess_with_iv(iv);
|
||||
|
||||
if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to load encryption key");
|
||||
break;
|
||||
}
|
||||
|
||||
File* output_file = flipper_file_get_file(output_flipper_file);
|
||||
char buffer[FILE_BUFFER_SIZE];
|
||||
bool result = true;
|
||||
|
||||
size_t ret = 0;
|
||||
furi_assert(FILE_BUFFER_SIZE % 16 == 0);
|
||||
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_read(input_file, buffer, 1);
|
||||
|
||||
do {
|
||||
memset(buffer, 0, FILE_BUFFER_SIZE);
|
||||
ret = storage_file_read(input_file, buffer, FILE_BUFFER_SIZE);
|
||||
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)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Encryption failed");
|
||||
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];
|
||||
}
|
||||
storage_file_write(output_file, encrypted_line, strlen(encrypted_line));
|
||||
|
||||
} while(ret > 0 && result);
|
||||
|
||||
flipper_file_close(output_flipper_file);
|
||||
flipper_file_free(output_flipper_file);
|
||||
|
||||
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
|
||||
if(!result) break;
|
||||
|
||||
encrypted = true;
|
||||
} while(0);
|
||||
|
||||
flipper_file_close(input_flipper_file);
|
||||
flipper_file_free(input_flipper_file);
|
||||
|
||||
free(encrypted_line);
|
||||
|
||||
furi_record_close("storage");
|
||||
|
||||
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;
|
||||
|
||||
string_t str_temp;
|
||||
string_init(str_temp);
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
|
||||
FlipperFile* flipper_file = flipper_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_read(flipper_file, file_name)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for read: %s", file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_header(flipper_file, str_temp, &version)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing or incorrect header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing encryption type");
|
||||
break;
|
||||
}
|
||||
|
||||
if(strcmp(string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
|
||||
version != SUBGHZ_KEYSTORE_FILE_VERSION) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
|
||||
break;
|
||||
}
|
||||
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
if(encryption != SubGhzKeystoreEncryptionAES256) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unknown encryption");
|
||||
break;
|
||||
}
|
||||
|
||||
if(offset < 16) {
|
||||
if(!flipper_file_read_hex_array(flipper_file, "IV", iv, 16)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing IV");
|
||||
break;
|
||||
}
|
||||
subghz_keystore_mess_with_iv(iv);
|
||||
}
|
||||
|
||||
if(!flipper_file_read_string(flipper_file, "Encrypt_data", str_temp)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing Encrypt_data");
|
||||
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);
|
||||
|
||||
char buffer[bufer_size];
|
||||
size_t ret = 0;
|
||||
bool decrypted = true;
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_read(file, buffer, 1);
|
||||
|
||||
size_t size = storage_file_size(file);
|
||||
size -= storage_file_tell(file);
|
||||
if(size < (offset * 2 + len * 2)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Seek position exceeds file size");
|
||||
break;
|
||||
}
|
||||
|
||||
if(offset >= 16) {
|
||||
storage_file_seek(file, ((offset / 16) - 1) * 32, false);
|
||||
ret = storage_file_read(file, buffer, 32);
|
||||
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)) {
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to load encryption key");
|
||||
break;
|
||||
}
|
||||
|
||||
do {
|
||||
memset(buffer, 0, bufer_size);
|
||||
ret = storage_file_read(file, buffer, bufer_size);
|
||||
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;
|
||||
FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Decryption failed");
|
||||
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);
|
||||
flipper_file_close(flipper_file);
|
||||
flipper_file_free(flipper_file);
|
||||
|
||||
furi_record_close("storage");
|
||||
|
||||
free(decrypted_line);
|
||||
|
||||
string_clear(str_temp);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -48,3 +48,22 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* filename, uint8_
|
||||
* @return SubGhzKeyArray_t*
|
||||
*/
|
||||
SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance);
|
||||
|
||||
/** Save RAW encrypted to file
|
||||
*
|
||||
* @param input_file_name - const char* full path to the input file
|
||||
* @param output_file_name - const char* full path to the output file
|
||||
*/
|
||||
bool subghz_keystore_raw_encrypted_save(
|
||||
const char* input_file_name,
|
||||
const char* output_file_name,
|
||||
uint8_t* iv);
|
||||
|
||||
/** Get decrypt RAW data to file
|
||||
*
|
||||
* @param file_name - const char* full path to the input file
|
||||
* @param offset - offset from the start of the RAW data
|
||||
* @param data - returned array
|
||||
* @param len - required data length
|
||||
*/
|
||||
bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len);
|
||||
|
Reference in New Issue
Block a user