totp_scene_authenticate.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "totp_scene_authenticate.h"
  2. #include <dialogs/dialogs.h>
  3. #include <totp_icons.h>
  4. #include "../../../types/common.h"
  5. #include "../../constants.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../scene_director.h"
  8. #include "../../totp_scenes_enum.h"
  9. #include "../../../services/crypto/crypto.h"
  10. #include "../../../types/user_pin_codes.h"
  11. #define MAX_CODE_LENGTH TOTP_IV_SIZE
  12. static const uint8_t PIN_ASTERISK_RADIUS = 3;
  13. static const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2;
  14. typedef struct {
  15. TotpUserPinCode code_input[MAX_CODE_LENGTH];
  16. uint8_t code_length;
  17. } SceneState;
  18. void totp_scene_authenticate_activate(PluginState* plugin_state) {
  19. SceneState* scene_state = malloc(sizeof(SceneState));
  20. furi_check(scene_state != NULL);
  21. scene_state->code_length = 0;
  22. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  23. plugin_state->current_scene_state = scene_state;
  24. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  25. }
  26. void totp_scene_authenticate_render(Canvas* const canvas, PluginState* plugin_state) {
  27. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  28. int v_shift = 0;
  29. if(scene_state->code_length > 0) {
  30. v_shift = -10;
  31. }
  32. if(plugin_state->crypto_verify_data == NULL) {
  33. canvas_draw_str_aligned(
  34. canvas,
  35. SCREEN_WIDTH_CENTER,
  36. SCREEN_HEIGHT_CENTER - 10 + v_shift,
  37. AlignCenter,
  38. AlignCenter,
  39. "Use arrow keys");
  40. canvas_draw_str_aligned(
  41. canvas,
  42. SCREEN_WIDTH_CENTER,
  43. SCREEN_HEIGHT_CENTER + 5 + v_shift,
  44. AlignCenter,
  45. AlignCenter,
  46. "to setup new PIN");
  47. } else {
  48. canvas_draw_str_aligned(
  49. canvas,
  50. SCREEN_WIDTH_CENTER,
  51. SCREEN_HEIGHT_CENTER + v_shift,
  52. AlignCenter,
  53. AlignCenter,
  54. "Use arrow keys to enter PIN");
  55. }
  56. if(scene_state->code_length > 0) {
  57. uint8_t left_start_x = ((scene_state->code_length - 1) * PIN_ASTERISK_STEP) >> 1;
  58. for(uint8_t i = 0; i < scene_state->code_length; i++) {
  59. canvas_draw_disc(
  60. canvas,
  61. SCREEN_WIDTH_CENTER - left_start_x + i * PIN_ASTERISK_STEP,
  62. SCREEN_HEIGHT_CENTER + 10,
  63. PIN_ASTERISK_RADIUS);
  64. }
  65. }
  66. }
  67. bool totp_scene_authenticate_handle_event(
  68. const PluginEvent* const event,
  69. PluginState* plugin_state) {
  70. if(event->type != EventTypeKey) {
  71. return true;
  72. }
  73. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  74. return false;
  75. }
  76. if(event->input.type != InputTypePress) {
  77. return true;
  78. }
  79. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  80. switch(event->input.key) {
  81. case InputKeyUp:
  82. if(scene_state->code_length < MAX_CODE_LENGTH) {
  83. scene_state->code_input[scene_state->code_length] = PinCodeArrowUp;
  84. scene_state->code_length++;
  85. }
  86. break;
  87. case InputKeyDown:
  88. if(scene_state->code_length < MAX_CODE_LENGTH) {
  89. scene_state->code_input[scene_state->code_length] = PinCodeArrowDown;
  90. scene_state->code_length++;
  91. }
  92. break;
  93. case InputKeyRight:
  94. if(scene_state->code_length < MAX_CODE_LENGTH) {
  95. scene_state->code_input[scene_state->code_length] = PinCodeArrowRight;
  96. scene_state->code_length++;
  97. }
  98. break;
  99. case InputKeyLeft:
  100. if(scene_state->code_length < MAX_CODE_LENGTH) {
  101. scene_state->code_input[scene_state->code_length] = PinCodeArrowLeft;
  102. scene_state->code_length++;
  103. }
  104. break;
  105. case InputKeyOk:
  106. totp_crypto_seed_iv(plugin_state, &scene_state->code_input[0], scene_state->code_length);
  107. if(totp_crypto_verify_key(plugin_state)) {
  108. FURI_LOG_D(LOGGING_TAG, "PIN is valid");
  109. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  110. } else {
  111. FURI_LOG_D(LOGGING_TAG, "PIN is NOT valid");
  112. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  113. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  114. scene_state->code_length = 0;
  115. DialogMessage* message = dialog_message_alloc();
  116. dialog_message_set_buttons(message, "Try again", NULL, NULL);
  117. dialog_message_set_header(
  118. message,
  119. "You entered\ninvalid PIN",
  120. SCREEN_WIDTH_CENTER - 25,
  121. SCREEN_HEIGHT_CENTER - 5,
  122. AlignCenter,
  123. AlignCenter);
  124. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  125. dialog_message_show(plugin_state->dialogs_app, message);
  126. dialog_message_free(message);
  127. }
  128. break;
  129. case InputKeyBack:
  130. if(scene_state->code_length > 0) {
  131. scene_state->code_input[scene_state->code_length - 1] = 0;
  132. scene_state->code_length--;
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. return true;
  139. }
  140. void totp_scene_authenticate_deactivate(PluginState* plugin_state) {
  141. if(plugin_state->current_scene_state == NULL) return;
  142. free(plugin_state->current_scene_state);
  143. plugin_state->current_scene_state = NULL;
  144. }