passgen.c 8.5 KB

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