subghz_keystore.c 22 KB

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