From 808299199336490ffbdc2cc975a7e8331fc0b693 Mon Sep 17 00:00:00 2001 From: redlineo Date: Tue, 1 Apr 2025 14:12:24 +0000 Subject: [PATCH] done adding passwords, some refactor --- kc_to_ascii.c | 154 +++++++++++++++++--------------------------------- kc_to_ascii.h | 1 + red_crypto.c | 120 +++++++++++++-------------------------- red_crypto.h | 71 +++++++++++++---------- red_menu.c | 9 ++- utils.c | 49 +++++++++++++++- utils.h | 8 ++- 7 files changed, 194 insertions(+), 218 deletions(-) diff --git a/kc_to_ascii.c b/kc_to_ascii.c index 51d44ec..ed54913 100644 --- a/kc_to_ascii.c +++ b/kc_to_ascii.c @@ -4,7 +4,7 @@ uint8_t shift_tracker = 0; -void write_keycode(uint8_t up_key, uint8_t low_key, keyrecord_t *record, uint8_t readed_key[], uint8_t *count_char_key) { +uint8_t write_keycode(uint8_t up_key, uint8_t low_key, keyrecord_t *record, uint8_t readed_key[], uint8_t *count_char_key) { if (record->event.pressed) { if (shift_tracker == 1) { // shift_tracker = 0; @@ -17,6 +17,7 @@ void write_keycode(uint8_t up_key, uint8_t low_key, keyrecord_t *record, uint8_t #endif *count_char_key = *count_char_key + 1; } + return RED_KEY_READ; }; uint8_t kc_to_ascii(uint16_t keycode, keyrecord_t *record, uint8_t readed_key[], uint8_t *count_char_key) { @@ -28,208 +29,159 @@ uint8_t kc_to_ascii(uint16_t keycode, keyrecord_t *record, uint8_t readed_key[], else { shift_tracker = 0; } - return 1; + return RED_KEY_READ; break; case KC_A: - write_keycode(ASCII_UP_A, ASCII_LOW_A, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_A, ASCII_LOW_A, record, readed_key, count_char_key); break; case KC_B: - write_keycode(ASCII_UP_B, ASCII_LOW_B, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_B, ASCII_LOW_B, record, readed_key, count_char_key); break; case KC_C: - write_keycode(ASCII_UP_C, ASCII_LOW_C, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_C, ASCII_LOW_C, record, readed_key, count_char_key); break; case KC_D: - write_keycode(ASCII_UP_D, ASCII_LOW_D, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_D, ASCII_LOW_D, record, readed_key, count_char_key); break; case KC_E: - write_keycode(ASCII_UP_E, ASCII_LOW_E, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_E, ASCII_LOW_E, record, readed_key, count_char_key); break; case KC_F: - write_keycode(ASCII_UP_F, ASCII_LOW_F, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_F, ASCII_LOW_F, record, readed_key, count_char_key); break; case KC_G: - write_keycode(ASCII_UP_G, ASCII_LOW_G, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_G, ASCII_LOW_G, record, readed_key, count_char_key); break; case KC_H: - write_keycode(ASCII_UP_H, ASCII_LOW_H, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_H, ASCII_LOW_H, record, readed_key, count_char_key); break; case KC_I: - write_keycode(ASCII_UP_I, ASCII_LOW_I, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_I, ASCII_LOW_I, record, readed_key, count_char_key); break; case KC_J: - write_keycode(ASCII_UP_J, ASCII_LOW_J, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_J, ASCII_LOW_J, record, readed_key, count_char_key); break; case KC_K: - write_keycode(ASCII_UP_K, ASCII_LOW_K, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_K, ASCII_LOW_K, record, readed_key, count_char_key); break; case KC_L: - write_keycode(ASCII_UP_L, ASCII_LOW_L, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_L, ASCII_LOW_L, record, readed_key, count_char_key); break; case KC_M: - write_keycode(ASCII_UP_M, ASCII_LOW_M, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_M, ASCII_LOW_M, record, readed_key, count_char_key); break; case KC_N: - write_keycode(ASCII_UP_N, ASCII_LOW_N, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_N, ASCII_LOW_N, record, readed_key, count_char_key); break; case KC_O: - write_keycode(ASCII_UP_O, ASCII_LOW_O, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_O, ASCII_LOW_O, record, readed_key, count_char_key); break; case KC_P: - write_keycode(ASCII_UP_P, ASCII_LOW_P, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_P, ASCII_LOW_P, record, readed_key, count_char_key); break; case KC_Q: - write_keycode(ASCII_UP_Q, ASCII_LOW_Q, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_Q, ASCII_LOW_Q, record, readed_key, count_char_key); break; case KC_R: - write_keycode(ASCII_UP_R, ASCII_LOW_R, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_R, ASCII_LOW_R, record, readed_key, count_char_key); break; case KC_S: - write_keycode(ASCII_UP_S, ASCII_LOW_S, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_S, ASCII_LOW_S, record, readed_key, count_char_key); break; case KC_T: - write_keycode(ASCII_UP_T, ASCII_LOW_T, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_T, ASCII_LOW_T, record, readed_key, count_char_key); break; case KC_U: - write_keycode(ASCII_UP_U, ASCII_LOW_U, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_U, ASCII_LOW_U, record, readed_key, count_char_key); break; case KC_V: - write_keycode(ASCII_UP_V, ASCII_LOW_V, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_V, ASCII_LOW_V, record, readed_key, count_char_key); break; case KC_W: - write_keycode(ASCII_UP_W, ASCII_LOW_W, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_W, ASCII_LOW_W, record, readed_key, count_char_key); break; case KC_X: - write_keycode(ASCII_UP_X, ASCII_LOW_X, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_X, ASCII_LOW_X, record, readed_key, count_char_key); break; case KC_Y: - write_keycode(ASCII_UP_Y, ASCII_LOW_Y, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_Y, ASCII_LOW_Y, record, readed_key, count_char_key); break; case KC_Z: - write_keycode(ASCII_UP_Z, ASCII_LOW_Z, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_Z, ASCII_LOW_Z, record, readed_key, count_char_key); break; case KC_1: - write_keycode(ASCII_UP_1, ASCII_LOW_1, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_1, ASCII_LOW_1, record, readed_key, count_char_key); break; case KC_2: - write_keycode(ASCII_UP_2, ASCII_LOW_2, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_2, ASCII_LOW_2, record, readed_key, count_char_key); break; case KC_3: - write_keycode(ASCII_UP_3, ASCII_LOW_3, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_3, ASCII_LOW_3, record, readed_key, count_char_key); break; case KC_4: - write_keycode(ASCII_UP_4, ASCII_LOW_4, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_4, ASCII_LOW_4, record, readed_key, count_char_key); break; case KC_5: - write_keycode(ASCII_UP_5, ASCII_LOW_5, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_5, ASCII_LOW_5, record, readed_key, count_char_key); break; case KC_6: - write_keycode(ASCII_UP_6, ASCII_LOW_6, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_6, ASCII_LOW_6, record, readed_key, count_char_key); break; case KC_7: - write_keycode(ASCII_UP_7, ASCII_LOW_7, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_7, ASCII_LOW_7, record, readed_key, count_char_key); break; case KC_8: - write_keycode(ASCII_UP_8, ASCII_LOW_8, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_8, ASCII_LOW_8, record, readed_key, count_char_key); break; case KC_9: - write_keycode(ASCII_UP_9, ASCII_LOW_9, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_9, ASCII_LOW_9, record, readed_key, count_char_key); break; case KC_0: - write_keycode(ASCII_UP_0, ASCII_LOW_0, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_0, ASCII_LOW_0, record, readed_key, count_char_key); break; case KC_SPACE: - write_keycode(ASCII_SPACE, ASCII_SPACE, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_SPACE, ASCII_SPACE, record, readed_key, count_char_key); break; case KC_MINUS: - write_keycode(ASCII_UP_MINUS, ASCII_LOW_MINUS, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_MINUS, ASCII_LOW_MINUS, record, readed_key, count_char_key); break; case KC_EQUAL: - write_keycode(ASCII_UP_EQUAL, ASCII_LOW_EQUAL, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_EQUAL, ASCII_LOW_EQUAL, record, readed_key, count_char_key); break; case KC_LEFT_BRACKET: - write_keycode(ASCII_UP_LEFT_BR, ASCII_LOW_LEFT_BR, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_LEFT_BR, ASCII_LOW_LEFT_BR, record, readed_key, count_char_key); break; case KC_RIGHT_BRACKET: - write_keycode(ASCII_UP_RIGHT_BR, ASCII_LOW_RIGHT_BR, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_RIGHT_BR, ASCII_LOW_RIGHT_BR, record, readed_key, count_char_key); break; case KC_BACKSLASH: - write_keycode(ASCII_UP_BACKSLASH, ASCII_LOW_BACKSLASH, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_BACKSLASH, ASCII_LOW_BACKSLASH, record, readed_key, count_char_key); break; case KC_NONUS_HASH: - write_keycode(ASCII_UP_HASH, ASCII_GRAVE, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_HASH, ASCII_GRAVE, record, readed_key, count_char_key); break; case KC_SEMICOLON: - write_keycode(ASCII_UP_SEMICOLON, ASCII_LOW_SEMICOLON, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_SEMICOLON, ASCII_LOW_SEMICOLON, record, readed_key, count_char_key); break; case KC_QUOTE: - write_keycode(ASCII_UP_QUOTE, ASCII_LOW_QUOTE, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_QUOTE, ASCII_LOW_QUOTE, record, readed_key, count_char_key); break; // i am not sure about KC_GRAVE and KC_NONUS_HASH // need to test case KC_GRAVE: - write_keycode(ASCII_NON_PRINTABLE, ASCII_GRAVE, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_NON_PRINTABLE, ASCII_GRAVE, record, readed_key, count_char_key); break; case KC_COMMA: - write_keycode(ASCII_UP_COMMA, ASCII_LOW_COMMA, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_COMMA, ASCII_LOW_COMMA, record, readed_key, count_char_key); break; case KC_DOT: - write_keycode(ASCII_UP_DOT, ASCII_LOW_DOT, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_DOT, ASCII_LOW_DOT, record, readed_key, count_char_key); break; case KC_SLASH: - write_keycode(ASCII_UP_SLASH, ASCII_LOW_SLASH, record, readed_key, count_char_key); - return 1; + return write_keycode(ASCII_UP_SLASH, ASCII_LOW_SLASH, record, readed_key, count_char_key); break; default: break; } - return 0; + return RED_IDLE; } \ No newline at end of file diff --git a/kc_to_ascii.h b/kc_to_ascii.h index 6fb1c79..10ab74a 100644 --- a/kc_to_ascii.h +++ b/kc_to_ascii.h @@ -2,6 +2,7 @@ #include QMK_KEYBOARD_H #include "quantum.h" +#include "red_crypto.h" #ifndef KC_TO_ASCII_H # define KC_TO_ASCII_H diff --git a/red_crypto.c b/red_crypto.c index 9994dd2..ca10b2a 100644 --- a/red_crypto.c +++ b/red_crypto.c @@ -2,7 +2,17 @@ #include "red_crypto.h" -uint8_t eeprom_inited = 0; +uint8_t eeprom_inited = 0; +uint8_t *master_key; // you can use key any length with SHA256 by defining MASTER_KEY_LEN, kuznechik use only 32byte +uint8_t **decrypted_passwords; +uint8_t *new_pass_enc; +#ifdef USE_SHA256_KEY +uint8_t master_key_hash[32]; +#endif +uint8_t count_char_key = 0; +uint8_t crypto_mode = 0; +uint8_t decrypted_mode = 0; +uint8_t red_menu_mode = 0; void crypto_eeprom_init() { uint8_t red_var[3] = {'R', 'E', 'D'}; @@ -29,8 +39,8 @@ void crypto_eeprom_init() { enc_pass.init_var[1] = 'E'; enc_pass.init_var[2] = 'D'; enc_pass.version[0] = '0'; - enc_pass.version[1] = '6'; - enc_pass.version[2] = '9'; + enc_pass.version[1] = '7'; + enc_pass.version[2] = '1'; enc_pass.memory_usage = 0; enc_pass.password_count = 0; enc_pass.storage_size = INIT_STORAGE_SIZE; @@ -45,48 +55,11 @@ void crypto_eeprom_init() { } } -uint8_t **decrypted_passwords; - -// initialize array "decrypted_passwords" with 0x00 -void init_dec_pass(void) { - decrypted_passwords = (uint8_t **)malloc(sizeof(uint8_t *) * enc_pass.storage_size); - for (uint8_t i = 0; i < enc_pass.storage_size; i++) { - decrypted_passwords[i] = (uint8_t *)malloc(sizeof(uint8_t) * enc_pass.storage_pass_len); - } - - for (uint8_t i = 0; i < enc_pass.storage_size; i++) { - for (uint8_t j = 0; j < enc_pass.storage_pass_len; j++) { - decrypted_passwords[i][j] = 0x00; - } - } -} - -// you can use key any length with SHA256, kuznechik use only 32byte -#ifdef USE_SHA256_KEY -uint8_t master_key[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}; -#endif -#ifndef USE_SHA256_KEY -uint8_t master_key[32] = {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}; -#endif - -uint8_t count_char_key = 0; - -uint8_t crypto_mode = 0; - -uint8_t decrypted_mode = 0; - -uint8_t red_menu_mode = 0; - // encrypt new password uint8_t *encrypt_pass_kuzn(const uint8_t *new_pass, const uint8_t new_pass_length) { kuz_key_t key; w128_t x; - uint8_t *new_pass_enc; - new_pass_enc = (uint8_t *)malloc(sizeof(uint8_t) * enc_pass.storage_pass_len); - // assign zeros to encrypted new password var - for (uint8_t byte_index = 0; byte_index < enc_pass.storage_pass_len; byte_index++) { - new_pass_enc = 0x00; - } + new_pass_enc = red_init_array(enc_pass.storage_pass_len, 0x00); // assign zeros to encrypted new password var // copy new password in decrypted_passwords for (uint8_t byte_index = 0; byte_index < new_pass_length; byte_index++) { decrypted_passwords[enc_pass.password_count][byte_index] = new_pass[byte_index]; @@ -94,19 +67,13 @@ uint8_t *encrypt_pass_kuzn(const uint8_t *new_pass, const uint8_t new_pass_lengt kuz_init(); -// using master_key for encrypting + // using master_key for encrypting #ifdef USE_SHA256_KEY - SHA256_CTX ctx; - sha256_init(&ctx); - sha256_update(&ctx, master_key, red_min_len(u_strlen(master_key), MAX_KEY_LEN)); - BYTE result_key[SHA256_BLOCK_SIZE]; - sha256_final(&ctx, result_key); - red_print_hex(result_key); - kuz_set_decrypt_key(&key, result_key); + kuz_set_encrypt_key(&key, hashing_string(master_key)); #endif // if you don't have enough FLASH in MCU #ifndef USE_SHA256_KEY - kuz_set_decrypt_key(&key, master_key); + kuz_set_encrypt_key(&key, master_key); #endif #ifdef USE_RED_CRY_DEBUG @@ -137,23 +104,18 @@ void decrypt_pass_kuzn(void) { kuz_key_t key; w128_t x; - init_dec_pass(); // clear decrypted_passwords array + decrypted_passwords = red_init_array_2d(enc_pass.storage_size, enc_pass.storage_pass_len, 0x00); // clear decrypted passwords kuz_init(); // for hashing key, like a KDF algorithm #ifdef USE_SHA256_KEY - SHA256_CTX ctx; - sha256_init(&ctx); - sha256_update(&ctx, master_key, red_min_len(u_strlen(master_key), MAX_KEY_LEN)); - BYTE result_key[SHA256_BLOCK_SIZE]; - sha256_final(&ctx, result_key); - red_print_hex(result_key); - kuz_set_decrypt_key(&key, result_key); + kuz_set_decrypt_key(&key, hashing_string(master_key)); #endif // if you don't have enough FLASH in MCU #ifndef USE_SHA256_KEY kuz_set_decrypt_key(&key, master_key); #endif + // decrypting all passwords in storage for (uint8_t password_index = 0; password_index < enc_pass.storage_size; password_index++) { #ifdef USE_RED_CRY_DEBUG @@ -199,7 +161,7 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { case RED_CRY_M: if (record->event.pressed) { #ifdef USE_RED_CRY_DEBUG - red_print_int(master_key); + red_print_int(master_key, MASTER_KEY_LEN); #endif crypto_mode = 0; count_char_key = 0; @@ -214,7 +176,7 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { } } else if (decrypted_mode && red_menu_mode) { uint8_t key_return = draw_red_menu(keycode, record); - if (key_return == 2) { + if (key_return == RED_MENU_EXIT) { red_menu_mode = 0; } else { return key_return; @@ -223,17 +185,15 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { switch (keycode) { case RED_RST_EE: if (record->event.pressed) { - enc_pass.init_var[0]=0x00; + enc_pass.init_var[0] = 0x00; crypto_eeprom_init(); } break; case RED_CRY_M: if (record->event.pressed) { - decrypted_mode = 0; - init_dec_pass(); //clear RAM - for (uint8_t i = 0; i < 32; i++) { - master_key[i] = 0; - } + decrypted_mode = 0; + decrypted_passwords = red_init_array_2d(enc_pass.storage_size, enc_pass.storage_pass_len, 0x00); // clear decrypted passwords + master_key = red_init_array(MASTER_KEY_LEN, 0); } break; case RED_TEST: @@ -259,52 +219,52 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { } break; case RED_PASS1: - if (record->event.pressed && enc_pass.password_count <= 1) { + if (record->event.pressed && enc_pass.password_count >= 1) { red_send_chars_pass(decrypted_passwords[0]); } break; case RED_PASS2: - if (record->event.pressed && enc_pass.password_count <= 2) { + if (record->event.pressed && enc_pass.password_count >= 2) { red_send_chars_pass(decrypted_passwords[1]); } break; case RED_PASS3: - if (record->event.pressed && enc_pass.password_count <= 3) { + if (record->event.pressed && enc_pass.password_count >= 3) { red_send_chars_pass(decrypted_passwords[2]); } break; case RED_PASS4: - if (record->event.pressed && enc_pass.password_count <= 4) { + if (record->event.pressed && enc_pass.password_count >= 4) { red_send_chars_pass(decrypted_passwords[3]); } break; case RED_PASS5: - if (record->event.pressed && enc_pass.password_count <= 5) { + if (record->event.pressed && enc_pass.password_count >= 5) { red_send_chars_pass(decrypted_passwords[4]); } break; case RED_PASS6: - if (record->event.pressed && enc_pass.password_count <= 6) { + if (record->event.pressed && enc_pass.password_count >= 6) { red_send_chars_pass(decrypted_passwords[5]); } break; case RED_PASS7: - if (record->event.pressed && enc_pass.password_count <= 7) { + if (record->event.pressed && enc_pass.password_count >= 7) { red_send_chars_pass(decrypted_passwords[6]); } break; case RED_PASS8: - if (record->event.pressed && enc_pass.password_count <= 8) { + if (record->event.pressed && enc_pass.password_count >= 8) { red_send_chars_pass(decrypted_passwords[7]); } break; case RED_PASS9: - if (record->event.pressed && enc_pass.password_count <= 9) { + if (record->event.pressed && enc_pass.password_count >= 9) { red_send_chars_pass(decrypted_passwords[8]); } break; case RED_PASS10: - if (record->event.pressed && enc_pass.password_count == 10) { + if (record->event.pressed && enc_pass.password_count >= 10) { red_send_chars_pass(decrypted_passwords[9]); } break; @@ -321,15 +281,13 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { case RED_CRY_M: if (record->event.pressed) { crypto_eeprom_init(); - crypto_mode = 1; // to enter in crypto mode block code above + crypto_mode = 1; // to enter in crypto mode and unlock other keys // clearing readed key - for (uint8_t i = 0; i < 32; i++) { - master_key[i] = 0; - } + master_key = red_init_array(MASTER_KEY_LEN, 0); // dprintf("%d", keycode); } break; } } - return 0; + return RED_IDLE; } diff --git a/red_crypto.h b/red_crypto.h index 3f7ff27..8af0303 100644 --- a/red_crypto.h +++ b/red_crypto.h @@ -1,15 +1,15 @@ // Author: redlineo, romanov.pd@gmail.com #ifndef RED_CRYPTO_H -# define RED_CRYPTO_H +#define RED_CRYPTO_H #include QMK_KEYBOARD_H #include "quantum.h" // includes of red_crypto -# include "kc_to_ascii.h" -# include "utils.h" -# include "red_menu.h" +#include "kc_to_ascii.h" +#include "utils.h" +#include "red_menu.h" // includes for EEPROM, tested on STM32L432xx #ifdef STM32L432xx # include "eeprom.h" @@ -17,37 +17,42 @@ # include "eeprom_stm32_l4.h" #endif +// not tested!!! // #ifdef USE_RED_KUZNECHIK_128 // #include "kuznechik_128bit.h" // #endif -# ifdef USE_RED_KUZNECHIK_8 -# include "kuznechik_8bit.h" -# ifdef USE_SHA256_KEY -# include "sha256.h" -# endif -# endif +#define MASTER_KEY_LEN 32 -# ifdef USE_RED_AES_256 -# include "aes.h" +#ifdef USE_RED_KUZNECHIK_8 +# include "kuznechik_8bit.h" +# ifdef USE_SHA256_KEY +# undef MASTER_KEY_LEN +# define MASTER_KEY_LEN 128 +# include "sha256.h" # endif +#endif -# define MAX_KEY_LEN 128 -# define BLOCK_SIZE 16 +#ifdef USE_RED_AES_256 +# include "aes.h" +#endif -# ifndef INIT_STORAGE_SIZE -# define INIT_STORAGE_SIZE 5 -# endif - -# ifndef INIT_STORAGE_PASS_LEN -# define INIT_STORAGE_PASS_LEN 64 -# endif - -# ifdef INIT_STORAGE_PASS_LEN -# if (INIT_STORAGE_PASS_LEN % 16 != 0) -# error "Password length must be multiply of 16 (16,32,64...)" -# endif +#define MAX_KEY_LEN 128 +#define BLOCK_SIZE 16 + +#ifndef INIT_STORAGE_SIZE +# define INIT_STORAGE_SIZE 5 +#endif + +#ifndef INIT_STORAGE_PASS_LEN +# define INIT_STORAGE_PASS_LEN 64 +#endif + +#ifdef INIT_STORAGE_PASS_LEN +# if (INIT_STORAGE_PASS_LEN % 16 != 0) +# error "Password length must be multiply of 16 (16,32,64...)" # endif +#endif // example for encrypted 64 byte password // const uint8_t red_enc_pass[STORAGE_SIZE][STORAGE_PASS_LEN] = { @@ -62,7 +67,7 @@ enum red_crypto_keys { RED_MENU, RED_RNG, RED_TEST, - RED_LOCK, // TODO: add autolock feature + RED_LOCK, // TODO: add autolock feature RED_RST_EE, // resetting EEPROM by clearing RED bytes RED_PASS1, RED_PASS2, @@ -73,13 +78,19 @@ enum red_crypto_keys { RED_PASS7, RED_PASS8, RED_PASS9, - RED_PASS10 + RED_PASS10, +}; + +enum red_crypto_states { + RED_IDLE = 0, + RED_KEY_READ, + RED_MENU_EXIT, }; // uint8_t storage_size = INIT_STORAGE_SIZE; // uint8_t storage_pass_len = INIT_STORAGE_PASS_LEN; -# define EEPROM_PASS_OFFSET 3 + 3 + 1 + 1 + 4 + 1 -# define STORAGE_SIZE (EEPROM_PASS_OFFSET + INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN) +#define EEPROM_PASS_OFFSET 3 + 3 + 1 + 1 + 4 + 1 +#define STORAGE_SIZE (EEPROM_PASS_OFFSET + INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN) typedef union { uint8_t raw[STORAGE_SIZE]; diff --git a/red_menu.c b/red_menu.c index 4251420..e66561f 100644 --- a/red_menu.c +++ b/red_menu.c @@ -21,8 +21,8 @@ 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; +uint8_t *readed_key_new; +uint8_t count_char_key_new = 0; // read new key uint8_t read_new_key(uint16_t keycode, keyrecord_t *record) { @@ -44,6 +44,7 @@ uint8_t read_new_key(uint16_t keycode, keyrecord_t *record) { } // clang-format on +// return values // 0 idle // 1 get key input // 2 exit menu @@ -75,7 +76,9 @@ uint8_t draw_red_menu(uint16_t keycode, keyrecord_t *record) { readed_option[0] = CHANGE_PASS_NAME; } if (readed_option[0] == ASCII_LOW_6) { - readed_option[0] = ADD_NEW_PASS; + readed_option[0] = ADD_NEW_PASS; + readed_key_new = red_init_array(MAX_KEY_LEN, 0x00); + count_char_key_new = 0; } 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)); diff --git a/utils.c b/utils.c index ae8a87e..d939a77 100644 --- a/utils.c +++ b/utils.c @@ -8,8 +8,8 @@ void red_print_chars(uint8_t *c) { dprintf("\n"); }; -void red_print_int(uint8_t *c) { - for (uint8_t i = 0; i < 32; i++) { +void red_print_int(uint8_t *c, uint8_t len) { + for (uint8_t i = 0; i < len; i++) { dprintf("%d", c[i]); } dprintf("\n"); @@ -35,6 +35,51 @@ void red_send_chars_pass(uint8_t *out) { } }; +uint8_t *red_init_array(uint8_t len, uint8_t val) { + uint8_t *arr = (uint8_t *)malloc(sizeof(uint8_t) * len); + for (uint8_t i = 0; i < len; i++) { + arr[i] = val; + } + return arr; +} + +uint8_t **red_init_array_2d(uint8_t len_row, uint8_t len_col, uint8_t val) { + uint8_t **arr = (uint8_t **)malloc(sizeof(uint8_t *) * len_row); + for (uint8_t i = 0; i < len_row; i++) { + arr[i] = (uint8_t *)malloc(sizeof(uint8_t) * len_col); + for (uint8_t j = 0; j < len_col; j++) { + arr[i][j] = val; + } + } + return arr; +} + +// count printable symbols of ASCII in string with safety check of max length +uint8_t red_strlen_printable(uint8_t *str_printable, uint8_t max_len) { + uint8_t result = 0; + while (result < max_len && *str_printable != '\0') { + result++; + str_printable++; + } + return result; +} + +#ifdef USE_SHA256_KEY +// hashing string to 32byte with sha256 +uint8_t *hashing_string(uint8_t *str_to_hash) { + SHA256_CTX ctx; + sha256_init(&ctx); + sha256_update(&ctx, str_to_hash, red_strlen_printable(str_to_hash, MAX_KEY_LEN)); + // BYTE result_key[SHA256_BLOCK_SIZE]; + BYTE *result_key = red_init_array(SHA256_BLOCK_SIZE, 0x00); + sha256_final(&ctx, result_key); +# ifdef USE_RED_CRY_DEBUG + red_print_hex(result_key); +# endif + return result_key; +} +#endif + // EEPROM utils void red_write_to_storage(void) { diff --git a/utils.h b/utils.h index 30abf10..adc72a8 100644 --- a/utils.h +++ b/utils.h @@ -6,7 +6,7 @@ void red_print_chars(uint8_t *c); -void red_print_int(uint8_t *c); +void red_print_int(uint8_t *c, uint8_t len); void red_print_hex(uint8_t *h); @@ -14,6 +14,12 @@ size_t red_min_len(size_t a, size_t b); void red_send_chars_pass(uint8_t *out); +uint8_t *hashing_string(uint8_t *str_to_hash); + +uint8_t *red_init_array(uint8_t len, uint8_t val); + +uint8_t **red_init_array_2d(uint8_t len_row, uint8_t len_col, uint8_t val); + void red_write_to_storage(void); void red_read_from_storage(void);