subghz_keystore.c 22 KB

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