passgen.c 9.4 KB

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