subghz_keystore.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. Storage* storage = furi_record_open("storage");
  163. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  164. do {
  165. if(!flipper_format_file_open_existing(flipper_format, file_name)) {
  166. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  167. break;
  168. }
  169. if(!flipper_format_read_header(flipper_format, filetype, &version)) {
  170. FURI_LOG_E(TAG, "Missing or incorrect header");
  171. break;
  172. }
  173. if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
  174. FURI_LOG_E(TAG, "Missing encryption type");
  175. break;
  176. }
  177. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
  178. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  179. FURI_LOG_E(TAG, "Type or version mismatch");
  180. break;
  181. }
  182. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  183. if(encryption == SubGhzKeystoreEncryptionNone) {
  184. result = subghz_keystore_read_file(instance, stream, NULL);
  185. } else if(encryption == SubGhzKeystoreEncryptionAES256) {
  186. if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
  187. FURI_LOG_E(TAG, "Missing IV");
  188. break;
  189. }
  190. subghz_keystore_mess_with_iv(iv);
  191. result = subghz_keystore_read_file(instance, stream, iv);
  192. } else {
  193. FURI_LOG_E(TAG, "Unknown encryption");
  194. break;
  195. }
  196. } while(0);
  197. flipper_format_free(flipper_format);
  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 = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  207. char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  208. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  209. do {
  210. if(!flipper_format_file_open_always(flipper_format, file_name)) {
  211. FURI_LOG_E(TAG, "Unable to open file for write: %s", file_name);
  212. break;
  213. }
  214. if(!flipper_format_write_header_cstr(
  215. flipper_format, 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_format_write_uint32(flipper_format, "Encryption", &encryption, 1)) {
  221. FURI_LOG_E(TAG, "Unable to add Encryption");
  222. break;
  223. }
  224. if(!flipper_format_write_hex(flipper_format, "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. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  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. stream_write_cstring(stream, encrypted_line);
  271. stream_write_char(stream, '\n');
  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_format_free(flipper_format);
  284. free(encrypted_line);
  285. free(decrypted_line);
  286. furi_record_close("storage");
  287. return result;
  288. }
  289. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  290. furi_assert(instance);
  291. return &instance->data;
  292. }
  293. bool subghz_keystore_raw_encrypted_save(
  294. const char* input_file_name,
  295. const char* output_file_name,
  296. uint8_t* iv) {
  297. bool encrypted = false;
  298. uint32_t version;
  299. string_t filetype;
  300. string_init(filetype);
  301. SubGhzKeystoreEncryption encryption;
  302. Storage* storage = furi_record_open("storage");
  303. char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  304. FlipperFormat* input_flipper_format = flipper_format_file_alloc(storage);
  305. do {
  306. if(!flipper_format_file_open_existing(input_flipper_format, input_file_name)) {
  307. FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
  308. break;
  309. }
  310. if(!flipper_format_read_header(input_flipper_format, filetype, &version)) {
  311. FURI_LOG_E(TAG, "Missing or incorrect header");
  312. break;
  313. }
  314. if(!flipper_format_read_uint32(
  315. input_flipper_format, "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. Stream* input_stream = flipper_format_get_raw_stream(input_flipper_format);
  329. FlipperFormat* output_flipper_format = flipper_format_file_alloc(storage);
  330. if(!flipper_format_file_open_always(output_flipper_format, output_file_name)) {
  331. FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
  332. break;
  333. }
  334. if(!flipper_format_write_header_cstr(
  335. output_flipper_format, 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_format_write_uint32(output_flipper_format, "Encryption", &encryption, 1)) {
  341. FURI_LOG_E(TAG, "Unable to add Encryption");
  342. break;
  343. }
  344. if(!flipper_format_write_hex(output_flipper_format, "IV", iv, 16)) {
  345. FURI_LOG_E(TAG, "Unable to add IV");
  346. break;
  347. }
  348. if(!flipper_format_write_string_cstr(output_flipper_format, "Encrypt_data", "RAW")) {
  349. FURI_LOG_E(TAG, "Unable to add Encrypt_data");
  350. break;
  351. }
  352. subghz_keystore_mess_with_iv(iv);
  353. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  354. FURI_LOG_E(TAG, "Unable to load encryption key");
  355. break;
  356. }
  357. Stream* output_stream = flipper_format_get_raw_stream(output_flipper_format);
  358. uint8_t buffer[FILE_BUFFER_SIZE];
  359. bool result = true;
  360. size_t ret = 0;
  361. furi_assert(FILE_BUFFER_SIZE % 16 == 0);
  362. //skip the end of the previous line "\n"
  363. stream_read(input_stream, buffer, 1);
  364. do {
  365. memset(buffer, 0, FILE_BUFFER_SIZE);
  366. ret = stream_read(input_stream, buffer, FILE_BUFFER_SIZE);
  367. if(ret == 0) {
  368. break;
  369. }
  370. for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
  371. uint8_t hi_nibble = 0;
  372. uint8_t lo_nibble = 0;
  373. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  374. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  375. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  376. }
  377. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  378. // Form encrypted line
  379. if(!furi_hal_crypto_encrypt(
  380. (uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
  381. FURI_LOG_E(TAG, "Encryption failed");
  382. result = false;
  383. break;
  384. }
  385. // HEX Encode encrypted line
  386. const char xx[] = "0123456789ABCDEF";
  387. for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
  388. size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
  389. size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
  390. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  391. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  392. }
  393. stream_write_cstring(output_stream, encrypted_line);
  394. } while(ret > 0 && result);
  395. flipper_format_free(output_flipper_format);
  396. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  397. if(!result) break;
  398. encrypted = true;
  399. } while(0);
  400. flipper_format_free(input_flipper_format);
  401. free(encrypted_line);
  402. furi_record_close("storage");
  403. return encrypted;
  404. }
  405. bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len) {
  406. bool result = false;
  407. uint8_t iv[16];
  408. uint32_t version;
  409. SubGhzKeystoreEncryption encryption;
  410. string_t str_temp;
  411. string_init(str_temp);
  412. Storage* storage = furi_record_open("storage");
  413. char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  414. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  415. do {
  416. if(!flipper_format_file_open_existing(flipper_format, file_name)) {
  417. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  418. break;
  419. }
  420. if(!flipper_format_read_header(flipper_format, str_temp, &version)) {
  421. FURI_LOG_E(TAG, "Missing or incorrect header");
  422. break;
  423. }
  424. if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
  425. FURI_LOG_E(TAG, "Missing encryption type");
  426. break;
  427. }
  428. if(strcmp(string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  429. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  430. FURI_LOG_E(TAG, "Type or version mismatch");
  431. break;
  432. }
  433. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  434. if(encryption != SubGhzKeystoreEncryptionAES256) {
  435. FURI_LOG_E(TAG, "Unknown encryption");
  436. break;
  437. }
  438. if(offset < 16) {
  439. if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
  440. FURI_LOG_E(TAG, "Missing IV");
  441. break;
  442. }
  443. subghz_keystore_mess_with_iv(iv);
  444. }
  445. if(!flipper_format_read_string(flipper_format, "Encrypt_data", str_temp)) {
  446. FURI_LOG_E(TAG, "Missing Encrypt_data");
  447. break;
  448. }
  449. size_t bufer_size;
  450. if(len <= (16 - offset % 16)) {
  451. bufer_size = 32;
  452. } else {
  453. bufer_size = (((len) / 16) + 2) * 32;
  454. }
  455. furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
  456. uint8_t buffer[bufer_size];
  457. size_t ret = 0;
  458. bool decrypted = true;
  459. //skip the end of the previous line "\n"
  460. stream_read(stream, buffer, 1);
  461. size_t size = stream_size(stream);
  462. size -= stream_tell(stream);
  463. if(size < (offset * 2 + len * 2)) {
  464. FURI_LOG_E(TAG, "Seek position exceeds file size");
  465. break;
  466. }
  467. if(offset >= 16) {
  468. stream_seek(stream, ((offset / 16) - 1) * 32, StreamOffsetFromCurrent);
  469. ret = stream_read(stream, buffer, 32);
  470. furi_assert(ret == 32);
  471. for(uint16_t i = 0; i < ret - 1; i += 2) {
  472. uint8_t hi_nibble = 0;
  473. uint8_t lo_nibble = 0;
  474. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  475. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  476. iv[i / 2] = (hi_nibble << 4) | lo_nibble;
  477. }
  478. }
  479. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  480. FURI_LOG_E(TAG, "Unable to load encryption key");
  481. break;
  482. }
  483. do {
  484. memset(buffer, 0, bufer_size);
  485. ret = stream_read(stream, buffer, bufer_size);
  486. furi_assert(ret == bufer_size);
  487. for(uint16_t i = 0; i < ret - 1; i += 2) {
  488. uint8_t hi_nibble = 0;
  489. uint8_t lo_nibble = 0;
  490. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  491. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  492. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  493. }
  494. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  495. if(!furi_hal_crypto_decrypt(
  496. (uint8_t*)buffer, (uint8_t*)decrypted_line, bufer_size / 2)) {
  497. decrypted = false;
  498. FURI_LOG_E(TAG, "Decryption failed");
  499. break;
  500. }
  501. memcpy(data, (uint8_t*)decrypted_line + (offset - (offset / 16) * 16), len);
  502. } while(0);
  503. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  504. if(decrypted) result = true;
  505. } while(0);
  506. flipper_format_free(flipper_format);
  507. furi_record_close("storage");
  508. free(decrypted_line);
  509. string_clear(str_temp);
  510. return result;
  511. }