esubghz_chat_pass_input.c 3.1 KB

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