flipbip_file.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "flipbip_file.h"
  2. #include "../helpers/flipbip_string.h"
  3. #include "../crypto/memzero.h"
  4. #include "../crypto/rand.h"
  5. #include <storage/storage.h>
  6. #define FLIPBIP_APP_BASE_FOLDER EXT_PATH("apps_data/flipbip")
  7. // #define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.dat"
  8. #define FLIPBIP_SETTINGS_FILE_NAME ".flipbip.txt"
  9. #define FLIPBIP_SETTINGS_FILE_NAME_BAK ".flipbip.bak"
  10. #define FLIPBIP_SETTINGS_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_SETTINGS_FILE_NAME
  11. #define FLIPBIP_SETTINGS_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_SETTINGS_FILE_NAME_BAK
  12. const size_t FILE_HLEN = 4;
  13. const size_t FILE_KLEN = 256;
  14. const size_t FILE_SLEN = 512;
  15. const char* FILE_HSTR = "fb01";
  16. const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
  17. "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
  18. bool flipbip_load_settings(char* settings) {
  19. Storage *fs_api = furi_record_open(RECORD_STORAGE);
  20. File* settings_file = storage_file_alloc(fs_api);
  21. if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  22. char chr;
  23. int i = 0;
  24. while((storage_file_read(settings_file, &chr, 1) == 1) &&
  25. !storage_file_eof(settings_file) && !isspace(chr)) {
  26. settings[i] = chr;
  27. i++;
  28. }
  29. } else {
  30. memzero(settings, strlen(settings));
  31. }
  32. storage_file_close(settings_file);
  33. storage_file_free(settings_file);
  34. furi_record_close(RECORD_STORAGE);
  35. if(!strlen(settings) == 0) {
  36. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  37. FileInfo layout_file_info;
  38. FS_Error file_check_err = storage_common_stat(
  39. fs_api, FLIPBIP_SETTINGS_PATH, &layout_file_info);
  40. furi_record_close(RECORD_STORAGE);
  41. if(file_check_err != FSE_OK) {
  42. memzero(settings, strlen(settings));
  43. settings[0] = '\0';
  44. return false;
  45. }
  46. // if(layout_file_info.size != 256) {
  47. // memzero(settings, strlen(settings));
  48. // settings[0] = '\0';
  49. // }
  50. }
  51. return true;
  52. }
  53. bool flipbip_save_settings(const char* settings, bool append) {
  54. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  55. storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
  56. int open_mode = FSOM_OPEN_ALWAYS;
  57. if(append) {
  58. open_mode = FSOM_OPEN_APPEND;
  59. }
  60. File* settings_file = storage_file_alloc(fs_api);
  61. if(storage_file_open(settings_file, FLIPBIP_SETTINGS_PATH, FSAM_WRITE, open_mode)) {
  62. storage_file_write(
  63. settings_file,
  64. settings,
  65. strlen(settings));
  66. storage_file_write(settings_file, "\n", 1);
  67. }
  68. storage_file_close(settings_file);
  69. storage_file_free(settings_file);
  70. File* settings_file_bak = storage_file_alloc(fs_api);
  71. if(storage_file_open(settings_file_bak, FLIPBIP_SETTINGS_PATH_BAK, FSAM_WRITE, open_mode)) {
  72. storage_file_write(
  73. settings_file_bak,
  74. settings,
  75. strlen(settings));
  76. storage_file_write(settings_file_bak, "\n", 1);
  77. }
  78. storage_file_close(settings_file_bak);
  79. storage_file_free(settings_file_bak);
  80. furi_record_close(RECORD_STORAGE);
  81. return true;
  82. }
  83. bool flipbip_load_settings_secure(char* settings) {
  84. const size_t dlen = FILE_HLEN + FILE_KLEN + FILE_SLEN + 1;
  85. // allocate memory for data
  86. char *data = malloc(dlen);
  87. memzero(data, dlen);
  88. // load data from file
  89. if (!flipbip_load_settings(data)) return false;
  90. // check header
  91. if (data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || data[3] != FILE_HSTR[3]) {
  92. memzero(data, dlen);
  93. free(data);
  94. return false;
  95. }
  96. data += FILE_HLEN;
  97. // load k2 from file using k1
  98. //uint8_t k1[16] = {0};
  99. //flipbip_xtob(FILE_K1, k1, 64);
  100. uint8_t k2[128];
  101. //flipbip_cipher(k1, 16, data, data, FILE_KLEN);
  102. flipbip_xtob(data, k2, 128);
  103. data += FILE_KLEN;
  104. // load settings from file using k2
  105. flipbip_cipher(k2, 128, data, data, FILE_SLEN);
  106. flipbip_xtob(data, (unsigned char*)settings, 256);
  107. data = data - FILE_KLEN - FILE_HLEN;
  108. // clear memory
  109. memzero(data, dlen);
  110. free(data);
  111. return true;
  112. }
  113. bool flipbip_save_settings_secure(const char* settings) {
  114. const size_t dlen = FILE_HLEN + FILE_KLEN + FILE_SLEN + 1;
  115. // cap settings to 256 bytes
  116. size_t len = strlen(settings);
  117. if (len > 256) len = 256;
  118. // allocate memory for data
  119. char *data = malloc(dlen + 1);
  120. memzero(data, dlen);
  121. // write header
  122. strncpy(data, FILE_HSTR, FILE_HLEN);
  123. data += FILE_HLEN;
  124. // generate key
  125. //uint8_t k1[16] = {0};
  126. //flipbip_xtob(FILE_K1, k1, 64);
  127. uint8_t k2[128];
  128. random_buffer(k2, 128);
  129. // write k2 to file (secured by k1)
  130. for (size_t i = 0; i < 128; i++) {
  131. flipbip_btox(k2[i], data + (i * 2));
  132. }
  133. //flipbip_cipher(k1, 16, data, data, FILE_KLEN);
  134. data += FILE_KLEN;
  135. // write settings to file (secured by k2)
  136. for (size_t i = 0; i < len; i++) {
  137. flipbip_btox(settings[i], data + (i * 2));
  138. }
  139. flipbip_cipher(k2, 128, data, data, FILE_SLEN);
  140. data = data - FILE_KLEN - FILE_HLEN;
  141. // save data
  142. flipbip_save_settings(data, false);
  143. // clear memory
  144. memzero(data, dlen);
  145. free(data);
  146. return true;
  147. }