Skip to content

Commit

Permalink
feat: finish initial pin code implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanOricil committed Apr 28, 2024
1 parent 6a489f8 commit c740ff1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 35 deletions.
6 changes: 1 addition & 5 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// NOTE: network to sync to NTP. Uncomment it to setup the wifi connection

// NOTE: used when reading data from the SD card
#define TF_CS 5

Expand Down Expand Up @@ -32,10 +30,8 @@
#define MQTT_WRITE_NEW_SECRET_TOPIC "esp32-totp-write-new-secret"

// PIN
#define PIN_MIN_LENGTH 10
#define PIN_MIN_LENGTH 6
#define PIN_MAX_LENGTH 20
#define PIN_HASH "ccab26629773e35c4f32748e829b382e42950f04a851b72ee6aa52c0bc1141f0"
#define PIN_SALT "=kxCImuICYdLOTPC-Xer!ksJSB5*+xIK"

// UI
#define PIN_SCREEN_NAME "pin"
Expand Down
46 changes: 41 additions & 5 deletions src/pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,43 @@
#include <mbedtls/md.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"

unsigned char *hex_to_bin(const char *hex_string)
{
size_t len = strlen(hex_string);
if (len % 2 != 0)
{
return NULL; // Not a valid hexadecimal string
}

size_t bin_len = len / 2;
unsigned char *bin_data = (unsigned char *)malloc(bin_len);
if (bin_data == NULL)
{
return NULL; // Memory allocation failed
}

for (size_t i = 0; i < len; i += 2)
{
char byte[3] = {hex_string[i], hex_string[i + 1], '\0'};
bin_data[i / 2] = strtol(byte, NULL, 16);
}

return bin_data;
}

void print_hash(unsigned char *hash, size_t length)
{
for (size_t i = 0; i < length; i++)
{
printf("%02x", hash[i]);
}
printf("\n");
}

bool validate_pin(const char *pin)
{
if (strlen(pin) < PIN_MIN_LENGTH)
Expand All @@ -13,11 +48,12 @@ bool validate_pin(const char *pin)
mbedtls_md_context_t ctx;
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
mbedtls_md_starts(&ctx);
mbedtls_md_update(&ctx, (const unsigned char *)pin, strlen(pin));
mbedtls_md_update(&ctx, PIN_SALT, strlen((char *)PIN_SALT));
mbedtls_md_finish(&ctx, generated_hash);
mbedtls_md_hmac_starts(&ctx, PIN_KEY, strlen((char *)PIN_KEY));
mbedtls_md_hmac_update(&ctx, (const unsigned char *)pin, strlen(pin));
mbedtls_md_hmac_finish(&ctx, generated_hash);
mbedtls_md_free(&ctx);

return memcmp(generated_hash, PIN_HASH, sizeof(generated_hash)) == 0;
print_hash(generated_hash, sizeof(generated_hash) / sizeof(generated_hash[0]));

return memcmp(generated_hash, hex_to_bin(PIN_HASH), sizeof(generated_hash)) == 0;
}
4 changes: 2 additions & 2 deletions src/ui/screens/ui_pin_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ void ui_pin_screen_screen_init(void)
lv_obj_align(ui_pin_textarea, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_set_pos(ui_pin_textarea, 0, 0);
lv_obj_set_width(ui_pin_textarea, LV_HOR_RES - 10);
lv_obj_set_height(ui_pin_textarea, 40);
lv_obj_set_height(ui_pin_textarea, 45);
lv_obj_add_event_cb(ui_pin_textarea, ui_event_pin_textarea, LV_EVENT_ALL, NULL);
lv_obj_set_style_text_font(ui_pin_textarea, &lv_font_montserrat_18, 0);

lv_obj_set_style_text_align(ui_pin_textarea, LV_TEXT_ALIGN_CENTER, 0);

static const char *btnm_map[] = {
"1", "2", "3", "4", "5", "\n",
Expand Down
73 changes: 50 additions & 23 deletions src/ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include "ui.h"
#include "ui_helpers.h"

#ifdef PIN_HASH
#pragma message "PIN_HASH is set to: " PIN_HASH
#endif

#ifdef PIN_KEY
#pragma message "PIN_KEY is set to: " PIN_KEY
#endif

// CUSTOM EVENTS
uint32_t LV_EVENT_SETUP_COMPLETE;

Expand All @@ -26,68 +34,85 @@ void ui_event_pin_textarea(lv_event_t *e);

///////////////////// TEST LVGL SETTINGS ////////////////////
#if LV_COLOR_DEPTH != 16
#error "LV_COLOR_DEPTH should be 16bit to match SquareLine Studio's settings"
#error "LV_COLOR_DEPTH should be 16bit to match SquareLine Studio's settings"
#endif

///////////////////// ANIMATIONS ////////////////////

///////////////////// FUNCTIONS ////////////////////
void ui_event_totp_component_label(lv_event_t * e) {
void ui_event_totp_component_label(lv_event_t *e)
{
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
if (event_code == LV_EVENT_VALUE_CHANGED)
{
on_totp_component_label_value_changed(e);
}
}

void ui_event_totp_component_bar(lv_event_t * e) {
void ui_event_totp_component_bar(lv_event_t *e)
{
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
if (event_code == LV_EVENT_VALUE_CHANGED)
{
on_totp_component_bar_value_changed(e);
}
}

void ui_event_keyboard_button(lv_event_t * e) {
void ui_event_keyboard_button(lv_event_t *e)
{
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
if (event_code == LV_EVENT_VALUE_CHANGED)
{
on_keyboard_button_clicked(e);
}
}

void ui_event_pin_textarea(lv_event_t * e) {
void ui_event_pin_textarea(lv_event_t *e)
{
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_READY) {
if (event_code == LV_EVENT_READY)
{
on_validate_pin(e);
}
}

///////////////////// SCREENS ////////////////////

void ui_init( void ){
void ui_init(void)
{
lv_disp_t *disp = lv_disp_get_default();
lv_theme_t *theme = lv_theme_default_init(
disp,
lv_palette_main(LV_PALETTE_BLUE),
lv_palette_main(LV_PALETTE_RED),
true,
LV_FONT_DEFAULT
);
disp,
lv_palette_main(LV_PALETTE_BLUE),
lv_palette_main(LV_PALETTE_RED),
true,
LV_FONT_DEFAULT);
LV_EVENT_SETUP_COMPLETE = lv_event_register_id();
lv_disp_set_theme(disp, theme);
ui_totp_screen_screen_init();
ui_pin_screen_screen_init();
ui____initial_actions0 = lv_obj_create(NULL);
lv_disp_load_scr(ui_pin_screen);
}

if (PIN_HASH && PIN_KEY)
{
ui_pin_screen_screen_init();
lv_disp_load_scr(ui_pin_screen);
}
else
{
lv_disp_load_scr(ui_totp_screen);
}
}

void refresh_totp_labels() {
void refresh_totp_labels()
{
LV_LOG_INFO("refresh_totp_labels");
lv_obj_t *container;
lv_obj_t *label;
int index = 0;

container = lv_obj_get_child(ui_totp_screen, index);
while (container) {
while (container)
{
label = lv_obj_get_child(container, 1);
TotpValueChangeEvent event;
event.index = index;
Expand All @@ -97,14 +122,16 @@ void refresh_totp_labels() {
}
}

void refresh_counter_bars(){
void refresh_counter_bars()
{
LV_LOG_INFO("refresh_counter_bars");
lv_obj_t *container;
lv_obj_t *bar;
int index = 0;

container = lv_obj_get_child(ui_totp_screen, index);
while (container) {
while (container)
{
bar = lv_obj_get_child(container, 2);
TotpValueChangeEvent event;
event.index = index;
Expand Down
1 change: 1 addition & 0 deletions src/ui/ui_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void on_validate_pin(lv_event_t *e){
if(!validate_pin(pin)){
lv_obj_t * wrongPasswordModal = lv_msgbox_create(NULL, "ERROR", "PIN isn't valid", NULL, true);
lv_obj_center(wrongPasswordModal);
return;
}

lv_scr_load(ui_totp_screen);
Expand Down
1 change: 1 addition & 0 deletions src/ui/ui_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct {
void on_totp_component_label_value_changed(lv_event_t *e);
void on_totp_component_bar_value_changed(lv_event_t *e);
void on_keyboard_button_clicked(lv_event_t *e);
void on_validate_pin(lv_event_t *e);

#ifdef __cplusplus
} /*extern "C"*/
Expand Down

0 comments on commit c740ff1

Please sign in to comment.