subghz_keystore.c 21 KB

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