2022-03-23 22:14:34 +00:00
|
|
|
#include "dict_attack.h"
|
|
|
|
|
|
|
|
#include <gui/elements.h>
|
|
|
|
|
|
|
|
typedef enum {
|
2022-07-26 15:30:49 +00:00
|
|
|
DictAttackStateRead,
|
2022-03-23 22:14:34 +00:00
|
|
|
DictAttackStateCardRemoved,
|
|
|
|
} DictAttackState;
|
|
|
|
|
|
|
|
struct DictAttack {
|
|
|
|
View* view;
|
2022-07-26 15:30:49 +00:00
|
|
|
DictAttackCallback callback;
|
2022-03-23 22:14:34 +00:00
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
DictAttackState state;
|
|
|
|
MfClassicType type;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* header;
|
2022-07-26 15:30:49 +00:00
|
|
|
uint8_t sectors_total;
|
|
|
|
uint8_t sectors_read;
|
|
|
|
uint8_t sector_current;
|
|
|
|
uint8_t keys_total;
|
|
|
|
uint8_t keys_found;
|
2022-08-03 17:07:35 +00:00
|
|
|
uint16_t dict_keys_total;
|
|
|
|
uint16_t dict_keys_current;
|
2022-03-23 22:14:34 +00:00
|
|
|
} DictAttackViewModel;
|
|
|
|
|
|
|
|
static void dict_attack_draw_callback(Canvas* canvas, void* model) {
|
|
|
|
DictAttackViewModel* m = model;
|
2022-07-26 15:30:49 +00:00
|
|
|
if(m->state == DictAttackStateCardRemoved) {
|
2022-03-23 22:14:34 +00:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
2022-07-26 15:30:49 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 64, 4, AlignCenter, AlignTop, "Lost the tag!");
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
elements_multiline_text_aligned(
|
|
|
|
canvas, 64, 23, AlignCenter, AlignTop, "Make sure the tag is\npositioned correctly.");
|
|
|
|
} else if(m->state == DictAttackStateRead) {
|
|
|
|
char draw_str[32] = {};
|
2022-03-23 22:14:34 +00:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
2022-10-05 15:15:23 +00:00
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, 64, 2, AlignCenter, AlignTop, furi_string_get_cstr(m->header));
|
2022-03-23 22:14:34 +00:00
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2022-08-03 17:07:35 +00:00
|
|
|
float dict_progress = m->dict_keys_total == 0 ?
|
|
|
|
0 :
|
|
|
|
(float)(m->dict_keys_current) / (float)(m->dict_keys_total);
|
|
|
|
float progress = m->sectors_total == 0 ? 0 :
|
|
|
|
((float)(m->sector_current) + dict_progress) /
|
|
|
|
(float)(m->sectors_total);
|
|
|
|
if(progress > 1.0) {
|
|
|
|
progress = 1.0;
|
|
|
|
}
|
2022-07-26 15:30:49 +00:00
|
|
|
elements_progress_bar(canvas, 5, 15, 120, progress);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
snprintf(draw_str, sizeof(draw_str), "Keys found: %d/%d", m->keys_found, m->keys_total);
|
|
|
|
canvas_draw_str_aligned(canvas, 1, 28, AlignLeft, AlignTop, draw_str);
|
2022-03-23 22:14:34 +00:00
|
|
|
snprintf(
|
2022-07-26 15:30:49 +00:00
|
|
|
draw_str, sizeof(draw_str), "Sectors Read: %d/%d", m->sectors_read, m->sectors_total);
|
|
|
|
canvas_draw_str_aligned(canvas, 1, 40, AlignLeft, AlignTop, draw_str);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
2022-07-26 15:30:49 +00:00
|
|
|
elements_button_center(canvas, "Skip");
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool dict_attack_input_callback(InputEvent* event, void* context) {
|
|
|
|
DictAttack* dict_attack = context;
|
|
|
|
bool consumed = false;
|
2022-07-26 15:30:49 +00:00
|
|
|
if(event->type == InputTypeShort && event->key == InputKeyOk) {
|
2022-03-23 22:14:34 +00:00
|
|
|
if(dict_attack->callback) {
|
|
|
|
dict_attack->callback(dict_attack->context);
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
DictAttack* dict_attack_alloc() {
|
|
|
|
DictAttack* dict_attack = malloc(sizeof(DictAttack));
|
|
|
|
dict_attack->view = view_alloc();
|
|
|
|
view_allocate_model(dict_attack->view, ViewModelTypeLocking, sizeof(DictAttackViewModel));
|
|
|
|
view_set_draw_callback(dict_attack->view, dict_attack_draw_callback);
|
|
|
|
view_set_input_callback(dict_attack->view, dict_attack_input_callback);
|
|
|
|
view_set_context(dict_attack->view, dict_attack);
|
2022-07-26 15:30:49 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{ model->header = furi_string_alloc(); },
|
|
|
|
false);
|
2022-03-23 22:14:34 +00:00
|
|
|
return dict_attack;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_free(DictAttack* dict_attack) {
|
|
|
|
furi_assert(dict_attack);
|
2022-07-26 15:30:49 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{ furi_string_free(model->header); },
|
|
|
|
false);
|
2022-03-23 22:14:34 +00:00
|
|
|
view_free(dict_attack->view);
|
|
|
|
free(dict_attack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_reset(DictAttack* dict_attack) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-07-26 15:30:49 +00:00
|
|
|
model->state = DictAttackStateRead;
|
|
|
|
model->type = MfClassicType1k;
|
|
|
|
model->sectors_total = 0;
|
|
|
|
model->sectors_read = 0;
|
|
|
|
model->sector_current = 0;
|
|
|
|
model->keys_total = 0;
|
|
|
|
model->keys_found = 0;
|
2022-08-03 17:07:35 +00:00
|
|
|
model->dict_keys_total = 0;
|
|
|
|
model->dict_keys_current = 0;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(model->header);
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
View* dict_attack_get_view(DictAttack* dict_attack) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
return dict_attack->view;
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_set_callback(DictAttack* dict_attack, DictAttackCallback callback, void* context) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
|
|
|
furi_assert(callback);
|
|
|
|
dict_attack->callback = callback;
|
|
|
|
dict_attack->context = context;
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_set_header(DictAttack* dict_attack, const char* header) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
2022-07-26 15:30:49 +00:00
|
|
|
furi_assert(header);
|
|
|
|
|
2022-03-23 22:14:34 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{ furi_string_set(model->header, header); },
|
|
|
|
true);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_set_card_detected(DictAttack* dict_attack, MfClassicType type) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-07-26 15:30:49 +00:00
|
|
|
model->state = DictAttackStateRead;
|
|
|
|
model->sectors_total = mf_classic_get_total_sectors_num(type);
|
|
|
|
model->keys_total = model->sectors_total * 2;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_set_card_removed(DictAttack* dict_attack) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{ model->state = DictAttackStateCardRemoved; },
|
|
|
|
true);
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_set_sector_read(DictAttack* dict_attack, uint8_t sec_read) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view, DictAttackViewModel * model, { model->sectors_read = sec_read; }, true);
|
2022-07-26 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_set_keys_found(DictAttack* dict_attack, uint8_t keys_found) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view, DictAttackViewModel * model, { model->keys_found = keys_found; }, true);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_set_current_sector(DictAttack* dict_attack, uint8_t curr_sec) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-07-26 15:30:49 +00:00
|
|
|
model->sector_current = curr_sec;
|
2022-08-03 17:07:35 +00:00
|
|
|
model->dict_keys_current = 0;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_inc_current_sector(DictAttack* dict_attack) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-07-26 15:30:49 +00:00
|
|
|
if(model->sector_current < model->sectors_total) {
|
|
|
|
model->sector_current++;
|
2022-08-03 17:07:35 +00:00
|
|
|
model->dict_keys_current = 0;
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
void dict_attack_inc_keys_found(DictAttack* dict_attack) {
|
2022-03-23 22:14:34 +00:00
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-07-26 15:30:49 +00:00
|
|
|
if(model->keys_found < model->keys_total) {
|
|
|
|
model->keys_found++;
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-03-23 22:14:34 +00:00
|
|
|
}
|
2022-08-03 17:07:35 +00:00
|
|
|
|
|
|
|
void dict_attack_set_total_dict_keys(DictAttack* dict_attack, uint16_t dict_keys_total) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{ model->dict_keys_total = dict_keys_total; },
|
|
|
|
true);
|
2022-08-03 17:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dict_attack_inc_current_dict_key(DictAttack* dict_attack, uint16_t keys_tried) {
|
|
|
|
furi_assert(dict_attack);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
dict_attack->view,
|
|
|
|
DictAttackViewModel * model,
|
|
|
|
{
|
2022-08-03 17:07:35 +00:00
|
|
|
if(model->dict_keys_current + keys_tried < model->dict_keys_total) {
|
|
|
|
model->dict_keys_current += keys_tried;
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-08-03 17:07:35 +00:00
|
|
|
}
|