subghz_keystore.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include "subghz_keystore.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <storage/storage.h>
  5. #include <lib/toolbox/hex.h>
  6. #include <lib/toolbox/flipper-file.h>
  7. #define SUBGHZ_KEYSTORE_TAG "SubGhzParser"
  8. #define FILE_BUFFER_SIZE 64
  9. #define SUBGHZ_KEYSTORE_FILE_TYPE "Flipper SubGhz Keystore File"
  10. #define SUBGHZ_KEYSTORE_FILE_RAW_TYPE "Flipper SubGhz Keystore RAW File"
  11. #define SUBGHZ_KEYSTORE_FILE_VERSION 0
  12. #define SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT 1
  13. #define SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE 512
  14. #define SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE (SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE * 2)
  15. typedef enum {
  16. SubGhzKeystoreEncryptionNone,
  17. SubGhzKeystoreEncryptionAES256,
  18. } SubGhzKeystoreEncryption;
  19. struct SubGhzKeystore {
  20. SubGhzKeyArray_t data;
  21. };
  22. SubGhzKeystore* subghz_keystore_alloc() {
  23. SubGhzKeystore* instance = furi_alloc(sizeof(SubGhzKeystore));
  24. SubGhzKeyArray_init(instance->data);
  25. return instance;
  26. }
  27. void subghz_keystore_free(SubGhzKeystore* instance) {
  28. furi_assert(instance);
  29. for
  30. M_EACH(manufacture_code, instance->data, SubGhzKeyArray_t) {
  31. string_clear(manufacture_code->name);
  32. manufacture_code->key = 0;
  33. }
  34. SubGhzKeyArray_clear(instance->data);
  35. free(instance);
  36. }
  37. static void subghz_keystore_add_key(
  38. SubGhzKeystore* instance,
  39. const char* name,
  40. uint64_t key,
  41. uint16_t type) {
  42. SubGhzKey* manufacture_code = SubGhzKeyArray_push_raw(instance->data);
  43. string_init_set_str(manufacture_code->name, name);
  44. manufacture_code->key = key;
  45. manufacture_code->type = type;
  46. }
  47. static bool subghz_keystore_process_line(SubGhzKeystore* instance, char* line) {
  48. uint64_t key = 0;
  49. uint16_t type = 0;
  50. char skey[17] = {0};
  51. char name[65] = {0};
  52. int ret = sscanf(line, "%16s:%hu:%64s", skey, &type, name);
  53. key = strtoull(skey, NULL, 16);
  54. if(ret == 3) {
  55. subghz_keystore_add_key(instance, name, key, type);
  56. return true;
  57. } else {
  58. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Failed to load line: %s\r\n", line);
  59. return false;
  60. }
  61. }
  62. static void subghz_keystore_mess_with_iv(uint8_t* iv) {
  63. // Please do not share decrypted manufacture keys
  64. // Sharing them will bring some discomfort to legal owners
  65. // And potential legal action against you
  66. // While you reading this code think about your own personal responsibility
  67. asm volatile("movs r0, #0x0 \n"
  68. "movs r1, #0x0 \n"
  69. "movs r2, #0x0 \n"
  70. "movs r3, #0x0 \n"
  71. "nani: \n"
  72. "ldrb r1, [r0, %0]\n"
  73. "mov r2, r1 \n"
  74. "add r1, r3 \n"
  75. "mov r3, r2 \n"
  76. "strb r1, [r0, %0]\n"
  77. "adds r0, #0x1 \n"
  78. "cmp r0, #0xF \n"
  79. "bls nani \n"
  80. :
  81. : "r"(iv)
  82. : "r0", "r1", "r2", "r3", "memory");
  83. }
  84. static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint8_t* iv) {
  85. bool result = true;
  86. char buffer[FILE_BUFFER_SIZE];
  87. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  88. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  89. size_t encrypted_line_cursor = 0;
  90. if(iv) furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv);
  91. size_t ret = 0;
  92. do {
  93. ret = storage_file_read(file, buffer, FILE_BUFFER_SIZE);
  94. for(uint16_t i = 0; i < ret; i++) {
  95. if(buffer[i] == '\n' && encrypted_line_cursor > 0) {
  96. // Process line
  97. if(iv) {
  98. // Data alignment check, 32 instead of 16 because of hex encoding
  99. size_t len = strlen(encrypted_line);
  100. if(len % 32 == 0) {
  101. // Inplace hex to bin conversion
  102. for(size_t i = 0; i < len; i += 2) {
  103. uint8_t hi_nibble = 0;
  104. uint8_t lo_nibble = 0;
  105. hex_char_to_hex_nibble(encrypted_line[i], &hi_nibble);
  106. hex_char_to_hex_nibble(encrypted_line[i + 1], &lo_nibble);
  107. encrypted_line[i / 2] = (hi_nibble << 4) | lo_nibble;
  108. }
  109. len /= 2;
  110. if(furi_hal_crypto_decrypt(
  111. (uint8_t*)encrypted_line, (uint8_t*)decrypted_line, len)) {
  112. subghz_keystore_process_line(instance, decrypted_line);
  113. } else {
  114. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Decryption failed");
  115. result = false;
  116. break;
  117. }
  118. } else {
  119. FURI_LOG_E(
  120. SUBGHZ_KEYSTORE_TAG, "Invalid encrypted data: %s", encrypted_line);
  121. }
  122. } else {
  123. subghz_keystore_process_line(instance, encrypted_line);
  124. }
  125. // reset line buffer
  126. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  127. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  128. encrypted_line_cursor = 0;
  129. } else if(buffer[i] == '\r' || buffer[i] == '\n') {
  130. // do not add line endings to the buffer
  131. } else {
  132. if(encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
  133. encrypted_line[encrypted_line_cursor] = buffer[i];
  134. encrypted_line_cursor++;
  135. } else {
  136. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Malformed file");
  137. result = false;
  138. break;
  139. }
  140. }
  141. }
  142. } while(ret > 0 && result);
  143. if(iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  144. free(encrypted_line);
  145. free(decrypted_line);
  146. return result;
  147. }
  148. bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
  149. furi_assert(instance);
  150. bool result = false;
  151. uint8_t iv[16];
  152. uint32_t version;
  153. SubGhzKeystoreEncryption encryption;
  154. string_t filetype;
  155. string_init(filetype);
  156. Storage* storage = furi_record_open("storage");
  157. FlipperFile* flipper_file = flipper_file_alloc(storage);
  158. do {
  159. if(!flipper_file_open_read(flipper_file, file_name)) {
  160. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for read: %s", file_name);
  161. break;
  162. }
  163. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  164. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing or incorrect header");
  165. break;
  166. }
  167. if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption)) {
  168. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing encryption type");
  169. break;
  170. }
  171. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
  172. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  173. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
  174. break;
  175. }
  176. File* file = flipper_file_get_file(flipper_file);
  177. if(encryption == SubGhzKeystoreEncryptionNone) {
  178. result = subghz_keystore_read_file(instance, file, NULL);
  179. } else if(encryption == SubGhzKeystoreEncryptionAES256) {
  180. if(!flipper_file_read_hex_array(flipper_file, "IV", iv, 16)) {
  181. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing IV");
  182. break;
  183. }
  184. subghz_keystore_mess_with_iv(iv);
  185. result = subghz_keystore_read_file(instance, file, iv);
  186. } else {
  187. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unknown encryption");
  188. break;
  189. }
  190. } while(0);
  191. flipper_file_close(flipper_file);
  192. flipper_file_free(flipper_file);
  193. furi_record_close("storage");
  194. string_clear(filetype);
  195. return result;
  196. }
  197. bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8_t* iv) {
  198. furi_assert(instance);
  199. bool result = false;
  200. Storage* storage = furi_record_open("storage");
  201. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  202. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  203. FlipperFile* flipper_file = flipper_file_alloc(storage);
  204. do {
  205. if(!flipper_file_new_write(flipper_file, file_name)) {
  206. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for write: %s", file_name);
  207. break;
  208. }
  209. if(!flipper_file_write_header_cstr(
  210. flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
  211. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add header");
  212. break;
  213. }
  214. if(!flipper_file_write_uint32(flipper_file, "Encryption", SubGhzKeystoreEncryptionAES256)) {
  215. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add Encryption");
  216. break;
  217. }
  218. if(!flipper_file_write_hex_array(flipper_file, "IV", iv, 16)) {
  219. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add IV");
  220. break;
  221. }
  222. subghz_keystore_mess_with_iv(iv);
  223. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  224. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to load encryption key");
  225. break;
  226. }
  227. File* file = flipper_file_get_file(flipper_file);
  228. size_t encrypted_line_count = 0;
  229. for
  230. M_EACH(key, instance->data, SubGhzKeyArray_t) {
  231. // Wipe buffer before packing
  232. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  233. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  234. // Form unecreypted line
  235. int len = snprintf(
  236. decrypted_line,
  237. SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
  238. "%08lX%08lX:%hu:%s",
  239. (uint32_t)(key->key >> 32),
  240. (uint32_t)key->key,
  241. key->type,
  242. string_get_cstr(key->name));
  243. // Verify length and align
  244. furi_assert(len > 0);
  245. if(len % 16 != 0) {
  246. len += (16 - len % 16);
  247. }
  248. furi_assert(len % 16 == 0);
  249. furi_assert(len <= SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  250. // Form encrypted line
  251. if(!furi_hal_crypto_encrypt(
  252. (uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
  253. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Encryption failed");
  254. break;
  255. }
  256. // HEX Encode encrypted line
  257. const char xx[] = "0123456789ABCDEF";
  258. for(size_t i = 0; i < len; i++) {
  259. size_t cursor = len - i - 1;
  260. size_t hex_cursor = len * 2 - i * 2 - 1;
  261. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  262. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  263. }
  264. storage_file_write(file, encrypted_line, strlen(encrypted_line));
  265. storage_file_write(file, "\n", 1);
  266. encrypted_line_count++;
  267. FURI_LOG_I(
  268. SUBGHZ_KEYSTORE_TAG, "Encrypted: `%s` -> `%s`", decrypted_line, encrypted_line);
  269. }
  270. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  271. result = encrypted_line_count == SubGhzKeyArray_size(instance->data);
  272. } while(0);
  273. flipper_file_close(flipper_file);
  274. flipper_file_free(flipper_file);
  275. free(encrypted_line);
  276. free(decrypted_line);
  277. furi_record_close("storage");
  278. return result;
  279. }
  280. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  281. furi_assert(instance);
  282. return &instance->data;
  283. }
  284. bool subghz_keystore_raw_encrypted_save(
  285. const char* input_file_name,
  286. const char* output_file_name,
  287. uint8_t* iv) {
  288. bool encrypted = false;
  289. uint32_t version;
  290. string_t filetype;
  291. string_init(filetype);
  292. SubGhzKeystoreEncryption encryption;
  293. Storage* storage = furi_record_open("storage");
  294. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  295. FlipperFile* input_flipper_file = flipper_file_alloc(storage);
  296. do {
  297. if(!flipper_file_open_read(input_flipper_file, input_file_name)) {
  298. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for read: %s", input_file_name);
  299. break;
  300. }
  301. if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
  302. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing or incorrect header");
  303. break;
  304. }
  305. if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption)) {
  306. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing encryption type");
  307. break;
  308. }
  309. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  310. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  311. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
  312. break;
  313. }
  314. if(encryption != SubGhzKeystoreEncryptionNone) {
  315. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Already encryption");
  316. break;
  317. }
  318. File* input_file = flipper_file_get_file(input_flipper_file);
  319. FlipperFile* output_flipper_file = flipper_file_alloc(storage);
  320. if(!flipper_file_new_write(output_flipper_file, output_file_name)) {
  321. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for write: %s", output_file_name);
  322. break;
  323. }
  324. if(!flipper_file_write_header_cstr(
  325. output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
  326. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add header");
  327. break;
  328. }
  329. if(!flipper_file_write_uint32(
  330. output_flipper_file, "Encryption", SubGhzKeystoreEncryptionAES256)) {
  331. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add Encryption");
  332. break;
  333. }
  334. if(!flipper_file_write_hex_array(output_flipper_file, "IV", iv, 16)) {
  335. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add IV");
  336. break;
  337. }
  338. if(!flipper_file_write_string_cstr(output_flipper_file, "Encrypt_data", "RAW")) {
  339. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to add Encrypt_data");
  340. break;
  341. }
  342. subghz_keystore_mess_with_iv(iv);
  343. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  344. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to load encryption key");
  345. break;
  346. }
  347. File* output_file = flipper_file_get_file(output_flipper_file);
  348. char buffer[FILE_BUFFER_SIZE];
  349. bool result = true;
  350. size_t ret = 0;
  351. furi_assert(FILE_BUFFER_SIZE % 16 == 0);
  352. //skip the end of the previous line "\n"
  353. storage_file_read(input_file, buffer, 1);
  354. do {
  355. memset(buffer, 0, FILE_BUFFER_SIZE);
  356. ret = storage_file_read(input_file, buffer, FILE_BUFFER_SIZE);
  357. if(ret == 0) {
  358. break;
  359. }
  360. for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
  361. uint8_t hi_nibble = 0;
  362. uint8_t lo_nibble = 0;
  363. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  364. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  365. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  366. }
  367. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  368. // Form encrypted line
  369. if(!furi_hal_crypto_encrypt(
  370. (uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
  371. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Encryption failed");
  372. result = false;
  373. break;
  374. }
  375. // HEX Encode encrypted line
  376. const char xx[] = "0123456789ABCDEF";
  377. for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
  378. size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
  379. size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
  380. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  381. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  382. }
  383. storage_file_write(output_file, encrypted_line, strlen(encrypted_line));
  384. } while(ret > 0 && result);
  385. flipper_file_close(output_flipper_file);
  386. flipper_file_free(output_flipper_file);
  387. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  388. if(!result) break;
  389. encrypted = true;
  390. } while(0);
  391. flipper_file_close(input_flipper_file);
  392. flipper_file_free(input_flipper_file);
  393. free(encrypted_line);
  394. furi_record_close("storage");
  395. return encrypted;
  396. }
  397. bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len) {
  398. bool result = false;
  399. uint8_t iv[16];
  400. uint32_t version;
  401. SubGhzKeystoreEncryption encryption;
  402. string_t str_temp;
  403. string_init(str_temp);
  404. Storage* storage = furi_record_open("storage");
  405. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  406. FlipperFile* flipper_file = flipper_file_alloc(storage);
  407. do {
  408. if(!flipper_file_open_read(flipper_file, file_name)) {
  409. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to open file for read: %s", file_name);
  410. break;
  411. }
  412. if(!flipper_file_read_header(flipper_file, str_temp, &version)) {
  413. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing or incorrect header");
  414. break;
  415. }
  416. if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption)) {
  417. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing encryption type");
  418. break;
  419. }
  420. if(strcmp(string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  421. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  422. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Type or version mismatch");
  423. break;
  424. }
  425. File* file = flipper_file_get_file(flipper_file);
  426. if(encryption != SubGhzKeystoreEncryptionAES256) {
  427. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unknown encryption");
  428. break;
  429. }
  430. if(offset < 16) {
  431. if(!flipper_file_read_hex_array(flipper_file, "IV", iv, 16)) {
  432. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing IV");
  433. break;
  434. }
  435. subghz_keystore_mess_with_iv(iv);
  436. }
  437. if(!flipper_file_read_string(flipper_file, "Encrypt_data", str_temp)) {
  438. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Missing Encrypt_data");
  439. break;
  440. }
  441. size_t bufer_size;
  442. if(len <= (16 - offset % 16)) {
  443. bufer_size = 32;
  444. } else {
  445. bufer_size = (((len) / 16) + 2) * 32;
  446. }
  447. furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
  448. char buffer[bufer_size];
  449. size_t ret = 0;
  450. bool decrypted = true;
  451. //skip the end of the previous line "\n"
  452. storage_file_read(file, buffer, 1);
  453. size_t size = storage_file_size(file);
  454. size -= storage_file_tell(file);
  455. if(size < (offset * 2 + len * 2)) {
  456. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Seek position exceeds file size");
  457. break;
  458. }
  459. if(offset >= 16) {
  460. storage_file_seek(file, ((offset / 16) - 1) * 32, false);
  461. ret = storage_file_read(file, buffer, 32);
  462. furi_assert(ret == 32);
  463. for(uint16_t i = 0; i < ret - 1; i += 2) {
  464. uint8_t hi_nibble = 0;
  465. uint8_t lo_nibble = 0;
  466. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  467. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  468. iv[i / 2] = (hi_nibble << 4) | lo_nibble;
  469. }
  470. }
  471. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  472. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Unable to load encryption key");
  473. break;
  474. }
  475. do {
  476. memset(buffer, 0, bufer_size);
  477. ret = storage_file_read(file, buffer, bufer_size);
  478. furi_assert(ret == bufer_size);
  479. for(uint16_t i = 0; i < ret - 1; i += 2) {
  480. uint8_t hi_nibble = 0;
  481. uint8_t lo_nibble = 0;
  482. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  483. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  484. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  485. }
  486. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  487. if(!furi_hal_crypto_decrypt(
  488. (uint8_t*)buffer, (uint8_t*)decrypted_line, bufer_size / 2)) {
  489. decrypted = false;
  490. FURI_LOG_E(SUBGHZ_KEYSTORE_TAG, "Decryption failed");
  491. break;
  492. }
  493. memcpy(data, (uint8_t*)decrypted_line + (offset - (offset / 16) * 16), len);
  494. } while(0);
  495. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  496. if(decrypted) result = true;
  497. } while(0);
  498. flipper_file_close(flipper_file);
  499. flipper_file_free(flipper_file);
  500. furi_record_close("storage");
  501. free(decrypted_line);
  502. string_clear(str_temp);
  503. return result;
  504. }