passgen.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. {
  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. {
  107. PassGen_Alphabet mode = AlphabetLevels[app->level];
  108. if (mode > 0 && mode < 16) {
  109. app->alphabet = PassGen_AlphabetChars[mode];
  110. } else {
  111. app->alphabet = 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. {
  132. memset(app->password, 0, PASSGEN_MAX_LENGTH+1);
  133. int char_option_count = strlen(app->alphabet);
  134. if (char_option_count < 0) {
  135. return;
  136. }
  137. // determine largest character value that avoids bias
  138. char ceil = CHAR_MAX - (CHAR_MAX % char_option_count) - 1;
  139. // iteratively fill the password buffer with random values
  140. // then keep only values that are in-range (no bias)
  141. void* remaining_buffer = app->password;
  142. size_t remaining_length = (app->length * sizeof(char));
  143. while (remaining_length != 0) {
  144. // fewer calls to hardware TRNG is more efficient
  145. furi_hal_random_fill_buf(remaining_buffer, remaining_length);
  146. // keep only values that are in-range (no bias)
  147. char* target = remaining_buffer;
  148. char* source = remaining_buffer;
  149. size_t valid_count = 0;
  150. for (size_t i = 0; i < remaining_length; i++) {
  151. int v = *source;
  152. // if the generated random value is in range, keep it
  153. if (v < ceil) {
  154. v %= char_option_count;
  155. *target = app->alphabet[v];
  156. // increment target pointer and count of valid items found
  157. target++;
  158. valid_count++;
  159. }
  160. // always increment the source pointer
  161. source++;
  162. }
  163. remaining_length -= valid_count;
  164. remaining_buffer = target;
  165. }
  166. }
  167. void update_password(PassGen* app, bool vibro)
  168. {
  169. generate(app);
  170. if (vibro)
  171. notification_message(app->notify, &PassGen_Alert_vibro);
  172. else
  173. notification_message(app->notify, &sequence_blink_blue_100);
  174. view_port_update(app->view_port);
  175. }
  176. int32_t passgenapp(void) {
  177. PassGen* app = state_init();
  178. generate(app);
  179. while(1) {
  180. InputEvent input;
  181. while(furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  182. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  183. if (input.type == InputTypeShort)
  184. {
  185. switch (input.key) {
  186. case InputKeyBack:
  187. furi_mutex_release(app->mutex);
  188. state_free(app);
  189. return 0;
  190. case InputKeyDown:
  191. if (app->level > 0)
  192. {
  193. app->level--;
  194. build_alphabet(app);
  195. update_password(app, false);
  196. }
  197. else
  198. notification_message(app->notify, &sequence_blink_red_100);
  199. break;
  200. case InputKeyUp:
  201. if (app->level < AlphabetLevelsCount - 1)
  202. {
  203. app->level++;
  204. build_alphabet(app);
  205. update_password(app, false);
  206. }
  207. else
  208. notification_message(app->notify, &sequence_blink_red_100);
  209. break;
  210. case InputKeyLeft:
  211. if (app->length > 1)
  212. {
  213. app->length--;
  214. update_password(app, false);
  215. }
  216. else
  217. notification_message(app->notify, &sequence_blink_red_100);
  218. break;
  219. case InputKeyRight:
  220. if (app->length < PASSGEN_MAX_LENGTH)
  221. {
  222. app->length++;
  223. update_password(app, false);
  224. }
  225. else
  226. notification_message(app->notify, &sequence_blink_red_100);
  227. break;
  228. case InputKeyOk:
  229. update_password(app, true);
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. furi_mutex_release(app->mutex);
  236. }
  237. }
  238. state_free(app);
  239. return 0;
  240. }