add eeprom reading
This commit is contained in:
1 parent
4a6afc4d11
commit
847923b74b
2 files changed
+61
-41
No files matched your search
+43
-26
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
#include "red_crypto.h"
|
#include "red_crypto.h"
|
||||||
|
|
||||||
uint8_t dec_pass[STORAGE_SIZE][STORAGE_PASS_LEN];
|
uint8_t *decrypted_passwords;
|
||||||
|
|
||||||
|
// initialize array "decrypted_passwords" with 0x00
|
||||||
void init_dec_pass(void) {
|
void init_dec_pass(void) {
|
||||||
for (uint8_t i = 0; i < STORAGE_SIZE; i++) {
|
// array = malloc(sizeof(int[ROWS][COLS])); // explicit 2D array notation
|
||||||
for (uint8_t j = 0; j < STORAGE_PASS_LEN; j++) {
|
decrypted_passwords = malloc(sizeof(uint8_t [enc_pass.storage_size][enc_pass.storage_pass_len]));
|
||||||
dec_pass[i][j] = 0x00;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,12 +31,14 @@ uint8_t decrypted_mode = 0;
|
|||||||
|
|
||||||
uint8_t red_menu_mode = 0;
|
uint8_t red_menu_mode = 0;
|
||||||
|
|
||||||
void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]) {
|
// decrypting passwords
|
||||||
|
void decrypt_pass_kuzn() {
|
||||||
kuz_key_t key;
|
kuz_key_t key;
|
||||||
w128_t x;
|
w128_t x;
|
||||||
|
|
||||||
init_dec_pass();
|
init_dec_pass();
|
||||||
|
|
||||||
|
// for hashing key, like a KDF algorithm
|
||||||
#ifdef USE_SHA256_KEY
|
#ifdef USE_SHA256_KEY
|
||||||
SHA256_CTX ctx;
|
SHA256_CTX ctx;
|
||||||
sha256_init(&ctx);
|
sha256_init(&ctx);
|
||||||
@@ -43,17 +48,21 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_P
|
|||||||
print_hex(result_key);
|
print_hex(result_key);
|
||||||
kuz_set_decrypt_key(&key, result_key);
|
kuz_set_decrypt_key(&key, result_key);
|
||||||
#endif
|
#endif
|
||||||
|
// if you don't have enough FLASH in MCU
|
||||||
#ifndef USE_SHA256_KEY
|
#ifndef USE_SHA256_KEY
|
||||||
kuz_set_decrypt_key(&key, readed_key);
|
kuz_set_decrypt_key(&key, readed_key);
|
||||||
#endif
|
#endif
|
||||||
for (uint8_t j = 0; j < STORAGE_SIZE; j++) {
|
// decrypting all passwords in storage
|
||||||
|
for (uint8_t password_index = 0; password_index < enc_pass.storage_size; password_index++) {
|
||||||
#ifdef USE_RED_CRY_DEBUG
|
#ifdef USE_RED_CRY_DEBUG
|
||||||
dprintf("decrypted\t=");
|
dprintf("decrypted\t=");
|
||||||
#endif
|
#endif
|
||||||
for (uint8_t k = 0; k < STORAGE_PASS_LEN / 16; k++) {
|
for (uint8_t block_index = 0; block_index < enc_pass.storage_pass_len / 16; block_index++) {
|
||||||
uint8_t cnt_zeros = 0;
|
uint8_t cnt_zeros = 0;
|
||||||
for (uint8_t z = 0 + k * 16; z < 16 + k * 16; z++) {
|
// checking is this empty block or not
|
||||||
if (encrypted_passwords[j][z] == 0x00) {
|
for (uint8_t byte_index = 0 + block_index * 16; byte_index < 16 + block_index * 16; byte_index++) {
|
||||||
|
// if (encrypted_passwords[password_index][byte_index] == 0x00) {
|
||||||
|
if (enc_pass.passwords[password_index * enc_pass.storage_pass_len + byte_index] == 0x00) {
|
||||||
cnt_zeros++;
|
cnt_zeros++;
|
||||||
} else {
|
} else {
|
||||||
cnt_zeros = 0;
|
cnt_zeros = 0;
|
||||||
@@ -62,14 +71,18 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_P
|
|||||||
}
|
}
|
||||||
if (cnt_zeros != 0) break; // if there is zero 8 byte block in password, e.g. password length <32 or <64
|
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++) {
|
// reading password's block for decrypting
|
||||||
x.b[i % 16] = encrypted_passwords[j][i];
|
for (uint8_t byte_index = 0 + block_index * 16; byte_index < 16 + block_index * 16; byte_index++) {
|
||||||
|
// x.b[byte_index % 16] = encrypted_passwords[password_index][byte_index];
|
||||||
|
x.b[byte_index % 16] = enc_pass.passwords[password_index * enc_pass.storage_pass_len + byte_index]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// decrypting via Kuznechik
|
||||||
kuz_decrypt_block(&key, &x);
|
kuz_decrypt_block(&key, &x);
|
||||||
|
|
||||||
for (uint8_t i = 0 + k * 16; i < 16 + k * 16; i++) {
|
// writing decrypted password's block
|
||||||
dec_pass[j][i] = x.b[i % 16];
|
for (uint8_t byte_index = 0 + block_index * 16; byte_index < 16 + block_index * 16; byte_index++) {
|
||||||
|
decrypted_passwords[password_index][byte_index] = x.b[byte_index % 16];
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_RED_CRY_DEBUG
|
#ifdef USE_RED_CRY_DEBUG
|
||||||
@@ -92,7 +105,8 @@ void decrypt_pass_kuzn(const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_P
|
|||||||
// print_chars_w128(&x);
|
// print_chars_w128(&x);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]) {
|
// function for embedding to "proccess_record_user" for adding crypto features
|
||||||
|
uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) {
|
||||||
if (crypto_mode) {
|
if (crypto_mode) {
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case RED_CRY_M:
|
case RED_CRY_M:
|
||||||
@@ -102,7 +116,7 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const
|
|||||||
#endif
|
#endif
|
||||||
crypto_mode = 0;
|
crypto_mode = 0;
|
||||||
count_char_key = 0;
|
count_char_key = 0;
|
||||||
decrypt_pass_kuzn(encrypted_passwords);
|
decrypt_pass_kuzn();
|
||||||
decrypted_mode = 1;
|
decrypted_mode = 1;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -114,38 +128,40 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const
|
|||||||
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 == 2) {
|
||||||
red_menu_mode = 0;
|
red_menu_mode = 0;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
return key_return;
|
return key_return;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (decrypted_mode) {
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case RED_PASS1:
|
case RED_PASS1:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
send_chars_pass(dec_pass[0]);
|
send_chars_pass(decrypted_passwords[0]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RED_PASS2:
|
case RED_PASS2:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
send_chars_pass(dec_pass[1]);
|
send_chars_pass(decrypted_passwords[1]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RED_PASS3:
|
case RED_PASS3:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
send_chars_pass(dec_pass[2]);
|
send_chars_pass(decrypted_passwords[2]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RED_PASS4:
|
case RED_PASS4:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
send_chars_pass(dec_pass[3]);
|
send_chars_pass(decrypted_passwords[3]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// if you want to add more keys for passwords, copy this `case`
|
// if you want to add more keys for passwords, copy this `case`
|
||||||
// case RED_PASSX: //change X here
|
// case RED_PASSX: //change X here
|
||||||
// if (record->event.pressed) {
|
// if (record->event.pressed) {
|
||||||
// send_chars_pass(dec_pass[X]); //change X here
|
// send_chars_pass(decrypted_passwords[X]); //change X here
|
||||||
// }
|
// }
|
||||||
// break;
|
// break;
|
||||||
|
}
|
||||||
|
} else { // by default
|
||||||
|
switch (keycode) {
|
||||||
case RED_RNG:
|
case RED_RNG:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
// TODO: change seed
|
// TODO: change seed
|
||||||
@@ -165,7 +181,8 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const
|
|||||||
break;
|
break;
|
||||||
case RED_CRY_M:
|
case RED_CRY_M:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
crypto_mode = 1;
|
crypto_mode = 1; // to enter in crypto mode block code above
|
||||||
|
// clearing readed key
|
||||||
for (uint8_t i = 0; i < 32; i++) {
|
for (uint8_t i = 0; i < 32; i++) {
|
||||||
readed_key[i] = 0;
|
readed_key[i] = 0;
|
||||||
}
|
}
|
||||||
@@ -175,11 +192,11 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const
|
|||||||
case RED_MENU:
|
case RED_MENU:
|
||||||
if (record->event.pressed) {
|
if (record->event.pressed) {
|
||||||
if (decrypted_mode == 1) {
|
if (decrypted_mode == 1) {
|
||||||
red_menu_mode = 1;
|
red_menu_mode = 1; // to enter in menu mode block code above
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 1; // for capturing entering of all keys from red_crypto
|
||||||
}
|
}
|
||||||
+18
-15
@@ -27,20 +27,7 @@
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
# define MAX_KEY_LEN 128
|
# define MAX_KEY_LEN 128
|
||||||
|
# define BLOCK_SIZE 16
|
||||||
# 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
|
// example for encrypted 64 byte password
|
||||||
@@ -56,6 +43,7 @@ enum red_crypto_keys {
|
|||||||
RED_MENU,
|
RED_MENU,
|
||||||
RED_RNG,
|
RED_RNG,
|
||||||
RED_TEST,
|
RED_TEST,
|
||||||
|
RED_LOCK, // TODO: add autolock feature
|
||||||
RED_PASS1,
|
RED_PASS1,
|
||||||
RED_PASS2,
|
RED_PASS2,
|
||||||
RED_PASS3,
|
RED_PASS3,
|
||||||
@@ -68,6 +56,21 @@ enum red_crypto_keys {
|
|||||||
RED_PASS10
|
RED_PASS10
|
||||||
};
|
};
|
||||||
|
|
||||||
uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record, const uint8_t encrypted_passwords[STORAGE_SIZE][STORAGE_PASS_LEN]);
|
uint8_t storage_size = 4;
|
||||||
|
uint8_t storage_pass_len = 64;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint8_t raw[1+1+4+(uint32_t)storage_size*(uint32_t)storage_pass_len];
|
||||||
|
struct {
|
||||||
|
uint8_t storage_size;
|
||||||
|
uint8_t storage_pass_len;
|
||||||
|
uint32_t memory_usage;
|
||||||
|
uint8_t passwords[(uint32_t)storage_size*(uint32_t)storage_pass_len];
|
||||||
|
};
|
||||||
|
} red_crypto_storage;
|
||||||
|
|
||||||
|
red_crypto_storage enc_pass;
|
||||||
|
|
||||||
|
uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user