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. // 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("nani: \n"
  68. "ldrd r0, r2, [%0, #0x0] \n"
  69. "lsl r1, r0, #8 \n"
  70. "lsl r3, r2, #8 \n"
  71. "orr r3, r3, r0, lsr #24\n"
  72. "uadd8 r1, r1, r0 \n"
  73. "uadd8 r3, r3, r2 \n"
  74. "strd r1, r3, [%0, #0x0] \n"
  75. "ldrd r1, r3, [%0, #0x8] \n"
  76. "lsl r0, r1, #8 \n"
  77. "orr r0, r0, r2, lsr #24\n"
  78. "lsl r2, r3, #8 \n"
  79. "orr r2, r2, r1, lsr #24\n"
  80. "uadd8 r1, r1, r0 \n"
  81. "uadd8 r3, r3, r2 \n"
  82. "strd r1, r3, [%0, #0x8] \n"
  83. :
  84. : "r"(iv)
  85. : "r0", "r1", "r2", "r3", "memory");
  86. }
  87. static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint8_t* iv) {
  88. bool result = true;
  89. char buffer[FILE_BUFFER_SIZE];
  90. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  91. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  92. size_t encrypted_line_cursor = 0;
  93. if(iv) furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv);
  94. size_t ret = 0;
  95. do {
  96. ret = storage_file_read(file, buffer, FILE_BUFFER_SIZE);
  97. for(uint16_t i = 0; i < ret; i++) {
  98. if(buffer[i] == '\n' && encrypted_line_cursor > 0) {
  99. // Process line
  100. if(iv) {
  101. // Data alignment check, 32 instead of 16 because of hex encoding
  102. size_t len = strlen(encrypted_line);
  103. if(len % 32 == 0) {
  104. // Inplace hex to bin conversion
  105. for(size_t i = 0; i < len; i += 2) {
  106. uint8_t hi_nibble = 0;
  107. uint8_t lo_nibble = 0;
  108. hex_char_to_hex_nibble(encrypted_line[i], &hi_nibble);
  109. hex_char_to_hex_nibble(encrypted_line[i + 1], &lo_nibble);
  110. encrypted_line[i / 2] = (hi_nibble << 4) | lo_nibble;
  111. }
  112. len /= 2;
  113. if(furi_hal_crypto_decrypt(
  114. (uint8_t*)encrypted_line, (uint8_t*)decrypted_line, len)) {
  115. subghz_keystore_process_line(instance, decrypted_line);
  116. } else {
  117. FURI_LOG_E(TAG, "Decryption failed");
  118. result = false;
  119. break;
  120. }
  121. } else {
  122. FURI_LOG_E(
  123. TAG, "Invalid encrypted data: %s", encrypted_line);
  124. }
  125. } else {
  126. subghz_keystore_process_line(instance, encrypted_line);
  127. }
  128. // reset line buffer
  129. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  130. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  131. encrypted_line_cursor = 0;
  132. } else if(buffer[i] == '\r' || buffer[i] == '\n') {
  133. // do not add line endings to the buffer
  134. } else {
  135. if(encrypted_line_cursor < SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE) {
  136. encrypted_line[encrypted_line_cursor] = buffer[i];
  137. encrypted_line_cursor++;
  138. } else {
  139. FURI_LOG_E(TAG, "Malformed file");
  140. result = false;
  141. break;
  142. }
  143. }
  144. }
  145. } while(ret > 0 && result);
  146. if(iv) furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  147. free(encrypted_line);
  148. free(decrypted_line);
  149. return result;
  150. }
  151. bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
  152. furi_assert(instance);
  153. bool result = false;
  154. uint8_t iv[16];
  155. uint32_t version;
  156. SubGhzKeystoreEncryption encryption;
  157. string_t filetype;
  158. string_init(filetype);
  159. Storage* storage = furi_record_open("storage");
  160. FlipperFile* flipper_file = flipper_file_alloc(storage);
  161. do {
  162. if(!flipper_file_open_existing(flipper_file, file_name)) {
  163. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  164. break;
  165. }
  166. if(!flipper_file_read_header(flipper_file, filetype, &version)) {
  167. FURI_LOG_E(TAG, "Missing or incorrect header");
  168. break;
  169. }
  170. if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  171. FURI_LOG_E(TAG, "Missing encryption type");
  172. break;
  173. }
  174. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_TYPE) != 0 ||
  175. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  176. FURI_LOG_E(TAG, "Type or version mismatch");
  177. break;
  178. }
  179. File* file = flipper_file_get_file(flipper_file);
  180. if(encryption == SubGhzKeystoreEncryptionNone) {
  181. result = subghz_keystore_read_file(instance, file, NULL);
  182. } else if(encryption == SubGhzKeystoreEncryptionAES256) {
  183. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  184. FURI_LOG_E(TAG, "Missing IV");
  185. break;
  186. }
  187. subghz_keystore_mess_with_iv(iv);
  188. result = subghz_keystore_read_file(instance, file, iv);
  189. } else {
  190. FURI_LOG_E(TAG, "Unknown encryption");
  191. break;
  192. }
  193. } while(0);
  194. flipper_file_close(flipper_file);
  195. flipper_file_free(flipper_file);
  196. furi_record_close("storage");
  197. string_clear(filetype);
  198. return result;
  199. }
  200. bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8_t* iv) {
  201. furi_assert(instance);
  202. bool result = false;
  203. Storage* storage = furi_record_open("storage");
  204. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  205. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  206. FlipperFile* flipper_file = flipper_file_alloc(storage);
  207. do {
  208. if(!flipper_file_open_always(flipper_file, file_name)) {
  209. FURI_LOG_E(TAG, "Unable to open file for write: %s", file_name);
  210. break;
  211. }
  212. if(!flipper_file_write_header_cstr(
  213. flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
  214. FURI_LOG_E(TAG, "Unable to add header");
  215. break;
  216. }
  217. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  218. if(!flipper_file_write_uint32(flipper_file, "Encryption", &encryption, 1)) {
  219. FURI_LOG_E(TAG, "Unable to add Encryption");
  220. break;
  221. }
  222. if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) {
  223. FURI_LOG_E(TAG, "Unable to add IV");
  224. break;
  225. }
  226. subghz_keystore_mess_with_iv(iv);
  227. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  228. FURI_LOG_E(TAG, "Unable to load encryption key");
  229. break;
  230. }
  231. File* file = flipper_file_get_file(flipper_file);
  232. size_t encrypted_line_count = 0;
  233. for
  234. M_EACH(key, instance->data, SubGhzKeyArray_t) {
  235. // Wipe buffer before packing
  236. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  237. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  238. // Form unecreypted line
  239. int len = snprintf(
  240. decrypted_line,
  241. SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE,
  242. "%08lX%08lX:%hu:%s",
  243. (uint32_t)(key->key >> 32),
  244. (uint32_t)key->key,
  245. key->type,
  246. string_get_cstr(key->name));
  247. // Verify length and align
  248. furi_assert(len > 0);
  249. if(len % 16 != 0) {
  250. len += (16 - len % 16);
  251. }
  252. furi_assert(len % 16 == 0);
  253. furi_assert(len <= SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  254. // Form encrypted line
  255. if(!furi_hal_crypto_encrypt(
  256. (uint8_t*)decrypted_line, (uint8_t*)encrypted_line, len)) {
  257. FURI_LOG_E(TAG, "Encryption failed");
  258. break;
  259. }
  260. // HEX Encode encrypted line
  261. const char xx[] = "0123456789ABCDEF";
  262. for(size_t i = 0; i < len; i++) {
  263. size_t cursor = len - i - 1;
  264. size_t hex_cursor = len * 2 - i * 2 - 1;
  265. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  266. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  267. }
  268. storage_file_write(file, encrypted_line, strlen(encrypted_line));
  269. storage_file_write(file, "\n", 1);
  270. encrypted_line_count++;
  271. }
  272. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  273. size_t total_keys = SubGhzKeyArray_size(instance->data);
  274. result = encrypted_line_count == total_keys;
  275. if (result) {
  276. FURI_LOG_I(TAG, "Success. Encrypted: %d of %d", encrypted_line_count, total_keys);
  277. } else {
  278. FURI_LOG_E(TAG, "Failure. Encrypted: %d of %d", encrypted_line_count, total_keys);
  279. }
  280. } while(0);
  281. flipper_file_close(flipper_file);
  282. flipper_file_free(flipper_file);
  283. free(encrypted_line);
  284. free(decrypted_line);
  285. furi_record_close("storage");
  286. return result;
  287. }
  288. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  289. furi_assert(instance);
  290. return &instance->data;
  291. }
  292. bool subghz_keystore_raw_encrypted_save(
  293. const char* input_file_name,
  294. const char* output_file_name,
  295. uint8_t* iv) {
  296. bool encrypted = false;
  297. uint32_t version;
  298. string_t filetype;
  299. string_init(filetype);
  300. SubGhzKeystoreEncryption encryption;
  301. Storage* storage = furi_record_open("storage");
  302. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  303. FlipperFile* input_flipper_file = flipper_file_alloc(storage);
  304. do {
  305. if(!flipper_file_open_existing(input_flipper_file, input_file_name)) {
  306. FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
  307. break;
  308. }
  309. if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
  310. FURI_LOG_E(TAG, "Missing or incorrect header");
  311. break;
  312. }
  313. if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  314. FURI_LOG_E(TAG, "Missing encryption type");
  315. break;
  316. }
  317. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  318. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  319. FURI_LOG_E(TAG, "Type or version mismatch");
  320. break;
  321. }
  322. if(encryption != SubGhzKeystoreEncryptionNone) {
  323. FURI_LOG_E(TAG, "Already encryption");
  324. break;
  325. }
  326. File* input_file = flipper_file_get_file(input_flipper_file);
  327. FlipperFile* output_flipper_file = flipper_file_alloc(storage);
  328. if(!flipper_file_open_always(output_flipper_file, output_file_name)) {
  329. FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
  330. break;
  331. }
  332. if(!flipper_file_write_header_cstr(
  333. output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
  334. FURI_LOG_E(TAG, "Unable to add header");
  335. break;
  336. }
  337. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  338. if(!flipper_file_write_uint32(
  339. 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. }