esubghz_chat_key_menu.c 4.3 KB

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