esubghz_chat_key_menu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "../esubghz_chat_i.h"
  2. typedef enum {
  3. ESubGhzChatKeyMenuItems_NoEncryption,
  4. ESubGhzChatKeyMenuItems_Password,
  5. ESubGhzChatKeyMenuItems_HexKey,
  6. ESubGhzChatKeyMenuItems_GenKey,
  7. ESubGhzChatKeyMenuItems_ReadKeyFromNfc,
  8. } ESubGhzChatKeyMenuItems;
  9. static void key_menu_cb(void* context, uint32_t index) {
  10. furi_assert(context);
  11. ESubGhzChatState* state = context;
  12. uint8_t key[KEY_BITS / 8];
  13. switch(index) {
  14. case ESubGhzChatKeyMenuItems_NoEncryption:
  15. state->encrypted = false;
  16. view_dispatcher_send_custom_event(
  17. state->view_dispatcher, ESubGhzChatEvent_KeyMenuNoEncryption);
  18. break;
  19. case ESubGhzChatKeyMenuItems_Password:
  20. view_dispatcher_send_custom_event(
  21. state->view_dispatcher, ESubGhzChatEvent_KeyMenuPassword);
  22. break;
  23. case ESubGhzChatKeyMenuItems_HexKey:
  24. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_KeyMenuHexKey);
  25. break;
  26. case ESubGhzChatKeyMenuItems_GenKey:
  27. /* generate a random key */
  28. furi_hal_random_fill_buf(key, KEY_BITS / 8);
  29. /* initiate the crypto context */
  30. bool ret = crypto_ctx_set_key(state->crypto_ctx, key, state->name_prefix, furi_get_tick());
  31. /* cleanup */
  32. crypto_explicit_bzero(key, sizeof(key));
  33. if(!ret) {
  34. crypto_ctx_clear(state->crypto_ctx);
  35. return;
  36. }
  37. /* set encrypted flag and enter the chat */
  38. state->encrypted = true;
  39. view_dispatcher_send_custom_event(state->view_dispatcher, ESubGhzChatEvent_KeyMenuGenKey);
  40. break;
  41. case ESubGhzChatKeyMenuItems_ReadKeyFromNfc:
  42. view_dispatcher_send_custom_event(
  43. state->view_dispatcher, ESubGhzChatEvent_KeyMenuReadKeyFromNfc);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. /* Prepares the key menu scene. */
  50. void scene_on_enter_key_menu(void* context) {
  51. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_menu");
  52. furi_assert(context);
  53. ESubGhzChatState* state = context;
  54. menu_reset(state->menu);
  55. /* clear the crypto CTX in case we got back from password or hex key
  56. * input */
  57. crypto_ctx_clear(state->crypto_ctx);
  58. menu_add_item(
  59. state->menu,
  60. "No encryption",
  61. &I_chat_14px,
  62. ESubGhzChatKeyMenuItems_NoEncryption,
  63. key_menu_cb,
  64. state);
  65. menu_add_item(
  66. state->menu,
  67. "Password",
  68. &I_keyboard_14px,
  69. ESubGhzChatKeyMenuItems_Password,
  70. key_menu_cb,
  71. state);
  72. menu_add_item(
  73. state->menu, "Hex Key", &I_hex_14px, ESubGhzChatKeyMenuItems_HexKey, key_menu_cb, state);
  74. menu_add_item(
  75. state->menu,
  76. "Generate Key",
  77. &I_u2f_14px,
  78. ESubGhzChatKeyMenuItems_GenKey,
  79. key_menu_cb,
  80. state);
  81. menu_add_item(
  82. state->menu,
  83. "Read Key from NFC",
  84. &I_Nfc_14px,
  85. ESubGhzChatKeyMenuItems_ReadKeyFromNfc,
  86. key_menu_cb,
  87. state);
  88. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
  89. }
  90. /* Handles scene manager events for the key menu scene. */
  91. bool scene_on_event_key_menu(void* context, SceneManagerEvent event) {
  92. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_menu");
  93. furi_assert(context);
  94. ESubGhzChatState* state = context;
  95. bool consumed = false;
  96. switch(event.type) {
  97. case SceneManagerEventTypeCustom:
  98. switch(event.event) {
  99. /* switch to frequency input scene */
  100. case ESubGhzChatEvent_KeyMenuNoEncryption:
  101. case ESubGhzChatEvent_KeyMenuGenKey:
  102. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  103. consumed = true;
  104. break;
  105. /* switch to password input scene */
  106. case ESubGhzChatEvent_KeyMenuPassword:
  107. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_PassInput);
  108. consumed = true;
  109. break;
  110. /* switch to hex key input scene */
  111. case ESubGhzChatEvent_KeyMenuHexKey:
  112. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_HexKeyInput);
  113. consumed = true;
  114. break;
  115. /* switch to hex key read scene */
  116. case ESubGhzChatEvent_KeyMenuReadKeyFromNfc:
  117. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_KeyReadPopup);
  118. consumed = true;
  119. break;
  120. }
  121. break;
  122. case SceneManagerEventTypeBack:
  123. /* stop the application if the user presses back here */
  124. view_dispatcher_stop(state->view_dispatcher);
  125. consumed = true;
  126. break;
  127. default:
  128. consumed = false;
  129. break;
  130. }
  131. return consumed;
  132. }
  133. /* Cleans up the key menu scene. */
  134. void scene_on_exit_key_menu(void* context) {
  135. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_menu");
  136. furi_assert(context);
  137. ESubGhzChatState* state = context;
  138. menu_reset(state->menu);
  139. }