dict_attack.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "dict_attack.h"
  2. #include <gui/elements.h>
  3. typedef enum {
  4. DictAttackStateRead,
  5. DictAttackStateCardRemoved,
  6. } DictAttackState;
  7. struct DictAttack {
  8. View* view;
  9. DictAttackCallback callback;
  10. void* context;
  11. };
  12. typedef struct {
  13. DictAttackState state;
  14. FuriString* header;
  15. uint8_t sectors_total;
  16. uint8_t sectors_read;
  17. uint8_t sector_current;
  18. uint8_t keys_total;
  19. uint8_t keys_found;
  20. uint16_t dict_keys_total;
  21. uint16_t dict_keys_current;
  22. bool is_key_attack;
  23. uint8_t key_attack_current_sector;
  24. } DictAttackViewModel;
  25. static void dict_attack_draw_callback(Canvas* canvas, void* model) {
  26. DictAttackViewModel* m = model;
  27. if(m->state == DictAttackStateCardRemoved) {
  28. canvas_set_font(canvas, FontPrimary);
  29. canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
  30. canvas_set_font(canvas, FontSecondary);
  31. elements_multiline_text_aligned(
  32. canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
  33. } else if(m->state == DictAttackStateRead) {
  34. char draw_str[32] = {};
  35. canvas_set_font(canvas, FontSecondary);
  36. canvas_draw_str_aligned(
  37. canvas, 64, 0, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
  38. if(m->is_key_attack) {
  39. snprintf(
  40. draw_str,
  41. sizeof(draw_str),
  42. "Reuse key check for sector: %d",
  43. m->key_attack_current_sector);
  44. } else {
  45. snprintf(draw_str, sizeof(draw_str), "Unlocking sector: %d", m->sector_current);
  46. }
  47. canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, draw_str);
  48. float dict_progress = m->dict_keys_total == 0 ?
  49. 0 :
  50. (float)(m->dict_keys_current) / (float)(m->dict_keys_total);
  51. float progress = m->sectors_total == 0 ? 0 :
  52. ((float)(m->sector_current) + dict_progress) /
  53. (float)(m->sectors_total);
  54. if(progress > 1.0) {
  55. progress = 1.0;
  56. }
  57. if(m->dict_keys_current == 0) {
  58. // Cause when people see 0 they think it's broken
  59. snprintf(draw_str, sizeof(draw_str), "%d/%d", 1, m->dict_keys_total);
  60. } else {
  61. snprintf(
  62. draw_str, sizeof(draw_str), "%d/%d", m->dict_keys_current, m->dict_keys_total);
  63. }
  64. elements_progress_bar_with_text(canvas, 0, 20, 128, dict_progress, draw_str);
  65. canvas_set_font(canvas, FontSecondary);
  66. snprintf(draw_str, sizeof(draw_str), "Keys found: %d/%d", m->keys_found, m->keys_total);
  67. canvas_draw_str_aligned(canvas, 0, 33, AlignLeft, AlignTop, draw_str);
  68. snprintf(
  69. draw_str, sizeof(draw_str), "Sectors Read: %d/%d", m->sectors_read, m->sectors_total);
  70. canvas_draw_str_aligned(canvas, 0, 43, AlignLeft, AlignTop, draw_str);
  71. }
  72. elements_button_center(canvas, "Skip");
  73. }
  74. static bool dict_attack_input_callback(InputEvent* event, void* context) {
  75. DictAttack* dict_attack = context;
  76. bool consumed = false;
  77. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  78. if(dict_attack->callback) {
  79. dict_attack->callback(dict_attack->context);
  80. }
  81. consumed = true;
  82. }
  83. return consumed;
  84. }
  85. DictAttack* dict_attack_alloc() {
  86. DictAttack* dict_attack = malloc(sizeof(DictAttack));
  87. dict_attack->view = view_alloc();
  88. view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
  89. view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
  90. view_set_input_callback(dict_attack->view, dict_attack_input_callback);
  91. view_set_context(dict_attack->view, dict_attack);
  92. with_view_model(
  93. dict_attack->view,
  94. DictAttackViewModel * model,
  95. { model->header = furi_string_alloc(); },
  96. false);
  97. return dict_attack;
  98. }
  99. void dict_attack_free(DictAttack* dict_attack) {
  100. furi_assert(dict_attack);
  101. with_view_model(
  102. dict_attack->view,
  103. DictAttackViewModel * model,
  104. { furi_string_free(model->header); },
  105. false);
  106. view_free(dict_attack->view);
  107. free(dict_attack);
  108. }
  109. void dict_attack_reset(DictAttack* dict_attack) {
  110. furi_assert(dict_attack);
  111. with_view_model(
  112. dict_attack->view,
  113. DictAttackViewModel * model,
  114. {
  115. model->state = DictAttackStateRead;
  116. model->sectors_total = 1;
  117. model->sectors_read = 0;
  118. model->sector_current = 0;
  119. model->keys_total = 0;
  120. model->keys_found = 0;
  121. model->dict_keys_total = 0;
  122. model->dict_keys_current = 0;
  123. model->is_key_attack = false;
  124. furi_string_reset(model->header);
  125. },
  126. false);
  127. }
  128. View* dict_attack_get_view(DictAttack* dict_attack) {
  129. furi_assert(dict_attack);
  130. return dict_attack->view;
  131. }
  132. void dict_attack_set_callback(DictAttack* dict_attack, DictAttackCallback callback, void* context) {
  133. furi_assert(dict_attack);
  134. furi_assert(callback);
  135. dict_attack->callback = callback;
  136. dict_attack->context = context;
  137. }
  138. void dict_attack_set_header(DictAttack* dict_attack, const char* header) {
  139. furi_assert(dict_attack);
  140. furi_assert(header);
  141. with_view_model(
  142. dict_attack->view,
  143. DictAttackViewModel * model,
  144. { furi_string_set(model->header, header); },
  145. true);
  146. }
  147. void dict_attack_set_card_detected(DictAttack* dict_attack) {
  148. furi_assert(dict_attack);
  149. with_view_model(
  150. dict_attack->view,
  151. DictAttackViewModel * model,
  152. {
  153. model->state = DictAttackStateRead;
  154. model->sectors_total = 1;
  155. model->keys_total = model->sectors_total;
  156. },
  157. true);
  158. }
  159. void dict_attack_set_card_removed(DictAttack* dict_attack) {
  160. furi_assert(dict_attack);
  161. with_view_model(
  162. dict_attack->view,
  163. DictAttackViewModel * model,
  164. { model->state = DictAttackStateCardRemoved; },
  165. true);
  166. }
  167. void dict_attack_set_sector_read(DictAttack* dict_attack, uint8_t sec_read) {
  168. furi_assert(dict_attack);
  169. with_view_model(
  170. dict_attack->view, DictAttackViewModel * model, { model->sectors_read = sec_read; }, true);
  171. }
  172. void dict_attack_set_keys_found(DictAttack* dict_attack, uint8_t keys_found) {
  173. furi_assert(dict_attack);
  174. with_view_model(
  175. dict_attack->view, DictAttackViewModel * model, { model->keys_found = keys_found; }, true);
  176. }
  177. void dict_attack_set_current_sector(DictAttack* dict_attack, uint8_t curr_sec) {
  178. furi_assert(dict_attack);
  179. with_view_model(
  180. dict_attack->view,
  181. DictAttackViewModel * model,
  182. {
  183. model->sector_current = curr_sec;
  184. model->dict_keys_current = 0;
  185. },
  186. true);
  187. }
  188. void dict_attack_inc_current_sector(DictAttack* dict_attack) {
  189. furi_assert(dict_attack);
  190. with_view_model(
  191. dict_attack->view,
  192. DictAttackViewModel * model,
  193. {
  194. if(model->sector_current < model->sectors_total) {
  195. model->sector_current++;
  196. model->dict_keys_current = 0;
  197. }
  198. },
  199. true);
  200. }
  201. void dict_attack_inc_keys_found(DictAttack* dict_attack) {
  202. furi_assert(dict_attack);
  203. with_view_model(
  204. dict_attack->view,
  205. DictAttackViewModel * model,
  206. {
  207. if(model->keys_found < model->keys_total) {
  208. model->keys_found++;
  209. }
  210. },
  211. true);
  212. }
  213. void dict_attack_set_total_dict_keys(DictAttack* dict_attack, uint16_t dict_keys_total) {
  214. furi_assert(dict_attack);
  215. with_view_model(
  216. dict_attack->view,
  217. DictAttackViewModel * model,
  218. { model->dict_keys_total = dict_keys_total; },
  219. true);
  220. }
  221. void dict_attack_set_current_dict_key(DictAttack* dict_attack, uint16_t current_key) {
  222. furi_assert(dict_attack);
  223. with_view_model(
  224. dict_attack->view,
  225. DictAttackViewModel * model,
  226. { model->dict_keys_current = current_key; },
  227. true);
  228. }
  229. void dict_attack_inc_current_dict_key(DictAttack* dict_attack, uint16_t keys_tried) {
  230. furi_assert(dict_attack);
  231. with_view_model(
  232. dict_attack->view,
  233. DictAttackViewModel * model,
  234. {
  235. if(model->dict_keys_current + keys_tried < model->dict_keys_total) {
  236. model->dict_keys_current += keys_tried;
  237. }
  238. },
  239. true);
  240. }
  241. void dict_attack_set_key_attack(DictAttack* dict_attack, bool is_key_attack, uint8_t sector) {
  242. furi_assert(dict_attack);
  243. with_view_model(
  244. dict_attack->view,
  245. DictAttackViewModel * model,
  246. {
  247. model->is_key_attack = is_key_attack;
  248. model->key_attack_current_sector = sector;
  249. },
  250. true);
  251. }
  252. void dict_attack_inc_key_attack_current_sector(DictAttack* dict_attack) {
  253. furi_assert(dict_attack);
  254. with_view_model(
  255. dict_attack->view,
  256. DictAttackViewModel * model,
  257. {
  258. if(model->key_attack_current_sector < model->sectors_total) {
  259. model->key_attack_current_sector++;
  260. }
  261. },
  262. true);
  263. }