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