done adding passwords, some refactor

This commit is contained in:
redlineo committed 2025-04-01 14:12:24 +00:00
1 parent 9ec31d0b52
commit 8082991993
7 files changed
+194 -218

No files matched your search

+53 -101
View File
@@ -4,7 +4,7 @@
uint8_t shift_tracker = 0; 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 (record->event.pressed) {
if (shift_tracker == 1) { if (shift_tracker == 1) {
// shift_tracker = 0; // shift_tracker = 0;
@@ -17,6 +17,7 @@ void write_keycode(uint8_t up_key, uint8_t low_key, keyrecord_t *record, uint8_t
#endif #endif
*count_char_key = *count_char_key + 1; *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) { 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 { else {
shift_tracker = 0; shift_tracker = 0;
} }
return 1; return RED_KEY_READ;
break; break;
case KC_A: case KC_A:
write_keycode(ASCII_UP_A, ASCII_LOW_A, record, readed_key, count_char_key); return write_keycode(ASCII_UP_A, ASCII_LOW_A, record, readed_key, count_char_key);
return 1;
break; break;
case KC_B: case KC_B:
write_keycode(ASCII_UP_B, ASCII_LOW_B, record, readed_key, count_char_key); return write_keycode(ASCII_UP_B, ASCII_LOW_B, record, readed_key, count_char_key);
return 1;
break; break;
case KC_C: case KC_C:
write_keycode(ASCII_UP_C, ASCII_LOW_C, record, readed_key, count_char_key); return write_keycode(ASCII_UP_C, ASCII_LOW_C, record, readed_key, count_char_key);
return 1;
break; break;
case KC_D: case KC_D:
write_keycode(ASCII_UP_D, ASCII_LOW_D, record, readed_key, count_char_key); return write_keycode(ASCII_UP_D, ASCII_LOW_D, record, readed_key, count_char_key);
return 1;
break; break;
case KC_E: case KC_E:
write_keycode(ASCII_UP_E, ASCII_LOW_E, record, readed_key, count_char_key); return write_keycode(ASCII_UP_E, ASCII_LOW_E, record, readed_key, count_char_key);
return 1;
break; break;
case KC_F: case KC_F:
write_keycode(ASCII_UP_F, ASCII_LOW_F, record, readed_key, count_char_key); return write_keycode(ASCII_UP_F, ASCII_LOW_F, record, readed_key, count_char_key);
return 1;
break; break;
case KC_G: case KC_G:
write_keycode(ASCII_UP_G, ASCII_LOW_G, record, readed_key, count_char_key); return write_keycode(ASCII_UP_G, ASCII_LOW_G, record, readed_key, count_char_key);
return 1;
break; break;
case KC_H: case KC_H:
write_keycode(ASCII_UP_H, ASCII_LOW_H, record, readed_key, count_char_key); return write_keycode(ASCII_UP_H, ASCII_LOW_H, record, readed_key, count_char_key);
return 1;
break; break;
case KC_I: case KC_I:
write_keycode(ASCII_UP_I, ASCII_LOW_I, record, readed_key, count_char_key); return write_keycode(ASCII_UP_I, ASCII_LOW_I, record, readed_key, count_char_key);
return 1;
break; break;
case KC_J: case KC_J:
write_keycode(ASCII_UP_J, ASCII_LOW_J, record, readed_key, count_char_key); return write_keycode(ASCII_UP_J, ASCII_LOW_J, record, readed_key, count_char_key);
return 1;
break; break;
case KC_K: case KC_K:
write_keycode(ASCII_UP_K, ASCII_LOW_K, record, readed_key, count_char_key); return write_keycode(ASCII_UP_K, ASCII_LOW_K, record, readed_key, count_char_key);
return 1;
break; break;
case KC_L: case KC_L:
write_keycode(ASCII_UP_L, ASCII_LOW_L, record, readed_key, count_char_key); return write_keycode(ASCII_UP_L, ASCII_LOW_L, record, readed_key, count_char_key);
return 1;
break; break;
case KC_M: case KC_M:
write_keycode(ASCII_UP_M, ASCII_LOW_M, record, readed_key, count_char_key); return write_keycode(ASCII_UP_M, ASCII_LOW_M, record, readed_key, count_char_key);
return 1;
break; break;
case KC_N: case KC_N:
write_keycode(ASCII_UP_N, ASCII_LOW_N, record, readed_key, count_char_key); return write_keycode(ASCII_UP_N, ASCII_LOW_N, record, readed_key, count_char_key);
return 1;
break; break;
case KC_O: case KC_O:
write_keycode(ASCII_UP_O, ASCII_LOW_O, record, readed_key, count_char_key); return write_keycode(ASCII_UP_O, ASCII_LOW_O, record, readed_key, count_char_key);
return 1;
break; break;
case KC_P: case KC_P:
write_keycode(ASCII_UP_P, ASCII_LOW_P, record, readed_key, count_char_key); return write_keycode(ASCII_UP_P, ASCII_LOW_P, record, readed_key, count_char_key);
return 1;
break; break;
case KC_Q: case KC_Q:
write_keycode(ASCII_UP_Q, ASCII_LOW_Q, record, readed_key, count_char_key); return write_keycode(ASCII_UP_Q, ASCII_LOW_Q, record, readed_key, count_char_key);
return 1;
break; break;
case KC_R: case KC_R:
write_keycode(ASCII_UP_R, ASCII_LOW_R, record, readed_key, count_char_key); return write_keycode(ASCII_UP_R, ASCII_LOW_R, record, readed_key, count_char_key);
return 1;
break; break;
case KC_S: case KC_S:
write_keycode(ASCII_UP_S, ASCII_LOW_S, record, readed_key, count_char_key); return write_keycode(ASCII_UP_S, ASCII_LOW_S, record, readed_key, count_char_key);
return 1;
break; break;
case KC_T: case KC_T:
write_keycode(ASCII_UP_T, ASCII_LOW_T, record, readed_key, count_char_key); return write_keycode(ASCII_UP_T, ASCII_LOW_T, record, readed_key, count_char_key);
return 1;
break; break;
case KC_U: case KC_U:
write_keycode(ASCII_UP_U, ASCII_LOW_U, record, readed_key, count_char_key); return write_keycode(ASCII_UP_U, ASCII_LOW_U, record, readed_key, count_char_key);
return 1;
break; break;
case KC_V: case KC_V:
write_keycode(ASCII_UP_V, ASCII_LOW_V, record, readed_key, count_char_key); return write_keycode(ASCII_UP_V, ASCII_LOW_V, record, readed_key, count_char_key);
return 1;
break; break;
case KC_W: case KC_W:
write_keycode(ASCII_UP_W, ASCII_LOW_W, record, readed_key, count_char_key); return write_keycode(ASCII_UP_W, ASCII_LOW_W, record, readed_key, count_char_key);
return 1;
break; break;
case KC_X: case KC_X:
write_keycode(ASCII_UP_X, ASCII_LOW_X, record, readed_key, count_char_key); return write_keycode(ASCII_UP_X, ASCII_LOW_X, record, readed_key, count_char_key);
return 1;
break; break;
case KC_Y: case KC_Y:
write_keycode(ASCII_UP_Y, ASCII_LOW_Y, record, readed_key, count_char_key); return write_keycode(ASCII_UP_Y, ASCII_LOW_Y, record, readed_key, count_char_key);
return 1;
break; break;
case KC_Z: case KC_Z:
write_keycode(ASCII_UP_Z, ASCII_LOW_Z, record, readed_key, count_char_key); return write_keycode(ASCII_UP_Z, ASCII_LOW_Z, record, readed_key, count_char_key);
return 1;
break; break;
case KC_1: case KC_1:
write_keycode(ASCII_UP_1, ASCII_LOW_1, record, readed_key, count_char_key); return write_keycode(ASCII_UP_1, ASCII_LOW_1, record, readed_key, count_char_key);
return 1;
break; break;
case KC_2: case KC_2:
write_keycode(ASCII_UP_2, ASCII_LOW_2, record, readed_key, count_char_key); return write_keycode(ASCII_UP_2, ASCII_LOW_2, record, readed_key, count_char_key);
return 1;
break; break;
case KC_3: case KC_3:
write_keycode(ASCII_UP_3, ASCII_LOW_3, record, readed_key, count_char_key); return write_keycode(ASCII_UP_3, ASCII_LOW_3, record, readed_key, count_char_key);
return 1;
break; break;
case KC_4: case KC_4:
write_keycode(ASCII_UP_4, ASCII_LOW_4, record, readed_key, count_char_key); return write_keycode(ASCII_UP_4, ASCII_LOW_4, record, readed_key, count_char_key);
return 1;
break; break;
case KC_5: case KC_5:
write_keycode(ASCII_UP_5, ASCII_LOW_5, record, readed_key, count_char_key); return write_keycode(ASCII_UP_5, ASCII_LOW_5, record, readed_key, count_char_key);
return 1;
break; break;
case KC_6: case KC_6:
write_keycode(ASCII_UP_6, ASCII_LOW_6, record, readed_key, count_char_key); return write_keycode(ASCII_UP_6, ASCII_LOW_6, record, readed_key, count_char_key);
return 1;
break; break;
case KC_7: case KC_7:
write_keycode(ASCII_UP_7, ASCII_LOW_7, record, readed_key, count_char_key); return write_keycode(ASCII_UP_7, ASCII_LOW_7, record, readed_key, count_char_key);
return 1;
break; break;
case KC_8: case KC_8:
write_keycode(ASCII_UP_8, ASCII_LOW_8, record, readed_key, count_char_key); return write_keycode(ASCII_UP_8, ASCII_LOW_8, record, readed_key, count_char_key);
return 1;
break; break;
case KC_9: case KC_9:
write_keycode(ASCII_UP_9, ASCII_LOW_9, record, readed_key, count_char_key); return write_keycode(ASCII_UP_9, ASCII_LOW_9, record, readed_key, count_char_key);
return 1;
break; break;
case KC_0: case KC_0:
write_keycode(ASCII_UP_0, ASCII_LOW_0, record, readed_key, count_char_key); return write_keycode(ASCII_UP_0, ASCII_LOW_0, record, readed_key, count_char_key);
return 1;
break; break;
case KC_SPACE: case KC_SPACE:
write_keycode(ASCII_SPACE, ASCII_SPACE, record, readed_key, count_char_key); return write_keycode(ASCII_SPACE, ASCII_SPACE, record, readed_key, count_char_key);
return 1;
break; break;
case KC_MINUS: case KC_MINUS:
write_keycode(ASCII_UP_MINUS, ASCII_LOW_MINUS, record, readed_key, count_char_key); return write_keycode(ASCII_UP_MINUS, ASCII_LOW_MINUS, record, readed_key, count_char_key);
return 1;
break; break;
case KC_EQUAL: case KC_EQUAL:
write_keycode(ASCII_UP_EQUAL, ASCII_LOW_EQUAL, record, readed_key, count_char_key); return write_keycode(ASCII_UP_EQUAL, ASCII_LOW_EQUAL, record, readed_key, count_char_key);
return 1;
break; break;
case KC_LEFT_BRACKET: case KC_LEFT_BRACKET:
write_keycode(ASCII_UP_LEFT_BR, ASCII_LOW_LEFT_BR, record, readed_key, count_char_key); return write_keycode(ASCII_UP_LEFT_BR, ASCII_LOW_LEFT_BR, record, readed_key, count_char_key);
return 1;
break; break;
case KC_RIGHT_BRACKET: case KC_RIGHT_BRACKET:
write_keycode(ASCII_UP_RIGHT_BR, ASCII_LOW_RIGHT_BR, record, readed_key, count_char_key); return write_keycode(ASCII_UP_RIGHT_BR, ASCII_LOW_RIGHT_BR, record, readed_key, count_char_key);
return 1;
break; break;
case KC_BACKSLASH: case KC_BACKSLASH:
write_keycode(ASCII_UP_BACKSLASH, ASCII_LOW_BACKSLASH, record, readed_key, count_char_key); return write_keycode(ASCII_UP_BACKSLASH, ASCII_LOW_BACKSLASH, record, readed_key, count_char_key);
return 1;
break; break;
case KC_NONUS_HASH: case KC_NONUS_HASH:
write_keycode(ASCII_UP_HASH, ASCII_GRAVE, record, readed_key, count_char_key); return write_keycode(ASCII_UP_HASH, ASCII_GRAVE, record, readed_key, count_char_key);
return 1;
break; break;
case KC_SEMICOLON: case KC_SEMICOLON:
write_keycode(ASCII_UP_SEMICOLON, ASCII_LOW_SEMICOLON, record, readed_key, count_char_key); return write_keycode(ASCII_UP_SEMICOLON, ASCII_LOW_SEMICOLON, record, readed_key, count_char_key);
return 1;
break; break;
case KC_QUOTE: case KC_QUOTE:
write_keycode(ASCII_UP_QUOTE, ASCII_LOW_QUOTE, record, readed_key, count_char_key); return write_keycode(ASCII_UP_QUOTE, ASCII_LOW_QUOTE, record, readed_key, count_char_key);
return 1;
break; break;
// i am not sure about KC_GRAVE and KC_NONUS_HASH // i am not sure about KC_GRAVE and KC_NONUS_HASH
// need to test // need to test
case KC_GRAVE: case KC_GRAVE:
write_keycode(ASCII_NON_PRINTABLE, ASCII_GRAVE, record, readed_key, count_char_key); return write_keycode(ASCII_NON_PRINTABLE, ASCII_GRAVE, record, readed_key, count_char_key);
return 1;
break; break;
case KC_COMMA: case KC_COMMA:
write_keycode(ASCII_UP_COMMA, ASCII_LOW_COMMA, record, readed_key, count_char_key); return write_keycode(ASCII_UP_COMMA, ASCII_LOW_COMMA, record, readed_key, count_char_key);
return 1;
break; break;
case KC_DOT: case KC_DOT:
write_keycode(ASCII_UP_DOT, ASCII_LOW_DOT, record, readed_key, count_char_key); return write_keycode(ASCII_UP_DOT, ASCII_LOW_DOT, record, readed_key, count_char_key);
return 1;
break; break;
case KC_SLASH: case KC_SLASH:
write_keycode(ASCII_UP_SLASH, ASCII_LOW_SLASH, record, readed_key, count_char_key); return write_keycode(ASCII_UP_SLASH, ASCII_LOW_SLASH, record, readed_key, count_char_key);
return 1;
break; break;
default: default:
break; break;
} }
return 0; return RED_IDLE;
} }
+1
View File
@@ -2,6 +2,7 @@
#include QMK_KEYBOARD_H #include QMK_KEYBOARD_H
#include "quantum.h" #include "quantum.h"
#include "red_crypto.h"
#ifndef KC_TO_ASCII_H #ifndef KC_TO_ASCII_H
# define KC_TO_ASCII_H # define KC_TO_ASCII_H
+39 -81
View File
@@ -2,7 +2,17 @@
#include "red_crypto.h" #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() { void crypto_eeprom_init() {
uint8_t red_var[3] = {'R', 'E', 'D'}; 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[1] = 'E';
enc_pass.init_var[2] = 'D'; enc_pass.init_var[2] = 'D';
enc_pass.version[0] = '0'; enc_pass.version[0] = '0';
enc_pass.version[1] = '6'; enc_pass.version[1] = '7';
enc_pass.version[2] = '9'; enc_pass.version[2] = '1';
enc_pass.memory_usage = 0; enc_pass.memory_usage = 0;
enc_pass.password_count = 0; enc_pass.password_count = 0;
enc_pass.storage_size = INIT_STORAGE_SIZE; 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 // encrypt new password
uint8_t *encrypt_pass_kuzn(const uint8_t *new_pass, const uint8_t new_pass_length) { uint8_t *encrypt_pass_kuzn(const uint8_t *new_pass, const uint8_t new_pass_length) {
kuz_key_t key; kuz_key_t key;
w128_t x; w128_t x;
uint8_t *new_pass_enc; new_pass_enc = red_init_array(enc_pass.storage_pass_len, 0x00); // assign zeros to encrypted new password var
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;
}
// copy new password in decrypted_passwords // copy new password in decrypted_passwords
for (uint8_t byte_index = 0; byte_index < new_pass_length; byte_index++) { 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]; 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(); kuz_init();
// using master_key for encrypting // using master_key for encrypting
#ifdef USE_SHA256_KEY #ifdef USE_SHA256_KEY
SHA256_CTX ctx; kuz_set_encrypt_key(&key, hashing_string(master_key));
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);
#endif #endif
// if you don't have enough FLASH in MCU // if you don't have enough FLASH in MCU
#ifndef USE_SHA256_KEY #ifndef USE_SHA256_KEY
kuz_set_decrypt_key(&key, master_key); kuz_set_encrypt_key(&key, master_key);
#endif #endif
#ifdef USE_RED_CRY_DEBUG #ifdef USE_RED_CRY_DEBUG
@@ -137,23 +104,18 @@ void decrypt_pass_kuzn(void) {
kuz_key_t key; kuz_key_t key;
w128_t x; 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(); kuz_init();
// for hashing key, like a KDF algorithm // for hashing key, like a KDF algorithm
#ifdef USE_SHA256_KEY #ifdef USE_SHA256_KEY
SHA256_CTX ctx; kuz_set_decrypt_key(&key, hashing_string(master_key));
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);
#endif #endif
// if you don't have enough FLASH in MCU // if you don't have enough FLASH in MCU
#ifndef USE_SHA256_KEY #ifndef USE_SHA256_KEY
kuz_set_decrypt_key(&key, master_key); kuz_set_decrypt_key(&key, master_key);
#endif #endif
// decrypting all passwords in storage // decrypting all passwords in storage
for (uint8_t password_index = 0; password_index < enc_pass.storage_size; password_index++) { for (uint8_t password_index = 0; password_index < enc_pass.storage_size; password_index++) {
#ifdef USE_RED_CRY_DEBUG #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: case RED_CRY_M:
if (record->event.pressed) { if (record->event.pressed) {
#ifdef USE_RED_CRY_DEBUG #ifdef USE_RED_CRY_DEBUG
red_print_int(master_key); red_print_int(master_key, MASTER_KEY_LEN);
#endif #endif
crypto_mode = 0; crypto_mode = 0;
count_char_key = 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) { } else if (decrypted_mode && red_menu_mode) {
uint8_t key_return = draw_red_menu(keycode, record); uint8_t key_return = draw_red_menu(keycode, record);
if (key_return == 2) { if (key_return == RED_MENU_EXIT) {
red_menu_mode = 0; red_menu_mode = 0;
} else { } else {
return key_return; return key_return;
@@ -223,17 +185,15 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) { switch (keycode) {
case RED_RST_EE: case RED_RST_EE:
if (record->event.pressed) { if (record->event.pressed) {
enc_pass.init_var[0]=0x00; enc_pass.init_var[0] = 0x00;
crypto_eeprom_init(); crypto_eeprom_init();
} }
break; break;
case RED_CRY_M: case RED_CRY_M:
if (record->event.pressed) { if (record->event.pressed) {
decrypted_mode = 0; decrypted_mode = 0;
init_dec_pass(); //clear RAM decrypted_passwords = red_init_array_2d(enc_pass.storage_size, enc_pass.storage_pass_len, 0x00); // clear decrypted passwords
for (uint8_t i = 0; i < 32; i++) { master_key = red_init_array(MASTER_KEY_LEN, 0);
master_key[i] = 0;
}
} }
break; break;
case RED_TEST: case RED_TEST:
@@ -259,52 +219,52 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) {
} }
break; break;
case RED_PASS1: 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]); red_send_chars_pass(decrypted_passwords[0]);
} }
break; break;
case RED_PASS2: 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]); red_send_chars_pass(decrypted_passwords[1]);
} }
break; break;
case RED_PASS3: 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]); red_send_chars_pass(decrypted_passwords[2]);
} }
break; break;
case RED_PASS4: 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]); red_send_chars_pass(decrypted_passwords[3]);
} }
break; break;
case RED_PASS5: 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]); red_send_chars_pass(decrypted_passwords[4]);
} }
break; break;
case RED_PASS6: 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]); red_send_chars_pass(decrypted_passwords[5]);
} }
break; break;
case RED_PASS7: 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]); red_send_chars_pass(decrypted_passwords[6]);
} }
break; break;
case RED_PASS8: 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]); red_send_chars_pass(decrypted_passwords[7]);
} }
break; break;
case RED_PASS9: 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]); red_send_chars_pass(decrypted_passwords[8]);
} }
break; break;
case RED_PASS10: 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]); red_send_chars_pass(decrypted_passwords[9]);
} }
break; break;
@@ -321,15 +281,13 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) {
case RED_CRY_M: case RED_CRY_M:
if (record->event.pressed) { if (record->event.pressed) {
crypto_eeprom_init(); 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 // clearing readed key
for (uint8_t i = 0; i < 32; i++) { master_key = red_init_array(MASTER_KEY_LEN, 0);
master_key[i] = 0;
}
// dprintf("%d", keycode); // dprintf("%d", keycode);
} }
break; break;
} }
} }
return 0; return RED_IDLE;
} }
+41 -30
View File
@@ -1,15 +1,15 @@
// Author: redlineo, romanov.pd@gmail.com // Author: redlineo, romanov.pd@gmail.com
#ifndef RED_CRYPTO_H #ifndef RED_CRYPTO_H
# define RED_CRYPTO_H #define RED_CRYPTO_H
#include QMK_KEYBOARD_H #include QMK_KEYBOARD_H
#include "quantum.h" #include "quantum.h"
// includes of red_crypto // includes of red_crypto
# include "kc_to_ascii.h" #include "kc_to_ascii.h"
# include "utils.h" #include "utils.h"
# include "red_menu.h" #include "red_menu.h"
// includes for EEPROM, tested on STM32L432xx // includes for EEPROM, tested on STM32L432xx
#ifdef STM32L432xx #ifdef STM32L432xx
# include "eeprom.h" # include "eeprom.h"
@@ -17,37 +17,42 @@
# include "eeprom_stm32_l4.h" # include "eeprom_stm32_l4.h"
#endif #endif
// not tested!!!
// #ifdef USE_RED_KUZNECHIK_128 // #ifdef USE_RED_KUZNECHIK_128
// #include "kuznechik_128bit.h" // #include "kuznechik_128bit.h"
// #endif // #endif
# ifdef USE_RED_KUZNECHIK_8 #define MASTER_KEY_LEN 32
# include "kuznechik_8bit.h"
# ifdef USE_SHA256_KEY
# include "sha256.h"
# endif
# endif
# ifdef USE_RED_AES_256 #ifdef USE_RED_KUZNECHIK_8
# include "aes.h" # include "kuznechik_8bit.h"
# ifdef USE_SHA256_KEY
# undef MASTER_KEY_LEN
# define MASTER_KEY_LEN 128
# include "sha256.h"
# endif # endif
#endif
# define MAX_KEY_LEN 128 #ifdef USE_RED_AES_256
# define BLOCK_SIZE 16 # include "aes.h"
#endif
# ifndef INIT_STORAGE_SIZE #define MAX_KEY_LEN 128
# define INIT_STORAGE_SIZE 5 #define BLOCK_SIZE 16
# endif
#ifndef INIT_STORAGE_SIZE
# ifndef INIT_STORAGE_PASS_LEN # define INIT_STORAGE_SIZE 5
# define INIT_STORAGE_PASS_LEN 64 #endif
# endif
#ifndef INIT_STORAGE_PASS_LEN
# ifdef INIT_STORAGE_PASS_LEN # define INIT_STORAGE_PASS_LEN 64
# if (INIT_STORAGE_PASS_LEN % 16 != 0) #endif
# error "Password length must be multiply of 16 (16,32,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
#endif
// example for encrypted 64 byte password // example for encrypted 64 byte password
// const uint8_t red_enc_pass[STORAGE_SIZE][STORAGE_PASS_LEN] = { // const uint8_t red_enc_pass[STORAGE_SIZE][STORAGE_PASS_LEN] = {
@@ -62,7 +67,7 @@ enum red_crypto_keys {
RED_MENU, RED_MENU,
RED_RNG, RED_RNG,
RED_TEST, RED_TEST,
RED_LOCK, // TODO: add autolock feature RED_LOCK, // TODO: add autolock feature
RED_RST_EE, // resetting EEPROM by clearing RED bytes RED_RST_EE, // resetting EEPROM by clearing RED bytes
RED_PASS1, RED_PASS1,
RED_PASS2, RED_PASS2,
@@ -73,13 +78,19 @@ enum red_crypto_keys {
RED_PASS7, RED_PASS7,
RED_PASS8, RED_PASS8,
RED_PASS9, 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_size = INIT_STORAGE_SIZE;
// uint8_t storage_pass_len = INIT_STORAGE_PASS_LEN; // uint8_t storage_pass_len = INIT_STORAGE_PASS_LEN;
# define EEPROM_PASS_OFFSET 3 + 3 + 1 + 1 + 4 + 1 #define EEPROM_PASS_OFFSET 3 + 3 + 1 + 1 + 4 + 1
# define STORAGE_SIZE (EEPROM_PASS_OFFSET + INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN) #define STORAGE_SIZE (EEPROM_PASS_OFFSET + INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN)
typedef union { typedef union {
uint8_t raw[STORAGE_SIZE]; uint8_t raw[STORAGE_SIZE];
+6 -3
View File
@@ -21,8 +21,8 @@ uint8_t count_readed = 0;
uint8_t return_key = 0; uint8_t return_key = 0;
uint8_t *enc_pass_new; 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 *readed_key_new;
uint8_t count_char_key_new = 0; uint8_t count_char_key_new = 0;
// read new key // read new key
uint8_t read_new_key(uint16_t keycode, keyrecord_t *record) { 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 // clang-format on
// return values
// 0 idle // 0 idle
// 1 get key input // 1 get key input
// 2 exit menu // 2 exit menu
@@ -75,7 +76,9 @@ uint8_t draw_red_menu(uint16_t keycode, keyrecord_t *record) {
readed_option[0] = CHANGE_PASS_NAME; readed_option[0] = CHANGE_PASS_NAME;
} }
if (readed_option[0] == ASCII_LOW_6) { 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) { 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)); send_string(SS_DOWN(X_LCTL) SS_DOWN(X_A) SS_UP(X_LCTL) SS_UP(X_A) SS_TAP(X_BACKSPACE));
+47 -2
View File
@@ -8,8 +8,8 @@ void red_print_chars(uint8_t *c) {
dprintf("\n"); dprintf("\n");
}; };
void red_print_int(uint8_t *c) { void red_print_int(uint8_t *c, uint8_t len) {
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < len; i++) {
dprintf("%d", c[i]); dprintf("%d", c[i]);
} }
dprintf("\n"); 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 // EEPROM utils
void red_write_to_storage(void) { void red_write_to_storage(void) {
+7 -1
View File
@@ -6,7 +6,7 @@
void red_print_chars(uint8_t *c); 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); 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); 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_write_to_storage(void);
void red_read_from_storage(void); void red_read_from_storage(void);