flipbip_file.c 7.7 KB

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