subghz_keystore.c 21 KB

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