esubghz_chat_pass_input.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "../esubghz_chat_i.h"
  2. /* Sends PassEntered event to scene manager and displays whether or not
  3. * encryption has been enabled in the text box. Also clears the text input
  4. * buffer to remove the password and starts the Sub-GHz worker. After starting
  5. * the worker a join message is transmitted. */
  6. static void pass_input_cb(void *context)
  7. {
  8. furi_assert(context);
  9. ESubGhzChatState* state = context;
  10. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  11. (state->encrypted ? "yes" : "no"));
  12. /* clear the text input buffer to remove the password */
  13. crypto_explicit_bzero(state->text_input_store,
  14. sizeof(state->text_input_store));
  15. subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
  16. state->frequency);
  17. /* concatenate the name prefix and join message */
  18. furi_string_set(state->msg_input, state->name_prefix);
  19. furi_string_cat_str(state->msg_input, " joined chat.");
  20. /* encrypt and transmit message */
  21. tx_msg_input(state);
  22. /* clear message input buffer */
  23. furi_string_set_char(state->msg_input, 0, 0);
  24. scene_manager_handle_custom_event(state->scene_manager,
  25. ESubGhzChatEvent_PassEntered);
  26. }
  27. /* If a password was entered this derives a key from the password using a
  28. * single pass of SHA256 and initiates the AES-GCM context for encryption. If
  29. * the initiation fails, the password is rejected. */
  30. static bool pass_input_validator(const char *text, FuriString *error,
  31. void *context)
  32. {
  33. furi_assert(text);
  34. furi_assert(error);
  35. furi_assert(context);
  36. ESubGhzChatState* state = context;
  37. #ifdef FW_ORIGIN_Official
  38. if (strlen(text) == 0) {
  39. furi_string_printf(error, "Enter a\npassword!");
  40. return false;
  41. }
  42. if (strcmp(text, " ") == 0) {
  43. #else /* FW_ORIGIN_Official */
  44. if (strlen(text) == 0) {
  45. #endif /* FW_ORIGIN_Official */
  46. state->encrypted = false;
  47. return true;
  48. }
  49. unsigned char key[KEY_BITS / 8];
  50. /* derive a key from the password */
  51. sha256((unsigned char *) text, strlen(text), key);
  52. /* initiate the crypto context */
  53. bool ret = crypto_ctx_set_key(state->crypto_ctx, key);
  54. /* cleanup */
  55. crypto_explicit_bzero(key, sizeof(key));
  56. if (!ret) {
  57. crypto_ctx_clear(state->crypto_ctx);
  58. furi_string_printf(error, "Failed to\nset key!");
  59. return false;
  60. }
  61. state->encrypted = true;
  62. return true;
  63. }
  64. /* Prepares the password input scene. */
  65. void scene_on_enter_pass_input(void* context)
  66. {
  67. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  68. furi_assert(context);
  69. ESubGhzChatState* state = context;
  70. state->text_input_store[0] = 0;
  71. text_input_reset(state->text_input);
  72. text_input_set_result_callback(
  73. state->text_input,
  74. pass_input_cb,
  75. state,
  76. state->text_input_store,
  77. sizeof(state->text_input_store),
  78. true);
  79. text_input_set_validator(
  80. state->text_input,
  81. pass_input_validator,
  82. state);
  83. text_input_set_header_text(
  84. state->text_input,
  85. #ifdef FW_ORIGIN_Official
  86. "Password (space for no encr.)");
  87. #else /* FW_ORIGIN_Official */
  88. "Password (empty for no encr.)");
  89. text_input_set_minimum_length(state->text_input, 0);
  90. #endif /* FW_ORIGIN_Official */
  91. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  92. }
  93. /* Handles scene manager events for the password input scene. */
  94. bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  95. {
  96. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  97. furi_assert(context);
  98. ESubGhzChatState* state = context;
  99. bool consumed = false;
  100. switch(event.type) {
  101. case SceneManagerEventTypeCustom:
  102. switch(event.event) {
  103. /* switch to message input scene */
  104. case ESubGhzChatEvent_PassEntered:
  105. scene_manager_next_scene(state->scene_manager,
  106. ESubGhzChatScene_ChatInput);
  107. consumed = true;
  108. break;
  109. }
  110. break;
  111. case SceneManagerEventTypeBack:
  112. /* stop the application if the user presses back here */
  113. view_dispatcher_stop(state->view_dispatcher);
  114. consumed = true;
  115. break;
  116. default:
  117. consumed = false;
  118. break;
  119. }
  120. return consumed;
  121. }
  122. /* Cleans up the password input scene. */
  123. void scene_on_exit_pass_input(void* context)
  124. {
  125. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  126. furi_assert(context);
  127. ESubGhzChatState* state = context;
  128. text_input_reset(state->text_input);
  129. }