diff --git a/README.md b/README.md index 7a17dc2..6221d96 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# RED_CRYPTO ver 0.4.0 +# RED_CRYPTO ver 0.5.0 ## Description @@ -34,7 +34,7 @@ include keyboards///red_crypto/red_cr #include "red_crypto/red_crypto.h" ``` -4. Enable chosen algorithms with define in `config.h`. Now you can use only `Kuznechik`. +4. Define chosen algorithms and parameters in `config.h`. Now you can use only `Kuznechik`. ```c #define USE_KUZNECHIK_8 @@ -46,6 +46,13 @@ If you want use SHA256 of your key to encrypt passwords, define this in `config. #define USE_SHA256_KEY ``` +Change storage size (default `4`) and maximum password length (default `64`). + +```c +#define STORAGE_SIZE 4 +#define STORAGE_PASS_LEN 64 // use a multiple of 16, e.g. 16, 32, 64... but don't forget about available memory! +``` + 5. Encrypt your passwords with chosen algorithm (AES, Kuznechik). Now available only Kuznechik. You can use `CyberChef`. > Don't use web version! We are here for security, right? diff --git a/red_crypto.c b/red_crypto.c index 919688a..86fac5a 100644 --- a/red_crypto.c +++ b/red_crypto.c @@ -2,9 +2,29 @@ #include "red_crypto.h" -uint8_t dec_pass[2][16] = {{0, 0, 0, 0, 0, 0, 0, 0, 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 dec_pass[STORAGE_SIZE][STORAGE_PASS_LEN] = { + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, -//you can use key any length with SHA256, kuznechik use only 32byte + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, +}; + +// you can use key any length with SHA256, kuznechik use only 32byte #ifdef USE_SHA256_KEY uint8_t readed_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 @@ -12,6 +32,8 @@ uint8_t readed_key[128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t readed_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 +const uint8_t test_pass_block[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + uint8_t count_char_key = 0; uint8_t crypto_mode = 0; @@ -53,7 +75,7 @@ size_t min_len(size_t a, size_t b) { return b; } -void decrypt_pass_kuzn(const uint8_t encrypted_passwords[2][16]) { +void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]) { kuz_key_t key; w128_t x; @@ -69,20 +91,36 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[2][16]) { #ifndef USE_SHA256_KEY kuz_set_decrypt_key(&key, readed_key); #endif - for (uint8_t j = 0; j < 2; j++) { - for (uint8_t i = 0; i < 16; i++) { - x.b[i] = encrypted_passwords[j][i]; - } - kuz_decrypt_block(&key, &x); - + for (uint8_t j = 0; j < STORAGE_SIZE; j++) { #ifdef USE_RED_CRY_DEBUG dprintf("decrypted\t="); - print_w128(&x); - print_chars_w128(&x); #endif + for (uint8_t k = 0; k < STORAGE_PASS_LEN / 16; k++) { + uint8_t cnt_zeros=0; + for (uint8_t z = 0+k*16; z < 16+k*16; z++) { + if (encrypted_passwords[j][z] == 0x00) { + cnt_zeros++; + } else { + cnt_zeros = 0; + break; + } + } + if (cnt_zeros!=0) break; // if there is zero 8 byte block in password, e.g. password length <32 or <64 - for (uint8_t i = 0; i < 16; i++) { - dec_pass[j][i] = x.b[i]; + for (uint8_t i = 0 + k * 16; i < 16 + k * 16; i++) { + x.b[i%16] = encrypted_passwords[j][i]; + } + + kuz_decrypt_block(&key, &x); + + for (uint8_t i = 0 + k * 16; i < 16 + k * 16; i++) { + dec_pass[j][i] = x.b[i%16]; + } + +#ifdef USE_RED_CRY_DEBUG + print_w128(&x); + print_chars_w128(&x); +#endif } } }; @@ -100,12 +138,12 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[2][16]) { // } void send_chars_pass(uint8_t *out) { - for (uint8_t i = 0; i < 16 && out[i] != 0; i++) { + for (uint8_t i = 0; i < STORAGE_PASS_LEN && out[i] != 0; i++) { send_char((char)out[i]); } }; -uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[2][16]) { +uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]) { if (crypto_mode) { switch (keycode) { case RED_CRY_M: @@ -134,6 +172,16 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const send_chars_pass(dec_pass[1]); } break; + case RED_PASS3: + if (record->event.pressed) { + send_chars_pass(dec_pass[2]); + } + break; + case RED_PASS4: + if (record->event.pressed) { + send_chars_pass(dec_pass[3]); + } + break; case RED_RNG: if (record->event.pressed) { // TODO: change seed diff --git a/red_crypto.h b/red_crypto.h index 7939790..741ef3c 100644 --- a/red_crypto.h +++ b/red_crypto.h @@ -25,14 +25,36 @@ # define MAX_KEY_LEN 128 +# ifndef STORAGE_SIZE +# define STORAGE_SIZE 4 +# endif + +# ifdef STORAGE_PASS_LEN +# if STORAGE_PASS_LEN % 16 != 0 +# error Storage password length must be a multiple of 16 +# endif +# endif + +# ifndef STORAGE_PASS_LEN +# define STORAGE_PASS_LEN 64 +# endif + + +//example for encrypted 64 byte password +// const uint8_t red_enc_pass[STORAGE_SIZE][STORAGE_PASS_LEN] = { +// {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, +// }; + enum red_crypto_keys { RED_CRY_M = SAFE_RANGE + 0x80, // added safe range offset RED_RNG, RED_TEST, - RED_PASS1, - RED_PASS2 + RED_PASS1, RED_PASS2, RED_PASS3, RED_PASS4, RED_PASS5, RED_PASS6, RED_PASS7, RED_PASS8, RED_PASS9, RED_PASS10, RED_PASS11, RED_PASS12 }; -uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[2][16]); +uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]); #endif