esubghz_chat_pass_input.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "../esubghz_chat_i.h"
  2. /* Sends PassEntered event to scene manager and enters the chat. */
  3. static void pass_input_cb(void *context)
  4. {
  5. furi_assert(context);
  6. ESubGhzChatState* state = context;
  7. enter_chat(state);
  8. scene_manager_handle_custom_event(state->scene_manager,
  9. ESubGhzChatEvent_PassEntered);
  10. }
  11. /* If a password was entered this derives a key from the password using a
  12. * single pass of SHA256 and initiates the AES-GCM context for encryption. If
  13. * the initiation fails, the password is rejected. */
  14. static bool pass_input_validator(const char *text, FuriString *error,
  15. void *context)
  16. {
  17. furi_assert(text);
  18. furi_assert(error);
  19. furi_assert(context);
  20. ESubGhzChatState* state = context;
  21. if (strlen(text) == 0) {
  22. furi_string_printf(error, "Enter a\npassword!");
  23. return false;
  24. }
  25. unsigned char key[KEY_BITS / 8];
  26. /* derive a key from the password */
  27. sha256((unsigned char *) text, strlen(text), key);
  28. /* initiate the crypto context */
  29. bool ret = crypto_ctx_set_key(state->crypto_ctx, key);
  30. /* cleanup */
  31. crypto_explicit_bzero(key, sizeof(key));
  32. if (!ret) {
  33. crypto_ctx_clear(state->crypto_ctx);
  34. furi_string_printf(error, "Failed to\nset key!");
  35. return false;
  36. }
  37. state->encrypted = true;
  38. return true;
  39. }
  40. /* Prepares the password input scene. */
  41. void scene_on_enter_pass_input(void* context)
  42. {
  43. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  44. furi_assert(context);
  45. ESubGhzChatState* state = context;
  46. state->text_input_store[0] = 0;
  47. text_input_reset(state->text_input);
  48. text_input_set_result_callback(
  49. state->text_input,
  50. pass_input_cb,
  51. state,
  52. state->text_input_store,
  53. sizeof(state->text_input_store),
  54. true);
  55. text_input_set_validator(
  56. state->text_input,
  57. pass_input_validator,
  58. state);
  59. text_input_set_header_text(
  60. state->text_input,
  61. "Password");
  62. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  63. }
  64. /* Handles scene manager events for the password input scene. */
  65. bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  66. {
  67. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  68. furi_assert(context);
  69. ESubGhzChatState* state = context;
  70. bool consumed = false;
  71. switch(event.type) {
  72. case SceneManagerEventTypeCustom:
  73. switch(event.event) {
  74. /* switch to message input scene */
  75. case ESubGhzChatEvent_PassEntered:
  76. scene_manager_next_scene(state->scene_manager,
  77. ESubGhzChatScene_ChatInput);
  78. consumed = true;
  79. break;
  80. }
  81. break;
  82. default:
  83. consumed = false;
  84. break;
  85. }
  86. return consumed;
  87. }
  88. /* Cleans up the password input scene. */
  89. void scene_on_exit_pass_input(void* context)
  90. {
  91. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  92. furi_assert(context);
  93. ESubGhzChatState* state = context;
  94. text_input_reset(state->text_input);
  95. }