subghz_keystore.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. furi_string_free(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. manufacture_code->name = furi_string_alloc_set(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. FuriString* filetype;
  168. filetype = furi_string_alloc();
  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(furi_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. furi_string_free(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. furi_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. FuriString* filetype;
  308. filetype = furi_string_alloc();
  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(furi_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,
  344. furi_string_get_cstr(filetype),
  345. SUBGHZ_KEYSTORE_FILE_VERSION)) {
  346. FURI_LOG_E(TAG, "Unable to add header");
  347. break;
  348. }
  349. uint32_t encryption = SubGhzKeystoreEncryptionAES256;
  350. if(!flipper_format_write_uint32(output_flipper_format, "Encryption", &encryption, 1)) {
  351. FURI_LOG_E(TAG, "Unable to add Encryption");
  352. break;
  353. }
  354. if(!flipper_format_write_hex(output_flipper_format, "IV", iv, 16)) {
  355. FURI_LOG_E(TAG, "Unable to add IV");
  356. break;
  357. }
  358. if(!flipper_format_write_string_cstr(output_flipper_format, "Encrypt_data", "RAW")) {
  359. FURI_LOG_E(TAG, "Unable to add Encrypt_data");
  360. break;
  361. }
  362. subghz_keystore_mess_with_iv(iv);
  363. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  364. FURI_LOG_E(TAG, "Unable to load encryption key");
  365. break;
  366. }
  367. Stream* output_stream = flipper_format_get_raw_stream(output_flipper_format);
  368. uint8_t buffer[FILE_BUFFER_SIZE];
  369. bool result = true;
  370. size_t ret = 0;
  371. furi_assert(FILE_BUFFER_SIZE % 16 == 0);
  372. //skip the end of the previous line "\n"
  373. stream_read(input_stream, buffer, 1);
  374. do {
  375. memset(buffer, 0, FILE_BUFFER_SIZE);
  376. ret = stream_read(input_stream, buffer, FILE_BUFFER_SIZE);
  377. if(ret == 0) {
  378. break;
  379. }
  380. for(uint16_t i = 0; i < FILE_BUFFER_SIZE - 1; i += 2) {
  381. uint8_t hi_nibble = 0;
  382. uint8_t lo_nibble = 0;
  383. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  384. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  385. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  386. }
  387. memset(encrypted_line, 0, SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
  388. // Form encrypted line
  389. if(!furi_hal_crypto_encrypt(
  390. (uint8_t*)buffer, (uint8_t*)encrypted_line, FILE_BUFFER_SIZE / 2)) {
  391. FURI_LOG_E(TAG, "Encryption failed");
  392. result = false;
  393. break;
  394. }
  395. // HEX Encode encrypted line
  396. const char xx[] = "0123456789ABCDEF";
  397. for(size_t i = 0; i < FILE_BUFFER_SIZE / 2; i++) {
  398. size_t cursor = FILE_BUFFER_SIZE / 2 - i - 1;
  399. size_t hex_cursor = FILE_BUFFER_SIZE - i * 2 - 1;
  400. encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
  401. encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
  402. }
  403. stream_write_cstring(output_stream, encrypted_line);
  404. } while(result);
  405. flipper_format_free(output_flipper_format);
  406. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  407. if(!result) break;
  408. encrypted = true;
  409. } while(0);
  410. flipper_format_free(input_flipper_format);
  411. free(encrypted_line);
  412. furi_record_close(RECORD_STORAGE);
  413. return encrypted;
  414. }
  415. bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t* data, size_t len) {
  416. bool result = false;
  417. uint8_t iv[16];
  418. uint32_t version;
  419. SubGhzKeystoreEncryption encryption;
  420. FuriString* str_temp;
  421. str_temp = furi_string_alloc();
  422. Storage* storage = furi_record_open(RECORD_STORAGE);
  423. char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  424. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  425. do {
  426. if(!flipper_format_file_open_existing(flipper_format, file_name)) {
  427. FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
  428. break;
  429. }
  430. if(!flipper_format_read_header(flipper_format, str_temp, &version)) {
  431. FURI_LOG_E(TAG, "Missing or incorrect header");
  432. break;
  433. }
  434. if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
  435. FURI_LOG_E(TAG, "Missing encryption type");
  436. break;
  437. }
  438. if(strcmp(furi_string_get_cstr(str_temp), SUBGHZ_KEYSTORE_FILE_RAW_TYPE) != 0 ||
  439. version != SUBGHZ_KEYSTORE_FILE_VERSION) {
  440. FURI_LOG_E(TAG, "Type or version mismatch");
  441. break;
  442. }
  443. Stream* stream = flipper_format_get_raw_stream(flipper_format);
  444. if(encryption != SubGhzKeystoreEncryptionAES256) {
  445. FURI_LOG_E(TAG, "Unknown encryption");
  446. break;
  447. }
  448. if(offset < 16) {
  449. if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
  450. FURI_LOG_E(TAG, "Missing IV");
  451. break;
  452. }
  453. subghz_keystore_mess_with_iv(iv);
  454. }
  455. if(!flipper_format_read_string(flipper_format, "Encrypt_data", str_temp)) {
  456. FURI_LOG_E(TAG, "Missing Encrypt_data");
  457. break;
  458. }
  459. size_t bufer_size;
  460. if(len <= (16 - offset % 16)) {
  461. bufer_size = 32;
  462. } else {
  463. bufer_size = (((len) / 16) + 2) * 32;
  464. }
  465. furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
  466. uint8_t buffer[bufer_size];
  467. size_t ret = 0;
  468. bool decrypted = true;
  469. //skip the end of the previous line "\n"
  470. stream_read(stream, buffer, 1);
  471. size_t size = stream_size(stream);
  472. size -= stream_tell(stream);
  473. if(size < (offset * 2 + len * 2)) {
  474. FURI_LOG_E(TAG, "Seek position exceeds file size");
  475. break;
  476. }
  477. if(offset >= 16) {
  478. stream_seek(stream, ((offset / 16) - 1) * 32, StreamOffsetFromCurrent);
  479. ret = stream_read(stream, buffer, 32);
  480. furi_assert(ret == 32);
  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. iv[i / 2] = (hi_nibble << 4) | lo_nibble;
  487. }
  488. }
  489. if(!furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv)) {
  490. FURI_LOG_E(TAG, "Unable to load encryption key");
  491. break;
  492. }
  493. do {
  494. memset(buffer, 0, bufer_size);
  495. ret = stream_read(stream, buffer, bufer_size);
  496. furi_assert(ret == bufer_size);
  497. for(uint16_t i = 0; i < ret - 1; i += 2) {
  498. uint8_t hi_nibble = 0;
  499. uint8_t lo_nibble = 0;
  500. hex_char_to_hex_nibble(buffer[i], &hi_nibble);
  501. hex_char_to_hex_nibble(buffer[i + 1], &lo_nibble);
  502. buffer[i / 2] = (hi_nibble << 4) | lo_nibble;
  503. }
  504. memset(decrypted_line, 0, SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
  505. if(!furi_hal_crypto_decrypt(
  506. (uint8_t*)buffer, (uint8_t*)decrypted_line, bufer_size / 2)) {
  507. decrypted = false;
  508. FURI_LOG_E(TAG, "Decryption failed");
  509. break;
  510. }
  511. memcpy(data, (uint8_t*)decrypted_line + (offset - (offset / 16) * 16), len);
  512. } while(0);
  513. furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
  514. if(decrypted) result = true;
  515. } while(0);
  516. flipper_format_free(flipper_format);
  517. furi_record_close(RECORD_STORAGE);
  518. free(decrypted_line);
  519. furi_string_free(str_temp);
  520. return result;
  521. }