esubghz_chat_key_read_popup.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "../esubghz_chat_i.h"
  2. #include "../helpers/nfc_helpers.h"
  3. #include <machine/endian.h>
  4. typedef enum {
  5. KeyReadPopupState_Idle,
  6. KeyReadPopupState_Detecting,
  7. KeyReadPopupState_Reading,
  8. KeyReadPopupState_Fail,
  9. KeyReadPopupState_Success,
  10. } KeyReadPopupState;
  11. static bool read_worker_cb(NfcWorkerEvent event, void* context) {
  12. furi_assert(context);
  13. ESubGhzChatState* state = context;
  14. view_dispatcher_send_custom_event(state->view_dispatcher, event);
  15. return true;
  16. }
  17. static void key_read_popup_timeout_cb(void* context) {
  18. furi_assert(context);
  19. ESubGhzChatState* state = context;
  20. uint32_t cur_state =
  21. scene_manager_get_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup);
  22. /* done displaying our failure */
  23. if(cur_state == KeyReadPopupState_Fail) {
  24. view_dispatcher_send_custom_event(
  25. state->view_dispatcher, ESubGhzChatEvent_KeyReadPopupFailed);
  26. /* done displaying our success */
  27. } else if(cur_state == KeyReadPopupState_Success) {
  28. view_dispatcher_send_custom_event(
  29. state->view_dispatcher, ESubGhzChatEvent_KeyReadPopupSucceeded);
  30. }
  31. }
  32. struct ReplayDictNfcReaderContext {
  33. uint8_t* cur;
  34. uint8_t* max;
  35. };
  36. static bool replay_dict_nfc_reader(uint64_t* run_id, uint32_t* counter, void* context) {
  37. struct ReplayDictNfcReaderContext* ctx = (struct ReplayDictNfcReaderContext*)context;
  38. if(ctx->cur + sizeof(struct ReplayDictNfcEntry) > ctx->max) {
  39. return false;
  40. }
  41. struct ReplayDictNfcEntry* entry = (struct ReplayDictNfcEntry*)ctx->cur;
  42. *run_id = entry->run_id;
  43. *counter = __ntohl(entry->counter);
  44. ctx->cur += sizeof(struct ReplayDictNfcEntry);
  45. return true;
  46. }
  47. static bool key_read_popup_handle_key_read(ESubGhzChatState* state) {
  48. NfcDeviceData* dev_data = state->nfc_dev_data;
  49. /* check for config pages */
  50. if(dev_data->mf_ul_data.data_read < NFC_CONFIG_PAGES * 4) {
  51. return false;
  52. }
  53. size_t data_read = dev_data->mf_ul_data.data_read - (NFC_CONFIG_PAGES * 4);
  54. /* check if key was transmitted */
  55. if(data_read < KEY_BITS / 8) {
  56. return false;
  57. }
  58. /* initiate the crypto context */
  59. bool ret = crypto_ctx_set_key(
  60. state->crypto_ctx, dev_data->mf_ul_data.data, state->name_prefix, furi_get_tick());
  61. /* cleanup */
  62. crypto_explicit_bzero(dev_data->mf_ul_data.data, KEY_BITS / 8);
  63. if(!ret) {
  64. crypto_ctx_clear(state->crypto_ctx);
  65. return false;
  66. }
  67. /* read the frequency */
  68. if(data_read >= (KEY_BITS / 8) + sizeof(struct FreqNfcEntry)) {
  69. struct FreqNfcEntry* freq_entry =
  70. (struct FreqNfcEntry*)(dev_data->mf_ul_data.data + (KEY_BITS / 8));
  71. state->frequency = __ntohl(freq_entry->frequency);
  72. }
  73. /* read the replay dict */
  74. struct ReplayDictNfcReaderContext rd_ctx = {
  75. .cur = dev_data->mf_ul_data.data + (KEY_BITS / 8) + sizeof(struct FreqNfcEntry),
  76. .max =
  77. dev_data->mf_ul_data.data + (data_read < NFC_MAX_BYTES ? data_read : NFC_MAX_BYTES)};
  78. crypto_ctx_read_replay_dict(state->crypto_ctx, replay_dict_nfc_reader, &rd_ctx);
  79. /* set encrypted flag */
  80. state->encrypted = true;
  81. return true;
  82. }
  83. static void key_read_popup_set_state(ESubGhzChatState* state, KeyReadPopupState new_state) {
  84. uint32_t cur_state =
  85. scene_manager_get_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup);
  86. if(cur_state == new_state) {
  87. return;
  88. }
  89. if(new_state == KeyReadPopupState_Detecting) {
  90. popup_reset(state->nfc_popup);
  91. popup_disable_timeout(state->nfc_popup);
  92. popup_set_text(state->nfc_popup, "Tap Flipper\n to sender", 97, 24, AlignCenter, AlignTop);
  93. popup_set_icon(state->nfc_popup, 0, 8, &I_NFC_manual_60x50);
  94. notification_message(state->notification, &sequence_blink_start_cyan);
  95. } else if(new_state == KeyReadPopupState_Reading) {
  96. popup_reset(state->nfc_popup);
  97. popup_disable_timeout(state->nfc_popup);
  98. popup_set_header(
  99. state->nfc_popup,
  100. "Reading key\nDon't "
  101. "move...",
  102. 85,
  103. 24,
  104. AlignCenter,
  105. AlignTop);
  106. popup_set_icon(state->nfc_popup, 12, 23, &I_Loading_24);
  107. notification_message(state->notification, &sequence_blink_start_yellow);
  108. } else if(new_state == KeyReadPopupState_Fail) {
  109. nfc_worker_stop(state->nfc_worker);
  110. popup_reset(state->nfc_popup);
  111. popup_set_header(state->nfc_popup, "Failure!", 64, 2, AlignCenter, AlignTop);
  112. popup_set_text(state->nfc_popup, "Failed\nto read\nkey.", 78, 16, AlignLeft, AlignTop);
  113. popup_set_icon(state->nfc_popup, 21, 13, &I_dolph_cry_49x54);
  114. popup_set_timeout(state->nfc_popup, KEY_READ_POPUP_MS);
  115. popup_set_context(state->nfc_popup, state);
  116. popup_set_callback(state->nfc_popup, key_read_popup_timeout_cb);
  117. popup_enable_timeout(state->nfc_popup);
  118. notification_message(state->notification, &sequence_blink_stop);
  119. } else if(new_state == KeyReadPopupState_Success) {
  120. nfc_worker_stop(state->nfc_worker);
  121. popup_reset(state->nfc_popup);
  122. popup_set_header(state->nfc_popup, "Key\nread!", 13, 22, AlignLeft, AlignBottom);
  123. popup_set_icon(state->nfc_popup, 36, 5, &I_DolphinDone_80x58);
  124. popup_set_timeout(state->nfc_popup, KEY_READ_POPUP_MS);
  125. popup_set_context(state->nfc_popup, state);
  126. popup_set_callback(state->nfc_popup, key_read_popup_timeout_cb);
  127. popup_enable_timeout(state->nfc_popup);
  128. notification_message(state->notification, &sequence_success);
  129. notification_message(state->notification, &sequence_blink_stop);
  130. }
  131. scene_manager_set_scene_state(state->scene_manager, ESubGhzChatScene_KeyReadPopup, new_state);
  132. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_NfcPopup);
  133. }
  134. /* Prepares the key share read scene. */
  135. void scene_on_enter_key_read_popup(void* context) {
  136. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_read_popup");
  137. furi_assert(context);
  138. ESubGhzChatState* state = context;
  139. key_read_popup_set_state(state, KeyReadPopupState_Detecting);
  140. nfc_worker_start(
  141. state->nfc_worker, NfcWorkerStateRead, state->nfc_dev_data, read_worker_cb, state);
  142. }
  143. /* Handles scene manager events for the key read popup scene. */
  144. bool scene_on_event_key_read_popup(void* context, SceneManagerEvent event) {
  145. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_read_popup");
  146. furi_assert(context);
  147. ESubGhzChatState* state = context;
  148. bool consumed = false;
  149. switch(event.type) {
  150. case SceneManagerEventTypeCustom:
  151. switch(event.event) {
  152. /* card detected */
  153. case NfcWorkerEventCardDetected:
  154. key_read_popup_set_state(state, KeyReadPopupState_Reading);
  155. consumed = true;
  156. break;
  157. /* no card detected */
  158. case NfcWorkerEventNoCardDetected:
  159. key_read_popup_set_state(state, KeyReadPopupState_Detecting);
  160. consumed = true;
  161. break;
  162. /* key probably read */
  163. case NfcWorkerEventReadMfUltralight:
  164. if(key_read_popup_handle_key_read(state)) {
  165. key_read_popup_set_state(state, KeyReadPopupState_Success);
  166. } else {
  167. key_read_popup_set_state(state, KeyReadPopupState_Fail);
  168. }
  169. consumed = true;
  170. break;
  171. /* close the popup and go back */
  172. case ESubGhzChatEvent_KeyReadPopupFailed:
  173. if(!scene_manager_previous_scene(state->scene_manager)) {
  174. view_dispatcher_stop(state->view_dispatcher);
  175. }
  176. consumed = true;
  177. break;
  178. /* success, go to frequency input */
  179. case ESubGhzChatEvent_KeyReadPopupSucceeded:
  180. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  181. consumed = true;
  182. break;
  183. /* something else happend, treat as failure */
  184. default:
  185. key_read_popup_set_state(state, KeyReadPopupState_Fail);
  186. consumed = true;
  187. break;
  188. }
  189. break;
  190. default:
  191. consumed = false;
  192. break;
  193. }
  194. return consumed;
  195. }
  196. /* Cleans up the key read popup scene. */
  197. void scene_on_exit_key_read_popup(void* context) {
  198. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_read_popup");
  199. furi_assert(context);
  200. ESubGhzChatState* state = context;
  201. popup_reset(state->nfc_popup);
  202. scene_manager_set_scene_state(
  203. state->scene_manager, ESubGhzChatScene_KeyReadPopup, KeyReadPopupState_Idle);
  204. notification_message(state->notification, &sequence_blink_stop);
  205. nfc_worker_stop(state->nfc_worker);
  206. crypto_explicit_bzero(state->nfc_dev_data->mf_ul_data.data, KEY_BITS / 8);
  207. /*if(state->nfc_dev_data->parsed_data != NULL) {
  208. furi_string_free(state->nfc_dev_data->parsed_data);
  209. }*/
  210. //memset(state->nfc_dev_data, 0, sizeof(NfcDeviceData));
  211. }