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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/structs/sio.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include "hardware/adc.h"
#include "./libs/pico-keypad4x4/pico_keypad4x4.h"
#include "./libs/pico-lcd1602/pico_lcd1602.h"
#define FLAG "REPLACE_WITH_FLAG"
// We must impose a 5 seconds timeout on boot to prevent bruteforce by turning power off/on
#define BOOT_TIMEOUT 5 * 1000
// Trigger lines to be turn on when an authentication session succeed
#define LINE_5V_LOW 16
#define LINE_5V_HIGH 17
#define LINE_12V_LOW 18
#define LINE_12V_HIGH 19
#define LINE_24V_LOW 20
#define LINE_24V_HIGH 21
// Leds
#define NO_LED -1
#define LED_GREEN 2
#define LED_RED 3
// Serial info
#define UART_ID uart0
#define BAUD_RATE 9600
#define UART_TX_PIN 0
#define UART_RX_PIN 1
#define UART_DATA_BITS 8
#define UART_STOP_BITS 1
#define UART_PARITY UART_PARITY_NONE
#define AT_PARSER_START 0
#define AT_PARSER_WAIT_RESP 1
#define AT_PARSER_ERROR 2
#define AT_PARSER_SUCCESS 3
#define UART_READ_BUFFER_SIZE 1000
// LCD screen conf
#define LCD_I2C_CHANNEL i2c1
#define LCD_I2C_SDA 26
#define LCD_I2C_SCL 27
// Constants for authentication delay
#define AUTH_SUCCESS_DELAY 30 * 1000
#define AUTH_FAILED_DELAY 30 * 1000
// Constants for SPIN reading
#define SPIN_MAXLEN 10
#define STATE_SPIN_WAITING 0
#define STATE_SPIN_SUCCESS 1
#define STATE_SPIN_FAILED 2
#define SPIN_MAX_TRY 3
#define SPIN_TIME_WINDOW 3 * 60 * 1000 * 1000 // 1 minute in microseconds
#define INVALID_SPIN_DELAY 5 * 1000
#define VALID_SPIN_DELAY 1 * 1000
// Constants for OTP reading
#define OTP_MAXLEN 10
#define STATE_OTP_WAITING 0
#define STATE_OTP_SUCCESS 1
#define STATE_OTP_FAILED 2
#define OTP_MAX_TRY 10
#define OTP_TIME_WINDOW 3 * 60 * 1000 * 1000 // 3 minutes in microseconds
#define INVALID_OTP_DELAY 3 * 1000
// OTP range
#define OTP_MIN 100000
#define OTP_MAX 999999
// Struct and list of all couple of secret pin + phone numbers
#define MAX_USERS 100
typedef struct user user;
struct user
{
char secret_pin[SPIN_MAXLEN + 1]; // Int from 100 000 to 999 999
char phone_number[15]; // Phone number format E.164 +XXXXXXXX...
};
const user * users[MAX_USERS] = {
&((user) {"324092", "+33612345678"}),
};
// A struct to store a pattern for led blinking
// set led to -1 to not blink, give GPIO pin number to blink
// led will bo on for duration ms, off for duration ms
typedef struct blinking_pattern blinking_pattern;
struct blinking_pattern
{
int led_green;
int led_red;
int duration;
};
// Define all led blinking patterns
const blinking_pattern BLINKING_WAITING_USER_SPIN = {LED_GREEN, NO_LED, 250};
const blinking_pattern BLINKING_WAITING_USER_OTP = {NO_LED, LED_RED, 250};
const blinking_pattern BLINKING_TRANSMIT_OTP = {LED_GREEN, LED_RED, 100};
// A variable to store the current blink pattern to use
volatile blinking_pattern blink_pattern;
// Keypad buttons
uint KEYPAD_COLUMNS[4] = {10, 11, 12, 13};
uint KEYPAD_ROWS[4] = {6, 7, 8, 9};
char KEYPAD_MATRIX[16] = {
'1', '2' , '3', 'A',
'4', '5' , '6', 'B',
'7', '8' , '9', 'C',
'x', '0' , '#', 'D'
};
// A counter to know how many auth session we have done
static int auth_session_counter = 0;
// Variables for uart communication
volatile int at_parser_state = AT_PARSER_START;
static char uart_read_buffer[UART_READ_BUFFER_SIZE] = "";
// RX interrupt handler
void on_uart_rx() {
while (uart_is_readable(UART_ID)) {
uint8_t ch = uart_getc(UART_ID);
char chstr[2] = {ch, '\0'};
strncat(uart_read_buffer, chstr, UART_READ_BUFFER_SIZE - 1);
printf("INPUT RX: ASCII %d -> %c\n", ch, ch);
}
char * line_end = strstr(uart_read_buffer, "\r\n");
while (line_end != NULL)
{
char line[UART_READ_BUFFER_SIZE] = "";
strncpy(line, uart_read_buffer, line_end - uart_read_buffer);
printf("Line: %s\n", line);
char * command_token = "AT";
if (strncmp(line, command_token, strlen(command_token)) == 0) // If string start with command token
{
at_parser_state = AT_PARSER_WAIT_RESP;
printf("Find command %s\n", line);
}
else if (at_parser_state == AT_PARSER_WAIT_RESP)
{
if (strstr(line, "ERROR") != NULL)
{
printf("Command failed\n");
at_parser_state = AT_PARSER_ERROR;
}
else if (strstr(line, "OK") != NULL)
{
printf("Command success\n");
at_parser_state = AT_PARSER_SUCCESS;
}
else
{
printf("Ignore line %s\n", line);
}
}
else
{
printf("Ignore line %s\n", line);
}
strncpy(uart_read_buffer, line_end + 2, UART_READ_BUFFER_SIZE - 1); // Remove line from buffer
line_end = strstr(uart_read_buffer, "\r\n");
printf("New state: %d\n", at_parser_state);
}
}
// Init all gpios used by the program
void init_gpios()
{
pico_keypad_init(KEYPAD_COLUMNS, KEYPAD_ROWS, KEYPAD_MATRIX);
adc_init();
// Turn board led on
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
gpio_put(PICO_DEFAULT_LED_PIN, 1);
gpio_init(LED_GREEN);
gpio_set_dir(LED_GREEN, GPIO_OUT);
gpio_pull_down(LED_GREEN);
gpio_init(LED_RED);
gpio_set_dir(LED_RED, GPIO_OUT);
gpio_pull_down(LED_RED);
gpio_init(LINE_5V_LOW);
gpio_set_dir(LINE_5V_LOW, GPIO_OUT);
gpio_pull_up(LINE_5V_LOW);
gpio_put(LINE_5V_LOW, 1);
gpio_init(LINE_12V_LOW);
gpio_set_dir(LINE_12V_LOW, GPIO_OUT);
gpio_pull_up(LINE_12V_LOW);
gpio_put(LINE_12V_LOW, 1);
gpio_init(LINE_24V_LOW);
gpio_set_dir(LINE_24V_LOW, GPIO_OUT);
gpio_pull_up(LINE_24V_LOW);
gpio_put(LINE_24V_LOW, 1);
gpio_init(LINE_5V_HIGH);
gpio_set_dir(LINE_5V_HIGH, GPIO_OUT);
gpio_pull_down(LINE_5V_HIGH);
gpio_init(LINE_12V_HIGH);
gpio_set_dir(LINE_12V_HIGH, GPIO_OUT);
gpio_pull_down(LINE_12V_HIGH);
gpio_init(LINE_24V_HIGH);
gpio_set_dir(LINE_24V_HIGH, GPIO_OUT);
gpio_pull_down(LINE_24V_HIGH);
// UART to communicate with GSM module
uart_init(UART_ID, BAUD_RATE);
uart_set_translate_crlf(UART_ID, false);
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART_ID, false, false); // Disable hardware flow control
uart_set_format(UART_ID, UART_DATA_BITS, UART_STOP_BITS, UART_PARITY); // Define UART format so we know how to read data
uart_set_fifo_enabled(UART_ID, false); // Disable FIFO because we will send and receive char by char
// We will setup an interrupt handler to read RX data
int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
irq_set_enabled(UART_IRQ, true);
// We enable interrupt on UART so it trigger the interrupt on RX data
uart_set_irq_enables(UART_ID, true, false);
// LCD
lcd1602_init(LCD_I2C_CHANNEL, LCD_I2C_SDA, LCD_I2C_SCL);
lcd_init();
}
// Read the code typed by the user
// Always remember to free return at the end
char* user_input(int len, int timeout_us)
{
absolute_time_t start = get_absolute_time();
char *code = malloc(len * sizeof(char));
code[0] = '\0';
while (1)
{
// If we reached timeout, end now
if (timeout_us != -1 && absolute_time_diff_us(start, get_absolute_time()) > timeout_us)
{
break;
}
char ch = pico_keypad_get_key();
if (ch == 0)
{
continue;
}
if (ch == '#')
{
break;
}
char chstr[2] = {ch, '\0'};
lcd_string(chstr);
strncat(code, chstr, len - 1);
// If user input max length reached, end there
if (strlen(code) == len)
{
break;
}
// Wait to do a simple debounce on the buttons
sleep_ms(250);
continue;
}
return code;
}
// Wait until user press any key on keypad
void wait_for_user_interraction()
{
while (1)
{
char ch = pico_keypad_get_key();
if (pico_keypad_get_key() != 0)
{
break;
}
sleep_ms(10);
}
}
/* References for this implementation:
* see rp2040-datasheet.pdf 4.9.5. Temperature Sensor
* pico-examples/adc/adc_console/adc_console.c */
// Get a int based on board temperature
uint16_t read_onboard_temperature() {
adc_set_temp_sensor_enabled(true);
adc_select_input(4); // Select adc for board temp
uint16_t raw = adc_read();
/*
const float conversion_factor = 3.3f / (1<<12);
float result = raw * conversion_factor;
float temp = 27 - (result -0.706)/0.001721;
printf("Temp = %f °C\n", temp);
*/
return raw;
}
// Generate a random OTP
// Always remember to free the return at the end
char * generate_otp (char* secret_pin)
{
unsigned int seed = read_onboard_temperature() + atoi(secret_pin);
srand(seed);
int otp = OTP_MIN + rand() % (OTP_MAX + 1 - OTP_MIN); // Somehow, this is how you get a random int between OTP_MIN and OTP_MAX included
char *code = malloc(OTP_MAXLEN * sizeof(char));
itoa(otp, code, 10);
return code;
}
// Transmit OTP through SMS
bool transmit_otp(char * otp, char * destination)
{
printf("Try send OTP to phone %s", destination);
// Make sure the module is up and running
uart_puts(UART_ID, "AT\r\n");
sleep_ms(10);
uart_puts(UART_ID, "AT\r\n");
sleep_ms(10);
uart_puts(UART_ID, "AT\r\n");
sleep_ms(10);
printf("UART 1");
// Check Pin
uart_puts(UART_ID, "AT+CPIN?\r\n");
sleep_ms(100);
// Ensure modem is in text mode
uart_puts(UART_ID, "AT+CMGF=1\r\n");
sleep_ms(10);
// Disable verbose error
uart_puts(UART_ID, "AT+CMEE=0\r\n");
sleep_ms(100);
// Forge the SMS command
char * command_string = "AT+CMGS=\"%s\"\r";
int command_size = snprintf(NULL, 0, command_string, destination) + 1;
char command[command_size];
snprintf(command, command_size, command_string, destination);
char * sms_string = "Votre code OTP : %s. Ne le transmettez a personne.%c";
int sms_size = snprintf(NULL, 0, sms_string, otp, 26) + 1;
char sms[sms_size];
snprintf(sms, sms_size, sms_string, otp, 26); //%c -> 26 = CTRL + Z char to indicate the end of message to sim800L
// Send the SMS
uart_puts(UART_ID, command);
sleep_ms(10);
uart_puts(UART_ID, sms);
sleep_ms(10);
// Wait for module response to be completed
while (at_parser_state == AT_PARSER_WAIT_RESP)
{
tight_loop_contents();
}
return (at_parser_state == AT_PARSER_ERROR ? false : true);
}
// A timer to blink leds depending on the state of the module
bool repeating_led_blinking_timer_callback (struct repeating_timer *timer)
{
if (blink_pattern.led_green != -1)
{
// sio_hw->gpio_togl expect a mask of bits where 1 indicate the gpio must be toogle and 0 no change
// for example the mask b...0010 will togl GPIO 1, the mask b...0100 will togl GPIO 3, b...0110 togl GPIO 1 et GPIO 3
// We can calculate our mask by shifting left 1 by the int number of the GPIO Pin
sio_hw->gpio_togl = (uint32_t) (1 << blink_pattern.led_green);
}
if (blink_pattern.led_red != -1)
{
sio_hw->gpio_togl = (uint32_t) (1 << blink_pattern.led_red);
}
return true;
}
// Check if a secret pin match one of users, if true return user index
user * find_user_for_spin(char * secret_pin)
{
int i;
for (i = 0; i < MAX_USERS; i++)
{
// Ignore null pointers
if (users[i] == NULL)
{
continue;
}
if (strncmp(secret_pin, users[i]->secret_pin, SPIN_MAXLEN) == 0)
{
// Do casting to prevent warning on const
return (user *) users[i];
}
}
return NULL;
}
// Function to be called when an authentication have been successfull
void on_successfull_authentication ()
{
printf("Auth session %d succeed.\n", auth_session_counter);
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Success ! Flag :");
lcd_set_cursor(1, 0);
lcd_string(FLAG);
gpio_put(LED_GREEN, 1);
gpio_put(LED_RED, 1);
// Turn trigger lines on
gpio_put(LINE_5V_LOW, 0);
gpio_put(LINE_5V_HIGH, 1);
gpio_put(LINE_12V_LOW, 0);
gpio_put(LINE_12V_HIGH, 1);
gpio_put(LINE_24V_LOW, 0);
gpio_put(LINE_24V_HIGH, 1);
sleep_ms(AUTH_SUCCESS_DELAY);
// Turn it back off
gpio_put(LINE_5V_LOW, 1);
gpio_put(LINE_5V_HIGH, 0);
gpio_put(LINE_12V_LOW, 1);
gpio_put(LINE_12V_HIGH, 0);
gpio_put(LINE_24V_LOW, 1);
gpio_put(LINE_24V_HIGH, 0);
gpio_put(LED_GREEN, 0);
gpio_put(LED_RED, 0);
}
// Function to be called when an authentication have failed
void on_failed_authentication ()
{
printf("Auth session %d failed.\n", auth_session_counter);
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Authentication");
lcd_set_cursor(1, 0);
lcd_string("Failed !");
gpio_put(LED_GREEN, 0);
gpio_put(LED_RED, 1);
sleep_ms(AUTH_FAILED_DELAY);
gpio_put(LED_RED, 0);
}
int main()
{
stdio_init_all();
// Sleep on boot to prevent power off bruteforce attack
sleep_ms(BOOT_TIMEOUT);
// Init GPIOs
init_gpios();
// Init the repeating timer we will use to managed led blinking
struct repeating_timer blinking_timer;
// Init the led struct we will use to transmit info to timer about wich led to blink
blinking_pattern leds_blink;
while (1)
{
auth_session_counter ++;
// We wait until the user start interracting
gpio_put(LED_GREEN, 1);
printf("Waiting for user interraction to start an authentication session...\n");
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Waiting...");
wait_for_user_interraction();
sleep_ms(250); // debounce
printf("Start authentication session #%d\n", auth_session_counter);
lcd_clear();
/* Start reading user SPIN */
char *secret_pin = NULL;
user * matching_user;
int state_reading_spin = STATE_SPIN_WAITING;
int reading_spin_try = 0;
absolute_time_t start = get_absolute_time();
while (state_reading_spin == STATE_SPIN_WAITING)
{
// If time window for reading spin have expired
if (absolute_time_diff_us(start, get_absolute_time()) > SPIN_TIME_WINDOW)
{
state_reading_spin = STATE_SPIN_FAILED;
continue;
}
reading_spin_try ++;
// We start a timer to blink led accordingly
// We use a negative value for duration, because we want timer to repeat every x ms since pointer start, not after pointer end
blink_pattern = BLINKING_WAITING_USER_SPIN;
add_repeating_timer_ms(-BLINKING_WAITING_USER_SPIN.duration, repeating_led_blinking_timer_callback, NULL, &blinking_timer);
//Read user secret_pin
printf("Type SPIN: ");
lcd_set_cursor(0, 0);
lcd_string("Type secret PIN: ");
lcd_set_cursor(1, 0); // Go next line to show pin while typed
int read_timeout = SPIN_TIME_WINDOW - absolute_time_diff_us(start, get_absolute_time()); // Timeout is timewindow - already spent time
secret_pin = user_input(SPIN_MAXLEN, read_timeout);
printf("\n");
// Stop blinking and make sure all led are off
cancel_repeating_timer(&blinking_timer);
gpio_put(LED_GREEN, 0);
gpio_put(LED_RED, 0);
// If invalid spin
matching_user = find_user_for_spin(secret_pin);
if (matching_user == NULL)
{
printf("Invalid SPIN: %s\n", secret_pin);
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Invalid SPIN !");
gpio_put(LED_RED, 1);
sleep_ms(INVALID_SPIN_DELAY);
gpio_put(LED_RED, 0);
if (reading_spin_try >= SPIN_MAX_TRY)
{
state_reading_spin = STATE_SPIN_FAILED;
}
continue;
}
// If valid spin, green led and go for transmission of datas
state_reading_spin = STATE_SPIN_SUCCESS;
}
// If we failed to provide a valid SPIN, timeout + red led and go back to wait for a new auth session
if (state_reading_spin == STATE_SPIN_FAILED)
{
// Free secret_pin from memory we wont need it anymore
free(secret_pin);
on_failed_authentication();
continue;
}
// If valid spin, turn green led on for a few seconds
gpio_put(LED_GREEN, 1);
sleep_ms(VALID_SPIN_DELAY);
gpio_put(LED_GREEN, 0);
/* We have a valid PIN, we will now generate and transmit OTP code */
printf("Start generation and transmission of OTP for SPIN %s\n", secret_pin);
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Sending OTP...");
// We start a timer to blink led accordingly
blink_pattern = BLINKING_TRANSMIT_OTP;
add_repeating_timer_ms(-BLINKING_TRANSMIT_OTP.duration, repeating_led_blinking_timer_callback, NULL, &blinking_timer);
char * one_time_password = generate_otp(secret_pin);
printf("OTP have been generated.\n");
// We can now free secret pin we dont need it anymore
free(secret_pin);
// Transmit OTP through
int success = transmit_otp(one_time_password, matching_user->phone_number);
// Only if we failed to transmit the password, we immediatly end the auth session and restart everything
if (success == false)
{
printf("OTP transmission failed.\n");
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Cannot send SMS, call the admin");
sleep_ms(5000);
cancel_repeating_timer(&blinking_timer);
free(one_time_password);
on_failed_authentication();
continue;
}
// Stop blinking
cancel_repeating_timer(&blinking_timer);
gpio_put(LED_GREEN, 0);
gpio_put(LED_RED, 0);
/* Start reading OTP from user */
char *user_one_time_password;
int state_reading_otp = STATE_OTP_WAITING;
int reading_otp_try = 0;
start = get_absolute_time();
while (state_reading_otp == STATE_SPIN_WAITING)
{
// If time window for reading otp have expired
if (absolute_time_diff_us(start, get_absolute_time()) > OTP_TIME_WINDOW)
{
state_reading_otp = STATE_OTP_FAILED;
continue;
}
reading_otp_try ++;
// We start a timer to blink led accordingly
// We use a negative value for duration, because we want timer to repeat every x ms since pointer start, not after pointer end
blink_pattern = BLINKING_WAITING_USER_OTP;
add_repeating_timer_ms(-BLINKING_WAITING_USER_SPIN.duration, repeating_led_blinking_timer_callback, NULL, &blinking_timer);
// Read user OTP
printf("Type the secret OTP you have received: ");
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Type OTP: ");
lcd_set_cursor(1, 0);
int read_timeout = OTP_TIME_WINDOW - absolute_time_diff_us(start, get_absolute_time()); // Timeout is timewindow - already spent time
char * user_one_time_password = user_input(OTP_MAXLEN, read_timeout);
printf("\n");
// Stop blinking and make sure all led are off
cancel_repeating_timer(&blinking_timer);
gpio_put(LED_GREEN, 0);
gpio_put(LED_RED, 0);
// If invalid otp
if (strncmp(user_one_time_password, one_time_password, OTP_MAXLEN) != 0)
{
printf("Invalid OTP: %s\n", user_one_time_password);
lcd_clear();
lcd_set_cursor(0, 0);
lcd_string("Invalid OTP !");
gpio_put(LED_RED, 1);
sleep_ms(INVALID_OTP_DELAY);
gpio_put(LED_RED, 0);
if (reading_otp_try >= OTP_MAX_TRY)
{
state_reading_otp = STATE_OTP_FAILED;
}
else
{
free(user_one_time_password);
}
continue;
}
// Valid otp
state_reading_otp = STATE_OTP_SUCCESS;
}
// We can free both generated and user provided OTP, we will not use it anymore
free(user_one_time_password);
free(one_time_password);
// If we failed to provide a valid OTP, timeout + red led and go back to wait for a new auth session
if (state_reading_otp == STATE_OTP_FAILED)
{
printf("Failed to provide a correct OTP %d times.\n", OTP_MAX_TRY);
on_failed_authentication();
continue;
}
// If valid otp, we finally call the on_successfull_authentication callback
on_successfull_authentication();
}
return 0;
}
|