passgen.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <furi.h>
  2. #include <furi_hal_random.h>
  3. #include <gui/gui.h>
  4. #include <gui/elements.h>
  5. #include <input/input.h>
  6. #include <notification/notification_messages.h>
  7. #include <stdlib.h>
  8. #include <passgen_icons.h>
  9. #define PASSGEN_MAX_LENGTH 16
  10. #define PASSGEN_CHARACTERS_LENGTH (26 * 4)
  11. #define PASSGEN_DIGITS "0123456789"
  12. #define PASSGEN_LETTERS_LOW "abcdefghijklmnopqrstuvwxyz"
  13. #define PASSGEN_LETTERS_UP "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  14. #define PASSGEN_SPECIAL "!#$%^&*.-_"
  15. typedef enum PassGen_Alphabet {
  16. Digits = 1,
  17. Lowercase = 2,
  18. Uppercase = 4,
  19. Special = 8,
  20. DigitsLower = Digits | Lowercase,
  21. DigitsAllLetters = Digits | Lowercase | Uppercase,
  22. Mixed = DigitsAllLetters | Special
  23. } PassGen_Alphabet;
  24. const char* const PassGen_AlphabetChars[16] = {
  25. "0", // invalid value
  26. /* PASSGEN_SPECIAL PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS,
  27. /* PASSGEN_SPECIAL PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
  28. /* PASSGEN_SPECIAL PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW PASSGEN_DIGITS,
  29. /* PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW PASSGEN_DIGITS */,
  30. /* PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS,
  31. /* PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
  32. /* PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW PASSGEN_DIGITS,
  33. PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW PASSGEN_DIGITS */,
  34. PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS,
  35. PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
  36. PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW PASSGEN_DIGITS,
  37. PASSGEN_SPECIAL PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW PASSGEN_DIGITS */,
  38. PASSGEN_SPECIAL PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS,
  39. PASSGEN_SPECIAL PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
  40. PASSGEN_SPECIAL PASSGEN_LETTERS_UP PASSGEN_LETTERS_LOW PASSGEN_DIGITS,
  41. };
  42. const int AlphabetLevels[] = {Digits, Lowercase, DigitsLower, DigitsAllLetters, Mixed};
  43. const char* AlphabetLevelNames[] = {"1234", "abcd", "ab12", "Ab12", "Ab1#"};
  44. const int AlphabetLevelsCount = sizeof(AlphabetLevels) / sizeof(int);
  45. const NotificationSequence PassGen_Alert_vibro = {
  46. &message_vibro_on,
  47. &message_blue_255,
  48. &message_delay_50,
  49. &message_vibro_off,
  50. NULL,
  51. };
  52. typedef struct {
  53. FuriMessageQueue* input_queue;
  54. ViewPort* view_port;
  55. Gui* gui;
  56. FuriMutex** mutex;
  57. NotificationApp* notify;
  58. const char* alphabet;
  59. char password[PASSGEN_MAX_LENGTH + 1];
  60. int length; // must be <= PASSGEN_MAX_LENGTH
  61. int level;
  62. } PassGen;
  63. void state_free(PassGen* app) {
  64. // NOTE: would have preferred if a "safe" memset() was available...
  65. // but, since cannot prevent optimization from removing
  66. // memset(), fill with random data instead.
  67. furi_hal_random_fill_buf((void*)(app->password), PASSGEN_MAX_LENGTH);
  68. gui_remove_view_port(app->gui, app->view_port);
  69. furi_record_close(RECORD_GUI);
  70. view_port_free(app->view_port);
  71. furi_message_queue_free(app->input_queue);
  72. furi_mutex_free(app->mutex);
  73. furi_record_close(RECORD_NOTIFICATION);
  74. free(app);
  75. }
  76. static void input_callback(InputEvent* input_event, void* ctx) {
  77. PassGen* app = ctx;
  78. if(input_event->type == InputTypeShort) {
  79. furi_message_queue_put(app->input_queue, input_event, 0);
  80. }
  81. }
  82. static void render_callback(Canvas* canvas, void* ctx) {
  83. char str_length[8];
  84. PassGen* app = ctx;
  85. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  86. canvas_clear(canvas);
  87. canvas_draw_box(canvas, 0, 0, 128, 14);
  88. canvas_set_color(canvas, ColorWhite);
  89. canvas_set_font(canvas, FontPrimary);
  90. canvas_draw_str(canvas, 2, 11, "Password Generator");
  91. canvas_set_color(canvas, ColorBlack);
  92. canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, app->password);
  93. // Navigation menu:
  94. canvas_set_font(canvas, FontSecondary);
  95. canvas_draw_icon(canvas, 96, 52, &I_Pin_back_arrow_10x8);
  96. canvas_draw_str(canvas, 108, 60, "Exit");
  97. canvas_draw_icon(canvas, 54, 52, &I_Vertical_arrow_7x9);
  98. canvas_draw_str(canvas, 64, 60, AlphabetLevelNames[app->level]);
  99. snprintf(str_length, sizeof(str_length), "Len: %d", app->length);
  100. canvas_draw_icon(canvas, 4, 53, &I_Horizontal_arrow_9x7);
  101. canvas_draw_str(canvas, 15, 60, str_length);
  102. furi_mutex_release(app->mutex);
  103. }
  104. void build_alphabet(PassGen* app) {
  105. PassGen_Alphabet mode = AlphabetLevels[app->level];
  106. if(mode > 0 && mode < 16) {
  107. app->alphabet = PassGen_AlphabetChars[mode];
  108. } else {
  109. app->alphabet =
  110. PassGen_AlphabetChars[0]; // Invalid mode ... password will be all zero digits
  111. }
  112. }
  113. PassGen* state_init() {
  114. PassGen* app = malloc(sizeof(PassGen));
  115. _Static_assert(8 <= PASSGEN_MAX_LENGTH, "app->length must be set <= PASSGEN_MAX_LENGTH");
  116. app->length = 8;
  117. app->level = 2;
  118. build_alphabet(app);
  119. app->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  120. app->view_port = view_port_alloc();
  121. app->gui = furi_record_open(RECORD_GUI);
  122. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  123. view_port_input_callback_set(app->view_port, input_callback, app);
  124. view_port_draw_callback_set(app->view_port, render_callback, app);
  125. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  126. app->notify = furi_record_open(RECORD_NOTIFICATION);
  127. return app;
  128. }
  129. void generate(PassGen* app) {
  130. memset(app->password, 0, PASSGEN_MAX_LENGTH + 1);
  131. int char_option_count = strlen(app->alphabet);
  132. if(char_option_count < 0) {
  133. return;
  134. }
  135. // determine largest character value that avoids bias
  136. char ceil = CHAR_MAX - (CHAR_MAX % char_option_count) - 1;
  137. // iteratively fill the password buffer with random values
  138. // then keep only values that are in-range (no bias)
  139. void* remaining_buffer = app->password;
  140. size_t remaining_length = (app->length * sizeof(char));
  141. while(remaining_length != 0) {
  142. // fewer calls to hardware TRNG is more efficient
  143. furi_hal_random_fill_buf(remaining_buffer, remaining_length);
  144. // keep only values that are in-range (no bias)
  145. char* target = remaining_buffer;
  146. char* source = remaining_buffer;
  147. size_t valid_count = 0;
  148. for(size_t i = 0; i < remaining_length; i++) {
  149. int v = *source;
  150. // if the generated random value is in range, keep it
  151. if(v < ceil) {
  152. v %= char_option_count;
  153. *target = app->alphabet[v];
  154. // increment target pointer and count of valid items found
  155. target++;
  156. valid_count++;
  157. }
  158. // always increment the source pointer
  159. source++;
  160. }
  161. remaining_length -= valid_count;
  162. remaining_buffer = target;
  163. }
  164. }
  165. void update_password(PassGen* app, bool vibro) {
  166. generate(app);
  167. if(vibro)
  168. notification_message(app->notify, &PassGen_Alert_vibro);
  169. else
  170. notification_message(app->notify, &sequence_blink_blue_100);
  171. view_port_update(app->view_port);
  172. }
  173. int32_t passgenapp(void) {
  174. PassGen* app = state_init();
  175. generate(app);
  176. while(1) {
  177. InputEvent input;
  178. while(furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  179. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  180. if(input.type == InputTypeShort) {
  181. switch(input.key) {
  182. case InputKeyBack:
  183. furi_mutex_release(app->mutex);
  184. state_free(app);
  185. return 0;
  186. case InputKeyDown:
  187. if(app->level > 0) {
  188. app->level--;
  189. build_alphabet(app);
  190. update_password(app, false);
  191. } else
  192. notification_message(app->notify, &sequence_blink_red_100);
  193. break;
  194. case InputKeyUp:
  195. if(app->level < AlphabetLevelsCount - 1) {
  196. app->level++;
  197. build_alphabet(app);
  198. update_password(app, false);
  199. } else
  200. notification_message(app->notify, &sequence_blink_red_100);
  201. break;
  202. case InputKeyLeft:
  203. if(app->length > 1) {
  204. app->length--;
  205. update_password(app, false);
  206. } else
  207. notification_message(app->notify, &sequence_blink_red_100);
  208. break;
  209. case InputKeyRight:
  210. if(app->length < PASSGEN_MAX_LENGTH) {
  211. app->length++;
  212. update_password(app, false);
  213. } else
  214. notification_message(app->notify, &sequence_blink_red_100);
  215. break;
  216. case InputKeyOk:
  217. update_password(app, true);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. furi_mutex_release(app->mutex);
  224. }
  225. }
  226. state_free(app);
  227. return 0;
  228. }