subghz_keystore.c 21 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 <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(TAG, "Invalid encrypted data: %s", encrypted_line);
  125. }
  126. } else {
  127. subghz_keystore_process_line(instance, encrypted_line);
  128. }
  129. // reset line buffer
  130. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  131. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  132. encrypted_line_cursor = 0;
  133. } else if(buffer[i] == '\r' || buffer[i] == '\n') {
  134. // do not add line endings to the buffer
  135. } else {
  136. if(encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
  137. encrypted_line[encrypted_line_cursor] = buffer[i];
  138. encrypted_line_cursor++;
  139. } else {
  140. FURI_LOG_E(TAG, "Malformed file");
  141. result = false;
  142. break;
  143. }
  144. }
  145. }
  146. } while(ret > 0 && result);
  147. if(iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  148. free(encrypted_line);
  149. free(decrypted_line);
  150. return result;
  151. }
  152. bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
  153. furi_assert(instance);
  154. bool result = false;
  155. uint8_t iv[16];
  156. uint32_t version;
  157. SubGhzKeystoreEncryption encryption;
  158. string_t filetype;
  159. string_init(filetype);
  160. Storage* storage = furi_record_open("storage");
  161. FlipperFile* flipper_file = flipper_file_alloc(storage);
  162. do {
  163. if(!flipper_file_open_existing(flipper_file, file_name)) {
  164. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  165. break;
  166. }
  167. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  168. FURI_LOG_E(TAG, "Missing or incorrect header");
  169. break;
  170. }
  171. if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  172. FURI_LOG_E(TAG, "Missing encryption type");
  173. break;
  174. }
  175. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
  176. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  177. FURI_LOG_E(TAG, "Type or version mismatch");
  178. break;
  179. }
  180. File* file = flipper_file_get_file(flipper_file);
  181. if(encryption == SubGhzKeystoreEncryptionNone) {
  182. result = subghz_keystore_read_file(instance, file, NULL);
  183. } else if(encryption == SubGhzKeystoreEncryptionAES256) {
  184. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  185. FURI_LOG_E(TAG, "Missing IV");
  186. break;
  187. }
  188. subghz_keystore_mess_with_iv(iv);
  189. result = subghz_keystore_read_file(instance, file, iv);
  190. } else {
  191. FURI_LOG_E(TAG, "Unknown encryption");
  192. break;
  193. }
  194. } while(0);
  195. flipper_file_close(flipper_file);
  196. flipper_file_free(flipper_file);
  197. furi_record_close("storage");
  198. string_clear(filetype);
  199. return result;
  200. }
  201. bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8_t* iv) {
  202. furi_assert(instance);
  203. bool result = false;
  204. Storage* storage = furi_record_open("storage");
  205. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  206. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  207. FlipperFile* flipper_file = flipper_file_alloc(storage);
  208. do {
  209. if(!flipper_file_open_always(flipper_file, file_name)) {
  210. FURI_LOG_E(TAG, "Unable to open file for write: %s", file_name);
  211. break;
  212. }
  213. if(!flipper_file_write_header_cstr(
  214. flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
  215. FURI_LOG_E(TAG, "Unable to add header");
  216. break;
  217. }
  218. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  219. if(!flipper_file_write_uint32(flipper_file, "Encryption", &encryption, 1)) {
  220. FURI_LOG_E(TAG, "Unable to add Encryption");
  221. break;
  222. }
  223. if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) {
  224. FURI_LOG_E(TAG, "Unable to add IV");
  225. break;
  226. }
  227. subghz_keystore_mess_with_iv(iv);
  228. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  229. FURI_LOG_E(TAG, "Unable to load encryption key");
  230. break;
  231. }
  232. File* file = flipper_file_get_file(flipper_file);
  233. size_t encrypted_line_count = 0;
  234. for
  235. M_EACH(key, instance->data, SubGhzKeyArray_t) {
  236. // Wipe buffer before packing
  237. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  238. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  239. // Form unecreypted line
  240. int len = snprintf(
  241. decrypted_line,
  242. SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
  243. "%08lX%08lX:%hu:%s",
  244. (uint32_t)(key->key >> 32),
  245. (uint32_t)key->key,
  246. key->type,
  247. string_get_cstr(key->name));
  248. // Verify length and align
  249. furi_assert(len > 0);
  250. if(len % 16 != 0) {
  251. len += (16 - len % 16);
  252. }
  253. furi_assert(len % 16 == 0);
  254. furi_assert(len <= SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  255. // Form encrypted line
  256. if(!furi_hal_crypto_encrypt(
  257. (uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
  258. FURI_LOG_E(TAG, "Encryption failed");
  259. break;
  260. }
  261. // HEX Encode encrypted line
  262. const char xx[] = "0123456789ABCDEF";
  263. for(size_t i = 0; i < len; i++) {
  264. size_t cursor = len - i - 1;
  265. size_t hex_cursor = len * 2 - i * 2 - 1;
  266. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  267. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  268. }
  269. storage_file_write(file, encrypted_line, strlen(encrypted_line));
  270. storage_file_write(file, "\n", 1);
  271. encrypted_line_count++;
  272. }
  273. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  274. size_t total_keys = SubGhzKeyArray_size(instance->data);
  275. result = encrypted_line_count == total_keys;
  276. if(result) {
  277. FURI_LOG_I(TAG, "Success. Encrypted: %d of %d", encrypted_line_count, total_keys);
  278. } else {
  279. FURI_LOG_E(TAG, "Failure. Encrypted: %d of %d", encrypted_line_count, total_keys);
  280. }
  281. } while(0);
  282. flipper_file_close(flipper_file);
  283. flipper_file_free(flipper_file);
  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 = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  304. FlipperFile* input_flipper_file = flipper_file_alloc(storage);
  305. do {
  306. if(!flipper_file_open_existing(input_flipper_file, input_file_name)) {
  307. FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
  308. break;
  309. }
  310. if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
  311. FURI_LOG_E(TAG, "Missing or incorrect header");
  312. break;
  313. }
  314. if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  315. FURI_LOG_E(TAG, "Missing encryption type");
  316. break;
  317. }
  318. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  319. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  320. FURI_LOG_E(TAG, "Type or version mismatch");
  321. break;
  322. }
  323. if(encryption != SubGhzKeystoreEncryptionNone) {
  324. FURI_LOG_E(TAG, "Already encryption");
  325. break;
  326. }
  327. File* input_file = flipper_file_get_file(input_flipper_file);
  328. FlipperFile* output_flipper_file = flipper_file_alloc(storage);
  329. if(!flipper_file_open_always(output_flipper_file, output_file_name)) {
  330. FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
  331. break;
  332. }
  333. if(!flipper_file_write_header_cstr(
  334. output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
  335. FURI_LOG_E(TAG, "Unable to add header");
  336. break;
  337. }
  338. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  339. if(!flipper_file_write_uint32(output_flipper_file, "Encryption", &encryption, 1)) {
  340. FURI_LOG_E(TAG, "Unable to add Encryption");
  341. break;
  342. }
  343. if(!flipper_file_write_hex(output_flipper_file, "IV", iv, 16)) {
  344. FURI_LOG_E(TAG, "Unable to add IV");
  345. break;
  346. }
  347. if(!flipper_file_write_string_cstr(output_flipper_file, "Encrypt_data", "RAW")) {
  348. FURI_LOG_E(TAG, "Unable to add Encrypt_data");
  349. break;
  350. }
  351. subghz_keystore_mess_with_iv(iv);
  352. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  353. FURI_LOG_E(TAG, "Unable to load encryption key");
  354. break;
  355. }
  356. File* output_file = flipper_file_get_file(output_flipper_file);
  357. char buffer[FILE_BUFFER_SIZE];
  358. bool result = true;
  359. size_t ret = 0;
  360. furi_assert(FILE_BUFFER_SIZE % 16 == 0);
  361. //skip the end of the previous line "\n"
  362. storage_file_read(input_file, buffer, 1);
  363. do {
  364. memset(buffer, 0, FILE_BUFFER_SIZE);
  365. ret = storage_file_read(input_file, buffer, FILE_BUFFER_SIZE);
  366. if(ret == 0) {
  367. break;
  368. }
  369. for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
  370. uint8_t hi_nibble = 0;
  371. uint8_t lo_nibble = 0;
  372. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  373. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  374. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  375. }
  376. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  377. // Form encrypted line
  378. if(!furi_hal_crypto_encrypt(
  379. (uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
  380. FURI_LOG_E(TAG, "Encryption failed");
  381. result = false;
  382. break;
  383. }
  384. // HEX Encode encrypted line
  385. const char xx[] = "0123456789ABCDEF";
  386. for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
  387. size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
  388. size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
  389. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  390. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  391. }
  392. storage_file_write(output_file, encrypted_line, strlen(encrypted_line));
  393. } while(ret > 0 && result);
  394. flipper_file_close(output_flipper_file);
  395. flipper_file_free(output_flipper_file);
  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_file_close(input_flipper_file);
  401. flipper_file_free(input_flipper_file);
  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 = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  415. FlipperFile* flipper_file = flipper_file_alloc(storage);
  416. do {
  417. if(!flipper_file_open_existing(flipper_file, file_name)) {
  418. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  419. break;
  420. }
  421. if(!flipper_file_read_header(flipper_file, str_temp, &version)) {
  422. FURI_LOG_E(TAG, "Missing or incorrect header");
  423. break;
  424. }
  425. if(!flipper_file_read_uint32(flipper_file, "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. File* file = flipper_file_get_file(flipper_file);
  435. if(encryption != SubGhzKeystoreEncryptionAES256) {
  436. FURI_LOG_E(TAG, "Unknown encryption");
  437. break;
  438. }
  439. if(offset < 16) {
  440. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  441. FURI_LOG_E(TAG, "Missing IV");
  442. break;
  443. }
  444. subghz_keystore_mess_with_iv(iv);
  445. }
  446. if(!flipper_file_read_string(flipper_file, "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. char buffer[bufer_size];
  458. size_t ret = 0;
  459. bool decrypted = true;
  460. //skip the end of the previous line "\n"
  461. storage_file_read(file, buffer, 1);
  462. size_t size = storage_file_size(file);
  463. size -= storage_file_tell(file);
  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. storage_file_seek(file, ((offset / 16) - 1) * 32, false);
  470. ret = storage_file_read(file, 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 = storage_file_read(file, 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_file_close(flipper_file);
  508. flipper_file_free(flipper_file);
  509. furi_record_close("storage");
  510. free(decrypted_line);
  511. string_clear(str_temp);
  512. return result;
  513. }