mifare_nested_scene_collecting.c 5.9 KB

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