esubghz_chat_key_read_popup.c 7.9 KB

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