flipbip_file.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_DAT_FILE_NAME ".flipbip.dat"
  8. #define FLIPBIP_DAT_FILE_NAME ".flipbip.dat.txt"
  9. #define FLIPBIP_DAT_FILE_NAME_BAK ".flipbip.dat.bak"
  10. // #define FLIPBIP_KEY_FILE_NAME ".flipbip.key"
  11. #define FLIPBIP_KEY_FILE_NAME ".flipbip.key.txt"
  12. #define FLIPBIP_KEY_FILE_NAME_BAK ".flipbip.key.bak"
  13. #define FLIPBIP_DAT_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME
  14. #define FLIPBIP_DAT_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_DAT_FILE_NAME_BAK
  15. #define FLIPBIP_KEY_PATH FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME
  16. #define FLIPBIP_KEY_PATH_BAK FLIPBIP_APP_BASE_FOLDER "/" FLIPBIP_KEY_FILE_NAME_BAK
  17. const size_t FILE_HLEN = 4;
  18. const size_t FILE_KLEN = 256;
  19. const size_t FILE_SLEN = 512;
  20. const char* FILE_HSTR = "fb01";
  21. const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a"
  22. "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
  23. bool flipbip_load_settings(char* settings, bool key_file) {
  24. bool ret = false;
  25. const char* path;
  26. if(key_file) {
  27. path = FLIPBIP_KEY_PATH;
  28. } else {
  29. path = FLIPBIP_DAT_PATH;
  30. }
  31. Storage *fs_api = furi_record_open(RECORD_STORAGE);
  32. File* settings_file = storage_file_alloc(fs_api);
  33. if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  34. char chr;
  35. int i = 0;
  36. while((storage_file_read(settings_file, &chr, 1) == 1) &&
  37. !storage_file_eof(settings_file) && !isspace(chr)) {
  38. settings[i] = chr;
  39. i++;
  40. }
  41. ret = true;
  42. } else {
  43. memzero(settings, strlen(settings));
  44. settings[0] = '\0';
  45. ret = false;
  46. }
  47. storage_file_close(settings_file);
  48. storage_file_free(settings_file);
  49. furi_record_close(RECORD_STORAGE);
  50. if(!strlen(settings) == 0) {
  51. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  52. FileInfo layout_file_info;
  53. FS_Error file_check_err = storage_common_stat(
  54. fs_api, path, &layout_file_info);
  55. furi_record_close(RECORD_STORAGE);
  56. if(file_check_err != FSE_OK) {
  57. memzero(settings, strlen(settings));
  58. settings[0] = '\0';
  59. ret = false;
  60. }
  61. // if(layout_file_info.size != 256) {
  62. // memzero(settings, strlen(settings));
  63. // settings[0] = '\0';
  64. // }
  65. }
  66. return ret;
  67. }
  68. bool flipbip_has_settings(bool key_file) {
  69. bool ret = false;
  70. const char* path;
  71. if(key_file) {
  72. path = FLIPBIP_KEY_PATH;
  73. } else {
  74. path = FLIPBIP_DAT_PATH;
  75. }
  76. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  77. if(storage_file_exists(fs_api, path)) {
  78. ret = true;
  79. }
  80. furi_record_close(RECORD_STORAGE);
  81. return ret;
  82. }
  83. bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
  84. bool ret = false;
  85. const char* path;
  86. const char* path_bak;
  87. if(key_file) {
  88. path = FLIPBIP_KEY_PATH;
  89. path_bak = FLIPBIP_KEY_PATH_BAK;
  90. } else {
  91. path = FLIPBIP_DAT_PATH;
  92. path_bak = FLIPBIP_DAT_PATH_BAK;
  93. }
  94. int open_mode = FSOM_OPEN_ALWAYS;
  95. if(append) {
  96. open_mode = FSOM_OPEN_APPEND;
  97. }
  98. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  99. // // if the key file exists, we don't want to overwrite it
  100. // if (key_file && storage_file_exists(fs_api, path)) {
  101. // furi_record_close(RECORD_STORAGE);
  102. // ret = true;
  103. // return ret;
  104. // }
  105. // try to create the folder
  106. storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
  107. File* settings_file = storage_file_alloc(fs_api);
  108. if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
  109. storage_file_write(
  110. settings_file,
  111. settings,
  112. strlen(settings));
  113. storage_file_write(settings_file, "\n", 1);
  114. ret = true;
  115. }
  116. storage_file_close(settings_file);
  117. storage_file_free(settings_file);
  118. File* settings_file_bak = storage_file_alloc(fs_api);
  119. if(storage_file_open(settings_file_bak, path_bak, FSAM_WRITE, open_mode)) {
  120. storage_file_write(
  121. settings_file_bak,
  122. settings,
  123. strlen(settings));
  124. storage_file_write(settings_file_bak, "\n", 1);
  125. }
  126. storage_file_close(settings_file_bak);
  127. storage_file_free(settings_file_bak);
  128. furi_record_close(RECORD_STORAGE);
  129. return ret;
  130. }
  131. bool flipbip_load_settings_secure(char* settings) {
  132. const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
  133. // allocate memory for key/data
  134. char *data = malloc(dlen);
  135. memzero(data, dlen);
  136. // load k2 from file
  137. if (!flipbip_load_settings(data, true)) return false;
  138. // check header
  139. if (data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || data[3] != FILE_HSTR[3]) {
  140. memzero(data, dlen);
  141. free(data);
  142. return false;
  143. }
  144. // seek --> header
  145. data += FILE_HLEN;
  146. // prepare k1
  147. uint8_t k1[64];
  148. flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);
  149. // load k2 from file buffer (secured by k1)
  150. flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);
  151. uint8_t k2[128];
  152. flipbip_xtob(data, k2, FILE_KLEN / 2);
  153. // zero k2 buffer
  154. memzero(data, FILE_KLEN);
  155. // seek <-- header
  156. data -= FILE_HLEN;
  157. // load data from file
  158. if (!flipbip_load_settings(data, false)) return false;
  159. // check header
  160. if (data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || data[3] != FILE_HSTR[3]) {
  161. memzero(data, dlen);
  162. free(data);
  163. return false;
  164. }
  165. // seek --> header
  166. data += FILE_HLEN;
  167. // load settings from file buffer (secured by k2)
  168. flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);
  169. flipbip_xtob(data, (unsigned char*)data, FILE_SLEN / 2);
  170. // copy to output
  171. strcpy(settings, data);
  172. // seek <-- header
  173. data -= FILE_HLEN;
  174. // clear memory
  175. memzero(data, dlen);
  176. free(data);
  177. return true;
  178. }
  179. bool flipbip_save_settings_secure(const char* settings) {
  180. const size_t dlen = FILE_HLEN + FILE_SLEN + 1;
  181. // cap settings to 256 bytes
  182. size_t len = strlen(settings);
  183. if (len > (FILE_SLEN / 2)) len = FILE_SLEN / 2;
  184. // allocate memory for key/data
  185. char *data = malloc(dlen);
  186. memzero(data, dlen);
  187. // write header
  188. strncpy(data, FILE_HSTR, FILE_HLEN);
  189. // seek --> header
  190. data += FILE_HLEN;
  191. // prepare k1
  192. uint8_t k1[64];
  193. flipbip_xtob(FILE_K1, k1, strlen(FILE_K1) / 2);
  194. // generate k2
  195. uint8_t k2[128];
  196. random_buffer(k2, FILE_KLEN / 2);
  197. // write k2 to file buffer (secured by k1)
  198. for (size_t i = 0; i < (FILE_KLEN / 2); i++) {
  199. flipbip_btox(k2[i], data + (i * 2));
  200. }
  201. flipbip_cipher(k1, strlen(FILE_K1) / 2, data, data, FILE_KLEN);
  202. // seek <-- header
  203. data -= FILE_HLEN;
  204. // save k2 to file
  205. flipbip_save_settings(data, true, false);
  206. // seek --> header
  207. data += FILE_HLEN;
  208. // zero k2 memory
  209. memzero(data, FILE_KLEN);
  210. // write settings to file buffer (secured by k2)
  211. for (size_t i = 0; i < len; i++) {
  212. flipbip_btox((uint8_t)settings[i], data + (i * 2));
  213. }
  214. flipbip_cipher(k2, FILE_KLEN / 2, data, data, FILE_SLEN);
  215. // seek <-- header
  216. data -= FILE_HLEN;
  217. // save data to file
  218. flipbip_save_settings(data, false, false);
  219. // clear memory
  220. memzero(data, dlen);
  221. free(data);
  222. return true;
  223. }