Author SHA1 Message Date
redlineo c890ad0c33 fix syntax for qmk 2025-01-22 07:11:18 +00:00
redlineo aaa6f71708 unified dec_pass array, update readme 2025-01-22 07:02:14 +00:00
redlineo 90399dc61b update readme 2025-01-21 17:43:33 +00:00
redlineo 1e18045a38 update readme 2025-01-21 17:40:40 +00:00
redlineo 6d8cbd1277 expanded storage size and password length 2025-01-21 10:11:23 +00:00
redlineo c615c136b1 update readme 2025-01-21 07:52:06 +00:00
redlineo 6b2605d270 add hashing key with SHA256, update readme 2025-01-21 07:47:11 +00:00
redlineo 842beb2716 expand key lenght, add key hashing 2025-01-20 14:55:18 +00:00
redlineo 4630460b88 update readme 2025-01-20 14:41:53 +00:00
redlineo 9e653cab69 merged kc_to_ascii with main, add debug define, update readme 2025-01-20 14:06:45 +00:00
6 changed files with 210 additions and 57 deletions

No files matched your search

+79 -21
View File
@@ -1,65 +1,123 @@
# RED_CRYPTO ver 0.2.0 # RED_CRYPTO ver 0.5.0
## Description ## Description
Password manager for (almost) any QMK keyboards. Password manager for (almost) any QMK keyboards.
Supports cryptographic algorithms: Supports cryptographic algorithms:
- [ ] AES256, - [x] Kuznechik (Russian GOST cipher)
- [x] Kuznechik (Russian GOST cipher), - [x] SHA256 (for hashing key)
- [ ] SHA256. - [ ] Streebog (for hashing key, Russian GOST hash algorithm)
- [ ] AES256
## Features and plans
- [x] add read all ASCII-printable symbols for key
- [x] add Kuznechik lib for non 128 bit MCU
- [x] use SHA256 for hashing key to set key length to 32 bytes
- [x] expand stored passwords length from 16 byte to 64
- [x] expand number of stored passwords (manual adding new keys)
- [ ] write new passwords to EEPROM without flashing firmware
- [ ] add CBC mode for Kuznechik, it will increase security
## Installation ## Installation
1. Clone this repo to your keymap folder. 1. Clone this repo to your keymap folder.
2. Copy this string to *rules.mk*. Don't forget to insert your names. This is make rules for including all needed file to firmwire. 2. Copy this string to **rules.mk**. Don't forget to insert your names. This is make rules for including all needed file to firmwire.
```make ```make
include keyboards/<keyboard>/<path to your keymap folder>/red_crypto/red_crypto.mk include keyboards/<your keyboard>/<path to your keymap folder>/red_crypto/red_crypto.mk
``` ```
3. Import library in *keymap.c*. 3. Import library in **keymap.c**.
```c ```c
#include "red_crypto/red_crypto.h" #include "red_crypto/red_crypto.h"
``` ```
4. Enable chosen algorithms. Now you can use only `Kuznechik`. 4. Define chosen algorithms and parameters in `config.h`. Now you can use only `Kuznechik`.
```c ```c
#define USE_KUZNECHIK_8 #define USE_KUZNECHIK_8
``` ```
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? Choose `GOST Cipher`, then choose type `Kuznechik 34.12-2014`, not Magma. Choose mode ECB. Now, there is no CBC or CTR. If you want use SHA256 of your key to encrypt passwords, define this in `config.h`.
> Don't forget IV for GOST
> ```94208510C2C001FB01C0C21085209401```
6. Add your encrypted passwords in "keymap.c". Now you can encrypt 2 passwords with length <=16 byte. Keep this information in secret. Even if there are no plaintext.
```c ```c
const uint8_t encrypted_passwords[2][16] = { {0,0,0,...,0,0,0}, {0,0,0,...,0,0,0} }; #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?
If you've enabled SHA256 for key, choose `SHA2`, then `SHA256` mode and `64` rounds. Hash your key, then copy it to next step.
Choose `GOST Encrypt`, then choose type `GOST R 34.12 (Kuznechik, 2015)`, not `Magma`. Choose mode `ECB`. Now there is no other modes for `Kuznechik`.
> Don't forget IV for GOST
```
94208510C2C001FB01C0C21085209401
```
6. Add your encrypted passwords in **keymap.c**. Now you can encrypt 4 passwords with any length (16, 32, 64, more not tested).
> Keep this information in secret. Even if there are no plaintext.
Chosen parameter `STORAGE_PASS_LEN` must be the same as the number of zeros in this array.
Chosen parameter `STORAGE_SIZE` must be the same as the number of passwords in this array.
```c
const uint8_t encrypted_passwords[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},
};
```
If you want more passwords in storage, you should:
- add keys `RED_PASSX` into `enum red_crypto_keys` in `red_crypto.h`
- add into `crypto_proccess_record_user` handling new keys
7. Add new layer or set new keyes everywhere you want. 7. Add new layer or set new keyes everywhere you want.
- `RED_TEST` - execute test functions of every algorithm with. Available only with `#define TEST_FUNCTIONS_ENABLED` and `COMMAND_ENABLE=yes`. You can see output via `hid_listen`. - `RED_TEST` - execute test functions of every algorithm with. Available only with `#define TEST_FUNCTIONS_ENABLED` and `COMMAND_ENABLE=yes`. You can see output via `hid_listen`.
- `GET_PASS` - execute reading password for encrypted passwords. After entering password, tap this key again. Now, you can use `RED_PASSX` keys. - `RED_CRY_M` - execute reading password for encrypted passwords. After entering password, tap this key again. Now, you can use `RED_PASSX` keys.
- `RED_PASSX` - where `X` is number of your password in database. After tapping, keyboard send decrypted password. - `RED_PASSX` - where `X` is number of your password in database. After tapping, keyboard send decrypted password.
- `RED_RNG` - use 32 times function named `tap_random_base64` from QMK. It's for generating random strings. But now random seed is not unique for every keyboard's power on. - `RED_RNG` - use 32 times function named `tap_random_base64` from QMK. It's for generating random strings. But now random seed is not unique for every keyboard's power on.
8. Copy this string to function *process_record_user* in *keymap.c* before all your code. It uses "switch case" construction inside. 8. Copy this string to function **process_record_user** in **keymap.c** before all your code. It uses "switch case" construction inside.
```c ```c
crypto_process_record_user(keycode,event,encrypted_passwords); uint8_t crypto_return = crypto_process_record_user(keycode, event, encrypted_passwords);
if (crypto_return == 1){
return false;
}
``` ```
9. Flash your keyboard. 9. Compile and flash your keyboard.
## Usage ## Usage
Tap `GET_PASS`. Enter password. Tap `GET_PASS` again to confirm. Tap `RED_CRY_M`. Enter password. Tap `RED_CRY_M` again to confirm.
Now you can use `RED_PASSX` to send decrypted passwords. Now you can use `RED_PASSX` to send decrypted passwords.
If you have missed while entering password, just tap `GET_PASS` and tap `GET_PASS`. If you have missed while entering password, just tap `RED_CRY_M` and tap `RED_CRY_M`.
## Related projects
Thanks to the authors of these repositories:
- https://github.com/mjosaarinen/kuznechik - kuznechik implementation for 8bit and 128bit
- https://github.com/B-Con/crypto-algorithms - other crypto algorithms
+2
View File
@@ -12,7 +12,9 @@ void write_keycode(uint8_t up_key, uint8_t low_key, keyrecord_t *record, uint8_t
} else { } else {
readed_key[*count_char_key] = low_key; readed_key[*count_char_key] = low_key;
} }
#ifdef USE_RED_CRY_DEBUG
dprintf("%d", readed_key[*count_char_key]); dprintf("%d", readed_key[*count_char_key]);
#endif
*count_char_key = *count_char_key + 1; *count_char_key = *count_char_key + 1;
} }
}; };
+92 -22
View File
@@ -2,9 +2,23 @@
#include "red_crypto.h" #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];
void init_dec_pass(void) {
for (uint8_t i = 0; i < STORAGE_SIZE; i++) {
for (uint8_t j = 0; j < STORAGE_PASS_LEN; j++) {
dec_pass[i][j] = 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
#ifndef USE_SHA256_KEY
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}; 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
uint8_t count_char_key = 0; uint8_t count_char_key = 0;
@@ -13,7 +27,7 @@ uint8_t crypto_mode = 0;
void print_chars_w128(w128_t *x) { void print_chars_w128(w128_t *x) {
for (uint8_t i = 0; i < 16; i++) { for (uint8_t i = 0; i < 16; i++) {
if (x->b[i] == 0x00) break; if (x->b[i] == 0x00) break;
printf("%c", x->b[i]); dprintf("%c", x->b[i]);
} }
dprintf("\n"); dprintf("\n");
}; };
@@ -21,38 +35,82 @@ void print_chars_w128(w128_t *x) {
void print_chars(uint8_t *c) { void print_chars(uint8_t *c) {
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < 32; i++) {
if (c[i] == 0x00) break; if (c[i] == 0x00) break;
printf("%c", c[i]); dprintf("%c", c[i]);
} }
dprintf("\n"); dprintf("\n");
}; };
void print_int(uint8_t *c) { void print_int(uint8_t *c) {
for (uint8_t i = 0; i < 32; i++) { for (uint8_t i = 0; i < 32; i++) {
printf("%d", c[i]); dprintf("%d", c[i]);
} }
dprintf("\n"); dprintf("\n");
}; };
void decrypt_pass_kuzn(const uint8_t encrypted_passwords[2][16]) { void print_hex(uint8_t *h) {
for (uint8_t i = 0; i < 32; i++) {
dprintf("%x ", h[i]);
}
dprintf("\n");
};
size_t min_len(size_t a, size_t b) {
if (a < b)
return a;
else
return b;
}
void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]) {
kuz_key_t key; kuz_key_t key;
w128_t x; w128_t x;
init_dec_pass();
#ifdef USE_SHA256_KEY
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, readed_key, min_len(u_strlen(readed_key), MAX_KEY_LEN));
BYTE result_key[SHA256_BLOCK_SIZE];
sha256_final(&ctx, result_key);
print_hex(result_key);
kuz_set_decrypt_key(&key, result_key);
#endif
#ifndef USE_SHA256_KEY
kuz_set_decrypt_key(&key, readed_key); kuz_set_decrypt_key(&key, readed_key);
for (uint8_t j = 0; j < 2; j++) { #endif
for (uint8_t i = 0; i < 16; i++) { for (uint8_t j = 0; j < STORAGE_SIZE; j++) {
x.b[i] = encrypted_passwords[j][i]; #ifdef USE_RED_CRY_DEBUG
dprintf("decrypted\t=");
#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 + k * 16; i < 16 + k * 16; i++) {
x.b[i % 16] = encrypted_passwords[j][i];
}
kuz_decrypt_block(&key, &x); kuz_decrypt_block(&key, &x);
dprintf("decrypted\t="); 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_w128(&x);
print_chars_w128(&x); print_chars_w128(&x);
#endif
for (uint8_t i = 0; i < 16; i++) {
dec_pass[j][i] = x.b[i];
} }
} }
}; };
// TODO: add encrypt new passwords with binding on new keys // TODO: add encrypt new passwords with binding on new keys
@@ -68,29 +126,25 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[2][16]) {
// } // }
void send_chars_pass(uint8_t *out) { 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]); 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) { if (crypto_mode) {
switch (keycode) { switch (keycode) {
// case KC_1:
// if (record->event.pressed) {
// readed_key[count_char_key] = ASCII_LOW_1;
// count_char_key++;
// }
// return 1;
// break;
case RED_CRY_M: case RED_CRY_M:
if (record->event.pressed) { if (record->event.pressed) {
#ifdef USE_RED_CRY_DEBUG
print_int(readed_key); print_int(readed_key);
#endif
crypto_mode = 0; crypto_mode = 0;
count_char_key = 0; count_char_key = 0;
decrypt_pass_kuzn(encrypted_passwords); decrypt_pass_kuzn(encrypted_passwords);
} }
default: default:
// reading key
return kc_to_ascii(keycode, record, readed_key, &count_char_key); return kc_to_ascii(keycode, record, readed_key, &count_char_key);
break; break;
} }
@@ -106,6 +160,22 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const
send_chars_pass(dec_pass[1]); send_chars_pass(dec_pass[1]);
} }
break; 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;
// if you want to add more keys for passwords, copy this `case`
// case RED_PASSX: //change X here
// if (record->event.pressed) {
// send_chars_pass(dec_pass[X]); //change X here
// }
// break;
case RED_RNG: case RED_RNG:
if (record->event.pressed) { if (record->event.pressed) {
// TODO: change seed // TODO: change seed
+32 -11
View File
@@ -3,7 +3,6 @@
#include QMK_KEYBOARD_H #include QMK_KEYBOARD_H
#include "quantum.h" #include "quantum.h"
#ifndef RED_CRYPTO_H #ifndef RED_CRYPTO_H
# define RED_CRYPTO_H # define RED_CRYPTO_H
@@ -12,28 +11,50 @@
// #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 # ifdef USE_RED_KUZNECHIK_8
# include "kuznechik_8bit.h" # include "kuznechik_8bit.h"
# ifdef USE_SHA256_KEY
# include "sha256.h" # include "sha256.h"
# endif # endif
# endif
# ifdef USE_RED_AES_256 # ifdef USE_RED_AES_256
# include "aes.h" # include "aes.h"
# endif # endif
# define MAX_KEY_LEN 128
# ifndef STORAGE_SIZE
# define STORAGE_SIZE 4
# endif
enum red_crypto_keys { # ifdef STORAGE_PASS_LEN
RED_CRY_M = SAFE_RANGE+0x80, // added safe range offset # if STORAGE_PASS_LEN % 16 != 0
RED_PASS1, # error Storage password length must be a multiple of 16
RED_PASS2, # endif
RED_RNG, # endif
RED_TEST
};
uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[2][16]);
# ifndef STORAGE_PASS_LEN
# define STORAGE_PASS_LEN 64
# endif # 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_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[STORAGE_SIZE][STORAGE_PASS_LEN]);
#endif
+4 -3
View File
@@ -158,13 +158,14 @@ void sha256_final(SHA256_CTX *ctx, BYTE hash[])
} }
} }
/*********************** TEST FUNCTION DEFINITIONS ***********************/ size_t u_strlen(const unsigned char * array)
#ifdef TEST_FUNCTIONS_ENABLED
inline size_t u_strlen(const unsigned char * array)
{ {
return strlen((const char*)array); return strlen((const char*)array);
} }
/*********************** TEST FUNCTION DEFINITIONS ***********************/
#ifdef TEST_FUNCTIONS_ENABLED
void exec_sha256_test(void) void exec_sha256_test(void)
{ {
BYTE text1[] = {"abc"}; BYTE text1[] = {"abc"};
+1
View File
@@ -31,6 +31,7 @@ typedef struct {
void sha256_init(SHA256_CTX *ctx); void sha256_init(SHA256_CTX *ctx);
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len); void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
void sha256_final(SHA256_CTX *ctx, BYTE hash[]); void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
size_t u_strlen(const unsigned char * array);
// test // test
#ifdef TEST_FUNCTIONS_ENABLED #ifdef TEST_FUNCTIONS_ENABLED