subghz_keystore.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. }
  269. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  270. size_t total_keys = SubGhzKeyArray_size(instance->data);
  271. result = encrypted_line_count == total_keys;
  272. if (result) {
  273. FURI_LOG_I(TAG, "Success. Encrypted: %d of %d", encrypted_line_count, total_keys);
  274. } else {
  275. FURI_LOG_E(TAG, "Failure. Encrypted: %d of %d", encrypted_line_count, total_keys);
  276. }
  277. } while(0);
  278. flipper_file_close(flipper_file);
  279. flipper_file_free(flipper_file);
  280. free(encrypted_line);
  281. free(decrypted_line);
  282. furi_record_close("storage");
  283. return result;
  284. }
  285. SubGhzKeyArray_t* subghz_keystore_get_data(SubGhzKeystore* instance) {
  286. furi_assert(instance);
  287. return &instance->data;
  288. }
  289. bool subghz_keystore_raw_encrypted_save(
  290. const char* input_file_name,
  291. const char* output_file_name,
  292. uint8_t* iv) {
  293. bool encrypted = false;
  294. uint32_t version;
  295. string_t filetype;
  296. string_init(filetype);
  297. SubGhzKeystoreEncryption encryption;
  298. Storage* storage = furi_record_open("storage");
  299. char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  300. FlipperFile* input_flipper_file = flipper_file_alloc(storage);
  301. do {
  302. if(!flipper_file_open_existing(input_flipper_file, input_file_name)) {
  303. FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
  304. break;
  305. }
  306. if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
  307. FURI_LOG_E(TAG, "Missing or incorrect header");
  308. break;
  309. }
  310. if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  311. FURI_LOG_E(TAG, "Missing encryption type");
  312. break;
  313. }
  314. if(strcmp(string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  315. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  316. FURI_LOG_E(TAG, "Type or version mismatch");
  317. break;
  318. }
  319. if(encryption != SubGhzKeystoreEncryptionNone) {
  320. FURI_LOG_E(TAG, "Already encryption");
  321. break;
  322. }
  323. File* input_file = flipper_file_get_file(input_flipper_file);
  324. FlipperFile* output_flipper_file = flipper_file_alloc(storage);
  325. if(!flipper_file_open_always(output_flipper_file, output_file_name)) {
  326. FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
  327. break;
  328. }
  329. if(!flipper_file_write_header_cstr(
  330. output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
  331. FURI_LOG_E(TAG, "Unable to add header");
  332. break;
  333. }
  334. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  335. if(!flipper_file_write_uint32(
  336. output_flipper_file, "Encryption", &encryption, 1)) {
  337. FURI_LOG_E(TAG, "Unable to add Encryption");
  338. break;
  339. }
  340. if(!flipper_file_write_hex(output_flipper_file, "IV", iv, 16)) {
  341. FURI_LOG_E(TAG, "Unable to add IV");
  342. break;
  343. }
  344. if(!flipper_file_write_string_cstr(output_flipper_file, "Encrypt_data", "RAW")) {
  345. FURI_LOG_E(TAG, "Unable to add Encrypt_data");
  346. break;
  347. }
  348. subghz_keystore_mess_with_iv(iv);
  349. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  350. FURI_LOG_E(TAG, "Unable to load encryption key");
  351. break;
  352. }
  353. File* output_file = flipper_file_get_file(output_flipper_file);
  354. char buffer[FILE_BUFFER_SIZE];
  355. bool result = true;
  356. size_t ret = 0;
  357. furi_assert(FILE_BUFFER_SIZE % 16 == 0);
  358. //skip the end of the previous line "\n"
  359. storage_file_read(input_file, buffer, 1);
  360. do {
  361. memset(buffer, 0, FILE_BUFFER_SIZE);
  362. ret = storage_file_read(input_file, buffer, FILE_BUFFER_SIZE);
  363. if(ret == 0) {
  364. break;
  365. }
  366. for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
  367. uint8_t hi_nibble = 0;
  368. uint8_t lo_nibble = 0;
  369. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  370. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  371. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  372. }
  373. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  374. // Form encrypted line
  375. if(!furi_hal_crypto_encrypt(
  376. (uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
  377. FURI_LOG_E(TAG, "Encryption failed");
  378. result = false;
  379. break;
  380. }
  381. // HEX Encode encrypted line
  382. const char xx[] = "0123456789ABCDEF";
  383. for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
  384. size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
  385. size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
  386. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  387. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  388. }
  389. storage_file_write(output_file, encrypted_line, strlen(encrypted_line));
  390. } while(ret > 0 && result);
  391. flipper_file_close(output_flipper_file);
  392. flipper_file_free(output_flipper_file);
  393. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  394. if(!result) break;
  395. encrypted = true;
  396. } while(0);
  397. flipper_file_close(input_flipper_file);
  398. flipper_file_free(input_flipper_file);
  399. free(encrypted_line);
  400. furi_record_close("storage");
  401. return encrypted;
  402. }
  403. bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len) {
  404. bool result = false;
  405. uint8_t iv[16];
  406. uint32_t version;
  407. SubGhzKeystoreEncryption encryption;
  408. string_t str_temp;
  409. string_init(str_temp);
  410. Storage* storage = furi_record_open("storage");
  411. char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  412. FlipperFile* flipper_file = flipper_file_alloc(storage);
  413. do {
  414. if(!flipper_file_open_existing(flipper_file, file_name)) {
  415. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  416. break;
  417. }
  418. if(!flipper_file_read_header(flipper_file, str_temp, &version)) {
  419. FURI_LOG_E(TAG, "Missing or incorrect header");
  420. break;
  421. }
  422. if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
  423. FURI_LOG_E(TAG, "Missing encryption type");
  424. break;
  425. }
  426. if(strcmp(string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  427. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  428. FURI_LOG_E(TAG, "Type or version mismatch");
  429. break;
  430. }
  431. File* file = flipper_file_get_file(flipper_file);
  432. if(encryption != SubGhzKeystoreEncryptionAES256) {
  433. FURI_LOG_E(TAG, "Unknown encryption");
  434. break;
  435. }
  436. if(offset < 16) {
  437. if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
  438. FURI_LOG_E(TAG, "Missing IV");
  439. break;
  440. }
  441. subghz_keystore_mess_with_iv(iv);
  442. }
  443. if(!flipper_file_read_string(flipper_file, "Encrypt_data", str_temp)) {
  444. FURI_LOG_E(TAG, "Missing Encrypt_data");
  445. break;
  446. }
  447. size_t bufer_size;
  448. if(len <= (16 - offset % 16)) {
  449. bufer_size = 32;
  450. } else {
  451. bufer_size = (((len) / 16) + 2) * 32;
  452. }
  453. furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
  454. char buffer[bufer_size];
  455. size_t ret = 0;
  456. bool decrypted = true;
  457. //skip the end of the previous line "\n"
  458. storage_file_read(file, buffer, 1);
  459. size_t size = storage_file_size(file);
  460. size -= storage_file_tell(file);
  461. if(size < (offset * 2 + len * 2)) {
  462. FURI_LOG_E(TAG, "Seek position exceeds file size");
  463. break;
  464. }
  465. if(offset >= 16) {
  466. storage_file_seek(file, ((offset / 16) - 1) * 32, false);
  467. ret = storage_file_read(file, buffer, 32);
  468. furi_assert(ret == 32);
  469. for(uint16_t i = 0; i < ret - 1; i += 2) {
  470. uint8_t hi_nibble = 0;
  471. uint8_t lo_nibble = 0;
  472. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  473. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  474. iv[i / 2] = (hi_nibble << 4) | lo_nibble;
  475. }
  476. }
  477. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  478. FURI_LOG_E(TAG, "Unable to load encryption key");
  479. break;
  480. }
  481. do {
  482. memset(buffer, 0, bufer_size);
  483. ret = storage_file_read(file, buffer, bufer_size);
  484. furi_assert(ret == bufer_size);
  485. for(uint16_t i = 0; i < ret - 1; i += 2) {
  486. uint8_t hi_nibble = 0;
  487. uint8_t lo_nibble = 0;
  488. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  489. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  490. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  491. }
  492. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  493. if(!furi_hal_crypto_decrypt(
  494. (uint8_t*)buffer, (uint8_t*)decrypted_line, bufer_size / 2)) {
  495. decrypted = false;
  496. FURI_LOG_E(TAG, "Decryption failed");
  497. break;
  498. }
  499. memcpy(data, (uint8_t*)decrypted_line + (offset - (offset / 16) * 16), len);
  500. } while(0);
  501. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  502. if(decrypted) result = true;
  503. } while(0);
  504. flipper_file_close(flipper_file);
  505. flipper_file_free(flipper_file);
  506. furi_record_close("storage");
  507. free(decrypted_line);
  508. string_clear(str_temp);
  509. return result;
  510. }