esubghz_chat_key_share_popup.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "../esubghz_chat_i.h"
  2. #include "../helpers/nfc_helpers.h"
  3. struct ReplayDictNfcWriterContext {
  4. uint8_t* cur;
  5. uint8_t* max;
  6. };
  7. static bool replay_dict_nfc_writer(uint64_t run_id, uint32_t counter, void* context) {
  8. struct ReplayDictNfcWriterContext* ctx = (struct ReplayDictNfcWriterContext*)context;
  9. struct ReplayDictNfcEntry entry = {.run_id = run_id, .counter = __htonl(counter), .unused = 0};
  10. if(ctx->cur + sizeof(entry) > ctx->max) {
  11. return false;
  12. }
  13. memcpy(ctx->cur, &entry, sizeof(entry));
  14. ctx->cur += sizeof(entry);
  15. return true;
  16. }
  17. static void prepare_nfc_dev_data(ESubGhzChatState* state) {
  18. NfcDeviceData* dev_data = state->nfc_dev_data;
  19. dev_data->protocol = NfcDeviceProtocolMifareUl;
  20. furi_hal_random_fill_buf(dev_data->nfc_data.uid, 7);
  21. dev_data->nfc_data.uid_len = 7;
  22. dev_data->nfc_data.atqa[0] = 0x44;
  23. dev_data->nfc_data.atqa[1] = 0x00;
  24. dev_data->nfc_data.sak = 0x00;
  25. dev_data->mf_ul_data.type = MfUltralightTypeNTAG215;
  26. dev_data->mf_ul_data.version.header = 0x00;
  27. dev_data->mf_ul_data.version.vendor_id = 0x04;
  28. dev_data->mf_ul_data.version.prod_type = 0x04;
  29. dev_data->mf_ul_data.version.prod_subtype = 0x02;
  30. dev_data->mf_ul_data.version.prod_ver_major = 0x01;
  31. dev_data->mf_ul_data.version.prod_ver_minor = 0x00;
  32. dev_data->mf_ul_data.version.storage_size = 0x11;
  33. dev_data->mf_ul_data.version.protocol_type = 0x03;
  34. size_t data_written = 0;
  35. /* write key */
  36. crypto_ctx_get_key(state->crypto_ctx, dev_data->mf_ul_data.data);
  37. data_written += (KEY_BITS / 8);
  38. /* write frequency */
  39. struct FreqNfcEntry* freq_entry =
  40. (struct FreqNfcEntry*)(dev_data->mf_ul_data.data + data_written);
  41. freq_entry->frequency = __htonl(state->frequency);
  42. freq_entry->unused1 = 0;
  43. freq_entry->unused2 = 0;
  44. freq_entry->unused3 = 0;
  45. data_written += sizeof(struct FreqNfcEntry);
  46. /* write the replay dict */
  47. struct ReplayDictNfcWriterContext wr_ctx = {
  48. .cur = dev_data->mf_ul_data.data + data_written,
  49. .max = dev_data->mf_ul_data.data + NFC_MAX_BYTES};
  50. size_t n_entries =
  51. crypto_ctx_dump_replay_dict(state->crypto_ctx, replay_dict_nfc_writer, &wr_ctx);
  52. data_written += n_entries * sizeof(struct ReplayDictNfcEntry);
  53. /* calculate size of data, add 16 for config pages */
  54. dev_data->mf_ul_data.data_size = data_written + (NFC_CONFIG_PAGES * 4);
  55. }
  56. /* Prepares the key share popup scene. */
  57. void scene_on_enter_key_share_popup(void* context) {
  58. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_share_popup");
  59. furi_assert(context);
  60. ESubGhzChatState* state = context;
  61. popup_reset(state->nfc_popup);
  62. popup_disable_timeout(state->nfc_popup);
  63. popup_set_header(state->nfc_popup, "Sharing...", 67, 13, AlignLeft, AlignTop);
  64. popup_set_icon(state->nfc_popup, 0, 3, &I_NFC_dolphin_emulation_47x61);
  65. popup_set_text(state->nfc_popup, "Sharing\nKey via\nNFC", 90, 28, AlignCenter, AlignTop);
  66. prepare_nfc_dev_data(state);
  67. nfc_worker_start(
  68. state->nfc_worker, NfcWorkerStateMfUltralightEmulate, state->nfc_dev_data, NULL, NULL);
  69. notification_message(state->notification, &sequence_blink_start_magenta);
  70. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_NfcPopup);
  71. }
  72. /* Handles scene manager events for the key share popup scene. */
  73. bool scene_on_event_key_share_popup(void* context, SceneManagerEvent event) {
  74. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_share_popup");
  75. furi_assert(context);
  76. ESubGhzChatState* state = context;
  77. UNUSED(state);
  78. UNUSED(event);
  79. return false;
  80. }
  81. /* Cleans up the key share popup scene. */
  82. void scene_on_exit_key_share_popup(void* context) {
  83. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_share_popup");
  84. furi_assert(context);
  85. ESubGhzChatState* state = context;
  86. popup_reset(state->nfc_popup);
  87. notification_message(state->notification, &sequence_blink_stop);
  88. nfc_worker_stop(state->nfc_worker);
  89. crypto_explicit_bzero(state->nfc_dev_data->mf_ul_data.data, KEY_BITS / 8);
  90. memset(state->nfc_dev_data, 0, sizeof(NfcDeviceData));
  91. }