mifare_nested_scene_collecting.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "../mifare_nested_i.h"
  2. void mifare_nested_scene_collecting_widget_callback(
  3. GuiButtonType result,
  4. InputType type,
  5. void* context) {
  6. MifareNested* mifare_nested = context;
  7. if(type == InputTypeShort) {
  8. view_dispatcher_send_custom_event(mifare_nested->view_dispatcher, result);
  9. }
  10. }
  11. bool mifare_nested_collecting_worker_callback(MifareNestedWorkerEvent event, void* context) {
  12. MifareNested* mifare_nested = context;
  13. NestedState* plugin_state = mifare_nested->nested_state;
  14. if(event == MifareNestedWorkerEventNewNonce) {
  15. mifare_nested_blink_nonce_collection_start(mifare_nested);
  16. uint8_t collected = 0;
  17. NonceList_t* nonces = mifare_nested->nonces;
  18. for(uint8_t tries = 0; tries < nonces->tries; tries++) {
  19. for(uint8_t sector = 0; sector < nonces->sector_count; sector++) {
  20. for(uint8_t keyType = 0; keyType < 2; keyType++) {
  21. Nonces* info = nonces->nonces[sector][keyType][tries];
  22. if(info->collected) {
  23. collected++;
  24. }
  25. }
  26. }
  27. }
  28. with_view_model(
  29. plugin_state->view,
  30. NestedAttackViewModel * model,
  31. {
  32. model->calibrating = false;
  33. model->lost_tag = false;
  34. model->nonces_collected = collected;
  35. model->keys_count = nonces->sector_count * nonces->tries * 2;
  36. },
  37. true);
  38. } else if(event == MifareNestedWorkerEventNoTagDetected) {
  39. mifare_nested_blink_start(mifare_nested);
  40. with_view_model(
  41. plugin_state->view, NestedAttackViewModel * model, { model->lost_tag = true; }, true);
  42. } else if(event == MifareNestedWorkerEventCalibrating) {
  43. mifare_nested_blink_calibration_start(mifare_nested);
  44. with_view_model(
  45. plugin_state->view,
  46. NestedAttackViewModel * model,
  47. {
  48. model->calibrating = true;
  49. model->lost_tag = false;
  50. model->need_prediction = false;
  51. model->hardnested = false;
  52. },
  53. true);
  54. } else if(event == MifareNestedWorkerEventNeedPrediction) {
  55. with_view_model(
  56. plugin_state->view,
  57. NestedAttackViewModel * model,
  58. { model->need_prediction = true; },
  59. true);
  60. } else if(event == MifareNestedWorkerEventHardnestedStatesFound) {
  61. NonceList_t* nonces = mifare_nested->nonces;
  62. with_view_model(
  63. plugin_state->view,
  64. NestedAttackViewModel * model,
  65. {
  66. model->calibrating = false;
  67. model->lost_tag = false;
  68. model->hardnested = true;
  69. model->hardnested_states = nonces->hardnested_states;
  70. },
  71. true);
  72. }
  73. view_dispatcher_send_custom_event(mifare_nested->view_dispatcher, event);
  74. return true;
  75. }
  76. void mifare_nested_scene_collecting_on_enter(void* context) {
  77. MifareNested* mifare_nested = context;
  78. NestedState* nested = mifare_nested->nested_state;
  79. mifare_nested_worker_start(
  80. mifare_nested->worker,
  81. mifare_nested->collecting_type,
  82. &mifare_nested->nfc_dev->dev_data,
  83. mifare_nested_collecting_worker_callback,
  84. mifare_nested);
  85. mifare_nested_blink_start(mifare_nested);
  86. with_view_model(
  87. nested->view,
  88. NestedAttackViewModel * model,
  89. {
  90. model->lost_tag = false;
  91. model->nonces_collected = 0;
  92. },
  93. false);
  94. view_dispatcher_switch_to_view(mifare_nested->view_dispatcher, MifareNestedViewCollecting);
  95. }
  96. bool mifare_nested_scene_collecting_on_event(void* context, SceneManagerEvent event) {
  97. MifareNested* mifare_nested = context;
  98. bool consumed = false;
  99. if(event.type == SceneManagerEventTypeCustom) {
  100. if(event.event == GuiButtonTypeCenter) {
  101. scene_manager_search_and_switch_to_previous_scene(mifare_nested->scene_manager, 0);
  102. consumed = true;
  103. } else if(event.event == MifareNestedWorkerEventNoncesCollected) {
  104. scene_manager_next_scene(
  105. mifare_nested->scene_manager, MifareNestedSceneNoncesCollected);
  106. consumed = true;
  107. } else if(event.event == MifareNestedWorkerEventAttackFailed) {
  108. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneFailed);
  109. consumed = true;
  110. } else if(event.event == MifareNestedWorkerEventNeedKey) {
  111. scene_manager_next_scene(mifare_nested->scene_manager, MifareNestedSceneNoKeys);
  112. consumed = true;
  113. } else if(event.event == MifareNestedWorkerEventStaticEncryptedNonce) {
  114. scene_manager_next_scene(
  115. mifare_nested->scene_manager, MifareNestedSceneStaticEncryptedNonce);
  116. consumed = true;
  117. } else if(
  118. event.event == MifareNestedWorkerEventNewNonce ||
  119. event.event == MifareNestedWorkerEventNoTagDetected ||
  120. event.event == MifareNestedWorkerEventCalibrating ||
  121. event.event == MifareNestedWorkerEventNeedPrediction ||
  122. event.event == MifareNestedWorkerEventHardnestedStatesFound) {
  123. consumed = true;
  124. }
  125. }
  126. return consumed;
  127. }
  128. void mifare_nested_scene_collecting_on_exit(void* context) {
  129. MifareNested* mifare_nested = context;
  130. mifare_nested_worker_stop(mifare_nested->worker);
  131. // Clear view
  132. mifare_nested_blink_stop(mifare_nested);
  133. popup_reset(mifare_nested->popup);
  134. widget_reset(mifare_nested->widget);
  135. }