From 9ec31d0b521b1c86dd7731f7b580908f40c3a11b Mon Sep 17 00:00:00 2001 From: redlineo Date: Tue, 1 Apr 2025 09:01:05 +0000 Subject: [PATCH] add missed files --- red_menu.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ red_menu.h | 12 +++++ utils.c | 53 +++++++++++++++++++++++ utils.h | 21 +++++++++ 4 files changed, 211 insertions(+) create mode 100644 red_menu.c create mode 100644 red_menu.h create mode 100644 utils.c create mode 100644 utils.h diff --git a/red_menu.c b/red_menu.c new file mode 100644 index 0000000..4251420 --- /dev/null +++ b/red_menu.c @@ -0,0 +1,125 @@ +#include "red_menu.h" + +// clang-format off +enum menu_options { + EXIT, + SHOW_PASS_NAMES, + SHOW_MEMORY_USAGE, + CHANGE_MASTER_KEY, + CHANGE_PASS_IN_STORAGE, + CHANGE_PASS_NAME, + ADD_NEW_PASS, + SAVE_NEW_PASS, + READING_PASS, + READ_USER_CHOICE, + SHOW_THIS_MENU +}; + +// clang-format on +uint8_t readed_option[1] = {255}; +uint8_t count_readed = 0; +uint8_t return_key = 0; +uint8_t *enc_pass_new; + +uint8_t readed_key_new[128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +uint8_t count_char_key_new = 0; + +// read new key +uint8_t read_new_key(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { + case KC_ENTER: + readed_option[0] = SAVE_NEW_PASS; + return 1; + break; + default: + if (count_char_key_new + 1 == enc_pass.storage_pass_len) { + readed_option[0] = SAVE_NEW_PASS; + send_string("Length limit!\n"); + // TODO: done this feature + } + return kc_to_ascii(keycode, record, readed_key_new, &count_char_key_new); + break; + } + return 0; +} + +// clang-format on +// 0 idle +// 1 get key input +// 2 exit menu +uint8_t draw_red_menu(uint16_t keycode, keyrecord_t *record) { + switch (readed_option[0]) { + case EXIT: + readed_option[0] = 255; + return 2; + break; + case READ_USER_CHOICE: { + return_key = kc_to_ascii(keycode, record, readed_option, &count_readed); + count_readed = 0; + if (readed_option[0] == ASCII_LOW_0) { + readed_option[0] = EXIT; + } + if (readed_option[0] == ASCII_LOW_1) { + readed_option[0] = SHOW_PASS_NAMES; + } + if (readed_option[0] == ASCII_LOW_2) { + readed_option[0] = SHOW_MEMORY_USAGE; + } + if (readed_option[0] == ASCII_LOW_3) { + readed_option[0] = CHANGE_MASTER_KEY; + } + if (readed_option[0] == ASCII_LOW_4) { + readed_option[0] = CHANGE_PASS_IN_STORAGE; + } + if (readed_option[0] == ASCII_LOW_5) { + readed_option[0] = CHANGE_PASS_NAME; + } + if (readed_option[0] == ASCII_LOW_6) { + readed_option[0] = ADD_NEW_PASS; + } + if (return_key == 1) { + send_string(SS_DOWN(X_LCTL) SS_DOWN(X_A) SS_UP(X_LCTL) SS_UP(X_A) SS_TAP(X_BACKSPACE)); + return 1; + } + break; + } + case SHOW_PASS_NAMES: + case SHOW_MEMORY_USAGE: + case CHANGE_MASTER_KEY: + case CHANGE_PASS_IN_STORAGE: + case CHANGE_PASS_NAME: + case SAVE_NEW_PASS: + enc_pass_new = (uint8_t *)malloc(sizeof(uint8_t) * enc_pass.storage_pass_len); + enc_pass_new = encrypt_pass_kuzn(readed_key_new, count_char_key_new); + for (uint8_t new_pass_index = 0; new_pass_index < enc_pass.storage_pass_len; new_pass_index++) { + enc_pass.passwords[enc_pass.password_count * enc_pass.storage_pass_len + new_pass_index] = enc_pass_new[new_pass_index]; + } + enc_pass.password_count++; + // eeconfig_update_user_datablock(enc_pass.raw); + red_write_to_storage(); + send_string("Successful adding new key on keycode RED_PASS"); + send_byte(enc_pass.password_count); + send_string("\n"); + send_string(SS_DELAY(1000)); + readed_option[0] = 255; + break; + case READING_PASS: + return read_new_key(keycode, record); + break; + case ADD_NEW_PASS: + if (enc_pass.password_count == enc_pass.storage_size) { + send_string("Limit error\n"); + readed_option[0] = 255; + } else { + send_string("Write new password. Tap `Enter` for confirm\n"); + readed_option[0] = READING_PASS; + } + break; + default: // SHOW_THIS_MENU + send_string("RED_CRYPTO MENU:\n0-EXIT\n1-Show password's names\n2-Show memory usage\n3-Change master key\n4-Change password in storage\n5-Change password's name\n6-Add new password\n"); + send_string("Choose option(0-6): "); + readed_option[0] = READ_USER_CHOICE; + break; + } + return 0; +} \ No newline at end of file diff --git a/red_menu.h b/red_menu.h new file mode 100644 index 0000000..3f7902a --- /dev/null +++ b/red_menu.h @@ -0,0 +1,12 @@ +#include QMK_KEYBOARD_H +#include "quantum.h" + +#ifndef RED_MENU_H +# define RED_MENU_H + +# include "kc_to_ascii.h" +# include "red_crypto.h" + +uint8_t draw_red_menu(uint16_t keycode, keyrecord_t *record); + +#endif \ No newline at end of file diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..ae8a87e --- /dev/null +++ b/utils.c @@ -0,0 +1,53 @@ +#include "utils.h" + +void red_print_chars(uint8_t *c) { + for (uint8_t i = 0; i < 32; i++) { + if (c[i] == 0x00) break; + dprintf("%c", c[i]); + } + dprintf("\n"); +}; + +void red_print_int(uint8_t *c) { + for (uint8_t i = 0; i < 32; i++) { + dprintf("%d", c[i]); + } + dprintf("\n"); +}; + +void red_print_hex(uint8_t *h) { + for (uint8_t i = 0; i < 32; i++) { + dprintf("%x ", h[i]); + } + dprintf("\n"); +}; + +size_t red_min_len(size_t a, size_t b) { + if (a < b) + return a; + else + return b; +} + +void red_send_chars_pass(uint8_t *out) { + for (uint8_t i = 0; i < INIT_STORAGE_PASS_LEN && out[i] != 0; i++) { + send_char((char)out[i]); + } +}; + +// EEPROM utils + +void red_write_to_storage(void) { + // uint8_t res = 0; + for (uint16_t raw_index = 0; raw_index < STORAGE_SIZE; raw_index++) { + // res = EEPROM_WriteDataByte(0x0000 + raw_index, enc_pass.raw[raw_index]); + EEPROM_WriteDataByte(0x0040 + raw_index, enc_pass.raw[raw_index]); + } + // return res; +} + +void red_read_from_storage(void) { + for (uint16_t raw_index = 0; raw_index < STORAGE_SIZE; raw_index++) { + enc_pass.raw[raw_index] = EEPROM_ReadDataByte(0x0040 + raw_index); + } +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..30abf10 --- /dev/null +++ b/utils.h @@ -0,0 +1,21 @@ +#ifndef RED_UTILS_H +#define RED_UTILS_H +#include QMK_KEYBOARD_H +#include "quantum.h" +#include "red_crypto.h" + +void red_print_chars(uint8_t *c); + +void red_print_int(uint8_t *c); + +void red_print_hex(uint8_t *h); + +size_t red_min_len(size_t a, size_t b); + +void red_send_chars_pass(uint8_t *out); + +void red_write_to_storage(void); + +void red_read_from_storage(void); + +#endif \ No newline at end of file